-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBishop.cpp
142 lines (128 loc) · 5.07 KB
/
Bishop.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
// Created by orjie on 14/04/2023.
//
#include "Bishop.h"
#include <iostream>
Bishop::Bishop() {
bisLength = 0;
}
//make up different constructors with all the parameters set the hop length to a random number between 2 and 4
Bishop::Bishop(int id, std::pair<int, int> position, Direction direction, int size, bool alive, int bisLength) : Bug(id,
position,
direction,
size,
alive) {
this->bisLength = bisLength;
}
//getter amd setter for bisLength
int Bishop::getBisLength() const {
return bisLength;
}
void Bishop::setBisLength(int b) {
Bishop::bisLength = b;
}
/*This move method is similar to the move method in the Crawler class
* The bishop can only move diagonally and the maximum distance
* it can hop in the current direction is the minimum of the distance to the edge of the board
* in the x and y direction (note that the board is 10x10)
* If the bishop can hop the full distance, move it and update its position
* If the bishop can't hop the full distance, move it to the edge of the board and update its position
* */
void Bishop::move() {
//if the bug is at the edge of the board and can't move in the current direction, set a new direction at random
while (isWayBlocked()) {
int newDirection = rand() % 4 + 5;
setDirection((Direction) newDirection);
}
// get the current position, direction and path of the bug
std::pair<int, int> currentPosition = getPosition();
Direction currentDirection = getDirection();
std::list<std::pair<int, int>> currentPath = getPath();
// calculate the maximum distance the bug can hop in the current direction
//note that the bishop can only move diagonally
int maxHopDistance = 0;
switch (currentDirection) {
case NorthEast:
maxHopDistance = std::min(currentPosition.second, 9 - currentPosition.first);
break;
case SouthEast:
maxHopDistance = std::min(9 - currentPosition.second, 9 - currentPosition.first);
break;
case SouthWest:
maxHopDistance = std::min(9 - currentPosition.second, currentPosition.first);
break;
case NorthWest:
maxHopDistance = std::min(currentPosition.second, currentPosition.first);
break;
}
// if the bug can hop the full distance, move it and update its position
if (maxHopDistance >= bisLength) {
switch (currentDirection) {
case NorthEast:
currentPosition.first += bisLength;
currentPosition.second -= bisLength;
break;
case SouthEast:
currentPosition.first += bisLength;
currentPosition.second += bisLength;
break;
case SouthWest:
currentPosition.first -= bisLength;
currentPosition.second += bisLength;
break;
case NorthWest:
currentPosition.first -= bisLength;
currentPosition.second -= bisLength;
break;
}
} else {
// if the bug can't hop the full distance, move it to the edge and update its position
switch (currentDirection) {
case NorthEast:
currentPosition.first += maxHopDistance;
currentPosition.second -= maxHopDistance;
break;
case SouthEast:
currentPosition.first += maxHopDistance;
currentPosition.second += maxHopDistance;
break;
case SouthWest:
currentPosition.first -= maxHopDistance;
currentPosition.second += maxHopDistance;
break;
case NorthWest:
currentPosition.first -= maxHopDistance;
currentPosition.second -= maxHopDistance;
break;
}
}
setPosition(currentPosition);
currentPath.push_back(currentPosition);
setPath(currentPath);
}
void Bishop::displayBug() {
std::cout << getId() << " Bishop (" << getPosition().first << "," << getPosition().second << ") " << getSize()
<< " ";
switch (getDirection()) {
case NorthEast:
std::cout << "NorthEast ";
break;
case SouthEast:
std::cout << "SouthEast ";
break;
case SouthWest:
std::cout << "SouthWest ";
break;
case NorthWest:
std::cout << "NorthWest ";
break;
}
std::cout << bisLength << " ";
if (isAlive()) {
std::cout << "Alive" << std::endl;
} else {
std::cout << "Dead" << std::endl;
}
}
Bishop::~Bishop() {
}