-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtwoplayers.cpp
More file actions
83 lines (75 loc) · 1.86 KB
/
Copy pathtwoplayers.cpp
File metadata and controls
83 lines (75 loc) · 1.86 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
#include "twoplayers.h"
#include "Game.h"
#include <QDebug>
extern Game * game;
/*!
* Set all the necessary variables for the game to be played
*/
void TwoPlayers::initializeGame()
{
cellsPerSide = 32;
vector<bool> empty (cellsPerSide*cellsPerSide, false);
tables[0] = empty;
tables[1] = empty;
actualPlayer = 0;
ownFieldMoves = 6;
adversaryFieldMoves = 2;
movesLeft[0] = ownFieldMoves;
movesLeft[1] = adversaryFieldMoves;
numberOfRounds = 20;
actualRound = 1;
}
/*!
* Moves the game to the next turn
*/
void TwoPlayers::nextTurn()
{
// only if it is not the actual round
if(actualRound == numberOfRounds) return;
// set the moves for the other field
movesLeft[actualPlayer] = adversaryFieldMoves;
// switch player
actualPlayer = actualPlayer == 0 ? 1 : 0;
// set the moves for the own field
movesLeft[actualPlayer] = ownFieldMoves;
// increase round count
actualRound++;
}
/*!
* Let bot fields changed based on the GoL algorithm
*/
void TwoPlayers::letLifeGrow()
{
vector<bool> newTable = game->calculateLogicOfLife(tables[0], cellsPerSide);
tables[0] = newTable;
newTable = game->calculateLogicOfLife(tables[1], cellsPerSide);
tables[1] = newTable;
}
/*!
* Tries to make a move, returns true if the move is available and updates the moves count
*/
bool TwoPlayers::makeAMove(int player)
{
if(movesLeft[player] > 0){
movesLeft[player]--;
return true;
} else {
return false;
}
return false;
}
/*!
* Counts the cells in each field and returns the index of the player who won (0 for 1, 1 for 2). In case of a tie, player 2 wins.
*/
int TwoPlayers::checkWhoWon()
{
scores[0] = 0;
scores[1] = 0;
for(int a: tables[0]){
scores[0] += a;
}
for(int a: tables[1]){
scores[1] += a;
}
return scores[0] > scores[1];
}