Guest User

Untitled

a guest
Apr 25th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.33 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 << "Winer: player 1\n";
  37. else if (res == GAME_STATUS_WINNER_PLAYER_2)
  38. cout << "Winer: 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. while (row < 1 && row > SIZE)
  78. {
  79. cout << "invalid row, please insert again" << endl;
  80. cin >> row;
  81. }
  82.  
  83. // TODO: as long as the row is not between 1-SIZE, ask the user to to enter data again
  84. while (col < 1 && col > SIZE)
  85. {
  86. cout << "invalid col, please insert again" << endl;
  87. cin >> col;
  88. }
  89.  
  90. // TODO: complete the if to check if cell is free
  91. if (board[row-1][col-1] != 0)
  92. {
  93. cout << "The cell is already taken!\n";
  94. system("pause");
  95. continue;
  96. }
  97.  
  98. // if here, cell is valid...
  99. //-----------------------------
  100.  
  101. // mark cell and switch player
  102. fIsCellFree = true;
  103. if (currentPlayer == 1)
  104. {
  105. board[row-1][col-1] = PLAYER_1_SIGN;
  106. currentPlayer = 2;
  107. }
  108. else
  109. {
  110. board[row-1][col-1] = PLAYER_2_SIGN;
  111. currentPlayer = 1;
  112. }
  113. }
  114. system("pause");
  115. } while (checkGameStatus(board) == GAME_STATUS_NO_RESULT_YET);
  116.  
  117.  
  118. cout << "Final Board:\n";
  119. drawBoard(board);
  120.  
  121. res = checkGameStatus(board);
  122. return res;
  123. }
  124.  
  125. int checkGameStatus(char board[][SIZE])
  126. {
  127. bool fHasWinner = false;
  128. int res,i,j;
  129.  
  130.  
  131. res = checkIfWinnerInRow(board);
  132. if (res == GAME_STATUS_WINNER_PLAYER_1 || res == GAME_STATUS_WINNER_PLAYER_2)
  133. return res;
  134.  
  135. // TODO: you need to declare the following functions..
  136. res = checkIfWinnerInCol(board);
  137. if (res == GAME_STATUS_WINNER_PLAYER_1 || res == GAME_STATUS_WINNER_PLAYER_2)
  138. return res;
  139.  
  140. res = checkIfWinnerInMainDiagonal(board);
  141. if (res == GAME_STATUS_WINNER_PLAYER_1 || res == GAME_STATUS_WINNER_PLAYER_2)
  142. return res;
  143.  
  144. res = checkIfWinnerInSecondaryDiagonal(board);
  145. if (res == GAME_STATUS_WINNER_PLAYER_1 || res == GAME_STATUS_WINNER_PLAYER_2)
  146. return res;
  147.  
  148. // TODO: check if there is a cell that is not taken yet, and return GAME_STATUS_NO_RESULT_YET
  149. for (i=0; i<SIZE; i++)
  150. {
  151. for (j=0; j<SIZE; j++)
  152. {
  153. if (board[i][j] == 0)
  154. {
  155. return GAME_STATUS_NO_RESULT_YET;
  156. }
  157. }
  158. }
  159.  
  160.  
  161. // if we haven't returned yet, then there is no winner and the board is full, therefore TIE
  162. return GAME_STATUS_TIE;
  163. }
  164.  
  165.  
  166.  
  167. void drawBoard(char board[][SIZE])
  168. {
  169. for (int i=0 ; i < SIZE ; i++)
  170. {
  171. for (int j=0 ; j < SIZE ; j++)
  172. {
  173. cout << " ";
  174. if (board[i][j] != NO_PLAYER)
  175. cout << board[i][j] ;
  176. else
  177. cout << " ";
  178. cout << " |";
  179. }
  180. cout << "\n";
  181. for (int j=0 ; j < SIZE ; j++)
  182. cout << "---|";
  183. cout << "\n";
  184. }
  185. }
  186.  
  187. int checkIfWinnerInRow(char board[][SIZE])
  188. {
  189. bool fHasWinnerInCurrentCheck;
  190.  
  191. for (int i=0 ; i < SIZE ; i++)
  192. {
  193. fHasWinnerInCurrentCheck = true;
  194. for (int j=1 ; j < SIZE && fHasWinnerInCurrentCheck ; j++)
  195. {
  196. if (board[i][j] != board[i][0] || board[i][j] == NO_PLAYER)
  197. fHasWinnerInCurrentCheck = false;
  198. }
  199.  
  200. if (fHasWinnerInCurrentCheck)
  201. {
  202. if (board[i][0] == PLAYER_1_SIGN)
  203. return GAME_STATUS_WINNER_PLAYER_1;
  204. else
  205. return GAME_STATUS_WINNER_PLAYER_2;
  206. }
  207. }
  208. return GAME_STATUS_NO_RESULT_YET;
  209. }
  210.  
  211. int checkIfWinnerInCol(char board[][SIZE])
  212. {
  213. int i,j;
  214. bool fHasWinnerInCurrentCheck;
  215.  
  216. for (i=0 ; i < SIZE; i++)
  217. {
  218. fHasWinnerInCurrentCheck=true;
  219. for (j=0; j < SIZE-1 && fHasWinnerInCurrentCheck; j++)
  220. {
  221. if (board[j][i] != board[j+1][i] || board[j][i] == NO_PLAYER || board[j+1][i] == NO_PLAYER)
  222. {
  223. fHasWinnerInCurrentCheck = false;
  224. }
  225. }
  226. if (fHasWinnerInCurrentCheck)
  227. {
  228. if (board[1][i] == PLAYER_1_SIGN)
  229. return GAME_STATUS_WINNER_PLAYER_1;
  230. else
  231. return GAME_STATUS_WINNER_PLAYER_2;
  232. }
  233. }
  234. return GAME_STATUS_NO_RESULT_YET;
  235. }
  236.  
  237. int checkIfWinnerInMainDiagonal(char board[][SIZE])
  238. {
  239. int i,c=0;;
  240.  
  241.  
  242. for (i=0; i<SIZE-1; i++)
  243. {
  244. if (board[i][i] != board[i+1][i+1] || board[i][i] == NO_PLAYER || board[i+1][i+1] == NO_PLAYER)
  245. {
  246. c++;
  247. }
  248. }
  249. if (c == 0)
  250. {
  251. if (board[0][0] == PLAYER_1_SIGN)
  252. return GAME_STATUS_WINNER_PLAYER_1;
  253. else
  254. return GAME_STATUS_WINNER_PLAYER_2;
  255. }
  256.  
  257. return GAME_STATUS_NO_RESULT_YET;
  258. }
  259.  
  260.  
  261. int checkIfWinnerInSecondaryDiagonal(char board[][SIZE])
  262. {
  263. int i,j,c=0,temp;
  264. bool fHasWinnerInCurrentCheck;
  265. temp=SIZE-1;
  266.  
  267. for (i=0; i<SIZE-1; i++)
  268. {
  269. if (board[temp][i] != board [temp-1][i+1] || board[temp][i] == NO_PLAYER)
  270. {
  271. c++;
  272. }
  273. temp--;
  274. }
  275. if (c == 0)
  276. {
  277. if (board[2][2] == PLAYER_1_SIGN)
  278. return GAME_STATUS_WINNER_PLAYER_1;
  279. else
  280. return GAME_STATUS_WINNER_PLAYER_2;
  281. }
  282.  
  283.  
  284. return GAME_STATUS_NO_RESULT_YET;
  285. }
Add Comment
Please, Sign In to add comment