loop problem

Reply

Join Date: Sep 2005
Posts: 3
Reputation: plym4444 is an unknown quantity at this point 
Solved Threads: 0
plym4444 plym4444 is offline Offline
Newbie Poster

loop problem

 
0
  #1
Sep 16th, 2005
Hi all
I'm new to this site and hoping someone can assist with this problem. I've done all the code for the Tic Tac Toe game but having problems placing the 'o's & 'x's. This is the part of code I'm stuck on. Any help is greatly appreciated.

  1. char PLAYER_TURN()
  2. {
  3. int i;
  4. int x,y;
  5. board[9];
  6. drawX(x,y);
  7. do
  8. {
  9. displayBoard();
  10.  
  11. for (i = 0; i<9;i++);
  12. while (board[i] ==! -1)
  13. {
  14. i++;
  15. }
  16. board[i]=0;
  17. }
  18. return 0;
  19. }
Last edited by Dave Sinkula; Sep 16th, 2005 at 4:36 pm. Reason: Added [code][/code] tags.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: loop problem

 
0
  #2
Sep 16th, 2005
So, uh... what's board's type? Int? Char? (I'd go with char) And you could probably address it a little more logically as a 2-dimensional array.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 3
Reputation: plym4444 is an unknown quantity at this point 
Solved Threads: 0
plym4444 plym4444 is offline Offline
Newbie Poster

Re: loop problem

 
0
  #3
Sep 16th, 2005
Hello Drowzee

Yep it is a char board. If you like I could post the whole code if that would help?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: loop problem

 
0
  #5
Sep 16th, 2005
By all means, post it inside code /code tags (Put square brackets, [ and ], around the code and /code, and paste your code between 'em...)
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 3
Reputation: plym4444 is an unknown quantity at this point 
Solved Threads: 0
plym4444 plym4444 is offline Offline
Newbie Poster

Re: loop problem

 
0
  #6
Sep 16th, 2005
Here is my code - apologies if I haven't posted it correctly!! I'm having problems displaying the 'O's & 'X's.



  1. #include <iostream.h>
  2. #include <stdio.h>
  3. #include <conio.h>
  4. char board[9];
  5. int TURN = 0;
  6. int MOVE;
  7. //functions
  8. void instructions();
  9. int displayBoard();
  10. void hline (int x, int y, int col);
  11. void vline (int x, int y, int col);
  12. void playgame();
  13. char PLAYER_TURN();
  14. char CPU_TURN();
  15. void drawX(int x, int y);
  16. void drawO(int x, int y);
  17. int checkWinner();
  18.  
  19.  
  20. int main()
  21. {
  22. clrscr();
  23. char key; //define a character called key
  24. do
  25. {
  26.  
  27.  
  28. cout << "\n\n\n\n\n\n\t\t\t\tTIC TAC TOE\n\n\n\t\t";
  29. cout << "Think You Can Win a Game of Tic Tac Toe?\n\n\n\t\t";
  30. cout << "Please choose one of the following options...\n\n\n";
  31. cout << "\n\tView Instructions Press '1'";
  32. cout << "\n\tStart New Game Press '2'";
  33. cout << "\n\tExit Program Press '3'";
  34.  
  35. do
  36. {
  37. key= getch();//input choice
  38. }
  39. while (key!='1' && key!='2' && key!='3');
  40.  
  41. if (key=='1')
  42. {
  43. instructions();
  44. }
  45.  
  46. if (key=='2')
  47. {
  48. playgame();
  49. }
  50. }
  51.  
  52. while (key!='3');
  53. }
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60. void instructions()
  61. {
  62. clrscr();
  63. cout << " \n\tPlayer 1 always is always X";
  64. cout << "\n\n\tThe object of the game is to get 3 matching symbols in a row\n\n\tThe grid is numbered as shown below....";
  65. cout << "Press a key to continue...";
  66. gotoxy(20,15);
  67. cout << "0 | 1 | 2\n";
  68. gotoxy(20,16);
  69. cout << "---------\n";
  70. gotoxy(20,17);
  71. cout << "3 | 4 | 5\n";
  72. gotoxy(20,18);
  73. cout << "---------\n";
  74. gotoxy(20,19);
  75. cout << "6 | 7 | 8";
  76. getch();
  77. clrscr();
  78. }
  79.  
  80. void playgame()
  81. {
  82. int i;
  83. for (i=0;i<10;i++) board[i]= -1;
  84. PLAYER_TURN();
  85. CPU_TURN();
  86. PLAYER_TURN();
  87. CPU_TURN();
  88. PLAYER_TURN();
  89. checkWinner();
  90. CPU_TURN();
  91. checkWinner();
  92. PLAYER_TURN();
  93. checkWinner();
  94. CPU_TURN();
  95. checkWinner();
  96. PLAYER_TURN();
  97. checkWinner();
  98.  
  99. }
  100.  
  101.  
  102.  
  103.  
  104. int displayBoard()
  105. {
  106. int i;
  107. clrscr();
  108. hline(5,10,17);
  109. hline(5,16,17);
  110. vline(10,5,17);
  111. vline(16,5,17);
  112. for (i = 0; i<9;i++);
  113.  
  114. {
  115. if (i == 0)
  116. {
  117. drawX(5,5);
  118. }
  119. else if (i == 1)
  120. {
  121. drawX (11,5);
  122. }
  123. else if (i == 2)
  124. {
  125. drawX (17,5);
  126. }
  127. else if (i == 3)
  128. {
  129. drawX (5,11);
  130. }
  131. else if (i == 4)
  132. {
  133. drawX (11,11);
  134. }
  135. else if (i == 5)
  136. {
  137. drawX (17,11);
  138. }
  139. else if (i == 6)
  140. {
  141. drawX (5,17);
  142. }
  143. else if (i == 7)
  144. {
  145. drawX (11,17);
  146. }
  147. else if (i == 8)
  148. {
  149. drawX (17,17);
  150. }
  151. }
  152. {
  153. if (i == 0)
  154. {
  155. drawO (5,5);
  156. }
  157. else if (i == 1)
  158. {
  159. drawO (11,5);
  160. }
  161. else if (i == 2)
  162. {
  163. drawO (17,5);
  164. }
  165. else if (i == 3)
  166. {
  167. drawO (5,11);
  168. }
  169. else if (i == 4)
  170. {
  171. drawO (11,11);
  172. }
  173. else if (i == 5)
  174. {
  175. drawO (17,11);
  176. }
  177. else if (i == 6)
  178. {
  179. drawO (5,17);
  180. }
  181. else if (i == 7)
  182. {
  183. drawO (11,17);
  184. }
  185. else if (i == 8)
  186. {
  187. drawO (17,17);
  188. }
  189. return 0;
  190. }
  191. }
  192.  
  193.  
  194. void hline (int x, int y, int col)
  195. {
  196. int i;
  197. textcolor (col);
  198. for (i=0;i<17;i++)
  199. {
  200. gotoxy(i+x,y);
  201. cprintf("%c",219);
  202. }
  203. }
  204.  
  205.  
  206. void vline (int x, int y, int col)
  207. {
  208. int i;
  209. textcolor (col);
  210. for (i=0;i<17;i++)
  211. {
  212. gotoxy(x,i+y);
  213. cprintf("%c",219);
  214. }
  215. }
  216.  
  217.  
  218.  
  219. void drawX (int x, int y)
  220. {
  221. gotoxy(x,y+1);
  222. cprintf(" X X");
  223. gotoxy(x,y+2);
  224. cprintf(" X ");
  225. gotoxy(x,y+3);
  226. cprintf(" X X");
  227.  
  228. }
  229.  
  230.  
  231.  
  232. void drawO (int x, int y)
  233. {
  234. gotoxy(x,y+1);
  235. cprintf(" O ");
  236. gotoxy(x,y+2);
  237. cprintf(" O O");
  238. gotoxy(x,y+3);
  239. cprintf(" O ");
  240.  
  241. }
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248. char PLAYER_TURN()
  249. {
  250. int i;
  251. int x,y;
  252.  
  253. board[9];
  254. drawX(x,y);
  255. displayBoard();
  256. {
  257. for (i = 0; i<10;i++);
  258. {
  259. while(board[i] ==! -1);
  260. }
  261. checkWinner();
  262. }
  263.  
  264. return 0;
  265. }
  266.  
  267.  
  268.  
  269.  
  270. char CPU_TURN()
  271. {
  272. int i;
  273. int x,y;
  274.  
  275. board[9];
  276. drawO(x,y);
  277.  
  278. displayBoard();
  279.  
  280. for (i = 0; i<10;i++);
  281. {
  282. board[i] == -1;
  283. }
  284. checkWinner();
  285. }
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292. int checkWinner()
  293. {
  294.  
  295. while(TURN < 10)
  296. {
  297.  
  298. if (board[0]== 'X' && board[4]== 'X' && board[8]== 'X') {cout<<"Player 1 is the winner";break;}
  299. if (board[6]== 'X' && board[4]== 'X' && board[2]== 'X') {cout<<"Player 1 is the winner";break;}
  300. if (board[0]== 'X' && board[3]== 'X' && board[6]== 'X') {cout<<"Player 1 is the winner";break;}
  301. if (board[1]== 'X' && board[4]== 'X' && board[7]== 'X') {cout<<"Player 1 is the winner";break;}
  302. if (board[2]== 'X' && board[5]== 'X' && board[8]== 'X') {cout<<"Player 1 is the winner";break;}
  303. if (board[0]== 'X' && board[1]== 'X' && board[2]== 'X') {cout<<"Player 1 is the winner";break;}
  304. if (board[3]== 'X' && board[4]== 'X' && board[5]== 'X') {cout<<"Player 1 is the winner";break;}
  305. if (board[6]== 'X' && board[7]== 'X' && board[8]== 'X') {cout<<"Player 1 is the winner";break;}
  306.  
  307. // Check for Player 2...
  308.  
  309. if (board[0]== 'O' && board[4]== 'O' && board[8]== 'O') {cout<<"Player 2 is the winner";break;}
  310. if (board[6]== 'O' && board[4]== 'O' && board[2]== 'O') {cout<<"Player 2 is the winner";break;}
  311. if (board[0]== 'O' && board[3]== 'O' && board[6]== 'O') {cout<<"Player 2 is the winner";break;}
  312. if (board[1]== 'O' && board[4]== 'O' && board[7]== 'O') {cout<<"Player 2 is the winner";break;}
  313. if (board[2]== 'O' && board[5]== 'O' && board[8]== 'O') {cout<<"Player 2 is the winner";break;}
  314. if (board[0]== 'O' && board[1]== 'O' && board[2]== 'O') {cout<<"Player 2 is the winner";break;}
  315. if (board[3]== 'O' && board[4]== 'O' && board[5]== 'O') {cout<<"Player 2 is the winner";break;}
  316. if (board[6]== 'O' && board[7]== 'O' && board[8]== 'O') {cout<<"Player 2 is the winner";break;}
  317.  
  318. }
  319.  
  320. }
<< moderator edit: added code tags: [code][/code] >>
Last edited by Dave Sinkula; Sep 16th, 2005 at 4:37 pm. Reason: Added [code][/code] tags.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: loop problem

 
0
  #7
Sep 16th, 2005
I'm a bit busy at work, so I don't have time to run through this completely.
So, what's the problem? The X's and O's don't show up at all?

Oh, and you may find this helpful to try:
  1. char board[3][3];
;

That creates a 3x3 grid that you can use in a more reasonable fashion. I think it's x, then y, but the origin would most logically be in the upper left corner, like this:
  1. board[0][0] | board[0][1] | board[0][2]
  2. -------------------------------------
  3. board[1][0] | board[1][1] | board[1][2]
  4. -------------------------------------
  5. board[2][0] | board[2][1] | board[2][2]

With the horizontal and vertical bars just for the purpose of illustration, not as code.


I think your code is a bit too complex, personally. I don't know why I feel that way, but my intuition (admittedly poor) just tells me that you're overdefining things.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: loop problem

 
0
  #8
Sep 16th, 2005
assuming EX and OH are defined somewhere as non-zero ints then something like this can keep your checking routines simple.
  1. int MainWindow::IsWinner()
  2. {
  3. // this holds the patterns we will check for
  4. static int pattern[8][3]=
  5. {
  6. 0,1,2,
  7. 3,4,5,
  8. 6,7,8,
  9. 0,3,6,
  10. 1,4,7,
  11. 2,5,8,
  12. 0,4,8,
  13. 2,4,6
  14. };
  15. // this loop does the checking
  16. for( int i = 0; i < 8; ++i)
  17. {
  18. if( (GameGrid_[pattern[i][0]] == EX) &&
  19. (GameGrid_[pattern[i][1]] == EX) &&
  20. (GameGrid_[pattern[i][2]] == EX))
  21. return EX;
  22. if( (GameGrid_[pattern[i][0]] == OH) &&
  23. (GameGrid_[pattern[i][1]] == OH) &&
  24. (GameGrid_[pattern[i][2]] == OH))
  25. return OH;
  26. }
  27. // no winner so return 0
  28. return 0;
  29. }
this assumes GameGrid_ is defined as a an array of 9 ints.
Similarly your drawing routines can be simplified but ill leave you to work that out on your own.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC