-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
203 lines (183 loc) · 6.17 KB
/
main.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
203
#include <iostream>
#include <bits/stdc++.h>
#include <climits>
using namespace std;
const char PLAYER_X = 'X';
const char PLAYER_O = 'O';
char board[3][3] =
{
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
};
void displayBoard()
{
char lastPlayer = PLAYER_O;
char currentPlayer = PLAYER_X;
int chosenSquare=0;
/*Loop through and display the current board state!*/
for (chosenSquare; chosenSquare <=INT_MAX; chosenSquare--)
{
cout << " " << endl;
cout << "current board state: "<<endl;
cout << " " << endl;
cout << "+-----+" << endl;
for(int y = 0; y < 3; y++)
{
for(int x = 0; x < 3; x++)
{
cout << board[y][x]<<"|";
}
cout << endl;
}
cout <<"+-----+"<<endl;
cout << " " << endl;
cout << "Player " << currentPlayer <<" enter a number between 1 and 9: ";
cin >> chosenSquare;
// if input is invalid
if(chosenSquare > 9 || chosenSquare < 1)
{
cout << "Not a valid choice. Try Again." <<endl;
}
/* check for availability of squares */
else if(chosenSquare == 1 && (board[0][0]=='X' || board[0][0]=='O'))
{
cout<<"That square is not available. Try again." <<endl;
}
else if(chosenSquare == 2 && (board[0][1]=='X' || board[0][1]=='O'))
{
cout<<"That square is not available. Try again." <<endl;
}
else if(chosenSquare == 3 && (board[0][2]=='X' || board[0][2]=='O'))
{
cout<<"That square is not available. Try again." <<endl;
}
else if(chosenSquare == 4 && (board[1][0]=='X' || board[1][0]=='O'))
{
cout<<"That square is not available. Try again." <<endl;
}
else if(chosenSquare == 5 && (board[1][1]=='X' || board[1][1]=='O'))
{
cout<<"That square is not available. Try again." <<endl;
}
else if(chosenSquare == 6 && (board[1][2]=='X' || board[1][2]=='O'))
{
cout<<"That square is not available. Try again." <<endl;
}
else if(chosenSquare == 7 && (board[2][0]=='X' || board[2][0]=='O'))
{
cout<<"That square is not available. Try again." <<endl;
}
else if(chosenSquare == 8 && (board[2][1]=='X' || board[2][1]=='O'))
{
cout<<"That square is not available. Try again." <<endl;
}
else if(chosenSquare == 9 && (board[2][2]=='X' || board[2][2]=='O'))
{
cout<<"That square is not available. Try again." <<endl;
}
else
{
// update the array with the player's input and swap player
switch (chosenSquare)
{
case 1:
board[0][0] = currentPlayer;
swap(lastPlayer,currentPlayer);
break;
case 2:
board[0][1] = currentPlayer;
swap(lastPlayer,currentPlayer);
break;
case 3:
board[0][2] = currentPlayer;
swap(lastPlayer,currentPlayer);
break;
case 4:
board[1][0] = currentPlayer;
swap(lastPlayer,currentPlayer);
break;
case 5:
board[1][1] = currentPlayer;
swap(lastPlayer,currentPlayer);
break;
case 6:
board[1][2] = currentPlayer;
swap(lastPlayer,currentPlayer);
break;
case 7:
board[2][0] = currentPlayer;
swap(lastPlayer,currentPlayer);
break;
case 8:
board[2][1] = currentPlayer;
swap(lastPlayer,currentPlayer);
break;
case 9:
board[2][2] = currentPlayer;
swap(lastPlayer,currentPlayer);
break;
default:
break;
}
/*check for winning conditions*/
if(board[0][0]==currentPlayer && board[0][1]==currentPlayer && board[0][2]==currentPlayer)
{
cout<<"Player "<<currentPlayer<<" wins on the top row"<<endl;
break;
}
if(board[1][0]==currentPlayer && board[1][1]==currentPlayer && board[1][2]==currentPlayer)
{
cout<<"Player "<<currentPlayer<<" wins on the middle row"<<endl;
break;
}
if(board[2][0]==currentPlayer && board[2][1]==currentPlayer && board[2][2]==currentPlayer)
{
cout<<"Player "<<currentPlayer<<" wins on the last row"<<endl;
break;
}
if(board[0][0]==currentPlayer && board[1][0]==currentPlayer && board[2][0]==currentPlayer)
{
cout<<"Player "<<currentPlayer<<" wins on the first column"<<endl;
break;
}
if(board[0][1]==currentPlayer && board[1][1]==currentPlayer && board[2][1]==currentPlayer)
{
cout<<"Player "<<currentPlayer<<" wins on the second column"<<endl;
break;
}
if(board[0][2]==currentPlayer && board[1][2]==currentPlayer && board[2][2]==currentPlayer)
{
cout<<"Player "<<currentPlayer<<" wins on the third column"<<endl;
break;
}
if(board[0][0]==currentPlayer && board[1][1]==currentPlayer && board[2][2]==currentPlayer)
{
cout<<"Player "<<currentPlayer<<" wins diagonally"<<endl;
break;
}
if(board[0][2]==currentPlayer && board[1][1]==currentPlayer && board[2][0]==currentPlayer)
{
cout<<"Player "<<currentPlayer<<" wins diagonally"<<endl;
break;
}
// check for stalemate
if((board[0][0]== 'X'|| board[0][0]=='O')&& (board[0][1]== 'X'|| board[0][1]=='O') && (board[0][2]== 'X'|| board[0][2]=='O')
&& (board[1][0]== 'X'|| board[1][0]=='O') && (board[1][1]== 'X'|| board[1][1]=='O') && (board[1][2]== 'X'|| board[1][2]=='O')
&& (board[2][0]== 'X'|| board[2][0]=='O') && (board[2][1]== 'X'|| board[2][1]=='O') && (board[2][2]== 'X'|| board[2][2]=='O'))
{
cout<<"Stalemate!"<<endl;
break;
}
}
}
// line breaks
cout<<" "<<endl;
cout<<" "<<endl;
}
int main()
{
cout<<"Welcome to my TicTacToe game. Developed in C++ by Uchenna Adubasim."<<endl;
displayBoard();
return 0;
}