-
Notifications
You must be signed in to change notification settings - Fork 0
/
Omokgame.cpp
261 lines (230 loc) · 4.63 KB
/
Omokgame.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
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
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;
void rowDefin(char[][5], int&);
void columnDefin(char[][5], int&);
void crossDefin(char[][5], int&);
void gamestatus(char[][5]);
void computerdrop(char[][5]);
void gameDefin(char[][5], int&);
void computerFirst(char[][5], int&);
void userFirst(char[][5], int&);
int main()
{
srand(time(0));
char board[5][5] = {};
int firstdrop;
int gamesetter = 1;
cout << "Welcome to the funny Omok game!\n\nThis is the game board.\n\n";
cout << " 0 1 2 3 4\n" << " -------------------\n";
for (int i = 0; i < 5; ++i)
{
cout << i << " | ";
for (int j = 0; j < 5; ++j)
{
board[i][j] = ' ';
cout << board[i][j] << " | ";
}
cout << "\n -------------------\n";
}
cout << "\nPlease select first drop.\n\nComputer = 1, User = 2\n\nWho? : ";
cin >> firstdrop;
while (firstdrop != 1 && firstdrop != 2)
{
cout << "\nOpen your eyes and select again.\n\n";
cin >> firstdrop;
}
cout << "\nOK. If you want to exit, Please enter -1 -1 in your coordinate.\n";
if (firstdrop == 1)
{
computerFirst(board, gamesetter);
}
else if (firstdrop == 2)
{
userFirst(board, gamesetter);
}
cout << "\n\nGame End.\n";
return 0;
}
void rowDefin(char board[][5], int& gamesetter)
{
for (int i = 0; i < 5; ++i)
{
int rowo = 0;
int rowx = 0;
for (int j = 0; j < 5; ++j)
{
if (board[i][j] == 'X')
{
++rowx;
}
else if (board[i][j] == 'O')
{
++rowo;
}
}
if (rowx == 5 || rowo == 5)
{
if (board[i][4] == 'X')
cout << "\nComputer Win!";
else if (board[i][4] == 'O')
cout << "\nUser Win!";
gamesetter = 0;
}
}
}
void columnDefin(char board[][5], int& gamesetter)
{
for (int i = 0; i < 5; ++i)
{
int columnx = 0;
int columno = 0;
for (int j = 0; j < 5; ++j)
{
if (board[j][i] == 'X')
++columnx;
else if (board[j][i] == 'O')
++columno;
}
if (columnx == 5 || columno == 5)
{
if (board[4][i] == 'X')
cout << "\nComputer Win!";
else if (board[4][i] == 'O')
cout << "\nUser Win!";
gamesetter = 0;
}
}
}
void crossDefin(char board[][5], int& gamesetter)
{
int cross1x = 0;
int cross1o = 0;
int cross2x = 0;
int cross2o = 0;
int i = 0;
for (i = 0; i < 5; ++i)
{
if (board[i][i] == 'X')
++cross1x;
else if (board[i][i] == 'O')
++cross1o;
}
if (cross1x == 5 || cross1o == 5)
{
if (board[4][4] == 'X')
cout << "\nComputer Win!";
else if (board[4][4] == 'O')
cout << "\nUser Win!";
gamesetter = 0;
}
int j = 4;
for (int i = 0; i < 5; ++i) //(0,4) (1,3) (2,2) (3,1) (4,0)
{
if (board[i][j] == 'X')
{
++cross2x;
}
else if (board[i][j] == 'O')
{
++cross2o;
}
--j;
}
if (cross2x == 5 || cross2o == 5)
{
if (board[4][0] == 'X')
cout << "\nComputer Win!";
else if (board[4][0] == 'O')
cout << "\nUser Win!";
gamesetter = 0;
}
}
void gamestatus(char board[][5])
{
cout << " 0 1 2 3 4\n" << " -------------------\n";
for (int i = 0; i < 5; ++i)
{
cout << i << " | ";
for (int j = 0; j < 5; ++j)
{
cout << board[i][j] << " | ";
}
cout << "\n -------------------\n";
}
}
void computerdrop(char board[][5])
{
int x = rand() % 5;
int y = rand() % 5;
while (board[x][y] != ' ')
{
x = rand() % 5;
y = rand() % 5;
}
board[x][y] = 'X';
}
void gameDefin(char board[][5], int& gamesetter)
{
rowDefin(board, gamesetter);
columnDefin(board, gamesetter);
crossDefin(board, gamesetter);
}
void computerFirst(char board[][5], int& gamesetter)
{
int x = 0;
int y = 0;
cout << "\nComputer first.\n";
while (gamesetter == 1)
{
cout << "\nComputer drop\n\n";
computerdrop(board);
gamestatus(board);
gameDefin(board, gamesetter);
if (gamesetter == 0)
break;
cout << "\nYour turn. Please enter your coordinate : ";
cin >> x >> y;
if (x == -1 || y == -1)
break;
while (board[x][y] != ' ')
{
cout << "\nNot empty space. Please enter your coordinate again : ";
cin >> x >> y;
}
cout << endl;
board[x][y] = 'O';
gamestatus(board);
gameDefin(board, gamesetter);
}
}
void userFirst(char board[][5], int& gamesetter)
{
int x = 0;
int y = 0;
cout << "\nUser first.\n";
while (gamesetter == 1)
{
cout << "\nYour turn. Please enter your coordinate : ";
cin >> x >> y;
if (x == -1 || y == -1)
break;
while (board[x][y] != ' ')
{
cout << "\nNot empty space. Please enter your coordinate again : ";
cin >> x >> y;
}
cout << endl;
board[x][y] = 'O';
gamestatus(board);
gameDefin(board, gamesetter);
if (gamesetter == 0)
break;
cout << "\nComputer drop.\n\n";
computerdrop(board);
gamestatus(board);
gameDefin(board, gamesetter);
}
}