fork download
  1. /*
  2. Sophia Ali
  3. Template for TicTacToe.cpp (CS-509 Assignment 5)
  4. can't get program to store x and o in the board
  5. */
  6.  
  7.  
  8. #include<iostream>
  9. #include <cstdlib>
  10. using namespace std;
  11.  
  12. /*
  13. Game status enumeration
  14. */
  15. enum Status { WIN, DRAW, CONTINUE, QUIT };
  16.  
  17.  
  18. /*
  19. Function prototypes
  20. */
  21. // showBoard: Show current state of board
  22. void showBoard( const char board[], int boardSize );
  23. // checkGameState: Returns WIN or CONTINUE
  24. Status checkGameState( const char board[] );
  25. int getHumanSquare( const char board[] );
  26. int getComputerSquare( const char board[] );
  27. // checkBadSquare: Checks to see if a chosen square is already taken; returns
  28. // true if already taken; used by getHumanSquare and
  29. // getComputerSquare functions above.
  30. bool checkBadSquare( const char board[], int squareNum );
  31. int getrandint( int min, int max );
  32.  
  33.  
  34.  
  35.  
  36. int main()
  37. {
  38. char board[] = "123456789"; // 10 element char board
  39. const int boardSize = 10;
  40. Status gameState = CONTINUE;
  41. int gametype, squareChoice, turnNum = 0;
  42. char currentSymbol; // 'o' or 'x'
  43.  
  44.  
  45.  
  46. cout << "\n This is a Tic Tac Toe program. Choose the type of game: "
  47. << "\n (1) human o vs. human x (2) human o vs. dumb computer x"
  48. << "\n\n -> ";
  49. cin >> gametype;
  50.  
  51.  
  52. /* Show the current state of Tic Tac Toe board. */
  53. cout << gameState;
  54. cout << "\n\n";
  55.  
  56.  
  57.  
  58.  
  59. /*
  60. Main game loop
  61. */
  62. while ( gameState == 2 )
  63. {
  64. /* Increment turnNum by 1. */
  65. turnNum++;
  66.  
  67.  
  68. /* If turnNum equal to 10
  69. Set gameState to DRAW.
  70. Break out of while loop. */
  71. if ( turnNum == 10 )
  72. {
  73. gameState = DRAW;
  74. break;
  75. }
  76. /* If we are on an odd-numbered turn
  77. Print "It's o's turn."
  78. Set currentSymbol to 'o'.
  79. Call getHumanSquare function to get squareChoice.*/
  80. if ( turnNum%2 != 0)
  81. {
  82. cout << "It's o's turn.";
  83. currentSymbol = 'o';
  84. cout << "\n\n";
  85.  
  86. getHumanSquare(board);
  87. showBoard( board, boardSize );
  88. }
  89.  
  90. /* Else (we are on an even-numbered turn)
  91. Print "It's x's turn."
  92. Set currentSymbol to 'x'. */
  93. else
  94. {
  95. cout << "It's x's turn.";
  96. currentSymbol = 'x';
  97. cout << "\n\n";
  98. showBoard( board, boardSize );
  99.  
  100. }
  101.  
  102.  
  103. /* If the gametype is 1 (human vs. human)
  104. Call getHumanSquare function to get squareChoice.*/
  105. if ( gametype == 1 )
  106. {
  107. getHumanSquare(board);
  108.  
  109.  
  110. }
  111.  
  112.  
  113. /* Else (gametype is 2 (human vs. computer))
  114. Call getComputerSquare function to get squareChoice. */
  115. else
  116. getComputerSquare(board);
  117.  
  118.  
  119.  
  120. /* If squareChoice is -1 (human player quit)
  121. Set gameState to QUIT.*/
  122. if ( squareChoice == -1 )
  123. {
  124. gameState = QUIT;
  125. }
  126.  
  127. /* Else
  128. Insert currentSymbol into board at (squareChoice - 1).
  129. Show the current state of the Tic Tac Toe board.
  130. Call checkGameState function to determine the gameState. */
  131. else
  132. {
  133. if (squareChoice == 1 && board[1] == '1')
  134. board[1] = currentSymbol;
  135. else if (squareChoice == 2 && board[2] == '2')
  136. board[2] = currentSymbol;
  137. else if (squareChoice == 3 && board[3] == '3')
  138. board[3] = currentSymbol;
  139. else if (squareChoice == 4 && board[4] == '4')
  140. board[4] = currentSymbol;
  141. else if (squareChoice == 5 && board[5] == '5')/*SPELLING MISTAKE*/
  142. board[5] = currentSymbol;
  143. else if (squareChoice == 6 && board[6] == '6')
  144. board[6] = currentSymbol;
  145. else if (squareChoice == 7 && board[7] == '7')
  146. board[7] = currentSymbol;
  147. else if (squareChoice == 8 && board[8] == '8')
  148. board[8] = currentSymbol;
  149. else if (squareChoice == 9 && board[9] == '9')
  150. board[9] = currentSymbol;
  151. else
  152. {
  153. cout <<"Invalid move";
  154. turnNum--;
  155. }
  156. }
  157.  
  158. checkGameState(board);
  159.  
  160. }
  161.  
  162.  
  163. /* If gameState is WIN
  164. print "Player " currentSymbol " is the winner." */
  165. if(gameState == WIN)
  166. cout << "Player " << currentSymbol << " is the winner.";
  167.  
  168. /* If gameState is DRAW
  169. print "It's a draw." */
  170. if ( gameState == DRAW )
  171. cout << "It's a draw.";
  172.  
  173. return 0;
  174. }
  175.  
  176. /////////////////////////////////////////////////////////////////////
  177.  
  178. void showBoard( const char board [], int size )
  179. {
  180. cout << endl;
  181.  
  182. for ( int i = 0; i < size ; i++ )
  183. {
  184. cout << board[ i ] << " ";
  185. if ( ( i + 1 ) % 3 == 0 )
  186. cout << endl;
  187. }
  188.  
  189. cout << endl;
  190. }
  191.  
  192. /////////////////////////////////////////////////////////////////////
  193.  
  194. Status checkGameState( const char board[] )
  195. {
  196. // Board Array
  197. //
  198. // 1 2 3 0 1 2
  199. // 4 5 6 --> 3 4 5
  200. // 7 8 9 6 7 8
  201. //
  202. // Diagonal winners
  203. if ( board[ 0 ] == board[ 4 ] && board[ 0 ] == board[ 8 ] )
  204. return WIN;
  205. else if ( board[ 2 ] == board[ 4 ] && board[ 4 ] == board[ 6 ] )
  206. return WIN;
  207. // Horizontal winners
  208. else if ( board[ 0 ] == board[ 1 ] && board[ 1 ] == board[ 2 ] )
  209. return WIN;
  210. else if ( board[ 3 ] == board[ 4 ] && board[ 4 ] == board[ 5 ] )
  211. return WIN;
  212. else if ( board[ 6 ] == board[ 7 ] && board[ 7 ] == board[ 8 ] )
  213. return WIN;
  214. // Vertical winners
  215. else if ( board[ 0 ] == board[ 3 ] && board[ 3 ] == board[ 6 ] )
  216. return WIN;
  217. else if ( board[ 1 ] == board[ 4 ] && board[ 4 ] == board[ 7 ] )
  218. return WIN;
  219. else if ( board[ 2 ] == board[ 5 ] && board[ 5 ] == board[ 8 ] )
  220. return WIN;
  221. else
  222. // No one has won yet
  223. return CONTINUE;
  224. }
  225.  
  226. /////////////////////////////////////////////////////////////////////
  227.  
  228. int getHumanSquare( const char board[] )
  229. {
  230. int squareNum;
  231.  
  232. cout << "\n Input the number of an empty square: (-1 to quit) ";
  233. cin >> squareNum;
  234.  
  235. while ( checkBadSquare( board, squareNum ) == true )
  236. {
  237. cout << "\n Bad input. Choose another square: ";
  238. cin >> squareNum;
  239. }
  240.  
  241. return squareNum;
  242. }
  243.  
  244. /////////////////////////////////////////////////////////////////////
  245.  
  246. int getComputerSquare( const char board[] )
  247. {
  248. int squareNum;
  249.  
  250. squareNum = getrandint( 1, 9 );
  251.  
  252. while ( checkBadSquare( board, squareNum ) == true )
  253. {
  254. squareNum = getrandint( 1, 9 );
  255. }
  256.  
  257. return squareNum;
  258. }
  259.  
  260. /////////////////////////////////////////////////////////////////////
  261.  
  262. bool checkBadSquare( const char board[], int squareNum )
  263. {
  264. int realSquareNum = squareNum - 1; // count from 0
  265.  
  266. if ( squareNum == -1 )
  267. return false; // Let quit code pass as a valid square
  268. else if ( squareNum > 9 )
  269. return true; // Square numbers out of range are invalid
  270. else if ( board[ realSquareNum ] == 'o' || board[ realSquareNum ] == 'x' )
  271. return true; // Already taken squares are invalid
  272. else
  273. return false; // Valid square number
  274. }
  275.  
  276. /////////////////////////////////////////////////////////////////////
  277.  
  278. int getrandint( int min, int max )
  279. {
  280.  
  281. int scale, shift;
  282. scale = max - min + 1;
  283. shift = min;
  284. return rand() % scale + shift;
  285. }
Runtime error #stdin #stdout 0s 2904KB
stdin
Standard input is empty
stdout
 This is a Tic Tac Toe program. Choose the type of game: 
 (1) human o vs. human x    (2) human o vs. dumb computer x

 ->