Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 31st, 2012  |  syntax: None  |  size: 8.03 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //Name:Rita Vaitman ID:307472050
  2. //Its a game of Tic tac toe
  3. #include <iostream>
  4. #include <string.h>
  5. using namespace std;
  6.  
  7. const int SIZE=3;
  8.  
  9. const int  NO_PLAYER  = 0;
  10. const char PLAYER_1_SIGN = 'X';
  11. const char PLAYER_2_SIGN = 'O';
  12.  
  13. const int GAME_STATUS_WINNER_PLAYER_1 = 1;
  14. const int GAME_STATUS_WINNER_PLAYER_2 = 2;
  15. const int GAME_STATUS_TIE             = 3;
  16. const int GAME_STATUS_NO_RESULT_YET   = 0;
  17.  
  18. int playGame();
  19. // NOTE: all the functions that gets the matrix don't need to get the rows, since it is the same as the SIZE
  20. void drawBoard(char board[][SIZE]);
  21. int checkGameStatus(char board[][SIZE]);
  22. int checkIfWinnerInRow(char board[][SIZE]);
  23. int checkIfWinnerInCol(char board[][SIZE]);
  24. int checkIfWinnerInMainDiagonal(char board[][SIZE]);
  25. int checkIfWinnerInSecondaryDiagonal(char board[][SIZE]);
  26. int checkIfThereIsEmptyCell(char board[][SIZE]);
  27.  
  28. int main()
  29. {
  30.         int res;
  31.         bool fPlayAgain = true;
  32.         char answer;
  33.        
  34.         do
  35.         {
  36.                 res = playGame();
  37.  
  38.                 // NOTE: look how nice the main is when we use the const variables instead of just numbers
  39.                 if (res == GAME_STATUS_WINNER_PLAYER_1)
  40.                         cout << "Winer: player 1\n";
  41.                 else if (res == GAME_STATUS_WINNER_PLAYER_2)
  42.                         cout << "Winer: player 2\n";
  43.                 else
  44.                         cout << "TIE, no winner...\n";
  45.  
  46.                 cout << "Would you like to play again (Y/N)? ";
  47.                 cin >> answer;
  48.                 while (answer != 'n' && answer != 'N' && answer != 'y' && answer != 'Y')
  49.                 {
  50.                         cout << "Invalid answer. Try again: ";
  51.                         cin >> answer;
  52.                 }
  53.                 if (answer == 'n' || answer == 'N')
  54.                         fPlayAgain = false;
  55.         } while (fPlayAgain);
  56. }
  57.  
  58.  
  59. int playGame()
  60. {
  61.         char board[SIZE][SIZE] = {NO_PLAYER};
  62.         int  currentPlayer = 1;
  63.         bool fIsCellFree, fHasWinner=false;
  64.         int row, col, res;
  65.  
  66.         do
  67.         {
  68.                 system("cls");
  69.  
  70.                 cout << "The current board:\n\n";
  71.                 drawBoard(board);
  72.                 cout << "\n";
  73.  
  74.                 fIsCellFree = false;
  75.                 while (fIsCellFree == false)
  76.                 {
  77.                         cout << "Please enter cordination for player #" << currentPlayer << ": ";
  78.                         cin >> row >> col;
  79.  
  80.                         // TODO: as long as the row is not between 1-SIZE, ask the user to to enter data again
  81.                        
  82.                         while (row < 1 || row > SIZE)
  83.                         {
  84.                                 cout<<"You have entered illegal cordinarion, please enter data again: ";
  85.                                 cin >> row >> col;
  86.                         }
  87.                         // TODO: as long as the col is not between 1-SIZE, ask the user to to enter data again
  88.                        
  89.                         while (col < 1 || col > SIZE)
  90.                         {
  91.                                 cout<<"You have entered illegal cordinarion, please enter data again: ";
  92.                                 cin >> row >> col;
  93.                         }
  94.                        
  95.                         // TODO: complete the if to check if cell is free
  96.                         if (board[row-1][col-1] == PLAYER_1_SIGN || board[row-1][col-1] == PLAYER_2_SIGN )
  97.                         {
  98.                                 cout << "The cell is already taken!\n";
  99.                                 system("pause");
  100.                                 continue;
  101.                         }
  102.  
  103.                         // if here, cell is valid...
  104.                        
  105.                         fIsCellFree = true;
  106.                        
  107.  
  108.                         //  mark cell and switch player
  109.                        
  110.                        
  111.  
  112.                         if (currentPlayer == 1)
  113.                         {
  114.                                 board[row-1][col-1] = PLAYER_1_SIGN;
  115.                                 currentPlayer = 2;
  116.                         }
  117.                         else
  118.                         {
  119.                                 board[row-1][col-1] = PLAYER_2_SIGN;
  120.                                 currentPlayer = 1;
  121.                         }
  122.                 }
  123.                 system("pause");
  124.         } while (checkGameStatus(board) == GAME_STATUS_NO_RESULT_YET);
  125.        
  126.        
  127.         cout << "Final Board:\n";
  128.         drawBoard(board);
  129.  
  130.         res = checkGameStatus(board);
  131.         return res;
  132. }
  133.  
  134. int checkGameStatus(char board[][SIZE])
  135. {
  136.         bool fHasWinner = false;
  137.         int res;
  138.  
  139.         res = checkIfWinnerInRow(board);
  140.         if (res == GAME_STATUS_WINNER_PLAYER_1 || res == GAME_STATUS_WINNER_PLAYER_2)
  141.                 return res;
  142.  
  143.         // TODO: you need to declare the following functions..
  144.         res = checkIfWinnerInCol(board);
  145.         if (res == GAME_STATUS_WINNER_PLAYER_1 || res == GAME_STATUS_WINNER_PLAYER_2)
  146.                 return res;
  147.  
  148.         res = checkIfWinnerInMainDiagonal(board);
  149.         if (res == GAME_STATUS_WINNER_PLAYER_1 || res == GAME_STATUS_WINNER_PLAYER_2)
  150.                 return res;
  151.  
  152.         res = checkIfWinnerInSecondaryDiagonal(board);
  153.         if (res == GAME_STATUS_WINNER_PLAYER_1 || res == GAME_STATUS_WINNER_PLAYER_2)
  154.                 return res;
  155.  
  156.         // TODO: check if there is a cell that is not taken yet, and return GAME_STATUS_NO_RESULT_YET
  157.        
  158.         res = checkIfThereIsEmptyCell(board);
  159.         if(res == GAME_STATUS_NO_RESULT_YET)
  160.         {
  161.                 return GAME_STATUS_NO_RESULT_YET;
  162.         }
  163.  
  164.         // if we haven't returned yet, then there is no winner and the board is full, therefore TIE
  165.        
  166.         else   
  167.         {
  168.                 return GAME_STATUS_TIE;
  169.         }
  170. }
  171.  
  172. void drawBoard(char board[][SIZE])
  173. {
  174.         for (int i=0 ; i < SIZE ; i++)
  175.         {
  176.                 for (int j=0 ; j < SIZE ; j++)
  177.                 {
  178.                         cout << " ";
  179.                         if (board[i][j] != NO_PLAYER)
  180.                                 cout << board[i][j] ;
  181.                         else
  182.                                 cout << " ";
  183.                         cout << " |";
  184.                 }
  185.                 cout << "\n";
  186.                 for (int j=0 ; j < SIZE ; j++)
  187.                         cout << "---|";
  188.                 cout << "\n";
  189.         }
  190. }
  191.  
  192. int checkIfWinnerInRow(char board[][SIZE])
  193. {
  194.         bool fHasWinnerInCurrentCheck;
  195.  
  196.         for (int i=0 ; i < SIZE ; i++)
  197.         {
  198.                 fHasWinnerInCurrentCheck = true;
  199.                 for (int j=1 ; j < SIZE && fHasWinnerInCurrentCheck ; j++)
  200.                 {
  201.                         if (board[i][j] != board[i][0] || board[i][j] == NO_PLAYER)
  202.                                 fHasWinnerInCurrentCheck = false;
  203.                 }
  204.  
  205.                 if (fHasWinnerInCurrentCheck)
  206.                 {
  207.                         if (board[i][0] == PLAYER_1_SIGN)
  208.                                 return GAME_STATUS_WINNER_PLAYER_1;
  209.                         else
  210.                                 return GAME_STATUS_WINNER_PLAYER_2;
  211.                 }
  212.         }
  213.         return GAME_STATUS_NO_RESULT_YET;
  214. }
  215.  
  216.  
  217. int checkIfWinnerInCol(char board[][SIZE])
  218. {
  219.         bool fHasWinnerInCurrentCheck;
  220.  
  221.         for (int j=0 ; j < SIZE ; j++)
  222.         {
  223.                 fHasWinnerInCurrentCheck = true;
  224.                 for (int i=1 ; i < SIZE && fHasWinnerInCurrentCheck ; i++)
  225.                 {
  226.                         if (board[i][j] != board[0][j] || board[i][j] == NO_PLAYER)
  227.                                 fHasWinnerInCurrentCheck = false;
  228.                 }
  229.  
  230.                 if (fHasWinnerInCurrentCheck)
  231.                 {
  232.                         if (board[0][j] == PLAYER_1_SIGN)
  233.                                 return GAME_STATUS_WINNER_PLAYER_1;
  234.                         else
  235.                                 return GAME_STATUS_WINNER_PLAYER_2;
  236.                 }
  237.         }
  238.         return GAME_STATUS_NO_RESULT_YET;
  239. }
  240.  
  241. int checkIfWinnerInMainDiagonal(char board[][SIZE])
  242. {
  243.         bool fHasWinnerInCurrentCheck;
  244.  
  245.         for (int i=1,j=1 ; i < SIZE ; i++,j++)
  246.         {
  247.                 fHasWinnerInCurrentCheck = true;
  248.                        
  249.                 if (board[i][j] != board[0][0] || board[i][j] == NO_PLAYER)
  250.                 {
  251.                         fHasWinnerInCurrentCheck = false;
  252.                 }
  253.                
  254.         }
  255.         if (fHasWinnerInCurrentCheck)
  256.         {
  257.                 if (board[0][0] == PLAYER_1_SIGN)
  258.                 {
  259.                         return GAME_STATUS_WINNER_PLAYER_1;
  260.                 }
  261.                 else
  262.                 {                      
  263.                         return GAME_STATUS_WINNER_PLAYER_2;
  264.                 }
  265.         }
  266.  
  267.         return GAME_STATUS_NO_RESULT_YET;
  268. }
  269. int checkIfWinnerInSecondaryDiagonal(char board[][SIZE])
  270. {
  271.         bool fHasWinnerInCurrentCheck;
  272.  
  273.         for (int i=1,j=1; i < SIZE ; i++,j--)
  274.         {
  275.                 fHasWinnerInCurrentCheck = true;
  276.                
  277.                         if (board[i][j] != board[0][2] || board[i][j] == NO_PLAYER)
  278.                         {
  279.                                 fHasWinnerInCurrentCheck = false;
  280.                         }
  281.         }
  282.  
  283.         if (fHasWinnerInCurrentCheck)
  284.         {
  285.                 if (board[0][2] == PLAYER_1_SIGN)
  286.                 {
  287.                         return GAME_STATUS_WINNER_PLAYER_1;
  288.                 }              
  289.                 else
  290.                 {              
  291.                         return GAME_STATUS_WINNER_PLAYER_2;
  292.        
  293.                 }
  294.         }
  295.         return GAME_STATUS_NO_RESULT_YET;
  296. }
  297.  
  298. int checkIfThereIsEmptyCell(char board[][SIZE])
  299. {
  300.        
  301.         for (int i=0 ; i < SIZE ; i++)
  302.         {
  303.                 for (int j=0 ;j < SIZE; j++)
  304.                
  305.                         if (board[i][j] ==  GAME_STATUS_NO_RESULT_YET)
  306.                         {
  307.                                 return GAME_STATUS_NO_RESULT_YET;
  308.                         }
  309.                        
  310.         }
  311.         return GAME_STATUS_TIE;
  312. }
  313.  
  314. //The current board:
  315. //
  316. // X |   |   |
  317. //---|---|---|
  318. // O | X |   |
  319. //---|---|---|
  320. // O |   |   |
  321. //---|---|---|
  322. //
  323. //Please enter cordination for player #1: 3 3
  324. //Press any key to continue . . .
  325. //Final Board:
  326. // X |   |   |
  327. //---|---|---|
  328. // O | X |   |
  329. //---|---|---|
  330. // O |   | X |
  331. //---|---|---|
  332. //Winer: player 1
  333. //Would you like to play again (Y/N)?
  334.  
  335. //The current board:
  336. //
  337. // X | X |   |
  338. //---|---|---|
  339. // O |   | O |
  340. //---|---|---|
  341. // X |   |   |
  342. //---|---|---|
  343. //
  344. //Please enter cordination for player #2: 2 2
  345. //Press any key to continue . . .
  346. //Final Board:
  347. // X | X |   |
  348. //---|---|---|
  349. // O | O | O |
  350. //---|---|---|
  351. // X |   |   |
  352. //---|---|---|
  353. //Winer: player 2
  354. //Would you like to play again (Y/N)?
  355.  
  356. //The current board:
  357. //
  358. // X |   |   |
  359. //---|---|---|
  360. // X | O |   |
  361. //---|---|---|
  362. //   |   | O |
  363. //---|---|---|
  364. //
  365. //Please enter cordination for player #1: 3 1
  366. //Press any key to continue . . .
  367. //Final Board:
  368. // X |   |   |
  369. //---|---|---|
  370. // X | O |   |
  371. //---|---|---|
  372. // X |   | O |
  373. //---|---|---|
  374. //Winer: player 1
  375. //Would you like to play again (Y/N)?