Advertisement
Guest User

C++ Tic Tac Toe

a guest
Sep 25th, 2017
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.49 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. /*
  6.  * print the game board
  7.  *
  8.  * this function is given and already complete
  9.  * do NOT modify anything of it
  10.  */
  11. void printGameBoard(char s1, char s2, char s3, char s4, char s5, char s6, char s7, char s8, char s9)
  12. {
  13.     cout << "     |     |     " << endl;
  14.     cout << "  " << s1 << "  |  " << s2 << "  |  " << s3 << endl;
  15.     cout << "_____|_____|_____" << endl;
  16.     cout << "     |     |     " << endl;
  17.     cout << "  " << s4 << "  |  " << s5 << "  |  " << s6 << endl;
  18.     cout << "_____|_____|_____" << endl;
  19.     cout << "     |     |     " << endl;
  20.     cout << "  " << s7 << "  |  " << s8 << "  |  " << s9 << endl;
  21.     cout << "     |     |     " << endl << endl;
  22. }
  23.  
  24. /*
  25.  * print the message that asks the current player for his/her input
  26.  *
  27.  * you MUST call this function in your getValidInput implementation
  28.  *
  29.  * this function is given and already complete
  30.  * do NOT modify anything of it
  31.  */
  32. void printQuestion(char currentPlayerSymbol)
  33. {
  34.      cout << "Player " << currentPlayerSymbol << ", please choose an unoccupied space (0 to quit): " << endl;
  35. }
  36.  
  37. /*
  38.  * return 'X' (uppercase English letter X) when roundNumber is odd
  39.  * return 'O' (uppercase English letter O) otherwise
  40.  *
  41.  * complete the function body code, but do NOT modify the function prototype (including the function name, parameters, and return type)
  42.  */
  43. char getCurrentPlayerSymbol(int roundNumber)
  44. {
  45.     return (roundNumber%2)?'X':'O';
  46. }
  47.  
  48. /*
  49.  * return whether the input is valid
  50.  * an input is valid if and only if the input is in the valid range of [0, 9]
  51.  * and that the input refers to an unoccupied space (i.e. not 'O' and not 'X')
  52.  *
  53.  * be reminded to always return either true or false in all scenarios
  54.  *
  55.  * complete the function body code, but do NOT modify the function prototype (including the function name, parameters, and return type)
  56.  */
  57. bool isInputValid(int input, char s1, char s2, char s3, char s4, char s5, char s6, char s7, char s8, char s9)
  58. {
  59.     char tempGrid[] = {s1, s2, s3, s4, s5, s6, s7, s8 , s9};
  60.     if(input >= 0 && input <= 9 && tempGrid[input-1] != 'X' && tempGrid[input-1] != 'O'){
  61.         return true;
  62.     }
  63. }
  64.  
  65. /*
  66.  * use some kind of loop to retrieve a valid integer input from the player, and return it at the end of the function
  67.  * the flow is as follows
  68.  * 1. Print exactly the question message "Player X, please choose an unoccupied space (0 to quit): "
  69.  *    or "Player O, please choose an unoccupied space (0 to quit): " depending on what currentPlayerSymbol is,
  70.  *    using the given printQuestion function
  71.  * 2. Retrieve the integer input using cin
  72.  * 3. If the input is valid (use the isInputValid function), return the input; if the input is invalid, go back to step 1
  73.  *
  74.  * *** IMPORTANT REQUIREMENT ***
  75.  * you must NOT use any cout statement directly to print the question message in this function - you MUST call the given printQuestion function to print the message
  76.  *
  77.  * you can assume the player is sane and always inputs an integer (e.g. no alphabet/symbol input etc.)
  78.  *
  79.  * complete the function body code, but do NOT modify the function prototype (including the function name, parameters, and return type)
  80.  */
  81. int getValidInput(char currentPlayerSymbol, char s1, char s2, char s3, char s4, char s5, char s6, char s7, char s8, char s9)
  82. {
  83.     int userInput = -1;
  84.     do{
  85.         printQuestion(currentPlayerSymbol);
  86.         cin >> userInput;
  87.     }while(!isInputValid(userInput, s1, s2, s3, s4, s5, s6, s7, s8, s9));
  88.     return userInput;
  89. }
  90.  
  91. /*
  92.  * put the currentPlayerSymbol to one of the s1, s2, ..., s9 variables according to the input which is guaranteed to be in the range of [1, 9]
  93.  *
  94.  * complete the function body code, but do NOT modify the function prototype (including the function name, parameters, and return type)
  95.  */
  96. void putSymbol(int input, char currentPlayerSymbol, char& s1, char& s2, char& s3, char& s4, char& s5, char& s6, char& s7, char& s8, char& s9)
  97. {
  98.     switch(input){
  99.         case 1: s1 = currentPlayerSymbol;break;
  100.         case 2: s2 = currentPlayerSymbol;break;
  101.         case 3: s3 = currentPlayerSymbol;break;
  102.         case 4: s4 = currentPlayerSymbol;break;
  103.         case 5: s5 = currentPlayerSymbol;break;
  104.         case 6: s6 = currentPlayerSymbol;break;
  105.         case 7: s7 = currentPlayerSymbol;break;
  106.         case 8: s8 = currentPlayerSymbol;break;
  107.         case 9: s9 = currentPlayerSymbol;break;
  108.     }
  109. }
  110.  
  111. /*
  112.  * return the game state
  113.  * game state is 1 if player 1 (X) has won the game
  114.  * game state is 2 if player 2 (O) has won the game
  115.  * game state is -1 if the broad is full and no one has won the game yet (i.e. draw game)
  116.  * game state is 0 otherwise
  117.  *
  118.  * complete the function body code, but do NOT modify the function prototype (including the function name, parameters, and return type)
  119.  */
  120. int getGameState(char s1, char s2, char s3, char s4, char s5, char s6, char s7, char s8, char s9)
  121. {
  122.     //Function Variables
  123.     char tempGrid[] = {s1, s2, s3, s4, s5, s6, s7, s8 , s9};
  124.  
  125.     if (s1 == s2 && s2 == s3){
  126.         return(s1=='X')?1:2;
  127.     }
  128.     else if(s4 == s5 && s5 == s6){
  129.         return(s4=='X')?1:2;
  130.     }
  131.     else if(s7 == s8 && s8 == s9){
  132.         return(s7=='X')?1:2;
  133.     }
  134.     else if(s1 == s4 && s4 == s7){
  135.         return(s1=='X')?1:2;
  136.     }
  137.     else if(s2 == s5 && s5 == s8){
  138.         return(s2=='X')?1:2;
  139.     }
  140.     else if(s3 == s6 && s6 == s9){
  141.         return(s3=='X')?1:2;
  142.     }
  143.     else if(s1 == s5 && s5 == s9){
  144.         return(s1=='X')?1:2;
  145.     }
  146.     else if(s3 == s5 && s5 == s7){
  147.         return(s3=='X')?1:2;
  148.     }
  149.     for(int i = 0;i < 9; i++){
  150.         if(tempGrid[i] != 'X' && tempGrid[i] !='O')
  151.             return 0;
  152.     }
  153.     return -1;
  154. }
  155.  
  156. /*
  157.  * return whether the game is over
  158.  * the game is over when the game is a draw game, or either of the players has won
  159.  *
  160.  * complete the function body code, but do NOT modify the function prototype (including the function name, parameters, and return type)
  161.  */
  162. bool isGameOver(char s1, char s2, char s3, char s4, char s5, char s6, char s7, char s8, char s9)
  163. {
  164. //Function Variables
  165.     char tempGrid[] = {s1, s2, s3, s4, s5, s6, s7, s8 , s9};
  166.  
  167.     if (s1 == s2 && s2 == s3){
  168.         return 1;
  169.     }
  170.     else if(s4 == s5 && s5 == s6){
  171.         return 1;
  172.     }
  173.     else if(s7 == s8 && s8 == s9){
  174.         return 1;
  175.     }
  176.     else if(s1 == s4 && s4 == s7){
  177.         return 1;
  178.     }
  179.     else if(s2 == s5 && s5 == s8){
  180.         return 1;
  181.     }
  182.     else if(s3 == s6 && s6 == s9){
  183.         return 1;
  184.     }
  185.     else if(s1 == s5 && s5 == s9){
  186.         return 1;
  187.     }
  188.     else if(s3 == s5 && s5 == s7){
  189.         return 1;
  190.     }
  191.     for(int i = 0;i < 9; i++){
  192.         if(tempGrid[i] != 'X' && tempGrid[i] !='O')
  193.             return 0;
  194.         cout << "i is " << i << endl;
  195.         if(i == 8)
  196.             return 1;
  197.     }
  198. }
  199.  
  200. /*
  201.  * this is the main function
  202.  *
  203.  * this function is given and already complete
  204.  * do NOT modify anything of it
  205.  */
  206. int main()
  207. {
  208.     char s1 = '1', s2 = '2', s3 = '3', s4 = '4', s5 = '5', s6 = '6', s7 = '7', s8 = '8', s9 = '9'; //variables that represent the 9 spaces in the game board
  209.     int roundNumber = 1; //the round number, starts with 1
  210.     int gameState = 0; //game state, refer to the getGameState function for a description
  211.  
  212.     while(!isGameOver(s1, s2, s3, s4, s5, s6, s7, s8, s9)) //keep going until the game is over
  213.     {
  214.         char currentPlayerSymbol = getCurrentPlayerSymbol(roundNumber); //player symbol is either 'X' or 'O', refer to the getCurrentPlayerSymbol function for a description
  215.  
  216.         //print the round number and game board
  217.         cout << "Round " << roundNumber << ":" << endl << endl;
  218.         printGameBoard(s1, s2, s3, s4, s5, s6, s7, s8, s9);
  219.         //cin.get();
  220.         //get a valid input
  221.         int input = getValidInput(currentPlayerSymbol, s1, s2, s3, s4, s5, s6, s7, s8, s9);
  222.  
  223.         if(input == 0) //exit the program if input is 0
  224.         {
  225.             cout << endl << "Bye!" << endl;
  226.             return 0;
  227.         }
  228.         else //put the current player symbol to the board otherwise
  229.             putSymbol(input, currentPlayerSymbol, s1, s2, s3, s4, s5, s6, s7, s8, s9);
  230.  
  231.         //update the game state
  232.         gameState = getGameState(s1, s2, s3, s4, s5, s6, s7, s8, s9);
  233.  
  234.         //increase the round number
  235.         roundNumber++;
  236.  
  237.         cout << endl << endl;
  238.     }
  239.  
  240.     //game is done at this point, show the result
  241.     cout << "Final state:" << endl << endl;
  242.     printGameBoard(s1, s2, s3, s4, s5, s6, s7, s8, s9);
  243.     if(gameState == -1) cout << "Draw game! Whoops!" << endl;
  244.     else cout << "Congratulations, player " << (gameState==1?'X':'O') << "! You won! Wow!" << endl;
  245.  
  246.     return 0;
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement