/
tron.cpp
173 lines (156 loc) · 4.37 KB
/
tron.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <exception>
#include "tron.h"
#include "clamp.h"
Tron::Tron(QSize mapSize,
int playerCount,
std::vector<QString> playerNames,
std::vector<QColor> playerColors) :
mapSize(mapSize)
, playerCount(playerCount)
{
if (mapSize.width() < MIN_MAP_WIDTH
|| mapSize.width() > MAX_MAP_WIDTH
|| mapSize.height() < MIN_MAP_HEIGHT
|| mapSize.height() > MAX_MAP_HEIGHT) {
throw std::logic_error{"Bad map size."};
}
if (playerCount < MIN_PLAYER_COUNT
|| playerCount > MAX_PLAYER_COUNT) {
throw std::logic_error{"Bad player count."};
}
for (int i = 0; i < playerCount; ++i) {
players.emplace_back(playerNames[i], playerColors[i], startPos(i));
}
}
/*!
* The object iterates over all players, updates them
* and then checks for collisions and removes players
* from the game accordingly.
* \return Whether the game is now over.
*/
auto Tron::step() -> bool
{
if (allReady() && !gameIsOver()) {
// Update each player
for (Player &player : players) {
player.step();
}
// Check each player for collision;
// We do this after all updates to ensure
// _both_ players are stopped in event of tie
for (Player &player : players) {
if (isColliding(player)) {
player.setIsPlaying(false);
}
}
}
return !gameIsOver();
}
/*!
* A game is considered "over" when it has either a winner or
* is a draw (tie).
* \return Whether the game is over.
*/
auto Tron::gameIsOver() -> bool
{
auto still_playing = std::count_if(players.begin(), players.end(),
[](Player &p) { return p.getIsPlaying(); });
return still_playing <= 1;
}
/*!
* If the game is over, this function will return an iterator to
* its winner. In the event of a tie this iterator will point
* to the end() of the `players` container.
* \return Iterator to winning player.
*/
auto Tron::getWinner() -> PlayerContainer::iterator
{
if (!gameIsOver()) {
throw std::logic_error{"Game has no winner (game not over)."};
}
PlayerContainer::iterator iter;
for (iter = players.begin(); iter != players.end(); ++iter) {
if (iter->getIsPlaying()) {
break;
}
}
return iter;
}
auto Tron::allReady() -> bool
{
return std::all_of(players.begin(), players.end(),
[](const Player &p){return p.getDirection() != Player::Direction::None;});
}
/*!
* Determine if `player` is colliding with map geometry
* or any other players.
* \param player to check collision status of.
* \return Whether `player` is colliding.
*/
auto Tron::isColliding(const Player &player) -> bool
{
QPoint position = player.getPosition();
int x = position.x();
int y = position.y();
// Check map bound collisions
if (x < 0 || y < 0
|| x >= mapSize.width()
|| y >= mapSize.height()) {
return true;
}
// Check collisions with other players
for(const Player& otherPlayer : players) {
if (player.collidesWith(otherPlayer))
return true;
}
return false;
}
auto Tron::startPos(int index) -> QPoint
{
// TODO: This just assumes at most 4 players.
int x, y;
switch(index) {
case 0: // Top Left
x = (mapSize.width() / 4);
y = (mapSize.height() / 4);
break;
case 1: // Bottom Right
x = 3 * (mapSize.width() / 4);
y = 3 * (mapSize.height() / 4);
break;
case 2: // Top Right
x = 3 * (mapSize.width() / 4);
y = (mapSize.height() / 4);
break;
case 3: // Bottom Left
x = (mapSize.width() / 4);
y = 3 * (mapSize.height() / 4);
break;
default:
throw std::logic_error{"Can't compute start position for player."};
}
return {x, y};
}
auto Tron::getMapSize() const -> QSize
{
return mapSize;
}
auto Tron::getPlayerCount() const -> int
{
return playerCount;
}
auto Tron::getPlayer(int index) -> Player&
{
return players[index];
}
auto Tron::getPlayers() const -> const PlayerContainer&
{
return players;
}
// Constants
const int Tron::MIN_PLAYER_COUNT{2};
const int Tron::MAX_PLAYER_COUNT{4};
const int Tron::MIN_MAP_WIDTH{10};
const int Tron::MAX_MAP_WIDTH{100};
const int Tron::MIN_MAP_HEIGHT{10};
const int Tron::MAX_MAP_HEIGHT{100};