943,151 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 593
  • C++ RSS
Jan 30th, 2010
0

c++ Tic Tac Toe help

Expand Post »
hi all,im new here and im a beginner at c++, im making a simple tic tac toe program, and its not working properly, it cant determine the winner,could someone please help.
heres the code
C++ Syntax (Toggle Plain Text)
  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4. #include<stdio.h>
  5. #include<graphics.h>
  6. #include<ctype.h>
  7. #include<time.h>
  8. void main()
  9. {
  10. /* request auto detection */
  11. int gdriver = DETECT, gmode, errorcode;
  12. /* initialize graphics and local variables */
  13. initgraph(&gdriver, &gmode, "");
  14.  
  15. cleardevice();
  16. new_game: //label to start new game;
  17. int i,j;
  18. char choice;
  19. int count1=0,count2=0,count3=0;
  20. char square[3][3];
  21. struct game_var
  22. {
  23. int row_user,column_user,row_comp,column_comp;
  24. char plyr,comp;
  25. }game_variables; //declaring game variables
  26.  
  27. settextstyle(3,0,20); //setting font
  28.  
  29. for(i=0;i<3;i++) //for1 loop to initialize all elements to space
  30. {
  31. for(j=0;j<3;j++)
  32. {
  33. square[i][j]=' ';
  34. }
  35. } //end of for1loop
  36. cout<<endl;
  37. cout<<"Welcome To Tic Tac Toe"<<endl;
  38. cout<<"Choose x or o"<<endl;
  39. cin>>game_variables.plyr;
  40.  
  41. switch(game_variables.plyr) //checking players choice
  42. {
  43. case 'x':game_variables.comp='o';
  44. break;
  45. case 'o':game_variables.comp='x';
  46. break;
  47. default :cout<<"Wrong Choice!!!! Starting Over..............";
  48. goto new_game;
  49. }//end of switch
  50. cout<<"Generating board"<<endl;
  51. for(i=0;i<3;i++) //for2 loop to display empty board
  52. {
  53. cout<<endl;
  54. cout<<"|";
  55. for(j=0;j<3;j++)
  56. {
  57. cout<<square[i][j]<<"|";
  58. }
  59. } //end of for2
  60. cout<<endl;
  61. user_input:
  62. cout<<"Enter Row and Column(x and y Coordinates)"
  63. <<" That Are Greater Than 0 And Less Than 3"<<endl;
  64. cout<<"Row = "<<endl;
  65. cin>>game_variables.row_user;
  66. cout<<"Column = "<<endl;
  67. cin>>game_variables.column_user;
  68. if(game_variables.row_user>3||game_variables.column_user>3||game_variables.row_user<0||game_variables.column_user<0)
  69. {
  70. cout<<"Board Cannot Be Created, Starting Over............";
  71. goto user_input;
  72. }
  73.  
  74. cout<<"Computers Turn............."<<endl;
  75. if(square[game_variables.row_user][game_variables.column_user]==game_variables.plyr||square[game_variables.row_user][game_variables.column_user]==game_variables.comp)
  76. {
  77. cout<<"Place Already Taken, Enter New Coordinates";
  78. goto user_input;
  79. }
  80. comp_start:
  81. randomize();
  82. game_variables.row_comp=random(1-0+1)+0;
  83. game_variables.column_comp=random(1-0+1)+0;
  84. if(square[game_variables.row_comp][game_variables.column_comp]==game_variables.plyr)
  85. {
  86. goto comp_start;
  87. }
  88.  
  89. square[game_variables.row_comp][game_variables.column_comp]=game_variables.comp;
  90. square[game_variables.row_user][game_variables.column_user]=game_variables.plyr;
  91.  
  92. for(i=0;i<3;i++) //for3 loop to display current board
  93. {
  94. cout<<endl;
  95. cout<<"|";
  96. for(j=0;j<3;j++)
  97. {
  98. cout<<square[i][j]<<"|";
  99. }
  100. } //end of for3
  101. cout<<endl;
  102. for(i=0;i<3;i++) //for4 to check the numbers of squares used
  103. {
  104. for(j=0;j<3;j++)
  105. {
  106. if(square[i][j]==game_variables.plyr||square[i][j]==game_variables.comp)
  107. count1++;
  108. }
  109. } //end of for4
  110.  
  111. for(i=0;i<3;i++) //for5 checking winning conditions for diagonals
  112. {
  113. for(j=0;j<3;j++)
  114. {
  115. if(i==j||i+j==2)
  116. {
  117. if(square[i][j]==game_variables.plyr)
  118. {
  119. count2++;
  120. if(count2<3)
  121. count2=0;
  122. }
  123. else if(square[i][j]==game_variables.comp)
  124. {
  125. count3++;
  126. if(count3<3)
  127. count3=0;
  128. }
  129. } //end of if
  130. } //end of j
  131. } //end of for5
  132.  
  133. for(j=0;j<3;j++)
  134. {
  135. if(square[0][j]==game_variables.plyr) //for user row
  136. count2++;
  137. else if(count2<3)
  138. count2=0;
  139. if(square[1][j]==game_variables.plyr)
  140. count2++;
  141. if(count2<3)
  142. count2=0;
  143. if(square[2][j]==game_variables.plyr)
  144. count2++;
  145. else if(count2<3)
  146. count2=0;
  147. }
  148. for(i=0;i<3;i++)
  149. {
  150. if(square[i][0]==game_variables.plyr) //for user column
  151. count2++;
  152. else if(count2<3)
  153. count2=0;
  154. if(square[i][1]==game_variables.plyr)
  155. count2++;
  156. if(count3<=3)
  157. count2=0;
  158. if(square[i][2]==game_variables.plyr)
  159. count2++;
  160. else if(count2<3)
  161. count2=0;
  162. }
  163.  
  164. for(j=0;j<3;j++)
  165. {
  166. if(square[0][j]==game_variables.comp) //for comp row
  167. count3++;
  168. else if(count3<3)
  169. count3=0;
  170. if(square[1][j]==game_variables.comp)
  171. count3++;
  172. if(count2<3)
  173. count3=0;
  174. if(square[2][j]==game_variables.comp)
  175. count3++;
  176. else if(count3<3)
  177. count3=0;
  178. }
  179.  
  180. for(i=0;i<3;i++)
  181. {
  182. if(square[i][0]==game_variables.comp) //for comp comlumn
  183. count3++;
  184. else if(count3<3)
  185. count3=0;
  186. if(square[i][1]==game_variables.comp)
  187. count3++;
  188. if(count3<3)
  189. count3=0;
  190. if(square[i][2]==game_variables.comp)
  191. count3++;
  192. else if(count3<3)
  193. count3=0;
  194. }
  195.  
  196. check:
  197. if(count3==3)
  198. {
  199. cout<<"Computer Has Won"<<endl;
  200. goto game;
  201. }
  202. else if(count2==3)
  203. {
  204. cout<<"You have Won!! Congratulations"<<endl;getch();
  205. game:
  206. cout<<"Do You Want To Start A New Game y\n"<<endl;
  207. cin>>choice;
  208. switch(choice)
  209. {
  210. case 'y':goto new_game;
  211. case 'Y':goto new_game;
  212. case 'n':exit(0);
  213. case 'N':exit(0);
  214. } //end of switch
  215. }
  216. else if(count1<9&&count2<3&&count2<3)
  217. goto user_input;
  218. else if(count1==9&&count2<3&&count2<3 )
  219. {
  220. cout<<"Draw Game, Do You Want Start New Game y\n"<<endl;
  221. cin>>choice;
  222. switch(choice)
  223. {
  224. case 'y':goto new_game;
  225. case 'Y':goto new_game;
  226. case 'n':exit(0);
  227. case 'N':exit(0);
  228. }
  229. }
  230. getch();
  231. }
also i wasnt able to use graphics to draw a grid, could someone please tell how to do that.im using turbo C++,also if someone could give a simpler code for determing the winner or draw match,it would be very helpful.
thank you
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vin1 is offline Offline
1 posts
since Jan 2010
Jan 30th, 2010
1
Re: c++ Tic Tac Toe help
Your code is really hard to look at. But from your question : "cant
determine the winner" I figure that the winner checker is not working.
There are 2 things you can do.

Option1 : Make a hard-coded function that checks for winner
Option2 : create a for loop to that checks the winner.

I would say Option1 is easier. So For now, you should get that working.

To check for winner you need to first create a function like so :
C++ Syntax (Toggle Plain Text)
  1. bool checkForWinner(const char *Board[3], const char key){
  2. if( Board[0][0] == key && Board[0][1] == key && Board[0][2] == key)
  3. return true;
  4. //and so one
  5. }

Thats the hard-coded way. Try and see if you can get that working.
Also try to make your code neater and easier to read.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,859 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Count elements in array
Next Thread in C++ Forum Timeline: Algorithum help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC