Guest User

Untitled

a guest
May 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.26 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. int checkIftie(char board[][SIZE]);
  24.  
  25. int main()
  26. {
  27. int res;
  28. bool fPlayAgain=true;
  29. char answer;
  30.  
  31. do
  32. {
  33. res = playGame();
  34.  
  35. // NOTE: look how nice the main is when we use the const variables instead of just numbers
  36. if (res == GAME_STATUS_WINNER_PLAYER_1)
  37. cout << "Winer: player 1\n";
  38. else if (res == GAME_STATUS_WINNER_PLAYER_2)
  39. cout << "Winer: player 2\n";
  40. else
  41. cout << "TIE, no winner...\n";
  42.  
  43. cout << "Would you like to play again (Y/N)? ";
  44. cin >> answer;
  45. while (answer != 'n' && answer != 'N' && answer != 'y' && answer != 'Y')
  46. {
  47. cout << "Invalid answer. Try again: ";
  48. cin >> answer;
  49. }
  50. if (answer == 'n' || answer == 'N')
  51. fPlayAgain = false;
  52. } while (fPlayAgain);
  53. }
  54.  
  55.  
  56. int playGame()
  57. {
  58. char board[SIZE][SIZE] = {NO_PLAYER};
  59. int currentPlayer = 1;
  60. bool fIsCellFree, fHasWinner=false;
  61. int row, col, res;
  62.  
  63. do
  64. {
  65. system("cls");
  66.  
  67. cout << "The current board:\n\n";
  68. drawBoard(board);
  69. cout << "\n";
  70.  
  71. fIsCellFree = false;
  72. while (!fIsCellFree)
  73. {
  74. cout << "Please enter cordination for player #" << currentPlayer << ": ";
  75. cin >> row >> col;
  76.  
  77. // TODO: as long as the row is not between 1-SIZE, ask the user to to enter data again;
  78. if (row>SIZE || row<1)
  79. {
  80. cout<<"data is invalid, please enter data again";
  81. cin >> row >> col;
  82. }
  83. // TODO: as long as the row is not between 1-SIZE, ask the user to to enter data again
  84. if (col>SIZE || col<1)
  85. {
  86. cout<<"data is invalid, please enter data again";
  87. cin >> row >> col;
  88. }
  89. // TODO: complete the if to check if cell is free
  90. if (board [row-1][col-1] != NO_PLAYER)
  91. {
  92. cout << "The cell is already taken!\n";
  93. system("pause");
  94. continue;
  95. }
  96.  
  97. // if here, cell is valid...
  98. //-----------------------------
  99.  
  100. // mark cell and switch player
  101. fIsCellFree = true;
  102. if (currentPlayer == 1)
  103. {
  104. board[row-1][col-1] = PLAYER_1_SIGN;
  105. currentPlayer = 2;
  106. }
  107. else
  108. {
  109. board[row-1][col-1] = PLAYER_2_SIGN;
  110. currentPlayer = 1;
  111. }
  112. }
  113. system("pause");
  114. } while (checkGameStatus(board) == GAME_STATUS_NO_RESULT_YET);
  115.  
  116.  
  117. cout << "Final Board:\n";
  118. drawBoard(board);
  119.  
  120. res = checkGameStatus(board);
  121. return res;
  122. }
  123.  
  124. int checkGameStatus(char board[][SIZE])
  125. {
  126. bool fHasWinner = false;
  127. int res;
  128.  
  129. res = checkIfWinnerInRow(board);
  130. if (res == GAME_STATUS_WINNER_PLAYER_1 || res == GAME_STATUS_WINNER_PLAYER_2)
  131. return res;
  132.  
  133. // TODO: you need to declare the following functions..
  134. res = checkIfWinnerInCol(board);
  135. if (res == GAME_STATUS_WINNER_PLAYER_1 || res == GAME_STATUS_WINNER_PLAYER_2)
  136. return res;
  137.  
  138. res = checkIfWinnerInMainDiagonal(board);
  139. if (res == GAME_STATUS_WINNER_PLAYER_1 || res == GAME_STATUS_WINNER_PLAYER_2)
  140. return res;
  141.  
  142. res = checkIfWinnerInSecondaryDiagonal(board);
  143. if (res == GAME_STATUS_WINNER_PLAYER_1 || res == GAME_STATUS_WINNER_PLAYER_2)
  144. return res;
  145.  
  146. res = checkIftie(board);
  147. if (res == GAME_STATUS_TIE)
  148. return res;
  149.  
  150. }
  151.  
  152. // TODO: check if there is a cell that is not taken yet, and return GAME_STATUS_NO_RESULT_YET
  153.  
  154.  
  155. int checkIftie(char board[][SIZE]) // new
  156. {
  157.  
  158. for (int i=0; i < SIZE ;i++)
  159. {
  160. for (int j=0; j < SIZE; j++)
  161. {
  162. if (board[i][j] == NO_PLAYER)
  163. {
  164. return GAME_STATUS_NO_RESULT_YET;
  165. }
  166. }
  167. }
  168. return GAME_STATUS_TIE;
  169. }
  170.  
  171. // if we haven't returned yet, then there is no winner and the board is full, therefore TIE
  172.  
  173.  
  174. void drawBoard(char board[][SIZE])
  175. {
  176.  
  177. for (int i=0 ; i < SIZE ; i++)
  178. {
  179. for (int j=0 ; j < SIZE ; j++)
  180. {
  181. cout << " ";
  182. if (board[i][j] != NO_PLAYER)
  183. cout << board[i][j] ;
  184. else
  185. cout << " ";
  186. cout << " |";
  187. }
  188. cout << "\n";
  189. for (int j=0 ; j < SIZE ; j++)
  190. cout << "---|";
  191. cout << "\n";
  192. }
  193. }
  194.  
  195. int checkIfWinnerInRow(char board[][SIZE])
  196. {
  197. bool fHasWinnerInCurrentCheck;
  198.  
  199. for (int i=0 ; i < SIZE ; i++)
  200. {
  201. fHasWinnerInCurrentCheck = true;
  202. for (int j=1 ; j < SIZE && fHasWinnerInCurrentCheck ; j++)
  203. {
  204. if (board[i][j] != board[i][0] || board[i][j] == NO_PLAYER)
  205. fHasWinnerInCurrentCheck = false;
  206. }
  207.  
  208. if (fHasWinnerInCurrentCheck)
  209. {
  210. if (board[i][0] == PLAYER_1_SIGN)
  211. return GAME_STATUS_WINNER_PLAYER_1;
  212. else
  213. return GAME_STATUS_WINNER_PLAYER_2;
  214. }
  215. }
  216. return GAME_STATUS_NO_RESULT_YET;
  217. }
  218.  
  219.  
  220. int checkIfWinnerInCol(char board[][SIZE])
  221.  
  222. {
  223. bool fHasWinnerInCurrentCheck;
  224.  
  225. for (int j=0 ; j < SIZE ; j++)
  226. {
  227. fHasWinnerInCurrentCheck = true;
  228. for (int i=1 ; i < SIZE && fHasWinnerInCurrentCheck ; i++)
  229. {
  230. if (board[i][j] != board[0][j] || board[i][j] == NO_PLAYER)
  231. fHasWinnerInCurrentCheck = false;
  232. }
  233.  
  234. if (fHasWinnerInCurrentCheck)
  235. {
  236. if (board[0][j] == PLAYER_1_SIGN)
  237. return GAME_STATUS_WINNER_PLAYER_1;
  238. else
  239. return GAME_STATUS_WINNER_PLAYER_2;
  240. }
  241. }
  242. return GAME_STATUS_NO_RESULT_YET;
  243. }
  244.  
  245.  
  246.  
  247. int checkIfWinnerInMainDiagonal(char board[][SIZE])
  248. {
  249.  
  250. if (board[0][0] == board[1][1] && board[1][1] == board[2][2])
  251. {
  252. if (board[0][0] != NO_PLAYER)
  253. {
  254. if (board[0][0] == PLAYER_1_SIGN)
  255.  
  256. return GAME_STATUS_WINNER_PLAYER_1;
  257. else
  258.  
  259. return GAME_STATUS_WINNER_PLAYER_2;
  260. }
  261. }
  262. return GAME_STATUS_NO_RESULT_YET;
  263.  
  264. }
  265.  
  266.  
  267. int checkIfWinnerInSecondaryDiagonal(char board[][SIZE])
  268. {
  269. if (board[0][2] == board[1][1] && board[1][1] == board[2][0])
  270. {
  271. if (board[0][2] != NO_PLAYER)
  272. {
  273. if (board[0][2] == PLAYER_1_SIGN)
  274.  
  275. return GAME_STATUS_WINNER_PLAYER_1;
  276. else
  277.  
  278. return GAME_STATUS_WINNER_PLAYER_2;
  279. }
  280. }
  281. return GAME_STATUS_NO_RESULT_YET;
  282. }
Add Comment
Please, Sign In to add comment