-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtictactoe.cpp
More file actions
220 lines (180 loc) · 5.83 KB
/
tictactoe.cpp
File metadata and controls
220 lines (180 loc) · 5.83 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <vector>
#include <iostream>
using namespace std;
const bool CLEAR_SCREEN = true;
/// @brief Utilizes an escape character sequence to clear the screen
void clearScreen()
{
cout << endl;
if (CLEAR_SCREEN)
{
cout << "\033c";
}
cout << endl;
}
/// @brief Draws the provided tic-tac-toe board to the screen
// @param board is the tic-tac-toe board that should be drawn
void drawBoard(const vector <char> &board)
{
clearScreen();
for (int i = 0; i < 9; i += 3)
{
cout << " " << board.at(i) << " | " << board.at(i + 1) << " | "
<< board.at(i + 2) << " " << endl;
if (i < 6)
cout << "-----|-----|-----" << endl;
}
cout << endl;
}
/// @brief Fills vector with characters starting at lower case a.
///
/// The amount of the alphabet placed into the vector depends on its size.
/// When the vector is size 1 then it will have only character a.
/// When the vector is size 5 then it will have characters a to e.
/// When the vector is size 26 then it will have characters a to z.
///
/// @param v the vector to initialize
/// @pre-condition the vector size will never be over 26
void initVector(vector <char> &v)
{
//not sure why we needed the entire alphabet, but whatever.
if (v.size() <= 26)
{
for (int i = 0; i < v.size(); i++)
{
v.at(i) = 'a' + i;
}
}
return;
}
/// @brief Converts a character representing a cell to associated vector index
/// @param the position to be converted to a vector index
/// @return the integer index in the vector, should be 0 to (vector size - 1)
int convertPosition(char position)
{
//using the function above, name a new vector to compare the inputted
//character position.
vector <char> v1(9);
initVector(v1);
for (int i = 0; i < v1.size(); i++)
{
if (position == v1.at(i))
return i;
}
return 10;
}
/// @brief Predicate function to determine if a spot in board is available.
/// @param board the current tic-tac-toe board
/// @param position is an index into vector to check if available
/// @return true if position's state is available (not marked) AND is in bounds
bool validPlacement(const vector <char> &board, int position)
{
//if there is an empty statement then this function becomes true.
if (position < board.size())
{
if ((board.at(position) != 'x' && board.at(position) != 'o'))
return true;
}
else
return false;
}
/// @brief Acquires a play from the user as to where to put her mark
///
/// Utilizes convertPosition and validPlacement functions to convert the
/// user input and then determine if the converted input is a valid play.
///
/// @param board the current tic-tac-toe board
/// @return an integer index in board vector of a chosen available board spot
int getPlay(const vector <char> &board)
{
//the position inputted by user.
char position;
int index;
do
{
cout << "Please choose a position: ";
cin >> position;
cout << endl;
index = convertPosition (position);
} while ((!validPlacement(board, index)));
return index;
}
/// @brief Predicate function to determine if the game has been won
///
/// Winning conditions in tic-tac-toe require three marks from same
/// player in a single row, column or diagonal.
///
/// @param board the current tic-tac-toe board
/// @return true if the game has been won, false otherwise
bool gameWon(const vector <char> &board)
{
//code to determine if we have 3 in a row horizontally, then vertically
for (int i = 0; i < 3; i++)
{
if (board.at(3*i) == board.at(3*i+1)
&& board.at(3*i+1) == board.at(3*i+2))
return true;
else if (board.at(i) == board.at(i+3) && board.at(i+3) == board.at(i+6))
return true;
}
//code to determine if we have 3 in a row diagonally.
if (board.at(0) == board.at(4) && board.at(4) == board.at(8))
return true;
else if (board.at(2) == board.at(4) && board.at(4) == board.at(6))
return true;
return false;
}
/// @brief Predicate function to determine if the board is full
/// @param board the current tic-tac-toe board
/// @return true iff the board is full (no cell is available)
bool boardFull(const vector <char> &board)
{
//game is a draw when 9 turns have gone by with no winner.
int j = 0;
for (int i = 0; i < board.size(); i++)
{
if (board.at(i) == 'x' || board.at(i) == 'o')
j++;
}
if(j == 9)
return true;
else
return false;
}
// Global constants for player representation
const int PLAYER1 = 0;
const int PLAYER2 = 1;
int main()
{
// Variables that you may find useful to utilize
vector <char> board(9);
int curPlay;
int turn = PLAYER1; // Player 1 always goes first and is 'x'
/// TODO: Initialize board to empty state
initVector(board);
/// TODO: Display empty board
drawBoard(board);
/// TODO: Play until game is over
while (!boardFull(board) && !gameWon(board))
{
curPlay = getPlay(board);
board.at(curPlay) = (turn == PLAYER1) ? 'x' : 'o';
// Turn switches only when board is not full or game is not won
if (!boardFull(board) && !gameWon(board))
{
if (turn == PLAYER1)
turn = PLAYER2;
else if (turn == PLAYER2)
turn = PLAYER1;
}
drawBoard(board);
}
if (turn == PLAYER1 && gameWon(board))
cout << "Player 1 (x's) wins!" << endl << endl;
else if (turn == PLAYER2 && gameWon(board))
cout << "Player 2 (o's) wins!" << endl << endl;
else
cout << "No one wins" << endl << endl;
/// TODO: Determine winner
return 0;
}