Guest User

Untitled

a guest
Jul 15th, 2012
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. string player1 = "Player 1";
  7. string player2 = "Player 2";
  8.  
  9. int player = 1;
  10. void gameboard();
  11. char space[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
  12. bool winCheck();
  13. void takeTurns(bool, int);
  14. void move(int);
  15. bool legalMove(int);
  16.  
  17.  
  18. int main()
  19. {
  20.     cout << "Welcome to Devyn's Super Awesome Tic Tac Toe Game." << endl
  21.          << "Please enter name for Player 1." << endl;
  22.     cin  >> player1;
  23.     cout << "Please enter name for Player 2." << endl;
  24.     cin  >> player2;
  25.     cout << endl;
  26.  
  27.     gameboard();
  28.  
  29.     while (!winCheck())
  30.     {
  31.         cout << "Player = " << player << endl;
  32.         takeTurns(winCheck(), player);
  33.         cout << "End of loop" << endl;
  34.         cout << player << endl;
  35.     }
  36.  
  37.  
  38.     char end;
  39.     cout << "Type any key, and press enter." << endl;
  40.     cin  >> end;
  41.  
  42.     return 0;
  43. }
  44.  
  45. void gameboard()
  46. {
  47.     cout << " " << space[0] << " | " << space[1] << " | " << space[2] << endl
  48.          << " " << "--------" << endl
  49.          << " " << space[3] << " | " << space[4] << " | " << space[5] << endl
  50.          << " " << "--------" << endl
  51.          << " " << space[6] << " | " << space[7] << " | " << space[8] << endl << endl;
  52. }
  53.  
  54. bool winCheck()
  55. {
  56.     if (space[0] == space[1] && space[2] == space[0])
  57.         return true;
  58.     if (space[3] == space[4] && space[5] == space[3])
  59.         return true;
  60.     if (space[6] == space[7] && space[8] == space[6])
  61.         return true;
  62.     if (space[0] == space[3] && space[6] == space[0])
  63.         return true;
  64.     if (space[1] == space[4] && space[7] == space[1])
  65.         return true;
  66.     if (space[2] == space[5] && space[8] == space[2])
  67.         return true;
  68.     if (space[0] == space[4] && space[8] == space[0])
  69.         return true;
  70.     if (space[6] == space[4] && space[2] == space[6])
  71.         return true;
  72.     else
  73.         return false;
  74. }
  75.  
  76. void takeTurns(bool winCheck, int player)
  77. {
  78.     if (!winCheck)
  79.     {
  80.         if (player == 1)
  81.             player = 0;
  82.         else
  83.             player = 1;
  84.     }
  85.  
  86.     move(player);
  87. }
  88.  
  89. void move(int player)
  90. {
  91.     int box;
  92.  
  93.     if (player == 1)
  94.         cout << "Enter move " << player1 << endl;
  95.     else
  96.         cout << "Enter move " << player2 << endl;
  97.  
  98.     cin >> box;
  99.  
  100.     if (legalMove(box))
  101.     {
  102.         if (player == 0)
  103.             space[box-1] = 'X';
  104.         else
  105.             space[box-1] = 'O';
  106.     }
  107.  
  108.     gameboard();
  109. }
  110.  
  111. bool legalMove(int box)
  112. {
  113.     if (space[box-1] == 'X' || space[box-1] == 'O')
  114.     {
  115.         cout << "You cannot place move on an already taken space." << endl;
  116.         return false;
  117.     }
  118.  
  119.     if (box > 9)
  120.     {
  121.         cout << "You must enter a number on the board." << endl;
  122.         return false;
  123.     }
  124.  
  125.     return true;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment