-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.cpp
80 lines (69 loc) · 2.44 KB
/
rules.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
//
// Created by iDarkDuck on 2018-10-19.
//
#include "rules.h"
//#define TEST_RULES
Rules::Rules(const int &nPlayers) : nPlayers(nPlayers), currentSide(0) {}
bool Rules::isValid(const Game &game) {
if (twoCardsSelected(game)) return (*game.getPreviousCard() == *game.getCurrentCard());
return true;
}
bool Rules::gameOver(const Game &game) { return game.getRound() >= 7; }
bool Rules::roundOver(const Game &game) {
int nActivePlayers = 0;
for (int i = 0; i < nPlayers; i++) {
auto player = getNextPlayer(game);
if (player.isActive()) { nActivePlayers++; }
}
return nActivePlayers == 1;
}
const Player &Rules::getNextPlayer(const Game &game) {
const Game *gamePtr = &game;
const Player &player = const_cast<Game *>(gamePtr)->getPlayer(sides[currentSide++]);
if (currentSide >= nPlayers) currentSide = 0;
return player;
}
bool Rules::twoCardsSelected(const Game &game) const {
try { return (game.getPreviousCard() != nullptr && game.getCurrentCard() != nullptr); }
catch (...) { return false; }
}
#ifdef TEST_RULES
int main() {
cout << "TEST_RULES" << endl;
int sideCounter, nPlayers;
Game game(sideCounter, nPlayers);
assert(sideCounter == nPlayers - 1);
assert(nPlayers >= 2 && nPlayers <= 4);
Player *currentPlayer = &game.getPlayer(sides[(sideCounter = (sideCounter + 1) % nPlayers)]);
assert(currentPlayer != nullptr);
assert(game.getRound() == 1);
Card *cardPtr = game.getCard(Z, Zero);
assert(cardPtr != nullptr);
game.setCurrentCard(cardPtr);
Card *cardPtr2 = game.getCard(Z, Zero);
assert(cardPtr2 != nullptr);
game.setCurrentCard(cardPtr2);
assert(game.getPreviousCard() != nullptr);
assert(game.getCurrentCard() != nullptr);
game.nextRound();
assert(game.getRound() == 2);
cout << game << endl;
//Rules
Rules rules(nPlayers);
//Rules Methods
cout << "Testing for two selected cards" << endl;
bool isValid = ((*game.getPreviousCard() == *game.getCurrentCard()));
assert(rules.isValid(game) == isValid);
assert(rules.gameOver(game) == false);
assert(rules.roundOver(game) == false);
const Player *currentPlayerFromRules = &rules.getNextPlayer(game);
assert(*currentPlayerFromRules == *currentPlayer);
game.nextRound();
game.nextRound();
game.nextRound();
game.nextRound();
game.nextRound();
assert(rules.gameOver(game) == true);
cout << "Testing Completed" << endl;
}
#endif