This Blog is dedicated to Sir Johnson Lin
Learn flow chart from C language , master C with the chart!

井字棋程式設計 Tic-Tac-Toe game design

D:\CPPcode\TicTacToe\TicTacToe.c++
  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          }//exit
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              ///+
276          }//end if
277          
278          
279          ///else
280          else
281          {
282              
283              
284              ///stuff anyblank
285              if( isquare[1] ==0)  {square[1] = 'X'; isquare[1] = -1;}
286              else if( isquare[2] ==0)  {square[2] = 'X'; isquare[2] = -1;}
287              else if( isquare[3] ==0)  {square[3] = 'X'; isquare[3] = -1;}
288              else if( isquare[4] ==0)  {square[4] = 'X'; isquare[4] = -1;}
289              else if( isquare[6] ==0)  {square[6] = 'X'; isquare[6] = -1;}
290              else if( isquare[7] ==0)  {square[7] = 'X'; isquare[7] = -1;}
291              else if( isquare[8] ==0)  {square[8] = 'X'; isquare[8] = -1;}
292              else if( isquare[9] ==0)  {square[9] = 'X'; isquare[9] = -1;}
293              
294              
295              ///+
296          }//end else
297          
298          
299          ///+
300      }//end while
301      
302      
303      ///main_end
304      return 0;
305  } //main_end
306  
307  
308  ///void PrnBoard
309  void PrnBoard()
310  {
311      //system("cls");
312      //clrscr();
313      //  cout << "\n\n\tTic Tac Toe\n\n";
314        cout << endl;
315        cout << "     |     |     "    <<endl;
316        cout << "  " << square[1] << "  |  " << square[2] << "  |  " << square[3] << endl;
317        cout << "_____|_____|_____" << endl;
318        cout << "     |     |     " << endl;
319        cout << "  " << square[4] << "  |  " << square[5] << "  |  " << square[6] << endl;
320        cout << "_____|_____|_____" << endl;
321        cout << "     |     |     " << endl;
322        cout << "  " << square[7] << "  |  " << square[8] << "  |  " << square[9] <<  "   Challenger (O)  -  Computer (X)"<<endl;
323        cout << "     |     |     " ;//<< endl << endl;
324  }
325  
326  
327  ///int iCriticalCHK
328  int iCriticalCHK(int iRisk)
329  {
330      
331      
332      ///row123
333      if ((isquare[1] +isquare[2] +isquare[3]) ==iRisk*2 )
334      {
335          if (isquare[1] == 0 ) return 1;  
336          if (isquare[2] == 0 ) return 2; 
337          if (isquare[3] == 0 ) return 3; 
338      }
339      
340      
341      ///row456
342      if (isquare[4] +isquare[5] +isquare[6] ==iRisk*2 )
343      {
344          if (isquare[4] == 0 ) return 4;  
345          if (isquare[5] == 0 ) return 5; 
346          if (isquare[6] == 0 ) return 6; 
347      }
348      
349      
350      ///row789
351      if (isquare[7] +isquare[8] +isquare[9] ==iRisk*2 )
352      {
353          if (isquare[7] == 0 ) return 7;  
354          if (isquare[8] == 0 ) return 8; 
355          if (isquare[9] == 0 ) return 9; 
356      }
357      
358      
359      ///col147
360      if (isquare[1] +isquare[4] +isquare[7] ==iRisk*2 )
361      {
362          if (isquare[1] == 0 ) return 1;  
363          if (isquare[4] == 0 ) return 4; 
364          if (isquare[7] == 0 ) return 7; 
365      }
366      
367      
368      ///col258
369      if (isquare[2] +isquare[5] +isquare[8] ==iRisk*2 )
370      {
371          if (isquare[2] ==0 ) return 2;  
372          if (isquare[5] == 0 ) return 5; 
373          if (isquare[8] == 0 ) return 8; 
374      }
375      
376      
377      ///col369
378      if (isquare[3] +isquare[6] +isquare[9] ==iRisk*2 )
379      {
380          if (isquare[3] == 0 ) return 3;  
381          if (isquare[6] == 0 ) return 6; 
382          if (isquare[9] == 0 ) return 9; 
383      }
384      
385      
386      ///cro159
387      if (isquare[1] +isquare[5] +isquare[9] ==iRisk*2 )
388      {
389          if (isquare[1] == 0 ) return 1;  
390          if (isquare[5] == 0 ) return 5; 
391          if (isquare[9] == 0 ) return 9; 
392      }
393      
394      
395      ///cro357
396      if (isquare[3] +isquare[5] +isquare[7] ==iRisk*2 )
397      {
398          if (isquare[3] == 0 ) return 3;  
399          if (isquare[5] == 0 ) return 5; 
400          if (isquare[7] == 0 ) return 7; 
401      }
402      
403      
404      ///+
405      return 0;
406  }//end Def , return critical loc or 0 
407  
408  
409  ///int iWinDraw
410  int iWinDraw()
411  {
412      
413      
414      ///challenger1 defender-1 draw 0
415      if (isquare[1] +isquare[2] +isquare[3] ==3) return 1;
416      if (isquare[4] +isquare[5] +isquare[6] ==3) return 1;
417      if (isquare[7] +isquare[8] +isquare[9] ==3) return 1;
418      if (isquare[1] +isquare[4] +isquare[7] ==3) return 1;
419      if (isquare[2] +isquare[5] +isquare[8] ==3) return 1;
420      if (isquare[3] +isquare[6] +isquare[9] ==3) return 1;
421      if (isquare[1] +isquare[5] +isquare[9] ==3) return 1;
422      if (isquare[3] +isquare[5] +isquare[7] ==3) return 1;
423      if (isquare[1] +isquare[2] +isquare[3] ==-3) return -1;
424      if (isquare[4] +isquare[5] +isquare[6] ==-3) return -1;
425      if (isquare[7] +isquare[8] +isquare[9] ==-3) return -1;
426      if (isquare[1] +isquare[4] +isquare[7] ==-3) return -1;
427      if (isquare[2] +isquare[5] +isquare[8] ==-3) return -1;
428      if (isquare[3] +isquare[6] +isquare[9] ==-3) return -1;
429      if (isquare[1] +isquare[5] +isquare[9] ==-3) return -1;
430      if (isquare[3] +isquare[5] +isquare[7] ==-3) return -1;
431      return 0;
432      
433      
434      ///+
435  }//end Def , return critical loc
436  
437  

@ 下載程式碼 Download source code

1 則留言: