blob: 6de11ddc46bf93c53a2d0547d40b090ed5a7d626 (
plain) (
blame)
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
|
/**
* Copyright (C) David Wolfe, 1999. All rights reserved.
* Ported to Qt and adapted for TDDD86, 2015.
*/
#include "GameState.h"
#include "utilities.h"
#include "constants.h"
GameState::GameState(){}
GameState::GameState(int numberOfRobots) {
for (int i = 0; i < numberOfRobots; i++) {
Robot *robot = new Robot;
while (!isEmpty(*robot)) {
robot->teleport();
}
robots.push_back(robot);
}
teleportHero();
}
GameState::GameState(const GameState &other) {
for (const auto &robot : other.robots) {
robots.push_back(robot->clone());
}
hero = other.hero;
}
void GameState::draw(QGraphicsScene *scene) const {
scene->clear();
for (int i = 0; i < robots.size(); i++) {
robots[i]->draw(scene);
}
hero.draw(scene);
}
void GameState::teleportHero() {
do {
hero.teleport();
} while (!isEmpty(hero));
}
void GameState::moveRobots() {
for (int i = 0; i < robots.size(); i++) {
robots[i]->moveTowards(hero);
}
}
int GameState::countCollisions() {
int numberDestroyed = 0;
for (int i = 0; i < robots.size(); i++) {
if (robots[i]->alive()) {
if (countRobotsAt(*robots[i]) > 1 || junkAt(*robots[i])) {
numberDestroyed++;
Junk *junk = new Junk(*robots[i]);
delete robots[i];
robots[i] = junk;
}
}
}
return numberDestroyed;
}
bool GameState::anyRobotsLeft() const {
for (const auto &robot : robots) {
if (robot->alive()) {
return true;
}
}
return false;
}
bool GameState::heroDead() const {
return !isEmpty(hero);
}
bool GameState::isSafe(const Unit& unit) const {
for (int i = 0; i < robots.size(); i++) {
if (robots[i]->attacks(unit)) {
return false;
}
}
return !junkAt(unit);
}
void GameState::moveHeroTowards(const Unit& dir) {
hero.moveTowards(dir);
}
Hero GameState::getHero() const {
return hero;
}
/*
* Free of robots and junk only
*/
bool GameState::isEmpty(const Unit& unit) const {
return countRobotsAt(unit) == 0;
}
/*
* Is there junk at unit?
*/
bool GameState::junkAt(const Unit& unit) const {
for (const auto &robot : robots) {
if (robot->at(unit) && !robot->alive()) {
return true;
}
}
return false;
}
/*
* How many robots are there at unit?
*/
int GameState::countRobotsAt(const Unit& unit) const {
int count = 0;
for (const auto &robot : robots) {
if (robot->at(unit) && robot->alive()) {
count++;
}
}
return count;
}
|