Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.69 KB | None | 0 0
  1. #include "myLibraryBMR.h"
  2.  
  3. using namespace std;
  4.  
  5. enum gamePiece { X_PIECE='X', O_PIECE='O', EMPTY_PIECE = ' '};
  6. enum gameState { X_WINS=1, O_WINS=2, TIE=3, NOT_OVER=4 };
  7.  
  8.  
  9. void displayRules();
  10. void displayGame(gamePiece[], const int);
  11. enum gamePiece whoFirst();
  12. int getHumanMove();
  13. int getComputerMove(gamePiece[], const int);
  14. enum gameState whoWins(gamePiece[], const int);
  15.  
  16.  
  17. int main()
  18. {
  19.     gamePiece gameBoard[9];
  20.     gamePiece computer, human;
  21.     int choice;
  22.     const int gameBoardSize = 9;
  23.     int keepGoing;
  24.  
  25.     while(1)//allows game to be played until exit
  26.     {
  27.         displayRules();
  28.  
  29.         keepGoing = 4;
  30.  
  31.         for (int i = 0; i < gameBoardSize; i++)
  32.         {
  33.             gameBoard[i] = EMPTY_PIECE;
  34.         } //intializes gameBoard array to black space
  35.  
  36.         human = whoFirst();
  37.  
  38.         if (human == X_PIECE)
  39.         {
  40.             computer = O_PIECE; //if human is X_PIECE then computer assigned O
  41.         }
  42.         else
  43.         {
  44.             computer = X_PIECE; //if human not X_PIECE assigns computer X.
  45.         }                       //human already assigned O at this point
  46.  
  47.         do
  48.         {
  49.             if (human == X_PIECE && keepGoing >TIE )//if human is first it uses this sequence
  50.             {
  51.                 displayGame(gameBoard, gameBoardSize);
  52.                 choice = getHumanMove();
  53.                 gameBoard[choice] = human;
  54.                 keepGoing=whoWins(gameBoard, gameBoardSize);
  55.                 //human cycle over
  56.                 if (keepGoing > TIE)
  57.                 {
  58.                     displayGame(gameBoard, gameBoardSize);
  59.                     choice = getComputerMove(gameBoard, gameBoardSize);
  60.                     gameBoard[choice] = computer;
  61.                     keepGoing = whoWins(gameBoard, gameBoardSize);
  62.                     //computer cycle over
  63.                 }
  64.             }
  65.             else if (computer == X_PIECE && keepGoing > TIE)//if computer is first it uses this sequence
  66.             {
  67.                 displayGame(gameBoard, gameBoardSize);
  68.                 choice = getComputerMove(gameBoard, gameBoardSize);
  69.                 gameBoard[choice] = computer;
  70.                 keepGoing = whoWins(gameBoard, gameBoardSize);
  71.                 //computer cycle over
  72.                 if (keepGoing > TIE) {
  73.                     displayGame(gameBoard, gameBoardSize);
  74.                     choice = getHumanMove();
  75.                     gameBoard[choice] = human;
  76.                     keepGoing = whoWins(gameBoard, gameBoardSize);
  77.                     //human cycle over
  78.                 }
  79.             }
  80.            
  81.         } while (keepGoing > TIE); //if greater than tie should always keep going
  82.  
  83.         switch (keepGoing)
  84.         {
  85.         case 1:
  86.             cout << "\n\nX WINS!\n \n\nX WINS!\n \n\nX WINS!\n\n";
  87.             displayGame(gameBoard, gameBoardSize);
  88.             break;
  89.  
  90.         case 2:
  91.             cout << "\n\nO WINS!\n \n\nO WINS!\n \n\nO WINS!\n\n";
  92.             displayGame(gameBoard, gameBoardSize);
  93.             break;
  94.         case 3:
  95.             cout << "\n\nIt's a Tie, better luck next time.\n";
  96.             displayGame(gameBoard, gameBoardSize);
  97.             break;
  98.         }
  99.  
  100.         cout << "\nPress any key to play again.\n";
  101.         system("pause");
  102.        
  103.     }
  104.     system("pause");
  105.     return 0;
  106. }
  107.  
  108. void displayRules()
  109. {
  110.     cout << "\nWelcome to the game of Tic-Tac-Toe!";
  111.     cout << "\n\nMake your move known by entering a number, 0-8.";
  112.     cout << "The number \ncorresponds to the desired board position as shown below: \n\n";
  113.  
  114.     for (int i = 0; i < 9; i++)
  115.     {
  116.         cout << i;
  117.         if (i == 0 || i == 1 || i == 3 || i == 4 || i == 6 || i == 7)
  118.         {
  119.             cout << " | ";
  120.         }
  121.         if (i == 2 || i == 5)
  122.         {
  123.             cout << "\n----------\n";
  124.         }
  125.     }
  126.     cout << "\n\n";
  127. }
  128.  
  129. void displayGame(gamePiece game[], const int size)  //passes this function gameBoard array and size of the array
  130. {
  131.     for (int i = 0; i < 9; i++)
  132.     {
  133.         cout << (char)game[i]; //cast enums to X, O, Blank space
  134.         if (i == 0 || i == 1 || i == 3 || i == 4 || i == 6 || i == 7)
  135.         {
  136.             cout << " | ";
  137.         }
  138.         if (i == 2 || i == 5)
  139.         {
  140.             cout << "\n----------\n";
  141.         }
  142.     }
  143.     cout << "\n\n";
  144. }
  145.  
  146. gamePiece whoFirst()
  147. {
  148.     cout << "\n\Would you like to go first? Enter Y for yes or N for no. ";
  149.     char move = readChar();  //checks for valid char, function from fund.1
  150.  
  151.     if (move == 'Y')
  152.     {
  153.         return X_PIECE;  //based on y or n they will be assigned to X or O
  154.     }
  155.     else
  156.     {
  157.         return O_PIECE;
  158.     }
  159. }
  160.  
  161. int getHumanMove()
  162. {
  163.     cout << "\nWhere will you move? (0-8): ";
  164.     return readInt("your choice", 0, 8);
  165. }
  166.  
  167. int getComputerMove(gamePiece game[], const int size)
  168. {
  169.     int choice;
  170.     const int BEST_MOVES[] = { 4, 0, 2, 6, 8, 1, 3, 5, 7 };
  171.     for (int i = 0; i < 9; i++)
  172.     {
  173.         choice = BEST_MOVES[i];
  174.         if(game[choice]==EMPTY_PIECE) //checks if square chosen is empty returns choice if true
  175.         {
  176.             cout << "You cannot out smart me. I choose square " << choice << endl << endl;
  177.             return choice;
  178.         }
  179.     }
  180. }
  181.  
  182. gameState whoWins(gamePiece game[], const int size)
  183. {   //checks all winning combos
  184.     if (game[0] == X_PIECE && game[1] == X_PIECE && game[2] == X_PIECE ||
  185.         game[3] == X_PIECE && game[4] == X_PIECE && game[5] == X_PIECE ||
  186.         game[6] == X_PIECE && game[7] == X_PIECE && game[8] == X_PIECE ||
  187.         game[0] == X_PIECE && game[3] == X_PIECE && game[6] == X_PIECE ||
  188.         game[1] == X_PIECE && game[4] == X_PIECE && game[7] == X_PIECE ||
  189.         game[2] == X_PIECE && game[5] == X_PIECE && game[8] == X_PIECE ||
  190.         game[0] == X_PIECE && game[4] == X_PIECE && game[8] == X_PIECE ||
  191.         game[2] == X_PIECE && game[4] == X_PIECE && game[6] == X_PIECE)
  192.     {
  193.         return X_WINS;
  194.     }
  195.  
  196.     else if (game[0] == O_PIECE && game[1] == O_PIECE && game[2] == O_PIECE ||
  197.         game[3] == O_PIECE && game[4] == O_PIECE && game[5] == O_PIECE ||
  198.         game[6] == O_PIECE && game[7] == O_PIECE && game[8] == O_PIECE ||
  199.         game[0] == O_PIECE && game[3] == O_PIECE && game[6] == O_PIECE ||
  200.         game[1] == O_PIECE && game[4] == O_PIECE && game[7] == O_PIECE ||
  201.         game[2] == O_PIECE && game[5] == O_PIECE && game[8] == O_PIECE ||
  202.         game[0] == O_PIECE && game[4] == O_PIECE && game[8] == O_PIECE ||
  203.         game[2] == O_PIECE && game[4] == O_PIECE && game[6] == O_PIECE)
  204.     {
  205.         return O_WINS;
  206.     }
  207.  
  208.  
  209.     for (int i = 0; i < 9; i++)
  210.     {
  211.          
  212.         if (game[i]==EMPTY_PIECE)
  213.         {
  214.             return NOT_OVER;
  215.         }
  216.     }
  217.     //if all prior fail it must be a tie
  218.     return TIE;
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement