Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // example.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- void Start (); // This is the Variable for the start of the game
- void Draw (); // This is the variable to the Instructions and the game board
- void AI (); // This is the variable to the AI
- void OnePlayer (); // This is the 2 player variable
- char cPosition [9]; // This is the variable for the board positions
- bool bPositionCheck [9]; // This is the variable for all the false moves
- char cPlayerMove; // This is the variable for the players move
- bool bPlayerTurn; // This sets whos turn it is
- bool bIsThereWinner; // This is the variable for the end of game scenario
- int iPlayAi = '1';
- int iPlayers = '2';
- int _tmain(int argc, _TCHAR* argv[])
- {
- // This portion of the code starts the game, firstly going to each section to gather the information needed
- // Once it had reached the Draw section, it starts the main loop.
- Start ();
- int yes_no;
- yes_no = 0;
- system("CLS");
- cout << "Welcome to Tic Tac Toe!" << endl;
- cout << "" << endl;
- cout << "Instructions!" << endl;
- cout << "" << endl;
- cout << "Only one player can occupie a position at any time" << endl;
- cout << "" << endl;
- cout << "To win the game you must either have three squares going" << endl;
- cout << "Horizontally, Vertically or Diagonally" << endl;
- cout << "" << endl;
- cout << "When all the squares are taken and there is no winner, you have a draw" << endl;
- cout << "" << endl;
- cout << "" << endl;
- cout << "Would you like to play versus the AI or Two Player?" << endl;
- cout << "Choose 1 for versus computer and 2 for Player" << endl;
- try_again:
- cin >> yes_no;
- if (yes_no == 1)
- {
- OnePlayer();
- }
- else if (yes_no == 2)
- {
- }
- else
- {
- cout << "Thats not a valid input, try again" << endl;
- goto try_again;
- }
- game_start:
- Draw ();
- cPlayerMoveCheck:
- cin >> cPlayerMove; // This is the players move section
- if (bPositionCheck [cPlayerMove - 48] == 1)
- {
- cout << "choose again" << endl; // If they enter an incorrect number they are asked to choose again
- goto cPlayerMoveCheck;
- };
- if (bPlayerTurn == 0)
- {
- cPosition [cPlayerMove - 48] = 88; // This displays X
- bPlayerTurn = 1;
- }
- else
- {
- cPosition [cPlayerMove - 48] = 79; // This Displays O
- bPlayerTurn = 0;
- };
- bPositionCheck [cPlayerMove - 48] = 1;
- // This section checks for win conditions for X
- if (cPosition [1] == 88 && cPosition [2] == 88 && cPosition [3] == 88)
- {
- bIsThereWinner = 1; // win conditions for spaces 1,2,3
- }
- else if (cPosition [4] == 88 && cPosition [5] == 88 && cPosition [6] == 88)
- {
- bIsThereWinner = 1; //win conditions for spaces 4,5,6
- }
- else if (cPosition [7] == 88 && cPosition [8] == 88 && cPosition [9] == 88)
- {
- bIsThereWinner = 1; //win conditions for spaces 7,8,9
- }
- else if (cPosition [1] == 88 && cPosition [4] == 88 && cPosition [7] == 88)
- {
- bIsThereWinner = 1; //win conditions for spaces 1,4,7
- }
- else if (cPosition [2] == 88 && cPosition [5] == 88 && cPosition [8] == 88)
- {
- bIsThereWinner = 1; //win conditions for spaces 2,5,8
- }
- else if (cPosition [3] == 88 && cPosition [6] == 88 && cPosition [9] == 88)
- {
- bIsThereWinner = 1; //win conditions for spaces 3,6,9
- }
- else if (cPosition [1] == 88 && cPosition [5] == 88 && cPosition [9] == 88)
- {
- bIsThereWinner = 1; //win conditions for spaces 1,5,7
- }
- else if (cPosition [3] == 88 && cPosition [5] == 88 && cPosition [7] == 88)
- {
- bIsThereWinner = 1; //win conditions for spaces 3,5,7
- };
- // This section checks for win conditions for O
- if (cPosition [1] == 79 && cPosition [2] == 79 && cPosition [3] == 79)
- {
- bIsThereWinner = 1; //win conditions for spaces 1,2,3
- }
- else if (cPosition [4] == 79 && cPosition [5] == 79 && cPosition [6] == 79)
- {
- bIsThereWinner = 1; //win conditions for spaces 4,5,6
- }
- else if (cPosition [7] == 79 && cPosition [8] == 79 && cPosition [9] == 79)
- {
- bIsThereWinner = 1; //win conditions for spaces 7,8,9
- }
- else if (cPosition [1] == 79 && cPosition [4] == 79 && cPosition [7] == 79)
- {
- bIsThereWinner = 1; //win conditions for spaces 1,4,7
- }
- else if (cPosition [2] == 79 && cPosition [5] == 79 && cPosition [8] == 79)
- {
- bIsThereWinner = 1; //win conditions for spaces 2,5,8
- }
- else if (cPosition [3] == 79 && cPosition [6] == 79 && cPosition [9] == 79)
- {
- bIsThereWinner = 1; //win conditions for spaces 3,6,9
- }
- else if (cPosition [1] == 79 && cPosition [5] == 79 && cPosition [9] == 79)
- {
- bIsThereWinner = 1; //win conditions for spaces 1,5,9
- }
- else if (cPosition [3] == 79 && cPosition [5] == 79 && cPosition [7] == 79)
- {
- bIsThereWinner = 1; //win conditions for spaces 3,5,7
- };
- if (bIsThereWinner == 0) // this states if there is no winner go to start
- {
- goto game_start; // this calls to the start
- }
- else if(bIsThereWinner == 1) // If there is a winner print out YOU HAVE WON!
- {
- cout << "You Have WON!!" << endl;
- }
- }
- void Start ()
- {
- // This section defines the position checks, and the ascii code for the places on the board, 1-9
- bPlayerTurn = 0;
- cPosition[1] = 49;
- cPosition[2] = 50;
- cPosition[3] = 51;
- cPosition[4] = 52;
- cPosition[5] = 53; // Ascii code for places on the board, 49-57 = 1-9
- cPosition[6] = 54;
- cPosition[7] = 55;
- cPosition[8] = 56;
- cPosition[9] = 57;
- bPositionCheck[1] = 0;
- bPositionCheck[2] = 0;
- bPositionCheck[3] = 0;
- bPositionCheck[4] = 0; // Checking for valid positions
- bPositionCheck[5] = 0;
- bPositionCheck[6] = 0;
- bPositionCheck[7] = 0;
- bPositionCheck[8] = 0;
- bPositionCheck[9] = 0;
- bIsThereWinner = 0;
- }
- void Draw ()
- {
- // This section is what is drawn at the beginning, the board and the instructions
- // Game board
- system("CLS");
- cout << cPosition[1] << " | " << cPosition[2] << " | " << cPosition[3] << endl << endl;
- cout << cPosition[4] << " | " << cPosition[5] << " | " << cPosition[6] << endl << endl;
- cout << cPosition[7] << " | " << cPosition[8] << " | " << cPosition[9] << endl << endl;
- }
- void OnePlayer ()
- {
- cPlayerMoveCheck:
- Draw ();
- cin >> cPlayerMove;
- if (bPositionCheck [cPlayerMove - 48] == 1) // Another valid move check
- {
- cout << "choose again" << endl; // cout statement asking for you to enter a valid number
- goto cPlayerMoveCheck; // Calling back to
- };
- cPosition [cPlayerMove - 48] = 88; // Referring to player X
- bPositionCheck [cPlayerMove - 48] = 1;
- AI();
- // Win COnditions for X AI
- if (cPosition [1] == 88 && cPosition [2] == 88 && cPosition [3] == 88)
- {
- bIsThereWinner = 1; //win conditions for spaces 1,2,3
- }
- else if (cPosition [4] == 88 && cPosition [5] == 88 && cPosition [6] == 88)
- {
- bIsThereWinner = 1; //win conditions for spaces 4,5,6
- }
- else if (cPosition [7] == 88 && cPosition [8] == 88 && cPosition [9] == 88)
- {
- bIsThereWinner = 1; //win conditions for spaces 7,8,9
- }
- else if (cPosition [1] == 88 && cPosition [4] == 88 && cPosition [7] == 88)
- {
- bIsThereWinner = 1; //win conditions for spaces 1,4,7
- }
- else if (cPosition [2] == 88 && cPosition [5] == 88 && cPosition [8] == 88)
- {
- bIsThereWinner = 1; //win conditions for spaces 2,5,8
- }
- else if (cPosition [3] == 88 && cPosition [6] == 88 && cPosition [9] == 88)
- {
- bIsThereWinner = 1; //win conditions for spaces 3,6,9
- }
- else if (cPosition [1] == 88 && cPosition [5] == 88 && cPosition [9] == 88)
- {
- bIsThereWinner = 1; //win conditions for spaces 1,5,9
- }
- else if (cPosition [3] == 88 && cPosition [5] == 88 && cPosition [7] == 88)
- {
- bIsThereWinner = 1; //win conditions for spaces 3,5,7
- };
- // Win Conditions for O AI
- if (cPosition [1] == 79 && cPosition [2] == 79 && cPosition [3] == 79)
- {
- bIsThereWinner = 1; //win conditions for spaces 1,2,3
- }
- else if (cPosition [4] == 79 && cPosition [5] == 79 && cPosition [6] == 79)
- {
- bIsThereWinner = 1; //win conditions for spaces 4,5,6
- }
- else if (cPosition [7] == 79 && cPosition [8] == 79 && cPosition [9] == 79)
- {
- bIsThereWinner = 1; //win conditions for spaces 7,8,9
- }
- else if (cPosition [1] == 79 && cPosition [4] == 79 && cPosition [7] == 79)
- {
- bIsThereWinner = 1; //win conditions for spaces 1,4,7
- }
- else if (cPosition [2] == 79 && cPosition [5] == 79 && cPosition [8] == 79)
- {
- bIsThereWinner = 1; //win conditions for spaces 2,5,8
- }
- else if (cPosition [3] == 79 && cPosition [6] == 79 && cPosition [9] == 79)
- {
- bIsThereWinner = 1; //win conditions for spaces 3,6,9
- }
- else if (cPosition [1] == 79 && cPosition [5] == 79 && cPosition [9] == 79)
- {
- bIsThereWinner = 1; //win conditions for spaces 1,5,9
- }
- else if (cPosition [3] == 79 && cPosition [5] == 79 && cPosition [7] == 79)
- {
- bIsThereWinner = 1; //win conditions for spaces 3,5,7
- };
- if (bIsThereWinner == 0)
- {
- goto cPlayerMoveCheck; // If Theres no winner go back to start
- }
- else if(bIsThereWinner == 1)
- {
- cout << "You Have Won!" << endl; // If there is a winner print out You Have Won!
- }
- }
- void AI ()
- {
- bool bMove;
- bMove = 0;
- while (bMove == 0)
- {
- if (bPositionCheck[5] == 0) // for the center spot
- {
- cPosition[5] = 79; // This is position to mark O
- bPositionCheck[5] = 1; // valid move check for the space
- bMove = 1; // Valid Move
- }
- // trying to block move by checking if squares on either side are taken
- else if (bPositionCheck[1] == 1 && bPositionCheck[3] == 1 && bPositionCheck[2] == 0)
- {
- cPosition[2] = 79; // This is position to mark O
- bPositionCheck[2] = 1; // valid move check for the space
- bMove = 1; // Valid Move
- }
- // trying to block move by checking if squares on either side are taken
- else if (bPositionCheck[4] == 1 && bPositionCheck[6] == 1 && bPositionCheck[5] == 0)
- {
- cPosition[5] = 79; // This is position to mark O
- bPositionCheck[5] = 1; // valid move check for the space
- bMove = 1; // Valid Move
- }
- // trying to block move by checking if squares on either side are taken
- else if (bPositionCheck[7] == 1 && bPositionCheck[9] == 1 && bPositionCheck[8] == 0)
- {
- cPosition[8] = 79; // This is position to mark O
- bPositionCheck[8] = 1; // valid move check for the space
- bMove = 1; // Valid Move
- }
- // trying to block move by checking if squares on either side are taken
- else if (bPositionCheck[1] == 1 && bPositionCheck[7] == 1 && bPositionCheck[4] == 0)
- {
- cPosition[4] = 79; // This is position to mark O
- bPositionCheck[4] = 1; // valid move check for the space
- bMove = 1; // Valid Move
- }
- // trying to block move by checking if squares on either side are taken
- else if (bPositionCheck[2] == 1 && bPositionCheck[8] == 1 && bPositionCheck[5] == 0)
- {
- cPosition[5] = 79; // This is position to mark O
- bPositionCheck[5] = 1; // valid move check for the space
- bMove = 1; // Valid Move
- }
- // trying to block move by checking if squares on either side are taken
- else if (bPositionCheck[3] == 1 && bPositionCheck[9] == 1 && bPositionCheck[6] == 0)
- {
- cPosition[6] = 79; // This is position to mark O
- bPositionCheck[6] = 1; // valid move check for the space
- bMove = 1; // Valid Move
- }
- // trying to block move by checking if squares on either side are taken
- else if (bPositionCheck[1] == 1 && bPositionCheck[9] == 1 && bPositionCheck[5] == 0)
- {
- cPosition[5] = 79; // This is position to mark O
- bPositionCheck[5] = 1; // valid move check for the space
- bMove = 1; // Valid Move
- }
- // trying to block move by checking if squares on either side are taken
- else if (bPositionCheck[3] == 1 && bPositionCheck[7] == 1 && bPositionCheck[5] == 0)
- {
- cPosition[5] = 79; // This is position to mark O
- bPositionCheck[5] = 1; // valid move check for the space
- bMove = 1; // Valid Move
- }
- else if (bPositionCheck[1] == 0) //Checking for empty space
- {
- cPosition[1] = 79; // This is position to mark O
- bPositionCheck[1] = 1; // valid move check for the space
- bMove = 1; // Valid Move
- }
- else if (bPositionCheck[3] == 0)
- {
- cPosition[3] = 79; // This is position to mark O
- bPositionCheck[3] = 1; // valid move check for the space
- bMove = 1; // Valid Move
- }
- else if (bPositionCheck[7] == 0)
- {
- cPosition[7] = 79; // This is position to mark O
- bPositionCheck[7] = 1; // valid move check for the space
- bMove = 1; // Valid Move
- }
- else if (bPositionCheck[9] == 0)
- {
- cPosition[9] = 79; // This is position to mark O
- bPositionCheck[9] = 1; // valid move check for the space
- bMove = 1; // Valid Move
- }
- else if (bPositionCheck[2] == 0)
- {
- cPosition[2] = 79; // This is position to mark O
- bPositionCheck[2] = 1; // valid move check for the space
- bMove = 1; // Valid Move
- }
- else if (bPositionCheck[8] == 0)
- {
- cPosition[8] = 79; // This is position to mark O
- bPositionCheck[8] = 1; // valid move check for the space
- bMove = 1; // Valid Move
- }
- else if (bPositionCheck[4] == 0)
- {
- cPosition[4] = 79; // This is position to mark O
- bPositionCheck[4] = 1; // valid move check for the space
- bMove = 1; // Valid Move
- }
- else if (bPositionCheck[6] == 0)
- {
- cPosition[6] = 79; // This is position to mark O
- bPositionCheck[6] = 1; // valid move check for the space
- bMove = 1; // Valid Move
- }
- };
- }
Add Comment
Please, Sign In to add comment