1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
#include <iostream>
#include <cstdlib>
void gameStructure(); //creates the board
char gameBoard[9] = {' ',' ',' ',' ',' ',' ',' ',' ',' ',}; //creates the output values
int gamePlay(); //how the game is played
char PlayerOne = 'X'; //Player 1's playing piece
char PlayerTwo = 'O'; //Player 2's playing piece
char Draw; //Determines the draw
char checkWin(); //function that checks for a winning answer
int turns; //helps users take turns
int player1; //input value for Player 1
int player2; //input value for Player 2
int main()
{
std::cout << "\nPlayer 1, your game piece is: " << PlayerOne << std::endl; //Prints Player 1's piece
std::cout << "Player 2, your game piece is: " << PlayerTwo << std::endl; //Prints Player 2's piece
gameStructure(); //creates the initial board
gamePlay();
while (1){
gamePlay();
if(checkWin() == PlayerOne){
std::cout << "Player 1 is the winner!" << std::endl; //Determines Player 1 victory
break;
}
if(checkWin() == PlayerTwo){
std::cout << "Player 2 is the winner!" << std::endl; //Determines Player 2 victory
break;
}
if(checkWin() == Draw){
std::cout << "The game is a draw!" << std::endl; //Determines Draw
break;
}
}
return 0;
}
//Prints the entire board
void gameStructure(){
std::cout << "\n Stavros Gogos'" << std::endl;
std::cout << " Tic-Tac-Toe v1.0" << std::endl;
std::cout << "\n 1 2 3" << std::endl;
std::cout << "\n1 " << gameBoard[0] << " |" << " " << gameBoard[1] << " |" << " " << gameBoard[2] << std::endl;
std::cout << " ____|____|___" << std::endl;
std::cout << "4 " << gameBoard[3] << " |" << " " << gameBoard[4] << " |" << " " << gameBoard[5] << std::endl;
std::cout << " ____|____|___" << std::endl;
std::cout << "7 " << gameBoard[6] << " |" << " " << gameBoard[7] << " |" << " " << gameBoard[8] << std::endl;
std::cout << " | | " << std::endl;
std::cout << std::endl;
}
//Plays the game
int gamePlay(){
int turns = 0;
int checkWin = 0;
int player1;
int player2;
while(turns < 10, turns++, checkWin == 0){
if(turns == 1 || turns == 3 || turns == 5 || turns == 7 || turns == 9){
std::cout << "Player 1, please enter your choice of box (1-9)." << std::endl;
std::cin >> player1; //input location for PlayerOne
if(gameBoard[0] == ' ' && player1 == 1){ gameBoard[0] = PlayerOne;}
else if(gameBoard[1] == ' ' && player1 == 2){gameBoard[1] = PlayerOne;}
else if(gameBoard[2] == ' ' && player1 == 3){gameBoard[2] = PlayerOne;}
else if(gameBoard[3] == ' ' && player1 == 4){gameBoard[3] = PlayerOne;}
else if(gameBoard[4] == ' ' && player1 == 5){gameBoard[4] = PlayerOne;}
else if(gameBoard[5] == ' ' && player1 == 6){gameBoard[5] = PlayerOne;}
else if(gameBoard[6] == ' ' && player1 == 7){gameBoard[6] = PlayerOne;}
else if(gameBoard[7] == ' ' && player1 == 8){gameBoard[7] = PlayerOne;}
else if(gameBoard[8] == ' ' && player1 == 9){gameBoard[8] = PlayerOne;}
gameStructure(); //updates the board with inputed value
}
if(turns == 2 || turns == 4 || turns == 6 || turns == 8){
std::cout << "Player 2, please enter your choice of box (1-9)." << std::endl;
std::cin >> player2; //input location for PlayerTwo
if(gameBoard[0] == ' ' && player2 == 1){ gameBoard[0] = PlayerTwo;}
else if(gameBoard[1] == ' ' && player2 == 2){gameBoard[1] = PlayerTwo;}
else if(gameBoard[2] == ' ' && player2 == 3){gameBoard[2] = PlayerTwo;}
else if(gameBoard[3] == ' ' && player2 == 4){gameBoard[3] = PlayerTwo;}
else if(gameBoard[4] == ' ' && player2 == 5){gameBoard[4] = PlayerTwo;}
else if(gameBoard[5] == ' ' && player2 == 6){gameBoard[5] = PlayerTwo;}
else if(gameBoard[6] == ' ' && player2 == 7){gameBoard[6] = PlayerTwo;}
else if(gameBoard[7] == ' ' && player2 == 8){gameBoard[7] = PlayerTwo;}
else if(gameBoard[8] == ' ' && player2 == 9){gameBoard[8] = PlayerTwo;}
gameStructure(); //updates the board with inputed value
}
}
return 0;
}
char checkWin(){
if(gameBoard[0] == PlayerOne && gameBoard[1] == PlayerOne && gameBoard[2] == PlayerOne)
{return PlayerOne;}
if(gameBoard[0] == PlayerOne && gameBoard[4] == PlayerOne && gameBoard[8] == PlayerOne)
{return PlayerOne;}
if(gameBoard[0] == PlayerOne && gameBoard[3] == PlayerOne && gameBoard[6] == PlayerOne)
{return PlayerOne;}
if(gameBoard[1] == PlayerOne && gameBoard[4] == PlayerOne && gameBoard[7] == PlayerOne)
{return PlayerOne;}
if(gameBoard[2] == PlayerOne && gameBoard[5] == PlayerOne && gameBoard[8] == PlayerOne)
{return PlayerOne;}
if(gameBoard[3] == PlayerOne && gameBoard[4] == PlayerOne && gameBoard[5] == PlayerOne)
{return PlayerOne;}
if(gameBoard[6] == PlayerOne && gameBoard[7] == PlayerOne && gameBoard[8] == PlayerOne)
{return PlayerOne;}
if(gameBoard[6] == PlayerOne && gameBoard[4] == PlayerOne && gameBoard[2] == PlayerOne)
{return PlayerOne;}
if(gameBoard[0] == PlayerTwo && gameBoard[1] == PlayerTwo && gameBoard[2] == PlayerTwo)
{return PlayerTwo;}
if(gameBoard[0] == PlayerTwo && gameBoard[4] == PlayerTwo && gameBoard[8] == PlayerTwo)
{return PlayerTwo;}
if(gameBoard[0] == PlayerTwo && gameBoard[3] == PlayerTwo && gameBoard[6] == PlayerTwo)
{return PlayerTwo;}
if(gameBoard[1] == PlayerTwo && gameBoard[4] == PlayerTwo && gameBoard[7] == PlayerTwo)
{return PlayerTwo;}
if(gameBoard[2] == PlayerTwo && gameBoard[5] == PlayerTwo && gameBoard[8] == PlayerTwo)
{return PlayerTwo;}
if(gameBoard[3] == PlayerTwo && gameBoard[4] == PlayerTwo && gameBoard[5] == PlayerTwo)
{return PlayerTwo;}
if(gameBoard[6] == PlayerTwo && gameBoard[7] == PlayerTwo && gameBoard[8] == PlayerTwo)
{return PlayerTwo;}
if(gameBoard[6] == PlayerTwo && gameBoard[4] == PlayerTwo && gameBoard[2] == PlayerTwo)
{return PlayerTwo;}
if(turns == 10)
{return Draw;}
return 0;
}
| |