Advertisement
Cow_Fu

Tic Tac Toe

Oct 20th, 2014
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.67 KB | None | 0 0
  1. /*
  2. Tic Tac Toe, by Cow_Fu
  3.  */
  4.  
  5. //include the libraries
  6. #include <iostream>
  7. #include <string>
  8.  
  9. //use standard namespace
  10.  
  11. using namespace std;
  12.  
  13. //declare functions
  14. void showBoard();
  15. bool moveIsValid(int m);
  16. int whoWon(); //returns 0 if no one has won, 1 if player 1 has won and 2 if player 2 has won
  17.  
  18. //declare global variables
  19. char Board[9];
  20.  
  21.  
  22. void main() {
  23.     //declare local variables
  24.     string Player_1_Name;
  25.     string Player_2_Name;
  26.     int Whose_Turn = 1;   //1 means its player 1's turn, 2 means its player 2's turn
  27.     int Move; //stores where the player wants to move
  28.     int Total_Moves = 0;
  29.  
  30.  
  31.  
  32.     //assign values to the playing board
  33.     Board[0] = '0';
  34.     Board[1] = '1';
  35.     Board[2] = '2';
  36.     Board[3] = '3';
  37.     Board[4] = '4';
  38.     Board[5] = '5';
  39.     Board[6] = '6';
  40.     Board[7] = '7';
  41.     Board[8] = '8';
  42.  
  43.  
  44.     //get player names
  45.     cout << "Player 1: Please enter your name." << endl;
  46.     cin >> Player_1_Name;
  47.  
  48.     cout << "Player 2: Please enter your name." << endl;
  49.     cin >> Player_2_Name;
  50.  
  51.     while(whoWon ( ) == 0 && Total_Moves < 9)
  52.     {
  53.  
  54.  
  55.  
  56.         //do this until the player chooses a valid move
  57.         do {
  58.  
  59.             //show the board
  60.             showBoard();
  61.  
  62.             //tell which player to move
  63.             if (Whose_Turn == 1) {
  64.                 cout << Player_1_Name << ": It's Your turn" << endl;
  65.             }
  66.             else {
  67.                 cout << Player_2_Name << ": It's Your turn" << endl;
  68.             }
  69.  
  70.             //get the move
  71.             cout << "Enter the number of the spot where you'd like to move" << endl;
  72.             cin >> Move;
  73.  
  74.         }
  75.         while (moveIsValid(Move) != true);
  76.  
  77.         //Add 1 to Total_Moves
  78.         Total_Moves++;
  79.  
  80.         cout << endl;
  81.         cout << Total_Moves << endl;
  82.         cout << endl;
  83.  
  84.  
  85.  
  86.         //change whose turn it is
  87.         switch (Whose_Turn) {
  88.             case (1): {
  89.                 Board[Move] = 'x';
  90.                 Whose_Turn = 2;
  91.                 break;
  92.             }
  93.  
  94.             case (2): {
  95.                 Board[Move] = 'o';
  96.                 Whose_Turn = 1;
  97.             }
  98.  
  99.         }
  100.     }
  101.     //show the board
  102.     showBoard();
  103.  
  104.     if(whoWon() == 1)
  105.     {
  106.         cout << Player_1_Name << " has won the game!" << endl;
  107.         system("PAUSE");
  108.     }
  109.     else if(whoWon() == 2)
  110.     {
  111.         cout << Player_2_Name << " has won the game!" << endl;
  112.         system("PAUSE");
  113.     }
  114.     else
  115.     {
  116.         cout << "It's a tie game!" << endl;
  117.         system("PAUSE");
  118.     }
  119. }
  120.  
  121. void showBoard ( )
  122. {
  123.     cout << endl;
  124.     cout << Board[0] << " | " <<  Board[1] << " | " << Board[2] << endl;
  125.     cout << "--+---+--" << endl;
  126.     cout << Board[3] << " | " <<  Board[4] << " | " << Board[5] << endl;
  127.     cout << "--+---+--" << endl;
  128.     cout << Board[6] << " | " <<  Board[7] << " | " << Board[8] << endl;
  129.     cout << endl;
  130. }
  131.  
  132. bool moveIsValid (int m)
  133. {
  134.     if(Board[m] != 'x' && Board[m] != 'o')
  135.     {
  136.         return true;
  137.     }
  138.     else
  139.     {
  140.         return false;
  141.     }
  142. }
  143.  
  144. int whoWon( )
  145. {
  146.     if(Board[0] == Board[1] && Board[1] == Board[2])
  147.     {
  148.         if(Board[0] == 'x')
  149.         {
  150.             return 1;
  151.         }
  152.         else
  153.         {
  154.             return 2;
  155.         }
  156.  
  157.     }
  158.  
  159.     if(Board[3] == Board[4] && Board[4] == Board[5])
  160.     {
  161.         if(Board[3] == 'x')
  162.         {
  163.             return 1;
  164.         }
  165.         else
  166.         {
  167.             return 2;
  168.         }
  169.  
  170.     }
  171.     if(Board[6] == Board[7] && Board[7] == Board[8])
  172.     {
  173.         if(Board[6] == 'x')
  174.         {
  175.             return 1;
  176.         }
  177.         else
  178.         {
  179.             return 2;
  180.         }
  181.  
  182.     }
  183.     if(Board[0] == Board[3] && Board[3] == Board[6])
  184.     {
  185.         if(Board[0] == 'x')
  186.         {
  187.             return 1;
  188.         }
  189.         else
  190.         {
  191.             return 2;
  192.         }
  193.  
  194.     }
  195.     if(Board[1] == Board[4] && Board[4] == Board[7])
  196.     {
  197.         if(Board[1] == 'x')
  198.         {
  199.             return 1;
  200.         }
  201.         else
  202.         {
  203.             return 2;
  204.         }
  205.  
  206.     }
  207.     if(Board[2] == Board[5] && Board[5] == Board[8])
  208.     {
  209.         if(Board[2] == 'x')
  210.         {
  211.             return 1;
  212.         }
  213.         else
  214.         {
  215.             return 2;
  216.         }
  217.  
  218.     }
  219.     if(Board[2] == Board[4] && Board[4] == Board[6])
  220.     {
  221.         if(Board[2] == 'x')
  222.         {
  223.             return 1;
  224.         }
  225.         else
  226.         {
  227.             return 2;
  228.         }
  229.  
  230.     }
  231.     if(Board[0] == Board[4] && Board[4] == Board[8])
  232.     {
  233.         if(Board[0] == 'x')
  234.         {
  235.             return 1;
  236.         }
  237.         else
  238.         {
  239.             return 2;
  240.         }
  241.  
  242.     }
  243.     return 0;
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement