Advertisement
Guest User

TIC TAC TOE

a guest
May 24th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.86 KB | None | 0 0
  1. //TIC TAC TOEE
  2. #include <iostream>
  3. #include <math.h>
  4. #include <stdio.h>
  5. using namespace std;
  6. //includes
  7. void printTable (string[3][3]); //Initialize the function to  print the table
  8. int
  9. main ()
  10. {
  11.   int v = 0, i = 0, x = 0, w = 0, column, row;  //Initialize the variables needed
  12.   string table[3][3] = { "   ", "   ", "   ", "   ", "   ", "   ", "   ", "   ", "   " };   //initialize the array 2D and each element
  13.   printTable (table);       //call the function
  14.   for (int turn = 0; turn < 9; turn++)
  15.     {               //for statement that repeats 9 times
  16.       cout << "TURN: ";     //shows the user who's turn it is
  17.       if (turn % 2 == 0)
  18.     {           //checks who's turn it is
  19.       cout << "Player 1" << endl;   //displays that it is player one's turn
  20.     }
  21.       else
  22.     {
  23.       cout << "Player 2" << endl;   //displays that it is player two's turn
  24.     }
  25.       while (x == 0)
  26.     {           //while statement checks for valid information
  27.  
  28.       while (i == 0)
  29.         {           //cheks for valid selection of row
  30.           cout << "Enter the row of your selection" << endl;    //displays message
  31.           cin >> row;   //gets nuber of row from player
  32.           if (0 < row and row < 4)
  33.         {       //if selected number meets the requirements
  34.           row--;    //subtract one to match the array position
  35.           i = 1;    //breaks while statement
  36.         }
  37.           else
  38.         {       //if selected number doesnt meet the requirements
  39.           cout << "Invalid!" << endl;   //display text and repet while statement
  40.         }
  41.         }
  42.  
  43.       while (v == 0)
  44.         {           //checks for valid selection of column
  45.           cout << "Enter the column of your selection" << endl; //displays message
  46.           cin >> column;    //gets nuber of column from player
  47.           if (0 < column and column < 4)
  48.         {       //if selected number meets the requirements
  49.           column--; //subtract one to match the array position
  50.           v = 1;    //breaks while statement
  51.         }
  52.           else
  53.         {       //if selected number doesn't meet the requirements
  54.           cout << "Invalid!" << endl;   //display text and repeat while statement
  55.         }
  56.         }
  57.  
  58.       if (table[row][column] == "   ")
  59.         {           //Checks that the position selected is valid
  60.           if (turn % 2 == 0)
  61.         {       //checks for who's turn it is
  62.           table[row][column] = " X ";   //player one's turn saves an X in the position to store information
  63.           x = 1;    //breaks validation while statement
  64.         }
  65.           else
  66.         {
  67.           table[row][column] = " O ";   //player two turn saves an O in the position
  68.           x = 1;    //breaks validation while statement
  69.         }
  70.         }
  71.        
  72.       else
  73.         {           //if the position is not avalible
  74.           cout << "INVALID. This box has already been selected." << endl;   //display text
  75.           cout << "Please enter a valid location NOW." << endl; //display text
  76.           printTable (table);   //function that displays the table
  77.           v = 0;        //resets variables for the  while statement
  78.           i = 0;        //resets variables for the  while statement
  79.           x = 0;        //resets variables for the  while statement
  80.         }
  81.     }
  82.  
  83.  
  84.       printTable (table);   //function that displays the table
  85.  
  86.       if (table[row][column] == table[row][0] and table[row][column] ==
  87.       table[row][1] and table[row][column] == table[row][2])
  88.     {
  89.       //cheks for winner in the row that was just selected
  90.       cout << "WiNnEr!" << endl;    //displays text                  
  91.       if (turn % 2 == 0)
  92.         {           //cheks for whos turn is it
  93.           cout << "Congratulations player 1, you are decent!" << endl;  //display text for player one
  94.           w = 1;        //declares that there is a winner
  95.           break;        //breaks all the statements and ends the code
  96.         }
  97.       else
  98.         {
  99.           cout << "Congratulations player 2, you are amazing!" << endl; //displays text for player two
  100.           w = 1;        //declares that there is a winner
  101.           break;        //breaks all the statements and ends the code
  102.         }
  103.     }
  104.       if (table[row][column] == table[0][column] and table[row][column] ==
  105.       table[1][column] and table[row][column] == table[2][column])
  106.     {
  107.       //cheks for winner in the column that was just selected
  108.       cout << "Weiner!" << endl;    //displays text                  
  109.       if (turn % 2 == 0)
  110.         {           //cheks for whos turn is it
  111.           cout << "Congratulations player 1, you are the ultimate warrior!" << endl;    //display text for player one
  112.           w = 1;        //declares that there is a winner
  113.           break;        //breaks all the statements and ends the code
  114.         }
  115.       else
  116.         {
  117.           cout << "Congratulations player 2, you are a sasquatch!" << endl; //displays text for player two
  118.           w = 1;        //declares that there is a winner
  119.           break;        //breaks all the statements and ends the code
  120.         }
  121.     }
  122.  
  123.       if (turn > 3)
  124.     {
  125.       if (table[0][0] == table[1][1] and table[1][1] == table[2][2])
  126.         {           //cheks for winner in an / patern
  127.           cout << "WINNER!" << endl;    //displays text                  
  128.           if (turn % 2 == 0)
  129.         {       //cheks for whos turn is it
  130.           cout << "Congratulations player 1, you are an actual baller!" << endl;    //display text for player one
  131.           w = 1;    //declares that there is a winner
  132.           break;    //breaks all the statements and ends the code
  133.         }
  134.           else
  135.         {
  136.           cout << "Congratulations player 2, you are the GOAT!" << endl;    //displays text for player two
  137.           w = 1;    //declares that there is a winner
  138.           break;    //breaks all the statements and ends the code
  139.         }
  140.         }
  141.       if (table[0][2] == table[1][1] and table[1][1] == table[2][0])
  142.         {           //cheks for winner in an \ patern
  143.           cout << "WIN, You are the ultimate champion!" << endl;    //displays text                  
  144.           if (turn % 2 == 0)
  145.         {       //cheks for whos turn is it
  146.           cout << "Congratulations player 1, you are a fantastic player!" << endl;  //display text for player one
  147.           w = 1;    //declares that there is a winner
  148.           break;    //breaks all the statements and ends the code
  149.         }
  150.           else
  151.         {
  152.           cout << "Congratulations player 2, you are mediocre!" << endl;    //displays text for player two
  153.           w = 1;    //declares that there is a winner
  154.           break;    //breaks all the statements and ends the code
  155.         }
  156.         }
  157.     }
  158.  
  159.       v = 0;            //resets the variables for the while statement
  160.       i = 0;            //resets the variables for the while statement
  161.       x = 0;            //resets the variables for the while statement
  162.     }
  163.  
  164.   if (w == 0)
  165.     {               //if there is no winner after all 9 turns
  166.       cout << "Nobody wins, you all are losers." << endl;   //display text
  167.     }
  168.  
  169.   return 0;
  170. }
  171.  
  172. void
  173. printTable (string table[][3])
  174. {               //void function that displays the table
  175.   cout << table[0][0] << "|" << table[0][1] << "|" << table[0][2] << endl;  //displays the fist row of the array
  176.   cout << "---|---|---" << endl;    //displays lines that separate the rows
  177.   cout << table[1][0] << "|" << table[1][1] << "|" << table[1][2] << endl;  //displays the second row of the table
  178.   cout << "---|---|---" << endl;    //displays lines that separate the rows
  179.   cout << table[2][0] << "|" << table[2][1] << "|" << table[2][2] << endl;  //displays the third row of the table
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement