1 //++++++++++++++++++++++++++
2 // TicTacToe.cpp
3 //++++++++++++++++++++++++++
4
5 ///#include header
6 #include <iostream>
7 #include <conio.h>
8
9
10 ///Using
11 using namespace std;
12
13
14 ///Declare
15 char square[10] = {'o','1','2','3','4','5','6','7','8','9'};
16 int isquare[10] = {0,0,0,0,0,0,0,0,0,0};
17 int iCase = 0, iCriticalno,iDraw;
18 int iWinDraw();
19 void PrnBoard();
20 int iCriticalCHK(int iRisk);
21
22
23 ///main_in
24 main(int argc,char *argv[])
25 {
26
27
28 ///declare
29 int challenger = 1,i,choice;
30 char mark;
31 int iDanger = 1,iChance =-1;
32
33
34 ///PrnBoard
35 PrnBoard();
36
37
38 ///Prompt /Keyin
39 cout << " challenger ! " << " Please enter a number: ";
40 cin >> choice;
41
42
43 ///Loop to valid blank
44 while(choice > 9 || choice < 1) //
45 {
46
47
48 ///PrnBoard
49 PrnBoard();
50
51
52 ///Prompt /Keyin
53 cout << " challenger ! " << " Invalid ! Please enter a number: ";
54 cin >> choice;
55
56
57 ///+
58 }//end while
59
60
61 ///challenger 1st_shoot
62 mark='O';
63 //if (choice == 1 ) {square[1] = mark;isquare[1] = 1;}
64 square[choice] = mark;
65 isquare[choice] = 1;
66
67
68 ///case1?
69 if (choice==1 || choice==3 || choice==7 || choice ==9)
70 {square[5] = 'X'; isquare[5] = -1; iCase =1;}
71
72
73 ///case2?
74 if (choice==2 || choice==4 || choice==6 || choice ==8)
75 {square[5] = 'X'; isquare[5] = -1;}
76
77
78 ///case5?
79 if (choice==5)
80 {square[1] = 'X'; isquare[1] = -1;}
81
82
83 ///Loop to blank
84 choice=5;
85 while(isquare[choice] != 0 || choice > 9 || choice < 1) // isquare[5] can't be 0
86 {
87
88
89 ///PrnBoard
90 PrnBoard();
91
92
93 ///Prompt /Keyin
94 cout << " challenger ! " << " Please enter a valid number: ";
95 cin >> choice;
96
97
98 ///+
99 }//end while
100
101
102 ///challenger 2nd_shoot
103 square[choice] = mark;
104 isquare[choice] = 1;
105
106
107 ///CriticalCHK
108 iCriticalno = iCriticalCHK(iDanger);
109
110
111 ///critical?
112 if (iCriticalno==0)
113 {
114
115
116 ///case16
117 if (isquare[1]==1 && isquare[6]==1 ){square[3] = 'X'; isquare[3] = -1;}
118 if (isquare[3]==1 && isquare[8]==1 ){square[9] = 'X'; isquare[9] = -1;}
119 if (isquare[4]==1 && isquare[9]==1 ){square[7] = 'X'; isquare[7] = -1;}
120 if (isquare[2]==1 && isquare[7]==1 ){square[1] = 'X'; isquare[1] = -1;}
121
122
123 ///case18
124 if (isquare[1]==1 && isquare[8]==1 ){square[7] = 'X'; isquare[7] = -1;}
125 if (isquare[3]==1 && isquare[4]==1 ){square[1] = 'X'; isquare[1] = -1;}
126 if (isquare[2]==1 && isquare[9]==1 ){square[3] = 'X'; isquare[3] = -1;}
127 if (isquare[6]==1 && isquare[7]==1 ){square[9] = 'X'; isquare[9] = -1;}
128
129
130 ///case19
131 if (isquare[1]==1 && isquare[9]==1 ){square[2] = 'X'; isquare[2] = -1;}
132 if (isquare[3]==1 && isquare[7]==1 ){square[6] = 'X'; isquare[6] = -1;}
133 //if (isquare[9]==1 && isquare[1]==1 ){square[8] = 'X'; isquare[8] = -1;}
134 //if (isquare[7]==1 && isquare[3]==1 ){square[4] = 'X'; isquare[4] = -1;}
135
136
137 ///case2426
138 if (isquare[2]==1 && isquare[4]==1 ){square[3] = 'X'; isquare[3] = -1;}
139 if (isquare[2]==1 && isquare[6]==1 ){square[9] = 'X'; isquare[9] = -1;}
140 if (isquare[6]==1 && isquare[8]==1 ){square[7] = 'X'; isquare[7] = -1;}
141 if (isquare[4]==1 && isquare[8]==1 ){square[1] = 'X'; isquare[1] = -1;}
142
143
144 ///case27
145 //case16
146
147
148 ///case28
149 if (isquare[2]==1 && isquare[8]==1 ){square[3] = 'X'; isquare[3] = -1;}
150 if (isquare[4]==1 && isquare[6]==1 ){square[9] = 'X'; isquare[9] = -1;}
151
152
153 ///case29
154 //case18
155
156
157 ///case59
158 if (isquare[5]==1 && isquare[9]==1 ){square[3] = 'X'; isquare[3] = -1;}
159
160
161 ///+
162 }//end if
163
164
165 ///else
166 else
167 {
168
169
170 ///stuff critical
171 square[iCriticalno] = 'X';
172 isquare[iCriticalno] = -1;
173
174
175 ///+
176 }//end else
177
178
179 ///Loop to win/draw
180 while(1)
181 {
182
183
184 ///Loop to blank
185 choice=5;
186 while(isquare[choice] != 0 || choice > 9 || choice < 1) // isquare[5] can't be 0
187 {
188
189
190 ///PrnBoard
191 PrnBoard();
192
193
194 ///Prompt /Keyin
195 cout << " challenger ! " << " Please enter one valid number: ";
196 cin >> choice;
197
198
199 ///+
200 }//end while
201
202
203 ///Keyin Step3~
204 square[choice] = 'O';
205 isquare[choice] = 1;
206
207
208 ///iWinDraw
209 iDraw = iWinDraw();
210
211
212 ///=1(att)
213 if (iDraw==1 )
214 {
215 PrnBoard();
216 cout<<"==>\aChallenger " <<" win ";
217 return 0;
218 }
219
220
221 ///Full?
222 if (isquare[1] *isquare[2] *isquare[3]*isquare[4] *isquare[6] *isquare[7]*isquare[7] *isquare[9] != 0 )
223 {
224 PrnBoard();
225 cout<<"==>\aDraw ";
226 return 0;
227 }
228
229
230 ///check chance
231 iCriticalno = iCriticalCHK(iChance);
232
233
234 ///favor?
235 if (iCriticalno != 0)
236 {
237
238
239 ///stuff favor
240 square[iCriticalno] = 'X';
241 isquare[iCriticalno] = -1;
242
243
244 ///iWinDraw
245 iDraw = iWinDraw();
246
247
248 ///=-1(dfn)
249 if (iDraw==-1 )
250 {
251 PrnBoard();
252 cout<<"==>\aComputer " <<" win ";
253 return 0;
254 }
255
256
257 ///+
258 }//end if
259
260
261 ///check danger
262 iCriticalno = iCriticalCHK(iDanger);
263
264
265 ///danger?
266 if (iCriticalno!=0) //critical number occur
267 {
268
269
270 ///stuff danger
271 square[iCriticalno] = 'X';
272 isquare[iCriticalno] = -1;
273
274
275 ///iWinDraw
276 iDraw = iWinDraw();
277
278
279 ///=-1(dfn)
280 if (iDraw==-1 )
281 {
282 PrnBoard();
283 cout<<"==>\aComputer " <<" win ";
284 return 0;
285 }
286
287
288 ///+
289 }//end if
290
291
292 ///else
293 else
294 {
295
296
297 ///stuff anyblank
298 if( isquare[1] ==0) {square[1] = 'X'; isquare[1] = -1;}
299 else if( isquare[2] ==0) {square[2] = 'X'; isquare[2] = -1;}
300 else if( isquare[3] ==0) {square[3] = 'X'; isquare[3] = -1;}
301 else if( isquare[4] ==0) {square[4] = 'X'; isquare[4] = -1;}
302 else if( isquare[6] ==0) {square[6] = 'X'; isquare[6] = -1;}
303 else if( isquare[7] ==0) {square[7] = 'X'; isquare[7] = -1;}
304 else if( isquare[8] ==0) {square[8] = 'X'; isquare[8] = -1;}
305 else if( isquare[9] ==0) {square[9] = 'X'; isquare[9] = -1;}
306
307
308 ///iWinDraw
309 iDraw = iWinDraw();
310
311
312 ///=-1(dfn)
313 if (iDraw==-1 )
314 {
315 PrnBoard();
316 cout<<"==>\aComputer " <<" win ";
317 return 0;
318 }
319
320
321 ///+
322 }//end else
323
324
325 ///+
326 }//end while
327
328
329 ///main_end
330 return 0;
331 } //main_end
332
333
334 ///void PrnBoard
335 void PrnBoard()
336 {
337 //system("cls");
338 //clrscr();
339 // cout << "\n\n\tTic Tac Toe\n\n";
340 cout << endl;
341 cout << " | | " <<endl;
342 cout << " " << square[1] << " | " << square[2] << " | " << square[3] << endl;
343 cout << "_____|_____|_____" << endl;
344 cout << " | | " << endl;
345 cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl;
346 cout << "_____|_____|_____" << endl;
347 cout << " | | " << endl;
348 cout << " " << square[7] << " | " << square[8] << " | " << square[9] << " Challenger (O) - Computer (X)"<<endl;
349 cout << " | | " ;//<< endl << endl;
350 }
351
352
353 ///int iCriticalCHK
354 int iCriticalCHK(int iRisk)
355 {
356
357
358 ///row123
359 if ((isquare[1] +isquare[2] +isquare[3]) ==iRisk*2 )
360 {
361 if (isquare[1] == 0 ) return 1;
362 if (isquare[2] == 0 ) return 2;
363 if (isquare[3] == 0 ) return 3;
364 }
365
366
367 ///row456
368 if (isquare[4] +isquare[5] +isquare[6] ==iRisk*2 )
369 {
370 if (isquare[4] == 0 ) return 4;
371 if (isquare[5] == 0 ) return 5;
372 if (isquare[6] == 0 ) return 6;
373 }
374
375
376 ///row789
377 if (isquare[7] +isquare[8] +isquare[9] ==iRisk*2 )
378 {
379 if (isquare[7] == 0 ) return 7;
380 if (isquare[8] == 0 ) return 8;
381 if (isquare[9] == 0 ) return 9;
382 }
383
384
385 ///col147
386 if (isquare[1] +isquare[4] +isquare[7] ==iRisk*2 )
387 {
388 if (isquare[1] == 0 ) return 1;
389 if (isquare[4] == 0 ) return 4;
390 if (isquare[7] == 0 ) return 7;
391 }
392
393
394 ///col258
395 if (isquare[2] +isquare[5] +isquare[8] ==iRisk*2 )
396 {
397 if (isquare[2] ==0 ) return 2;
398 if (isquare[5] == 0 ) return 5;
399 if (isquare[8] == 0 ) return 8;
400 }
401
402
403 ///col369
404 if (isquare[3] +isquare[6] +isquare[9] ==iRisk*2 )
405 {
406 if (isquare[3] == 0 ) return 3;
407 if (isquare[6] == 0 ) return 6;
408 if (isquare[9] == 0 ) return 9;
409 }
410
411
412 ///cro159
413 if (isquare[1] +isquare[5] +isquare[9] ==iRisk*2 )
414 {
415 if (isquare[1] == 0 ) return 1;
416 if (isquare[5] == 0 ) return 5;
417 if (isquare[9] == 0 ) return 9;
418 }
419
420
421 ///cro357
422 if (isquare[3] +isquare[5] +isquare[7] ==iRisk*2 )
423 {
424 if (isquare[3] == 0 ) return 3;
425 if (isquare[5] == 0 ) return 5;
426 if (isquare[7] == 0 ) return 7;
427 }
428
429
430 ///+
431 return 0;
432 }//end Def , return critical loc or 0
433
434
435 ///int iWinDraw
436 int iWinDraw()
437 {
438
439
440 ///challenger1 defender-1 draw 0
441 if (isquare[1] +isquare[2] +isquare[3] ==3) return 1;
442 if (isquare[4] +isquare[5] +isquare[6] ==3) return 1;
443 if (isquare[7] +isquare[8] +isquare[9] ==3) return 1;
444 if (isquare[1] +isquare[4] +isquare[7] ==3) return 1;
445 if (isquare[2] +isquare[5] +isquare[8] ==3) return 1;
446 if (isquare[3] +isquare[6] +isquare[9] ==3) return 1;
447 if (isquare[1] +isquare[5] +isquare[9] ==3) return 1;
448 if (isquare[3] +isquare[5] +isquare[7] ==3) return 1;
449 if (isquare[1] +isquare[2] +isquare[3] ==-3) return -1;
450 if (isquare[4] +isquare[5] +isquare[6] ==-3) return -1;
451 if (isquare[7] +isquare[8] +isquare[9] ==-3) return -1;
452 if (isquare[1] +isquare[4] +isquare[7] ==-3) return -1;
453 if (isquare[2] +isquare[5] +isquare[8] ==-3) return -1;
454 if (isquare[3] +isquare[6] +isquare[9] ==-3) return -1;
455 if (isquare[1] +isquare[5] +isquare[9] ==-3) return -1;
456 if (isquare[3] +isquare[5] +isquare[7] ==-3) return -1;
457 return 0;
458
459
460 ///+
461 }//end Def , return critical loc
462
463
This Blog is dedicated to Sir Johnson Lin
challenge computer on TicTacTo game 挑戰電腦井字棋
標籤:
井字棋,
流程圖,
程式碼,
C 語言,
C/C++,
C/CPP,
examples,
flow chart,
source code,
TicTacToe
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言