-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic tac toe.cpp
202 lines (157 loc) · 4 KB
/
tic tac toe.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int ROWS = 3, COLUMNS = 3;
void introAndRules();
void drawBoard(const vector<vector<char>>& board);
void game(char player, vector<vector<char>> board);
void playerTurn(char player, vector<vector<char>>& board);
char getMove();
bool validMove(char move, const vector<vector<char>>& board);
void updateBoard(char player, char move, vector<vector<char>>& board);
char gameStatus(char player, const vector<vector<char>>& board);
int main()
{
// Setup:
vector<vector<char>> board =
{
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'}
};
char player = ' ';
char again;
introAndRules();
drawBoard(board);
// Game Loop:
do
{
game(player, board);
cout << "Would you like to play again? (y/n): ";
cin >> again;
cout << "\n";
} while (again == 'y');
cout << "Game Over!\n";
return 0;
}
inline void introAndRules()
{
cout << "\t\tTIC-TAC-TOE\n\n";
cout << "How to play TIC-TAC-TOE:\n\n";
cout << "There will be 2 players: \nThe 1st player will play with the letter 'X'.\nThe 2nd player will play with the letter 'O'.\n\n";
cout << "Your Goal:\nGet 3 of your letters in a row (up, down, across, or diagonally) to win!\nMake sure to block your opponent's letters to stop them from wining!\n\n";
cout << "Instructions:\nInput where you'd like to place your character from numbers 1-9.\n\n";
}
void drawBoard(const vector<vector<char>>& board)
{
for (int i = 0; i < 3; ++i)
{
cout << "\t\t";
for (int j = 0; j < 3; ++j)
{
if (j != 2) cout << " " << board[i][j] << " |";
else cout << " " << board[i][j] << " ";
}
if (i != 2) cout << "\n\t\t--- --- ---\n";
else cout << "\n\n";
}
}
void game(char player, vector<vector<char>> board)
{
int moves = 0;
while (true)
{
player = 'X';
cout << "Player: " << player << ", it's your turn.\n";
playerTurn(player, board);
++moves;
if (moves == 9)
{
cout << "Tied!\n\n";
break;
}
drawBoard(board);
if (gameStatus(player, board) == player)
{
cout << "Player 'X' won!\n\n";
break;
}
player = 'O';
cout << "Player: " << player << ", it's your turn.\n";
playerTurn(player, board);
++moves;
if (moves == 9)
{
cout << "Tied!\n\n";
break;
}
drawBoard(board);
if (gameStatus(player, board) == player)
{
cout << "Player 'O' won!\n\n";
break;
}
}
}
void playerTurn(char player, vector<vector<char>>& board)
{
char move;
while (true)
{
move = getMove();
if (validMove(move, board)) break;
}
updateBoard(player, move, board);
}
char getMove()
{
char move;
do
{
cout << "Please enter your move (1-9): ";
cin >> move;
} while (!isdigit(move) || move == '0');
cout << "\n";
return move;
}
bool validMove(char move, const vector<vector<char>>& board)
{
for (vector<vector<char>>::const_iterator i = board.begin(); i != board.end(); ++i)
{
for (vector<char>::const_iterator j = i->begin(); j != i->end(); ++j)
if (*j == move) return true;
}
return false;
}
void updateBoard(char player, char move, vector<vector<char>>& board)
{
for (vector<vector<char>>::iterator i = board.begin(); i != board.end(); ++i)
{
for (vector<char>::iterator j = i->begin(); j != i->end(); ++j)
{
if (*j == move) *j = player;
}
}
}
char gameStatus(char player, const vector<vector<char>>& board)
{
// Verticals
for (int i = 0; i < 3; ++i)
{
if (board[0][i] == player && board[1][i] == player && board[2][i] == player)
return player;
}
// Horizontals
for (int i = 0; i < 3; ++i)
{
if (board[i][0] == player && board[i][1] == player && board[i][2] == player)
return player;
}
// Diagonals
if (board[0][0] == player && board[1][1] == player && board[2][2] == player)
return player;
if (board[0][2] == player && board[1][1] == player && board[2][0] == player)
return player;
return ' ';
}