Guest User

Untitled

a guest
Apr 25th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int SIZE=3;
  5.  
  6. const int NO_PLAYER = 0;
  7. const char PLAYER_1_SIGN = 'X';
  8. const char PLAYER_2_SIGN = 'O';
  9.  
  10. const int GAME_STATUS_WINNER_PLAYER_1 = 1;
  11. const int GAME_STATUS_WINNER_PLAYER_2 = 2;
  12. const int GAME_STATUS_TIE = 3;
  13. const int GAME_STATUS_NO_RESULT_YET = 0;
  14.  
  15. int playGame();
  16. // NOTE: all the functions that gets the matrix don't need to get the rows, since it is the same as the SIZE
  17. void drawBoard(char board[][SIZE]);
  18. int checkGameStatus(char board[][SIZE]);
  19. int checkIfWinnerInRow(char board[][SIZE]);
  20. int checkIfWinnerInCol(char board[][SIZE]);
  21. //int checkIfWinnerInMainDiagonal(char board[][SIZE]);
  22. //int checkIfWinnerInSecondaryDiagonal(char board[][SIZE]);
  23.  
  24. int main()
  25. {
  26. int res;
  27. bool fPlayAgain=true;
  28. char answer;
  29.  
  30. do
  31. {
  32. res = playGame();
  33.  
  34. // NOTE: look how nice the main is when we use the const variables instead of just numbers
  35. if (res == GAME_STATUS_WINNER_PLAYER_1)
  36. cout << "Winner: player 1\n";
  37. else if (res == GAME_STATUS_WINNER_PLAYER_2)
  38. cout << "Winner: player 2\n";
  39. else
  40. cout << "TIE, no winner...\n";
  41.  
  42. cout << "Would you like to play again (Y/N)? ";
  43. cin >> answer;
  44. while (answer != 'n' && answer != 'N' && answer != 'y' && answer != 'Y')
  45. {
  46. cout << "Invalid answer. Try again: ";
  47. cin >> answer;
  48. }
  49. if (answer == 'n' || answer == 'N')
  50. fPlayAgain = false;
  51. } while (fPlayAgain);
  52. }
  53.  
  54.  
  55. int playGame()
  56. {
  57. char board[SIZE][SIZE] = {NO_PLAYER};
  58. int currentPlayer = 1;
  59. bool fIsCellFree, fHasWinner=false;
  60. int row, col, res;
  61.  
  62. do
  63. {
  64. system("cls");
  65.  
  66. cout << "The current board:\n\n";
  67. drawBoard(board);
  68. cout << "\n";
  69.  
  70. fIsCellFree = false;
  71. while (!fIsCellFree)
  72. {
  73. cout << "Please enter cordination for player #" << currentPlayer << ": ";
  74. cin >> row >> col;
  75.  
  76. // TODO: as long as the row is not between 1-SIZE, ask the user to to enter data again
  77. // DONE
  78.  
  79. while (row<1 ||row>SIZE)
  80. {
  81. cout<<"please enter row between 1-" << SIZE << ":\n";
  82. cin>>row ;
  83. }
  84. // TODO: as long as the col is not between 1-SIZE, ask the user to to enter data again
  85. // DONE
  86.  
  87. while (col<1 ||col>SIZE)
  88. {
  89. cout<<"please enter col between 1-" << SIZE << ":\n";
  90. cin>>col ;
  91. }
  92.  
  93. // TODO: complete the if to check if cell is free
  94. // DONE
  95.  
  96. if (board[row-1][col-1]!=NO_PLAYER)
  97. {
  98. cout << "The cell is already taken!\n";
  99. system("pause");
  100. continue;
  101. }
  102.  
  103. // if here, cell is valid...
  104. //-----------------------------
  105.  
  106. // mark cell and switch player
  107. fIsCellFree = true;
  108. if (currentPlayer == 1)
  109. {
  110. board[row-1][col-1] = PLAYER_1_SIGN;
  111. currentPlayer = 2;
  112. }
  113. else
  114. {
  115. board[row-1][col-1] = PLAYER_2_SIGN;
  116. currentPlayer = 1;
  117. }
  118. }
  119. system("pause");
  120. } while (checkGameStatus(board) == GAME_STATUS_NO_RESULT_YET);
  121.  
  122.  
  123. cout << "Final Board:\n";
  124. drawBoard(board);
  125.  
  126. res = checkGameStatus(board);
  127. return res;
  128. }
  129.  
  130. int checkGameStatus(char board[][SIZE])
  131. {
  132. bool fHasWinner = false;
  133. int res;
  134.  
  135. res = checkIfWinnerInRow(board);
  136. if (res == GAME_STATUS_WINNER_PLAYER_1 || res == GAME_STATUS_WINNER_PLAYER_2)
  137. return res;
  138.  
  139. // TODO: you need to declare the following functions..
  140. res = checkIfWinnerInCol(board);
  141. if (res == GAME_STATUS_WINNER_PLAYER_1 || res == GAME_STATUS_WINNER_PLAYER_2)
  142. return res;
  143.  
  144. res = checkIfWinnerInMainDiagonal(board);
  145. if (res == GAME_STATUS_WINNER_PLAYER_1 || res == GAME_STATUS_WINNER_PLAYER_2)
  146. return res;
  147.  
  148. res = checkIfWinnerInSecondaryDiagonal(board);
  149. if (res == GAME_STATUS_WINNER_PLAYER_1 || res == GAME_STATUS_WINNER_PLAYER_2)
  150. return res;
  151.  
  152. // TODO: check if there is a cell that is not taken yet, and return GAME_STATUS_NO_RESULT_YET
  153. for ( int i=0 ; i<SIZE ; i++ )
  154. for (int j=0 ; j<SIZE ; j++ )
  155. if ( board[i][j] == NO_PLAYER )
  156. return GAME_STATUS_NO_RESULT_YET ;
  157.  
  158. // if we haven't returned yet, then there is no winner and the board is full, therefore TIE
  159. return GAME_STATUS_TIE;
  160. }
  161.  
  162. void drawBoard(char board[][SIZE])
  163. {
  164. for (int i=0 ; i < SIZE ; i++)
  165. {
  166. for (int j=0 ; j < SIZE ; j++)
  167. {
  168. cout << " ";
  169. if (board[i][j] != NO_PLAYER)
  170. cout << board[i][j] ;
  171. else
  172. cout << " ";
  173. cout << " |";
  174. }
  175. cout << "\n";
  176. for (int j=0 ; j < SIZE ; j++)
  177. cout << "---|";
  178. cout << "\n";
  179. }
  180. }
  181.  
  182. int checkIfWinnerInRow(char board[][SIZE])
  183. {
  184. bool fHasWinnerInCurrentCheck;
  185.  
  186. for (int i=0 ; i < SIZE ; i++)
  187. {
  188. fHasWinnerInCurrentCheck = true;
  189. for (int j=1 ; j < SIZE && fHasWinnerInCurrentCheck ; j++)
  190. {
  191. if (board[i][j] != board[i][0] || board[i][j] == NO_PLAYER)
  192. fHasWinnerInCurrentCheck = false;
  193. }
  194.  
  195. if (fHasWinnerInCurrentCheck)
  196. {
  197. if (board[i][0] == PLAYER_1_SIGN)
  198. return GAME_STATUS_WINNER_PLAYER_1;
  199. else
  200. return GAME_STATUS_WINNER_PLAYER_2;
  201. }
  202. }
  203. return GAME_STATUS_NO_RESULT_YET;
  204. }
  205.  
  206. int checkIfWinnerInCol(char board[][SIZE])
  207. {
  208. bool fHasWinnerInCurrentCheck;
  209.  
  210. for (int j=0 ; j < SIZE ; j++)
  211. {
  212. fHasWinnerInCurrentCheck = true;
  213. for (int i=1 ; i < SIZE && fHasWinnerInCurrentCheck ; i++)
  214. {
  215. if (board[i][j] != board[0][j] || board[i][j] == NO_PLAYER)
  216. fHasWinnerInCurrentCheck = false;
  217. }
  218.  
  219. if (fHasWinnerInCurrentCheck)
  220. {
  221. if (board[0][j] == PLAYER_1_SIGN)
  222. return GAME_STATUS_WINNER_PLAYER_1;
  223. else
  224. return GAME_STATUS_WINNER_PLAYER_2;
  225. }
  226. }
  227. return GAME_STATUS_NO_RESULT_YET;
  228. }
Add Comment
Please, Sign In to add comment