Guest User

Untitled

a guest
May 21st, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 17.71 KB | None | 0 0
  1. // example.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. void Start (); // This is the Variable for the start of the game
  9. void Draw (); // This is the variable to the Instructions and the game board
  10. void AI (); // This is the variable to the AI
  11. void OnePlayer (); // This is the 2 player variable
  12.  
  13. char cPosition [9]; // This is the variable for the board positions
  14. bool bPositionCheck [9]; // This is the variable for all the false moves
  15. char cPlayerMove; // This is the variable for the players move
  16. bool bPlayerTurn; // This sets whos turn it is
  17. bool bIsThereWinner; // This is the variable for the end of game scenario
  18. int iPlayAi = '1';
  19. int iPlayers = '2';
  20.  
  21.  
  22. int _tmain(int argc, _TCHAR* argv[])
  23. {
  24.         // This portion of the code starts the game, firstly going to each section to gather the information needed
  25.         // Once it had reached the Draw section, it starts the main loop.
  26.    
  27.     Start ();
  28.  
  29.     int yes_no;
  30.  
  31.     yes_no = 0;
  32.  
  33.     system("CLS");
  34.     cout << "Welcome to Tic Tac Toe!" << endl;
  35.     cout << "" << endl;
  36.     cout << "Instructions!" << endl;
  37.     cout << "" << endl;
  38.     cout << "Only one player can occupie a position at any time" << endl;
  39.     cout << "" << endl;
  40.     cout << "To win the game you must either have three squares going" << endl;
  41.     cout << "Horizontally, Vertically or Diagonally" << endl;
  42.     cout << "" << endl;
  43.     cout << "When all the squares are taken and there is no winner, you have a draw" << endl;
  44.     cout << "" << endl;
  45.     cout << "" << endl;
  46.     cout << "Would you like to play versus the AI or Two Player?" << endl;
  47.     cout << "Choose 1 for versus computer and 2 for Player" << endl;
  48.    
  49. try_again:
  50.     cin >> yes_no;
  51.     if (yes_no == 1)
  52.     {
  53.         OnePlayer();
  54.     }
  55.     else if (yes_no == 2)
  56.     {
  57.     }
  58.     else
  59.     {
  60.         cout << "Thats not a valid input, try again" << endl;
  61.         goto try_again;
  62.     }
  63.        
  64. game_start:
  65.         Draw ();
  66.  
  67. cPlayerMoveCheck:
  68.         cin >> cPlayerMove; // This is the players move section
  69.         if (bPositionCheck [cPlayerMove - 48] == 1)
  70.         {
  71.                 cout << "choose again" << endl; // If they enter an incorrect number they are asked to choose again
  72.                 goto cPlayerMoveCheck;
  73.         };    
  74.         if (bPlayerTurn == 0)
  75.         {      
  76.                 cPosition [cPlayerMove - 48] = 88; // This displays X
  77.                 bPlayerTurn = 1;
  78.         }
  79.         else
  80.         {
  81.                 cPosition [cPlayerMove - 48] = 79; // This Displays O
  82.                 bPlayerTurn = 0;
  83.         };
  84.         bPositionCheck [cPlayerMove - 48] = 1;
  85.  
  86.         // This section checks for win conditions for X
  87.  
  88.         if (cPosition [1] == 88 && cPosition [2] == 88 && cPosition [3] == 88)
  89.         {
  90.         bIsThereWinner = 1; // win conditions for spaces 1,2,3
  91.         }
  92.                 else if (cPosition [4] == 88 && cPosition [5] == 88 && cPosition [6] == 88)
  93.         {
  94.         bIsThereWinner = 1; //win conditions for spaces 4,5,6
  95.         }
  96.                 else if (cPosition [7] == 88 && cPosition [8] == 88 && cPosition [9] == 88)
  97.         {
  98.         bIsThereWinner = 1; //win conditions for spaces 7,8,9
  99.         }
  100.                 else if (cPosition [1] == 88 && cPosition [4] == 88 && cPosition [7] == 88)
  101.         {
  102.         bIsThereWinner = 1; //win conditions for spaces 1,4,7
  103.         }
  104.                 else if (cPosition [2] == 88 && cPosition [5] == 88 && cPosition [8] == 88)
  105.         {
  106.         bIsThereWinner = 1; //win conditions for spaces 2,5,8
  107.         }
  108.                 else if (cPosition [3] == 88 && cPosition [6] == 88 && cPosition [9] == 88)
  109.         {
  110.         bIsThereWinner = 1; //win conditions for spaces 3,6,9
  111.         }
  112.                 else if (cPosition [1] == 88 && cPosition [5] == 88 && cPosition [9] == 88)
  113.         {
  114.         bIsThereWinner = 1; //win conditions for spaces 1,5,7
  115.         }
  116.                 else if (cPosition [3] == 88 && cPosition [5] == 88 && cPosition [7] == 88)
  117.         {
  118.         bIsThereWinner = 1; //win conditions for spaces 3,5,7
  119.         };
  120.  
  121.                 // This section checks for win conditions for O
  122.  
  123.         if (cPosition [1] == 79 && cPosition [2] == 79 && cPosition [3] == 79)
  124.         {
  125.         bIsThereWinner = 1; //win conditions for spaces 1,2,3
  126.         }
  127.                 else if (cPosition [4] == 79 && cPosition [5] == 79 && cPosition [6] == 79)
  128.         {
  129.         bIsThereWinner = 1; //win conditions for spaces 4,5,6
  130.         }
  131.                 else if (cPosition [7] == 79 && cPosition [8] == 79 && cPosition [9] == 79)
  132.         {
  133.         bIsThereWinner = 1; //win conditions for spaces 7,8,9
  134.         }
  135.                 else if (cPosition [1] == 79 && cPosition [4] == 79 && cPosition [7] == 79)
  136.         {
  137.         bIsThereWinner = 1; //win conditions for spaces 1,4,7
  138.         }
  139.                 else if (cPosition [2] == 79 && cPosition [5] == 79 && cPosition [8] == 79)
  140.         {
  141.         bIsThereWinner = 1; //win conditions for spaces 2,5,8
  142.         }
  143.                 else if (cPosition [3] == 79 && cPosition [6] == 79 && cPosition [9] == 79)
  144.         {
  145.         bIsThereWinner = 1; //win conditions for spaces 3,6,9
  146.         }
  147.                 else if (cPosition [1] == 79 && cPosition [5] == 79 && cPosition [9] == 79)
  148.         {
  149.         bIsThereWinner = 1; //win conditions for spaces 1,5,9
  150.         }
  151.                 else if (cPosition [3] == 79 && cPosition [5] == 79 && cPosition [7] == 79)
  152.         {
  153.         bIsThereWinner = 1; //win conditions for spaces 3,5,7
  154.         };
  155.        
  156.         if (bIsThereWinner == 0) // this states if there is no winner go to start
  157.         {
  158.         goto game_start; // this calls to the start
  159.         }
  160.         else if(bIsThereWinner == 1) // If there is a winner print out YOU HAVE WON!
  161.         {
  162.                 cout << "You Have WON!!" << endl;
  163.         }
  164. }
  165.  
  166. void Start ()
  167. {
  168.         // This section defines the position checks, and the ascii code for the places on the board, 1-9
  169.  
  170.         bPlayerTurn = 0;
  171.         cPosition[1] = 49;
  172.         cPosition[2] = 50;
  173.         cPosition[3] = 51;
  174.         cPosition[4] = 52;
  175.         cPosition[5] = 53; // Ascii code for places on the board, 49-57 = 1-9
  176.         cPosition[6] = 54;
  177.         cPosition[7] = 55;
  178.         cPosition[8] = 56;
  179.         cPosition[9] = 57;
  180.  
  181.         bPositionCheck[1] = 0;
  182.         bPositionCheck[2] = 0;
  183.         bPositionCheck[3] = 0;
  184.         bPositionCheck[4] = 0; // Checking for valid positions
  185.         bPositionCheck[5] = 0;
  186.         bPositionCheck[6] = 0;
  187.         bPositionCheck[7] = 0;
  188.         bPositionCheck[8] = 0;
  189.         bPositionCheck[9] = 0;
  190.         bIsThereWinner = 0;
  191. }
  192.  
  193. void Draw ()
  194. {
  195.                         // This section is what is drawn at the beginning, the board and the instructions  
  196.         // Game board
  197.         system("CLS");
  198.         cout << cPosition[1] << " | " << cPosition[2] << " | " << cPosition[3] << endl << endl;
  199.         cout << cPosition[4] << " | " << cPosition[5] << " | " << cPosition[6] << endl << endl;
  200.         cout << cPosition[7] << " | " << cPosition[8] << " | " << cPosition[9] << endl << endl;    
  201.  
  202. }
  203.  
  204.  
  205. void OnePlayer ()
  206.  
  207. {
  208. cPlayerMoveCheck:      
  209.         Draw ();      
  210.         cin >> cPlayerMove;
  211.         if (bPositionCheck [cPlayerMove - 48] == 1) // Another valid move check
  212.         {
  213.                 cout << "choose again" << endl; // cout statement asking for you to enter a valid number
  214.                 goto cPlayerMoveCheck; // Calling back to
  215.         };    
  216.         cPosition [cPlayerMove - 48] = 88; // Referring to player X
  217.         bPositionCheck [cPlayerMove - 48] = 1;
  218.         AI();
  219.  
  220.                 // Win COnditions for X AI
  221.  
  222.         if (cPosition [1] == 88 && cPosition [2] == 88 && cPosition [3] == 88)
  223.         {
  224.         bIsThereWinner = 1; //win conditions for spaces 1,2,3
  225.         }
  226.                 else if (cPosition [4] == 88 && cPosition [5] == 88 && cPosition [6] == 88)
  227.         {
  228.         bIsThereWinner = 1; //win conditions for spaces 4,5,6
  229.         }
  230.                 else if (cPosition [7] == 88 && cPosition [8] == 88 && cPosition [9] == 88)
  231.         {
  232.         bIsThereWinner = 1; //win conditions for spaces 7,8,9
  233.         }
  234.                 else if (cPosition [1] == 88 && cPosition [4] == 88 && cPosition [7] == 88)
  235.         {
  236.         bIsThereWinner = 1; //win conditions for spaces 1,4,7
  237.         }
  238.                 else if (cPosition [2] == 88 && cPosition [5] == 88 && cPosition [8] == 88)
  239.         {
  240.         bIsThereWinner = 1; //win conditions for spaces 2,5,8
  241.         }
  242.                 else if (cPosition [3] == 88 && cPosition [6] == 88 && cPosition [9] == 88)
  243.         {
  244.         bIsThereWinner = 1; //win conditions for spaces 3,6,9
  245.         }
  246.                 else if (cPosition [1] == 88 && cPosition [5] == 88 && cPosition [9] == 88)
  247.         {
  248.         bIsThereWinner = 1; //win conditions for spaces 1,5,9
  249.         }
  250.                 else if (cPosition [3] == 88 && cPosition [5] == 88 && cPosition [7] == 88)
  251.         {
  252.         bIsThereWinner = 1; //win conditions for spaces 3,5,7
  253.         };
  254.  
  255.                 // Win Conditions for O AI
  256.  
  257.         if (cPosition [1] == 79 && cPosition [2] == 79 && cPosition [3] == 79)
  258.         {
  259.         bIsThereWinner = 1; //win conditions for spaces 1,2,3
  260.         }
  261.                 else if (cPosition [4] == 79 && cPosition [5] == 79 && cPosition [6] == 79)
  262.         {
  263.         bIsThereWinner = 1; //win conditions for spaces 4,5,6
  264.         }
  265.                 else if (cPosition [7] == 79 && cPosition [8] == 79 && cPosition [9] == 79)
  266.         {
  267.         bIsThereWinner = 1; //win conditions for spaces 7,8,9
  268.         }
  269.                 else if (cPosition [1] == 79 && cPosition [4] == 79 && cPosition [7] == 79)
  270.         {
  271.         bIsThereWinner = 1; //win conditions for spaces 1,4,7
  272.         }
  273.                 else if (cPosition [2] == 79 && cPosition [5] == 79 && cPosition [8] == 79)
  274.         {
  275.         bIsThereWinner = 1; //win conditions for spaces 2,5,8
  276.         }
  277.                 else if (cPosition [3] == 79 && cPosition [6] == 79 && cPosition [9] == 79)
  278.         {
  279.         bIsThereWinner = 1; //win conditions for spaces 3,6,9
  280.         }
  281.                 else if (cPosition [1] == 79 && cPosition [5] == 79 && cPosition [9] == 79)
  282.         {
  283.         bIsThereWinner = 1; //win conditions for spaces 1,5,9
  284.         }
  285.                 else if (cPosition [3] == 79 && cPosition [5] == 79 && cPosition [7] == 79)
  286.         {
  287.         bIsThereWinner = 1; //win conditions for spaces 3,5,7
  288.         };
  289.         if (bIsThereWinner == 0)
  290.         {
  291.                 goto cPlayerMoveCheck; // If Theres no winner go back to start
  292.         }
  293.         else if(bIsThereWinner == 1)
  294.         {
  295.                 cout << "You Have Won!" << endl; // If there is a winner print out You Have Won!
  296.                 }
  297.  
  298. }
  299.  
  300. void AI ()
  301. {
  302.         bool bMove;
  303.  
  304.         bMove = 0;
  305.  
  306.         while (bMove == 0)
  307.         {
  308.                 if (bPositionCheck[5] == 0) // for the center spot
  309.                 {                                                                
  310.                         cPosition[5] = 79;  // This is position to mark O
  311.  
  312.                         bPositionCheck[5] = 1;  // valid move check for the space
  313.  
  314.                         bMove = 1;       // Valid Move                  
  315.                 }
  316.                                 // trying to block move by checking if squares on either side are taken
  317.                 else if (bPositionCheck[1] == 1 && bPositionCheck[3] == 1 && bPositionCheck[2] == 0)  
  318.                 {
  319.                         cPosition[2] = 79;  // This is position to mark O
  320.  
  321.                         bPositionCheck[2] = 1; // valid move check for the space
  322.  
  323.                         bMove = 1; // Valid Move
  324.                 }
  325.                                 // trying to block move by checking if squares on either side are taken
  326.                 else if (bPositionCheck[4] == 1 && bPositionCheck[6] == 1 && bPositionCheck[5] == 0)
  327.                 {
  328.                         cPosition[5] = 79; // This is position to mark O
  329.  
  330.                         bPositionCheck[5] = 1;  // valid move check for the space
  331.  
  332.                         bMove = 1; // Valid Move
  333.                 }
  334.                                 // trying to block move by checking if squares on either side are taken
  335.                 else if (bPositionCheck[7] == 1 && bPositionCheck[9] == 1 && bPositionCheck[8] == 0)
  336.                 {
  337.                         cPosition[8] = 79; // This is position to mark O
  338.  
  339.                         bPositionCheck[8] = 1; // valid move check for the space
  340.  
  341.                         bMove = 1; // Valid Move
  342.                 }
  343.                                 // trying to block move by checking if squares on either side are taken
  344.                 else if (bPositionCheck[1] == 1 && bPositionCheck[7] == 1 && bPositionCheck[4] == 0)
  345.                 {
  346.                         cPosition[4] = 79; // This is position to mark O
  347.  
  348.                         bPositionCheck[4] = 1;  // valid move check for the space
  349.  
  350.                         bMove = 1; // Valid Move
  351.                 }
  352.                                 // trying to block move by checking if squares on either side are taken
  353.                 else if (bPositionCheck[2] == 1 && bPositionCheck[8] == 1 && bPositionCheck[5] == 0)
  354.                 {
  355.                         cPosition[5] = 79; // This is position to mark O
  356.  
  357.                         bPositionCheck[5] = 1; // valid move check for the space
  358.  
  359.                         bMove = 1; // Valid Move
  360.                 }
  361.                                 // trying to block move by checking if squares on either side are taken
  362.                 else if (bPositionCheck[3] == 1 && bPositionCheck[9] == 1 && bPositionCheck[6] == 0)
  363.                 {
  364.                         cPosition[6] = 79; // This is position to mark O
  365.  
  366.                         bPositionCheck[6] = 1; // valid move check for the space
  367.  
  368.                         bMove = 1; // Valid Move
  369.                 }
  370.                                 // trying to block move by checking if squares on either side are taken
  371.                 else if (bPositionCheck[1] == 1 && bPositionCheck[9] == 1 && bPositionCheck[5] == 0)
  372.                 {
  373.                         cPosition[5] = 79; // This is position to mark O
  374.  
  375.                         bPositionCheck[5] = 1; // valid move check for the space
  376.  
  377.                         bMove = 1; // Valid Move
  378.                 }
  379.                                 // trying to block move by checking if squares on either side are taken
  380.                 else if (bPositionCheck[3] == 1 && bPositionCheck[7] == 1 && bPositionCheck[5] == 0)
  381.                 {
  382.                         cPosition[5] = 79; // This is position to mark O
  383.  
  384.                         bPositionCheck[5] = 1;  // valid move check for the space
  385.  
  386.                         bMove = 1; // Valid Move
  387.                 }
  388.                 else if (bPositionCheck[1] == 0) //Checking for empty space
  389.                 {                                                                
  390.                         cPosition[1] = 79; // This is position to mark O
  391.  
  392.                         bPositionCheck[1] = 1;  // valid move check for the space
  393.  
  394.                         bMove = 1; // Valid Move
  395.                 }
  396.                 else if (bPositionCheck[3] == 0)
  397.                 {                                                                
  398.                         cPosition[3] = 79; // This is position to mark O
  399.  
  400.                         bPositionCheck[3] = 1; // valid move check for the space
  401.  
  402.                         bMove = 1; // Valid Move
  403.                 }
  404.                 else if (bPositionCheck[7] == 0)
  405.                 {                                                                
  406.                         cPosition[7] = 79; // This is position to mark O
  407.  
  408.                         bPositionCheck[7] = 1; // valid move check for the space
  409.  
  410.                         bMove = 1; // Valid Move
  411.                 }
  412.                 else if (bPositionCheck[9] == 0)
  413.                 {                                                                
  414.                         cPosition[9] = 79; // This is position to mark O
  415.  
  416.                         bPositionCheck[9] = 1; // valid move check for the space
  417.  
  418.                         bMove = 1; // Valid Move
  419.                 }
  420.                 else if (bPositionCheck[2] == 0)
  421.                 {                                                                
  422.                         cPosition[2] = 79; // This is position to mark O
  423.  
  424.                         bPositionCheck[2] = 1; // valid move check for the space
  425.  
  426.                         bMove = 1; // Valid Move
  427.                 }
  428.                 else if (bPositionCheck[8] == 0)
  429.                 {                                                                
  430.                         cPosition[8] = 79; // This is position to mark O
  431.  
  432.                         bPositionCheck[8] = 1; // valid move check for the space
  433.  
  434.                         bMove = 1; // Valid Move
  435.                 }
  436.                 else if (bPositionCheck[4] == 0)  
  437.                 {                                                                
  438.                         cPosition[4] = 79; // This is position to mark O
  439.  
  440.                         bPositionCheck[4] = 1; // valid move check for the space
  441.  
  442.                         bMove = 1; // Valid Move
  443.                 }
  444.                 else if (bPositionCheck[6] == 0)
  445.                 {                                                                
  446.                         cPosition[6] = 79; // This is position to mark O
  447.  
  448.                         bPositionCheck[6] = 1; // valid move check for the space
  449.  
  450.                         bMove = 1; // Valid Move
  451.  }
  452.  
  453.  
  454.                 };
  455. }
Add Comment
Please, Sign In to add comment