-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicTacToe.cpp
executable file
·64 lines (46 loc) · 1.31 KB
/
TicTacToe.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
#include "Board.h"
using namespace std;
int ChooseUserTurn(){
int input = 0;
while(input != 1 && input != -1){
cout << "Enter 1 to go first (X) or 2 to go second (O): ";
cin >> input;
if(input == 2) input = -1;
}
return input;
}
bool PlayOrQuit(){
string str;
cout << "Press Q to Quit, or any other key to play again: ";
cin.ignore();
getline (cin, str);
return !(str == "q" || str == "Q");
}
int main(){
int spot;
int first = 0;
bool UserIsStillPlaying = true;
Board BlankBoard;
Board * CurrentBoard;
BlankBoard.CreateTree();
BlankBoard.ScoreNodesInTree();
while(UserIsStillPlaying){
CurrentBoard = &BlankBoard;
first = ChooseUserTurn();
while(!CurrentBoard->GameIsOver()){
if(first == -1 && !CurrentBoard->GameIsOver())
CurrentBoard = &(*(CurrentBoard->MakeMachineMove((-1)*first)));
CurrentBoard->DrawBoard();
if(CurrentBoard->GameIsOver())
break;
spot = CurrentBoard->GetUserInput();
CurrentBoard = &(*(CurrentBoard->FindNextBoard(spot, first)));
if(first == 1 && !CurrentBoard->GameIsOver())
CurrentBoard = &(*(CurrentBoard->MakeMachineMove((-1)*first)));
}
CurrentBoard->PrintEndOfGameMessage(first);
UserIsStillPlaying = PlayOrQuit();
first = 0;
}
return 0;
}