Advertisement
Guest User

Untitled

a guest
May 17th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. void clear(char board[3][3]);
  4. void display(const char board[3][3]);
  5. void takeTurn(char board[3][3], char &nextPlayer);
  6. char winner(char board[3][3]);
  7.  
  8. void clear(char board[3][3]){
  9.  
  10. for(int i = 0; i <= 2; i++)
  11. for(int j = 0; j <= 2; j++)
  12. board[i][j] = ' ';
  13. }
  14.  
  15. void display(const char board[3][3]){
  16.  
  17.  
  18. for(int i = 0; i <= 2; i++) {
  19. for(int j = 0; j <= 2; j++) {
  20. cout << board[i][j];
  21. if (j < 2)
  22. cout << "|";
  23.  
  24. }
  25. if(i < 2)
  26. cout << endl << "-----" ;
  27. cout << endl;
  28. }
  29.  
  30.  
  31. }
  32.  
  33. void takeTurn(char board[3][3], char &nextPlayer) {
  34. int row, column;
  35.  
  36. //if the data that is entered is valid then this variable will be 1
  37. //otherwise if the row or column is out of range it's 0
  38. //otherwise if it already contain something it's 0
  39. int is_valid = 0;
  40.  
  41. while (is_valid != 1) {
  42.  
  43. cout << "It is now " << nextPlayer << "'s turn." << endl;
  44. cout << "Please enter your move in the form row column" << endl;
  45. cout << "So 0 0 would be the top left, and 0 2 would be the top right" << endl;
  46.  
  47. cin >> row >> column;
  48.  
  49. is_valid = 1;
  50.  
  51. // check to see if the input is in the valid range
  52. if ( (row < 0) || (row > 2) || (column < 0) || (column > 2) ) {
  53. cout << "Invalid entry: row and column must both be between 0 and 2 (inclusive)." << endl;
  54. cout << "Please try again." << endl;
  55. is_valid = 0;
  56. }
  57.  
  58. // then check to see if there is something already there
  59. if ( board[row][column] != ' ') {
  60. cout << "Invalid entry: Row " << row << "at Column " << column << " already contains: " << board[row][column] << endl;
  61. is_valid = 0;
  62. }
  63.  
  64.  
  65. }
  66.  
  67. board[row][column] = nextPlayer;
  68.  
  69. if (nextPlayer == 'X') { nextPlayer = 'O'; return; }
  70. if (nextPlayer == 'O') { nextPlayer = 'X' ; return;}
  71. }
  72.  
  73. char winner(char board[3][3]) {
  74.  
  75.  
  76. //check to see if any rows are all X
  77. for (int i=0; i <=2; i++) {
  78. if (board[i][0] == 'X' && board[i][1] == 'X' && board[i][2] == 'X') {
  79. display(board);
  80. cout << "Congratulations, X YOU WON!" << endl;
  81. return 'X';
  82. }
  83. }
  84.  
  85. // check to see if any columns are all X
  86. for (int i=0; i <=2; i++) {
  87. if (board[0][i] == 'X' && board[1][i] == 'X' && board[2][i] == 'X') {
  88. display(board);
  89. cout << "Congratulations, X YOU WON!" << endl;
  90. return 'X';
  91. }
  92. }
  93.  
  94.  
  95.  
  96. //check to see if any rows are all O
  97. for (int i=0; i <=2; i++) {
  98. if (board[i][0] == 'O' && board[i][1] == 'O' && board[i][2] == 'O') {
  99. display(board);
  100. cout << "Congratulations, O YOU WON!" << endl;
  101. return 'O';
  102. }
  103. }
  104.  
  105. // check to see if any columns are all X
  106. for (int i=0; i <=2; i++) {
  107. if (board[0][i] == 'O' && board[1][i] == 'O' && board[2][i] == 'O') {
  108. display(board);
  109. cout << "Congratulations, O YOU WON!" << endl;
  110. return 'O';
  111. }
  112. }
  113.  
  114. //check the left to right diagonal to see if there is a winner
  115. if (board[0][0] == 'X' && board[1][1] == 'X' && board [2][2] == 'X') {
  116. display(board);
  117. cout << "Congratulations, X YOU WON!" << endl;
  118. return 'X';
  119. }
  120.  
  121. if (board[0][0] == 'O' && board[1][1] == 'O' && board [2][2] == 'O') {
  122. display(board);
  123. cout << "Congratulations, O YOU WON!" << endl;
  124. return 'O';
  125. }
  126.  
  127. //check to see if the right to left diagonal is a winner
  128. if (board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X') {
  129. display(board);
  130. cout << "Congratulations, X YOU WON!" << endl;
  131. return 'X';
  132. }
  133.  
  134. if (board[0][2] == 'O' && board[1][1] == 'O' && board[2][0] == 'O') {
  135. display(board);
  136. cout << "Congratulations, O YOU WON!" << endl;
  137. return 'O';
  138. }
  139.  
  140. //now check to see if we have filled all the board positions
  141. //since we have checked for all the possible wining combinations
  142. //at this point, if the board is filled, it must be a tie
  143. int is_tie = 1;
  144. for (int i = 0; i <=2; i++) {
  145. for (int j = 0; j <=2; j++) {
  146. if (board[i][j] == ' ') { is_tie = 0; }
  147. }
  148. }
  149. if (is_tie == 1) {
  150. display(board);
  151. cout << "Nobody won. Please play again.";
  152. return '?';
  153. }
  154.  
  155. //Finally, if there is no winner, and there is no tie, then we will return a ' ' meaning that the game is still
  156. // active and we will play another round
  157. return ' ';
  158.  
  159. }
  160.  
  161. int main()
  162. {
  163.  
  164. char board[3][3];
  165. char nextPlayer;
  166. char winningPlayer;
  167.  
  168. clear(board);
  169.  
  170. //winningPlayer can be:
  171. //' ' no winner yet
  172. //? tie
  173. //X X wins
  174. //O O wins
  175. winningPlayer = ' ';
  176. nextPlayer = 'X';
  177.  
  178. /*
  179. board[0][0] = 'X';
  180. board[2][1] = 'O';
  181. board[1][2] = 'X';
  182. board[1][1] = 'O';
  183. board[2][0] = 'X';
  184. board[0][1] = 'O';*/
  185.  
  186. while (winningPlayer == ' '){
  187. display(board);
  188. takeTurn(board, nextPlayer);
  189. winningPlayer = winner(board);
  190. }
  191.  
  192. return 0;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement