-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
70 lines (63 loc) · 2.17 KB
/
main.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
// Main.cpp
// A basic Tic Tac Toe Game Written in C++ Language
// compile this three files at the same time "main.cpp", "TicTacToeClasses.cpp", "TicTacToeFunctions.cpp"
#include "TicTacToeClasses.cpp"
#include "TicTacToeFunctions.cpp"
int main()
{
Board gameBoard;
string nameX;
string nameO;
int tieGame = 0;
char gameWinner = 'z';
int numTurns = 0;
//get the names of the players
getUserNames(nameX, nameO);
//the game is played for 8 turns maximum
while(numTurns < 8)
{
//print a board that has the postions numbered
printTheBoard(gameBoard);
//ask player x where they want to put an 'x'
printUserPrompt(nameX, 'x');
//check that the input is a valid position and that
//it has not already been taken
checkResponse(gameBoard, 'x');
//check to see if player 'x' has four in a row somewhere on the board
gameWinner = gameBoard.determineWinner();
//if player 'x' has won, end the game
if(gameWinner != 'z')
{
printTheBoard(gameBoard);
writeTheBoard(gameBoard);
printGameWinner(gameBoard, nameX, nameO);
break;
}
//Now do the same for player 'o'
//print a board that has the postions numbered
printTheBoard(gameBoard);
writeTheBoard(gameBoard);
//ask player x where they want to put an 'o'
printUserPrompt(nameO, 'o');
//check that the input is a valid position and that
//it has not already been taken
checkResponse(gameBoard, 'o');
printTheBoard(gameBoard);
writeTheBoard(gameBoard);
//check to see if player 'o' has four in a row somewhere on the board
gameWinner = gameBoard.determineWinner();
//if player 'o' has won, end the game
if(gameWinner != 'z')
{
printTheBoard(gameBoard);
writeTheBoard(gameBoard);
printGameWinner(gameBoard, nameX, nameO);
break;
}
numTurns++;
}
//if there is no winner at this point, the game is a tie
if(numTurns >= 8)
cout<<"Tie game.\n\n";
return 0;
}