steamengines

Lab2

Sep 16th, 2015
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.13 KB | None | 0 0
  1. // Christian Ballard
  2. // COSC 1337 - 001
  3. // 9/10/15
  4. // TIC - TAC - TOE
  5.  
  6. #include <iostream>
  7. #include <ctime>
  8. using namespace std;
  9.  
  10. // Prototypes
  11. void displayBoard();
  12. void draw();
  13. void gameInstructions();
  14. int getHumanMove();
  15. int getComputerMove();
  16. bool winner();
  17. bool checkTie();
  18.  
  19. const int BEST_MOVES[] = { 4, 0, 2, 6, 8, 1, 3, 5, 7 };
  20. enum gamePieces{ X_PIECE, O_PIECE, EMPTY_PIECE };
  21. enum gameState{ X_WINS, O_WINS, NOT_OVER, TIE };
  22. gamePieces board[9] = { EMPTY_PIECE, EMPTY_PIECE, EMPTY_PIECE, EMPTY_PIECE, EMPTY_PIECE, EMPTY_PIECE, EMPTY_PIECE, EMPTY_PIECE, EMPTY_PIECE };
  23. char gamePieceChr[3] = { 'X', 'O', ' ' };
  24. gamePieces humanPiece;
  25. gamePieces computerPiece;
  26. int const HUMAN = 1;
  27. int const COMPUTER = 0;
  28.  
  29. int main()
  30. {
  31.     char whoGoes1st;
  32.     int playerTurn;
  33.  
  34.     gameInstructions();
  35.     displayBoard();
  36.  
  37.     do
  38.     {
  39.         cout << "Would you like to go first?(y/n): ";
  40.         cin >> whoGoes1st;
  41.         whoGoes1st = toupper(whoGoes1st);
  42.         if (whoGoes1st != 'Y' && whoGoes1st != 'N')
  43.             cout << "Invalid Choice try again." << endl << endl;
  44.     } while (whoGoes1st != 'N' && whoGoes1st != 'Y');
  45.  
  46.     switch (whoGoes1st)
  47.     {
  48.  
  49.         case 'Y':
  50.             playerTurn = HUMAN;
  51.             humanPiece = X_PIECE;
  52.             computerPiece =  O_PIECE;
  53.             break;
  54.         case 'N':
  55.             playerTurn = COMPUTER;
  56.             humanPiece = O_PIECE;
  57.             computerPiece =  X_PIECE;
  58.             break;
  59.     }
  60.    
  61.     draw();
  62.  
  63.     do
  64.     {
  65.         if(playerTurn == HUMAN)
  66.           {
  67.              board[getHumanMove()] = humanPiece;
  68.              draw();
  69.              if (winner() == true)
  70.                 {
  71.                     cout << "Human wins!" << endl;
  72.                 }
  73.              if(checkTie() == true)
  74.                 {
  75.                     cout << "It's Tie" << endl;
  76.                 }
  77.                 playerTurn = COMPUTER;
  78.                 }
  79.          else if(playerTurn == COMPUTER)
  80.                 {
  81.                     board[getComputerMove()] = computerPiece;
  82.                     draw();
  83.                  if(winner() == true)
  84.                     {
  85.                          cout << "Computer wins, better luck next time !" << endl;
  86.                     }
  87.                  if(checkTie() == true)
  88.                    {
  89.                       cout << "It's a tie !" << endl;
  90.                    }
  91.                  playerTurn = HUMAN;
  92.                  }
  93.  
  94.     }while(winner() == false && checkTie() == false);
  95.  
  96.  
  97.  
  98.     system("pause");
  99.     return 0;
  100. }
  101.  
  102. void gameInstructions()
  103. {
  104.     cout << "Tic - Tac - Toe" << endl << endl;
  105.     cout << "The player who succeeds in placing three respective marks\n in a horizontal, vertical, or diagonal row wins the game." << endl << endl;
  106. }
  107.  
  108. void displayBoard()
  109. {
  110.  
  111.  
  112.  
  113.     cout << "       " << 0 << "  |  " << 1 << "  |  " << 2 << endl;
  114.     cout << "     -----+-----+-----" << endl;
  115.     cout << "       " << 3 << "  |  " << 4 << "  |  " << 5 << endl;
  116.     cout << "     -----+-----+-----" << endl;
  117.     cout << "       " << 6 << "  |  " << 7 << "  |  " << 8 << endl;
  118.  
  119.  
  120. }
  121.  
  122. int getHumanMove()
  123. {
  124.     int move;
  125.     int hum = static_cast<int>(X_PIECE);
  126.  
  127.     do
  128.     {
  129.         cout << "What space would you like to take? (0-8): ";
  130.         cin >> move;
  131.  
  132.         if (move < 0 || move > 8)
  133.         {
  134.             cout << "Invalid space, please try again." << endl;
  135.         }
  136.         if (board[move] == X_PIECE || board[move] == O_PIECE)
  137.         {
  138.             move = 72;
  139.             cout << "Space is already taken please try again" << endl;
  140.         }
  141.  
  142.     } while (move < 0 || move > 8 || move == 72);
  143.  
  144.     return move;
  145. }
  146.  
  147. int getComputerMove()
  148. {
  149.     int move = 0;
  150.  
  151.     cout << "Computers turn." << endl;
  152.  
  153.  
  154.     //check if computer can win
  155.     for (int i = 0; i <= 8; i++)
  156.     {
  157.         if (board[i] == EMPTY_PIECE)
  158.         {
  159.             board[i] = computerPiece;
  160.             if (winner() == true)
  161.             {
  162.                 return i;
  163.             }
  164.             else
  165.             {
  166.                 board[i] = EMPTY_PIECE;
  167.             }
  168.         }
  169.     }
  170.  
  171.     // check if human can win on next move
  172.     for (int i = 0; i <= 8; i++)
  173.     {
  174.         if (board[i] == EMPTY_PIECE)
  175.         {
  176.             board[i] = humanPiece;
  177.             if (winner() == true)
  178.             {
  179.                 board[i] = computerPiece;
  180.                 return i;
  181.             }
  182.             else
  183.             {
  184.                 board[i] = EMPTY_PIECE;
  185.             }
  186.         }
  187.     }
  188.  
  189.     //computer can't on next move; human can't win
  190.     for (int i = 0; i <= 8; i++)
  191.     {
  192.         move = BEST_MOVES[i];
  193.         if (board[move] == EMPTY_PIECE)
  194.             break;
  195.     }
  196.  
  197.  
  198.  
  199.  
  200.     return move;
  201. }
  202.  
  203. void draw()
  204. {
  205.  
  206.     cout << endl;
  207.     cout << "       " << gamePieceChr[board[0]] << "  |  " << gamePieceChr[board[1]] << "  |  " << gamePieceChr[board[2]] << endl;
  208.     cout << "     -----+-----+-----" << endl;
  209.     cout << "       " << gamePieceChr[board[3]] << "  |  " << gamePieceChr[board[4]] << "  |  " << gamePieceChr[board[5]] << endl;
  210.     cout << "     -----+-----+-----" << endl;
  211.     cout << "       " << gamePieceChr[board[6]] << "  |  " << gamePieceChr[board[7]] << "  |  " << gamePieceChr[board[8]] << endl;
  212.  
  213.  
  214. }
  215.  
  216. bool winner()
  217. {
  218.     bool winneasdfr = false;
  219.     if (board[0] == X_PIECE && board[1] == X_PIECE && board[2] == X_PIECE)
  220.         winneasdfr = true;
  221.     if (board[3] == X_PIECE && board[4] == X_PIECE && board[5] == X_PIECE)
  222.         winneasdfr = true;
  223.     if (board[6] == X_PIECE && board[7] == X_PIECE && board[8] == X_PIECE)
  224.         winneasdfr = true;
  225.     if (board[0] == X_PIECE && board[3] == X_PIECE && board[6] == X_PIECE)
  226.         winneasdfr = true;
  227.     if (board[1] == X_PIECE && board[4] == X_PIECE && board[7] == X_PIECE)
  228.         winneasdfr = true;
  229.     if (board[2] == X_PIECE && board[5] == X_PIECE && board[8] == X_PIECE)
  230.         winneasdfr = true;
  231.     if (board[0] == X_PIECE && board[4] == X_PIECE && board[8] == X_PIECE)
  232.         winneasdfr = true;
  233.     if (board[2] == X_PIECE && board[4] == X_PIECE && board[6] == X_PIECE)
  234.         winneasdfr = true;
  235.  
  236.     //*************
  237.  
  238.     if (board[0] == O_PIECE && board[1] == O_PIECE && board[2] == O_PIECE)
  239.         winneasdfr = true;
  240.     if (board[3] == O_PIECE && board[4] == O_PIECE && board[5] == O_PIECE)
  241.         winneasdfr = true;
  242.     if (board[6] == O_PIECE && board[7] == O_PIECE && board[8] == O_PIECE)
  243.         winneasdfr = true;
  244.     if (board[0] == O_PIECE && board[3] == O_PIECE && board[6] == O_PIECE)
  245.         winneasdfr = true;
  246.     if (board[1] == O_PIECE && board[4] == O_PIECE && board[7] == O_PIECE)
  247.         winneasdfr = true;
  248.     if (board[2] == O_PIECE && board[5] == O_PIECE && board[8] == O_PIECE)
  249.         winneasdfr = true;
  250.     if (board[0] == O_PIECE && board[4] == O_PIECE && board[8] == O_PIECE)
  251.         winneasdfr = true;
  252.     if (board[2] == O_PIECE && board[4] == O_PIECE && board[6] == O_PIECE)
  253.         winneasdfr = true;
  254.  
  255.  
  256.     return winneasdfr;
  257. }
  258.  
  259. bool checkTie()
  260. {
  261.     bool tie = false;
  262.    
  263.     if (board[0, 1, 2, 3, 4, 5, 6, 7, 8] == X_PIECE || board[0, 1, 2, 3, 4, 5, 6, 7, 8] == O_PIECE)
  264.         tie = true;
  265.  
  266.     return tie;
  267. }
Advertisement
Add Comment
Please, Sign In to add comment