Advertisement
Guest User

Untitled

a guest
Feb 24th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. // Tic-Tac-Toe program
  2. //
  3. // <<<AMAR FAROOQI>>>
  4. //
  5. // U. of Illinois, Chicago
  6. // CS109, Fall 2012: HW11-PE2
  7.  
  8. #include <iostream>
  9. #include <string>
  10.  
  11. using namespace std;
  12.  
  13.  
  14. //
  15. // draw:
  16. //
  17. // Outputs the board on the console like this:
  18. //
  19. // X | O |
  20. // O | X | O
  21. // | | X
  22. //
  23. // In other words, board values of 1 are drawn as X, values of 0 are drawn
  24. // as O, and values of -1 are drawn empty.
  25. //
  26. void draw(int board[3][3])
  27. {
  28. int r, c;
  29. for (r=0; r<3; r++)
  30. {
  31. for (c=0; c<3; c++)
  32. {
  33. if (c==0)
  34. cout<< " ";
  35. if (board[r][c]==1)
  36. cout<<"X";
  37. else if (board [r][c]==0)
  38. cout<<"O";
  39. else if (board [r][c]==-1)
  40. cout<<" ";
  41.  
  42.  
  43. if (c==0 || c==1)
  44. cout<< " | ";
  45. else if (c==2)
  46. cout<<endl;
  47.  
  48.  
  49. }
  50. }
  51.  
  52.  
  53.  
  54. }//draw
  55.  
  56.  
  57.  
  58. //
  59. // winner:
  60. //
  61. // Returns:
  62. // 0: if player "O" has won
  63. // 1: if player "X" has won
  64. // -1: if neither player has won
  65. //
  66. int winner(int board[3][3])
  67. {
  68. //Across for X
  69. if ( board[0][0] == 1 && board[0][1] == 1 && board[0][2] == 1)
  70. return 1;
  71. else if ( board[1][0] == 1 && board[1][1] == 1 && board[1][2] == 1 )
  72. return 1;
  73. else if (board[2][0] == 1 && board[2][1] == 1 && board[2][2] == 1 )
  74. return 1;
  75. // Across for 0
  76. else if ( board[0][0] == 0 && board[0][1] == 0 && board[0][2] == 0)
  77. return 0;
  78. else if ( board[1][0] == 0 && board[1][1] == 0 && board[1][2] == 0 )
  79. return 0;
  80. else if (board[2][0] == 0 && board[2][1] == 0 && board[2][2] == 0 )
  81. return 0;
  82. //UP Down for X
  83. else if (board [0][0] ==1 && board [1][0]==1 && board[2][0]==1)
  84. return 1;
  85. else if (board [0][1] ==1 && board [1][1]==1 && board[2][1]==1)
  86. return 1;
  87. else if (board [0][2] ==1 && board [1][2]==1 && board[2][2]==1)
  88. return 1;
  89. //up down for 0
  90. else if (board [0][0] ==0 && board [1][0]==0 && board[2][0]==0)
  91. return 0;
  92. else if (board [0][1] ==0 && board [1][1]==0 && board[2][1]==0)
  93. return 0;
  94. else if (board [0][2] ==0 && board [1][2]==0 && board[2][2]==0)
  95. return 0;
  96. //diagnol for x
  97. else if (board [0][0] ==1 && board [1][1]==1 && board[2][2]==1)
  98. return 1;
  99. else if (board [0][2] ==1 && board [1][1]==1 && board[2][0]==1)
  100. return 1;
  101. else if (board [0][0] ==0 && board [1][1]==0 && board[2][2]==0)
  102. return 0;
  103. else if (board [0][2] == 0 && board [1][1]==0 && board[2][0]==0)
  104. return 0;
  105.  
  106. //
  107. // TODO! There are 8 ways for player O to win, and 8 ways for player X to win.
  108. //
  109.  
  110.  
  111. return -1;
  112. }//winner
  113.  
  114.  
  115. //
  116. // gameover:
  117. //
  118. // Returns true if the game is over, false if not
  119. //
  120. bool gameover(int board[3][3])
  121. {
  122. int r, c;
  123.  
  124. //
  125. // the game is over if someone has won:
  126. //
  127. if (winner(board) == 0 || winner(board) == 1)
  128. {
  129. return true;
  130. }
  131.  
  132. //
  133. // the game is also over if it's a draw --- i.e. neither player
  134. // has won, and there are no more moves available on the board:
  135. //
  136. bool moveOpen; // move open flag
  137. moveOpen = false; // assume there is no move open:
  138.  
  139. for (r = 0; r < 3; r++) // now search board for an open move...
  140. {
  141. for (c = 0; c < 3; c++)
  142. {
  143. if (board[r][c] == -1) // found one!
  144. {
  145. moveOpen = true; // set flag denoting an open move:
  146. }
  147. }//for
  148. }//for
  149.  
  150. // is the game over? Not if there is an open move...
  151. if (moveOpen)
  152. return false; // open move ==> not over
  153. else
  154. return true; // NO open moves ==> gave over
  155. }
  156.  
  157.  
  158. //
  159. // main:
  160. //
  161. // Plays a game of Tic-Tac-Toe, using a 3x3 integer array. The human is player X, and
  162. // represented by a 1 in the array. The computer is player O, and represented by a 0
  163. // in the array. Empty locations are denoted by -1. Human goes first.
  164. //
  165. int main()
  166. {
  167. //
  168. // NOTE: this is an initial configuration of the board for TESTING purposes
  169. // only; change values to test your draw and winner functions. Once those
  170. // functions are working, you can use this approach to initialize the board
  171. // to empty.
  172.  
  173. int r,c;
  174.  
  175. cout << "**TESTING: draw function: " << endl;
  176. int board [3][3]={-1, -1, -1,
  177. -1, -1, -1,
  178. -1, -1, -1};
  179.  
  180. while (!gameover(board))
  181. {
  182. draw(board);
  183. cin >> r >> c;
  184. r=r-1;
  185. c=c-1;
  186. board[r][c]=1;
  187.  
  188. for (r=0; r<3; r++)
  189. {
  190. for (c=0; c<3; c++)
  191. {
  192. if (board[r][c] == -1)
  193. break;
  194. }
  195. board[r][c] = 0;
  196. break;
  197. }
  198. }
  199.  
  200. // test your winner function:
  201. cout << endl;
  202. cout << "** TESTING: winner function returns: " << winner(board) << endl;
  203. cout << endl;
  204.  
  205.  
  206.  
  207. //
  208. // TODO!
  209. //
  210.  
  211.  
  212.  
  213.  
  214.  
  215. //
  216. // done!
  217. //
  218. //system("Pause"); // keep console open when running inside Dev-C++ or Visual Studio:
  219.  
  220. return 0;
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement