-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHexGame.h
71 lines (59 loc) · 1.93 KB
/
HexGame.h
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
#ifndef HEXGAME_H_
#define HEXGAME_H_
class HexGame
{
private:
Board *board;
Player *player[2];
public:
HexGame(Board *b, Player *p1, Player *p2)
{
board = b;
player[0] = p1;
player[1] = p2;
}
int Play(bool showAll, int startPlayer);
};
int HexGame::Play(bool showAll, int startPlayer)
{
if (!showAll)
system("CLS");
bool won = false; //By default, no one has won
board->PrintBoard(); //Print the game board to the screen
board->addCells(); //Initialise the list of free spots
board->SetTurn(startPlayer);
//Loop until someone has won or the board has been filled
while (!won && !board->isBoardFull())
{
int playerType = board->GetTurn(); //Determine who's turn it is
int playerIndex = (playerType == player[0]->GetType()) ? 0 : 1;
int x = -1;
int y = -1;
//Get the coordinates of the move from the appropriate player
if (!player[playerIndex]->GetMove(board, x, y))
{
//Debug if we are unable to make the move due to an error or full board
cout << "ERROR: No available move for ";
board->printCoord(x + 1, y + 1, false);
cout << endl;
return 0;
}
if (!showAll)
system("CLS");
//Display the move that was made
cout << player[playerIndex]->GetSymbol() << " played ";
board->printCoord(x + 1, y + 1, false);
//Add the move to our board
if (!board->AddMove(playerType, x, y))
return 0;
board->PrintBoard(); //Print the new board (old board + this move)
won = board->CheckForWin(playerType); //Check to see if this player has just won. If so, end the game and tell the players
if (won)
{
cout << player[playerIndex]->GetName() << " player wins!" << endl;
return playerType;
}
}
return 0;
}
#endif /* HEXGAME_H_ */