blob: 7ba60a1209ab2906b2560369ca1d0a30d57a8ce7 (
plain)
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
|
#include <iostream>
#include "Player.h"
#include "Gameboard.h"
using namespace std;
/*
* Logic for a player to make one complete turn
* @param gameboard [Gameboard] the gameboard this turn takes place on
* @param player [Player] the player who is making this turn
* @return [Boolean] this turn has ended the game
*/
int playerTurn(Gameboard& gameboard,Player& player) {
gameboard.printGameboard();
cout << player.getName() << "'s turn:" << endl;
bool playerMoved = gameboard.playMove(player.getSymbol(),player.getMoveY(),player.getMoveX());
while(playerMoved != true) {
cout << player.getName() << " made an invalid move! Try again." << endl;
playerMoved = gameboard.playMove(player.getSymbol(),player.getMoveY(),player.getMoveX());
}
int endGame = gameboard.checkWin(player.getPlayerID(),player.getSymbol());
return endGame;
}
int main()
{
// Initialize game
cout << "Welcome to TicTacToe!" << endl << endl;
// Initialize gameboard
Gameboard gameboard;
// Initialize players
Player player1(1);
Player player2(2);
// Do gameloop
int endGame = 0;
while(!endGame) {
endGame = playerTurn(gameboard,player1);
if(endGame){break;}
endGame = playerTurn(gameboard,player2);
}
// Print final gameboard
gameboard.printGameboard();
// Print winning player
if(endGame == player1.getPlayerID()) {
cout << player1.getName() << " Wins!" << endl;
}
if(endGame == player2.getPlayerID()) {
cout << player2.getName() << " Wins!" << endl;
}
return 0;
}
|