-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHopper.cpp
133 lines (120 loc) · 3.94 KB
/
Hopper.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
//
// Created by orjie on 27/03/2023.
//
#include "Hopper.h"
#include <iostream>
Hopper::Hopper() {
hopLength = 0;
}
//make up different constructors with all the parameters set the hop length to a random number between 2 and 4
Hopper::Hopper(int id, std::pair<int, int> position, Direction direction, int size, bool alive, int hopLength) : Bug(id, position, direction, size, alive) {
this->hopLength = hopLength;
}
//getter amd setter for hopLength
int Hopper::getHopLength() const {
return hopLength;
}
void Hopper::setHopLength(int h) {
Hopper::hopLength = h;
}
//int hopLength; The distance/length that a particular hopper bug can hop (in
//range (2-4 units)
//void move(){} A Hopper bug moves according to these rules:
//- moves by “hopLength” units in current direction
//- if at edge of board and can’t move over edge in
//current direction, set a new direction at random
//(repeat until bug can move forward) and then
//move.
//- if bug can’t hop the full ‘hopLength’, then the bug
//does move but ‘hits’ the edge and falls on the
//square where it hit the edge/wall
//- record new position in hoppers path history
void Hopper::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 + 1;
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
int maxHopDistance = 0;
switch (currentDirection) {
case North:
maxHopDistance = currentPosition.second;
break;
case East:
maxHopDistance = 9 - currentPosition.first;
break;
case South:
maxHopDistance = 9 - currentPosition.second;
break;
case West:
maxHopDistance = currentPosition.first;
break;
}
// if the bug can hop the full distance, move it and update its position
if (maxHopDistance >= hopLength) {
switch (currentDirection) {
case North:
currentPosition.second -= hopLength;
break;
case East:
currentPosition.first += hopLength;
break;
case South:
currentPosition.second += hopLength;
break;
case West:
currentPosition.first -= hopLength;
break;
}
}
// if the bug can't hop the full distance, it hits the edge and falls on that square
else {
switch (currentDirection) {
case North:
currentPosition.second = 0;
break;
case East:
currentPosition.first = 9;
break;
case South:
currentPosition.second = 9;
break;
case West:
currentPosition.first = 0;
break;
}
}
setPosition(currentPosition);
currentPath.push_back(currentPosition);
addPath(currentPosition);
}
void Hopper::displayBug() {
std::cout << getId() << " Hopper (" << getPosition().first << "," << getPosition().second << ") " << getSize() << " ";
switch (getDirection()) {
case North:
std::cout << "North ";
break;
case East:
std::cout << "East ";
break;
case South:
std::cout << "South ";
break;
case West:
std::cout << "West ";
break;
}
std::cout << hopLength << " ";
if (isAlive()) {
std::cout << "Alive" << std::endl;
} else {
std::cout << "Dead" << std::endl;
}
}
Hopper::~Hopper() {
}