Tic Tac Toe

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2009
Posts: 1
Reputation: PrincessT is an unknown quantity at this point 
Solved Threads: 0
PrincessT PrincessT is offline Offline
Newbie Poster

Tic Tac Toe

 
0
  #1
Jul 3rd, 2009
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.

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

  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. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 117
Reputation: u8sand is on a distinguished road 
Solved Threads: 15
u8sand's Avatar
u8sand u8sand is offline Offline
Junior Poster

Re: Tic Tac Toe

 
0
  #2
Jul 3rd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 630
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Tic Tac Toe

 
0
  #3
Jul 3rd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: Tic Tac Toe

 
0
  #4
Jul 4th, 2009
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.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson

my photography
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 38
Reputation: kangarooblood is an unknown quantity at this point 
Solved Threads: 2
kangarooblood kangarooblood is offline Offline
Light Poster

Re: Tic Tac Toe

 
0
  #5
Jul 4th, 2009
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!
// Maybe you like C++ put I prefer A++...
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