-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathticTacToe.cpp
More file actions
167 lines (156 loc) · 4.15 KB
/
ticTacToe.cpp
File metadata and controls
167 lines (156 loc) · 4.15 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include <cstring>
using namespace std;
const int BLANK = 0;
const int X_MOVE = 1;
const int O_MOVE = 2;
const int X_TURN = 0;
const int O_TURN = 1;
void printBoard(int board[3][3]);
bool checkWin(int player, int board[3][3]);
bool checkTie(int board[3][3]);
void clearBoard(int board[3][3]);
int main() {
int board[3][3];
char input[81];
int turn = X_TURN;
int totalXWins = 0;
int totalOWins = 0;
bool stillPlaying = true;
clearBoard(board);
// This is the start of the multi game loop
while (stillPlaying == true) {
// This is the start of a single game loop
while (checkWin(X_MOVE, board) == false && checkWin(O_MOVE, board) == false
&& checkTie(board) == false) {
printBoard(board);
// Ask user if they still want to play
cin >> input;
// Validate user input
if (strlen(input) != 2) {
cout << "Enter a letter followed by a number"<< endl;
} else if (input[0] != 'a' && input[0]!= 'b'
&& input[0] != 'c') {
cout <<"Row must be an a, b, or c."<< endl;
} else if (input[1] != '1' && input[1] != '2'
&& input[1] != '3') {
cout << "Column must be an 1, 2, or 3." << endl;
} else {
// User input was valid update game state
int row = input[0] - 'a';
int column = input[1] - '1';
// Make sure that the board space is available
if (board[row][column] == BLANK) {
if (turn == X_TURN) {
board[row][column] = X_MOVE;
turn = O_TURN;
} else {
board[row][column] = O_MOVE;
turn = X_TURN;
}
} else {
cout << "There is a piece there!" << endl;
}
}
} // End of single game loop
// Check who won
if (checkWin(X_MOVE, board) == true) {
cout << "X Wins!" << endl;
totalXWins++;
} else if (checkWin(O_MOVE, board) == true) {
cout << "O Wins!" << endl;
totalOWins++;
} else {
// Must be a tie
cout << "Tie Game!" << endl;
}
// Ask if they want to keep
// playing
cout << "Play again?"<< endl;
char yesno[81];
cin >> yesno;
if (strcmp(yesno, "yes") == 0 || strcmp(yesno, "y") == 0) {
stillPlaying = true;
// Reset the board
clearBoard(board);
//Set turn to X
turn = X_TURN;
// Print total Wins
cout << "X has won:" << totalXWins << " O has won:" <<
totalOWins << endl;
} else {
stillPlaying = false;
}
} // End of multi game loop
}
void printBoard(int board[3][3]) {
cout << " \t1\t2\t3" << endl;
for (int row = 0; row < 3; row++) {
cout << (char)('a' + row) << "\t";
for (int column = 0; column < 3; column++) {
if (board[row][column] == BLANK) {
cout << " \t";
} else if (board[row][column] == X_MOVE) {
cout << "X\t";
} else if (board[row][column] == O_MOVE) {
cout << "O\t";
}
}
cout << endl;
}
}
bool checkWin(int player, int board[3][3]) {
// Row Wins
if (board[0][0] == player && board[0][1] == player
&& board[0][2] == player) {
return true;
}
if (board[1][0] == player && board[1][1] == player
&& board[1][2] == player) {
return true;
}
if (board[2][0] == player && board[2][1] == player
&& board[2][2] == player) {
return true;
}
// Column Wins
if (board[0][0] == player && board[1][0] == player
&& board[2][0] == player) {
return true;
}
if (board[0][1] == player && board[1][1] == player
&& board[2][1] == player) {
return true;
}
if (board[0][2] == player && board[1][2] == player
&& board[2][2] == player) {
return true;
}
// Diagonal Wins
if (board[0][0] == player && board[1][1] == player
&& board[2][2] == player) {
return true;
}
if (board[0][2] == player && board[1][1] == player
&& board[2][0] == player) {
return true;
}
return false;
}
bool checkTie(int board[3][3]) {
for (int row = 0; row < 3; row++) {
for (int column = 0; column < 3; column++) {
if (board[row][column] == BLANK) {
return false;
}
}
}
return true;
}
void clearBoard(int board[3][3]) {
for (int row = 0; row < 3; row++) {
for (int column = 0; column < 3; column++) {
board[row][column] = 0;
}
}
}Cpp-TicTacToe