-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSuperBug.cpp
79 lines (71 loc) · 2.29 KB
/
SuperBug.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
//
// Created by orjie on 20/04/2023.
//
#include "SuperBug.h"
#include <iostream>
#include "Direction.h"
#include <SFML/Graphics.hpp>
//default constructor
SuperBug::SuperBug() : Bug() {
}
//make up different constructors with different parameters
SuperBug::SuperBug(int id, std::pair<int, int> position, Direction direction, int size, bool alive) : Bug(id, position,
direction, size,
alive) {
}
//A SuperBug bug moves according to these rules but is controlled by the user with the arrow keys:
//can move left, right, up, down by 1 unit but can’t move off the board so if iswayblocked() is true, then don’t move
//record new position in the superbug's path history
//note that board size is 10x10
void SuperBug::move() {
std::pair<int, int> currentPosition = getPosition();
int x = position.first;
int y = position.second;
if (x > 0 && sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
if (!isWayBlocked()) {
position.first -= 1;
}
}
if (x < 9 && sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
if (!isWayBlocked()) {
position.first += 1;
}
}
if (y > 0 && sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
if (!isWayBlocked()) {
position.second -= 1;
}
}
if (y < 9 && sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
if (!isWayBlocked()) {
position.second += 1;
}
}
addPath(currentPosition);
}
void SuperBug::displayBug() {
std::cout << getId() << " SuperBug (" << getPosition().first << "," << getPosition().second << ") " << getSize()
<< " ";
switch (getDirection()) {
case North:
std::cout << "North ";
break;
case South:
std::cout << "South ";
break;
case East:
std::cout << "East ";
break;
case West:
std::cout << "West ";
break;
}
if (isAlive()) {
std::cout << "Alive" << std::endl;
} else {
std::cout << "Dead" << std::endl;
}
}
//destructor
SuperBug::~SuperBug() {
}