-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGameField.cpp
More file actions
127 lines (105 loc) · 2.81 KB
/
GameField.cpp
File metadata and controls
127 lines (105 loc) · 2.81 KB
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
#include "GameField.h"
#include "Setup.h"
GameField::GameField() {
}
GameField::GameField(uint8_t xSize, uint8_t ySize, uint8_t zSize, uint8_t playersCount) {
_xSize = xSize;
_ySize = ySize;
_zSize = zSize;
_cubeSize = _xSize * _ySize * _zSize;
_playersCount = playersCount;
_field = new Cell[_cubeSize];
_moveAbility = true;
for (uint8_t z = 0; z < _zSize; z++) {
for (uint8_t y = 0; y < _ySize; y++) {
for (uint8_t x = 0; x < _xSize; x++) {
_field[index(x, y, z)] = Cell(x, y, z);
}
}
}
_players = new Player[_playersCount];
for (uint8_t p = (uint8_t) BLUE; p < _playersCount; p++) {
_players[p] = Player((Color)p);
}
}
GameField::~GameField() {
delete[] _field;
delete[] _players;
}
Player* GameField::getPlayer(Color color) {
return &_players[color];
}
Player* GameField::getOwner(uint8_t x, uint8_t y, uint8_t z) {
return _field[index(x,y,z)].getOwner();
}
bool GameField::setOwner(Player* player, uint8_t x, uint8_t y, uint8_t z) {
return getCell(x, y, z)->setOwner(player);
}
Player* GameField::nextPlayer(Player* currentPlayer) {
for (uint8_t p = BLUE; p < _playersCount; p++) {
if (_players == currentPlayer && p < (_playersCount - 1))
return &_players[p + 1];
else
return &_players[BLUE];
}
}
Cell* GameField::getCell(uint8_t x, uint8_t y, uint8_t z) {
return &_field[index(x, y, z)];
}
Cell* GameField::getCell(uint8_t index) {
return &_field[index];
}
uint8_t GameField::index(uint8_t x, uint8_t y, uint8_t z) {
return x + _xSize * (y + z * _ySize);
}
bool GameField::getMoveAbility() {
return _moveAbility;
}
bool GameField::isMoveAble() {
uint8_t counter = 0;
for (uint8_t i = 0; i < _cubeSize; i++) {
if (_field[i].getOwner())
counter++;
}
return _moveAbility = ( _cubeSize - counter > _cubeSize % _playersCount );
}
uint8_t GameField::getPlayersCount() {
return _playersCount;
}
uint8_t GameField::getCubeSize() {
return _cubeSize;
}
/*
* bool GameField::animate()
* Returns:
* 'false' if straights of all players are animated.
* 'true' if at least one straight is not animated yet.
*/
bool GameField::animate() {
clearOwners();
bool allIsEmpty;
for (uint8_t p = 0; p < _playersCount; p++) {
bool isEmpty = _players[p].getStraights()->isEmpty();
if (p == 0)
allIsEmpty = isEmpty;
else
allIsEmpty = allIsEmpty && isEmpty;
if (!isEmpty)
addStraight(_players[p].getStraights()->pop());
}
return allIsEmpty;
}
void GameField::reset() {
clearOwners();
_moveAbility = true;
}
void GameField::clearOwners() {
for (uint8_t i = 0; i < _cubeSize; i++) {
_field[i].removeOwner();
}
}
void GameField::addStraight(Straight straight) {
for (uint8_t length = 0, i = straight.getCubeTripStart(); length < straight.getStraightLength(); length++, i += straight.getStraightStep()) {
_field[i].setOwner(&_players[straight.getColor()]);
}
}