943,908 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 454
  • C++ RSS
Jul 3rd, 2009
0

Tic Tac Toe

Expand Post »
I'm a beginner at c++ and I have to do a game of tic tac toe and I'm having problems with asking the player to play. Please help.

*****************************************************

C++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. ///function prototype
  8. void inttableArray(int [][3], int );
  9. void displayArray (int [][3], int );
  10. void inputArrayFunction(int [][3], int );
  11. void getMove(int [][3], int );
  12. void getComputerMove(int [][3], int );
  13. char Check(int [][3], int );
  14.  
  15.  
  16. int main()
  17. {
  18. char end;
  19.  
  20.  
  21. end= ' ';
  22.  
  23. int tableOne[3][3]; //array of 3 row and 5 columns
  24.  
  25.  
  26. cout<<"This is the game of Tic Tac Toe.\n";
  27. cout<<"You will be playing against the computer.\n";
  28.  
  29.  
  30.  
  31. inttableArray(tableOne, 3);
  32.  
  33. displayArray(tableOne, 3);
  34.  
  35. getMove(tableOne, 3);
  36.  
  37. end = Check(tableOne, 3);
  38.  
  39. getComputerMove(tableOne, 3);
  40.  
  41. inputArrayFunction(tableOne, 3);
  42.  
  43. if(end== ' ');
  44.  
  45.  
  46. if(end=='X') cout<<"You won!";
  47. else cout<<"I won!!!!";
  48. displayArray(tableOne, 3);
  49.  
  50.  
  51.  
  52. return 1;
  53.  
  54. }
  55.  
  56. //**************************************************
  57. void inttableArray(int TempArray[][3], int size )
  58. {
  59. int row, column;
  60.  
  61. for ( row = 0; row <3; row ++)
  62. for (column = 0; column < size; column ++)
  63. TempArray[row][column] = ' ';
  64. }
  65.  
  66. //**************************************************
  67. void getMove(int TTempArray[][3], int Tsize)
  68. {
  69. int x =0;
  70. int y=0;
  71. int n=0;
  72.  
  73.  
  74. cout<<" Please make your move: ";
  75. cin>>n;
  76.  
  77. if(n<1||n>9)
  78. cout<<"Invalid move"<<endl;
  79. else
  80. if(TTempArray[x][y]!= ' ')
  81. {
  82. cout<<"Invalid move, try again.\n";
  83. getMove(TTempArray, 3);
  84. }
  85. else TTempArray[x][y] = 'X';
  86.  
  87.  
  88.  
  89.  
  90. }
  91.  
  92. //**************************************************
  93.  
  94. void getComputerMove(int STempArray[][3], int Ssize)
  95. {
  96. int x;
  97. int y;
  98. int n;
  99.  
  100.  
  101. cout<<"Player 2"<<endl;
  102. cout<<" Please make your move: ";
  103. cin>>n;
  104.  
  105. if(n<1||n>9)
  106. cout<<"Invalid move"<<endl;
  107. else
  108. if(n=' ';n<=9);
  109. if(STempArray[y][x]!= ' ')
  110. {
  111. cout<<"Invalid move, try again.\n";
  112. getMove(STempArray, 3);
  113. }
  114. else STempArray[y][x] = 'Y';
  115. }
  116.  
  117. //**************************************************
  118. void displayArray (int DTempArray[][3], int Dsize)
  119. {
  120. int x=0;
  121. int y=0;
  122.  
  123.  
  124. for ( x = 0; x < 3; x ++)
  125. {
  126. printf(" %c | %c | %c ",DTempArray[x][0],
  127. DTempArray[x][1], DTempArray [x][2]);
  128. if(x!=2) printf("\n---|---|---\n");
  129. }
  130. printf("\n");
  131.  
  132.  
  133. }
  134.  
  135.  
  136. //**************************************************
  137. void inputArrayFunction(int inputArray[][3], int inputsize)
  138. {
  139. int a, b;
  140. int x = 1;
  141.  
  142. printf("\n---|---|---\n");
  143.  
  144. for ( a = 0 ;a < 3; a++)
  145. for (b = 0; b < inputsize; b++)
  146. if ( b == 2 )
  147. inputArray[a][b] = 10;
  148. else
  149. inputArray[a][b] = b;
  150. }
  151. //**************************************************
  152.  
  153. char Check(int TinputArray[][3], int Tinputsize )
  154. {
  155. int i;
  156. int row;
  157. int tableOne[3][3];
  158.  
  159. for ( row = 0; row <3; row ++)
  160. if(tableOne[row][0]==tableOne[row][1] &&
  161. tableOne[row][0]==tableOne[row][2]) return tableOne[row][0];
  162.  
  163. for(i=0; i<3; i++)
  164. if(tableOne[0][row]==tableOne[1][row] &&
  165. tableOne[0][row]==tableOne[2][row]) return tableOne[0][row];
  166.  
  167.  
  168. if(tableOne[0][0]==tableOne[1][1] &&
  169. tableOne[1][1]==tableOne[2][2])
  170. return tableOne[0][0];
  171.  
  172. if(tableOne[0][2]==tableOne[1][1] &&
  173. tableOne[1][1]==tableOne[2][0])
  174. return tableOne[0][2];
  175.  
  176. return ' ';
  177. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
PrincessT is offline Offline
1 posts
since Jul 2009
Jul 3rd, 2009
0

Re: Tic Tac Toe

I didn't really look at anything accept for the main function. Wouldn't it make sense to have a loop? It looks like your only calling the human to move once, then the computer the move once. Then the game ends... Shouldn't you loop until someone wins or something? And also please be more specific as to what your error is.
Reputation Points: 78
Solved Threads: 15
Junior Poster
u8sand is offline Offline
131 posts
since Dec 2008
Jul 3rd, 2009
0

Re: Tic Tac Toe

It doesn't sound like the problem has anything to do with tic-tac-toe. Can you please make a much, much smaller demonstration of your problem along with input, expected output, and current (incorrect) output.
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Jul 4th, 2009
0

Re: Tic Tac Toe

And as a side note, you shouldn't be using stdio.h or stdlib.h as those aren't standard C++ headers. Use cstdio and cstdlib which are the standard C++ headers.

And as u8sand said, you're only executing things through once. Loop until the game is over, then ask to if the player wants to play again, and if so, re-loop it all.
Reputation Points: 186
Solved Threads: 77
Posting Pro in Training
shadwickman is offline Offline
495 posts
since Jul 2007
Jul 4th, 2009
0

Re: Tic Tac Toe

You could use a much easier type of game, here's a link from where you could get the code: http://brom8305.blogspot.com/2009/06...oe-script.html

try it out!
Reputation Points: 6
Solved Threads: 2
Light Poster
kangarooblood is offline Offline
42 posts
since Jun 2009

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: Program won't run outside Visual Studio 2008 Express
Next Thread in C++ Forum Timeline: Need help on homwork assignment.





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


Follow us on Twitter


© 2011 DaniWeb® LLC