-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtictactoe.cpp
More file actions
278 lines (241 loc) · 6.01 KB
/
tictactoe.cpp
File metadata and controls
278 lines (241 loc) · 6.01 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <iostream>
#include <string>
///error made:
//// -colInputs are row inouts
//// -rowInputs are column inputs
using namespace std;
bool gameOver;
char board[3][3];
string player1;
string player2;
int colInput1, rowInput1, colInput2, rowInput2, tempCol, tempRow;
int curr_player;
int gameMode;
int main_menu()
{
system("clear");
cout << "TIC-TAC-TOE Game" << endl << endl << "Choose Game Mode" << endl << endl << "1. Single Player" << endl << "2. MultiPlayer" << endl << "3. Options" << endl << "0. Exit Game" << endl;
cout << endl << "Which Mode: ";
cin >> gameMode;
if (gameMode > 3)
{
return 0;
}
return gameMode;
}
void setup()
{
gameOver = false;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
board[i][j] = ' ';
}
}
}
/////Draw the game board////
void draw()
{
system("clear");
cout << "Column:" << " 1 2 3" << endl;
for (int i = 0; i < 3; i++)
{
cout << "Row " << i+1 << ": ";
for (int j = 0; j< 3; j++)
{
cout << "[" << board[i][j] << "]";
}
cout << endl;
}
}
void input()
{
if (curr_player == 1)
{
cout << "Enter Row Number: ";
cin >> colInput1;
tempCol = colInput1;
cout << "Enter Column Number: ";
cin >> rowInput1;
tempRow = rowInput1;
curr_player = 2;
}
else if (curr_player == 2)
{
cout << "Enter Row Number: ";
cin >> colInput2;
tempCol = colInput2;
cout << "Enter Column Number: ";
cin >> rowInput2;
tempRow = rowInput2;
curr_player = 1;
}
}
void single_input()
{
if (curr_player == 1)
{
cout << "Enter Row Number: ";
cin >> colInput1;
tempCol = colInput1;
cout << "Enter Column Number: ";
cin >> rowInput1;
tempRow = rowInput1;
curr_player = 2;
}
else{
colInput2 = rand()%3;
tempCol = colInput2;
rowInput2 = rand()%3;
tempRow = rowInput2;
curr_player = 1;
}
}
bool rowCrossed()
{
for (int i = 0; i < 3; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
{
return true;
}
}
return false;
}
bool colCrossed()
{
for (int i = 0; i < 3; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
{
return true;
}
}
return false;
}
bool diagCrossed()
{
if ((board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ') || (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[2][0] != ' '))
{
return true;
}
else{
return false;
}
}
void logic()
{
if (curr_player == 2)
{
board[colInput1-1][rowInput1-1] = 'X';
}
else{
board[colInput2-1][rowInput2-1] = 'O';
}
if (rowCrossed() || colCrossed() || diagCrossed())
{
gameOver = true;
}
}
bool error_check()
{
////check for input out of bounds
if (colInput1 > 3 || colInput2 > 3 || rowInput1 > 3 || rowInput2 > 3)
{
cout << "Cell Out of Bounds" << endl;
//sleep(3); //////check how to use function
if (curr_player == 1)
curr_player = 2;
else{curr_player = 1;}
return true;
}
if (board[tempCol-1][tempRow-1] != ' ')
{
cout << "Cell already in use" << endl;
//sleep(3); //////check how to use function
if (curr_player == 1)
curr_player = 2;
else{curr_player = 1;}
return true;
}
return false;
}
int main()
{
while (gameMode != -1)
{
setup();
main_menu();
if (gameMode == 1)
{
system("clear");
cout << "Player Name [X]: ";
cin >> player1;
curr_player = 1;
while(!gameOver)
{
draw();
single_input();
if (error_check())
{
continue;
}
logic();
}
}
else if (gameMode == 2) {
system("clear");
cout << "Player 1 [X]: ";
cin >> player1;
///error checking here for player 1 inpout format
///////
cout << "Player 2 [O]: ";
cin >> player2;
//error checking here for player 2 input format
/////
curr_player = 1;
/////Game Loop//////
while(!gameOver)
{
draw();
input();
if(error_check())
{
continue;
}
logic();
}
////Winner Announcement/////
system("clear");
draw();
cout << "Winner is ";
if (curr_player == 2)
{
cout << player1 << endl;
}
else{
cout << player2 <<endl;
}
cout << "Enter <ANY LETTER> to continue...";
char enter;
cin >> enter;
}
else if (gameMode == 3)
{
system("clear");
cout << "HELP" << endl;
cout << "#################" << endl << endl;
cout << "Game Mode 1: Play against a Computer." << endl;
cout << "Game Mode 2: Play against a friend." << endl << endl;
cout << "Enter Row # and Column # to make a play!" << endl << endl;
cout << "GOOD LUCK" << endl << endl;
cout << "Enter <ANY LETTER> to continue...";
char enter;
cin >> enter;
}
else{
exit(0);
}
}
}