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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
# include <iostream>
using namespace std;
char board[3][3]={'1', '2', '3', '4', '5', '6', '7', '8', '9'};
int main()
{
cout<<"Welcome to Tic-Tac-Toe!\n";
int PlayerChoice;
int player(1);
bool GameFinish(true);
cout<<"Would you like to play Tic-Tac-Toe?"<<" If yes, press 1. If no, press 2.\n";
cin>>PlayerChoice;
if (PlayerChoice == 1){
do {
int player(1);
bool GameFinish(true);
cout<<"| "<<board[0][0]<<" | "<<board[0][1]<<" | "<<board[0][2]<<" |\n";
cout<<"|___|___|___|\n";
cout<<"| "<<board[1][0]<<" | "<<board[1][1]<<" | "<<board[1][2]<<" |\n";
cout<<"|___|___|___|\n";
cout<<"| "<<board[2][0]<<" | "<<board[2][1]<<" | "<<board[2][2]<<" |\n";
cout<<"|___|___|___|\n";
bool validturn;
do{
char playerMove;
char playerLetter = 'X';
cout<<"It's Player "<<player<<"'s turn. Press any number on the board.\n";
cin>>playerMove;
validturn= true;
//Create an if-else to determine if the move is valid
if(playerMove == '1' && board[0][0] == '1'){
board[0][0]= playerLetter;
}
else if (playerMove == '2' && board[0][1] == '2'){
board[0][1]= playerLetter;
}
else if (playerMove == '3' && board[0][2] == '3'){
board[0][2]= playerLetter;
}
else if (playerMove == '4' && board[1][0] == '4'){
board[1][0]= playerLetter;
}
else if(playerMove == '5' && board[1][1] == '5'){
board[1][1]= playerLetter;
}
else if(playerMove == '6' && board[1][2] == '6'){
board[1][2]= playerLetter;
}
else if(playerMove == '7' && board[2][0] == '7'){
board[2][0]= playerLetter;
}
else if(playerMove == '8' && board[2][1] == '8'){
board[2][1]= playerLetter;
}
else if(playerMove == '9' && board[2][2] == '9'){
board[2][2]= playerLetter;
}
else{
cout<<"Invalid Move, Try Again."<<endl;
validturn=false;
}
if(playerLetter == 'X'){
playerLetter == 'O';
}
else
playerLetter == 'X';
}while(!validturn);
GameFinish= false;
bool WinGame= true;
//player x
//If player x wins with rows
if(board[0][0]== 'X' && board[0][1]== 'X' && board[0][2]== 'X')
GameFinish=true;
if(board[1][0]== 'X' && board[1][1]== 'X' && board[1][2]== 'X')
GameFinish=true;
if(board[2][0]== 'X' && board[2][1]== 'X' && board[2][2]== 'X')
GameFinish=true;
//if player x wins with columns
if(board[0][0]== 'X' && board[1][0]== 'X' && board[2][0]== 'X')
GameFinish=true;
if(board[0][1]== 'X' && board[1][1]== 'X' && board[2][1]== 'X')
GameFinish=true;
if(board[0][2]== 'X' && board[1][2]== 'X' && board[2][2]== 'X')
GameFinish=true;
//if player x wins diagonally
if(board[0][0]== 'X' && board[1][1]== 'X' && board[2][2]== 'X')
GameFinish=true;
if(board[0][2]== 'X' && board[1][1]== 'X' && board[2][0]== 'X')
GameFinish=true;
//player o
//If player o wins with rows
if(board[0][0]== 'O' && board[0][1]== 'O' && board[0][2]== 'O')
GameFinish=true;
if(board[1][0]== 'O' && board[1][1]== 'O' && board[1][2]== 'O')
GameFinish=true;
if(board[2][0]== 'O' && board[2][1]== 'O' && board[2][2]== 'O')
GameFinish=true;
//If player o wins with columns
if(board[0][0]== 'O' && board[1][0]== 'O' && board[2][0]== 'O')
GameFinish=true;
if(board[0][1]== 'O' && board[1][1]== 'O' && board[2][1]== 'O')
GameFinish=true;
if(board[0][2]== 'O' && board[1][2]== 'O' && board[2][2]== 'O')
GameFinish=true;
//if player o wins diagonally
if(board[0][0]== 'O' && board[1][1]== 'O' && board[2][2]== 'O')
GameFinish=true;
if(board[0][2]== 'O' && board[1][1]== 'O' && board[2][0]== 'O')
GameFinish=true;
//In the event of tie
if(board[0][0] != '1' && board[0][1] != '2' && board[0][2] != '3' && board[1][0] != '4' && board[1][1] != '5' && board[1][2] != '6' && board[2][0] != '7' &&
board[2][1] != '8' && board[2][2] !='9' && !GameFinish){
GameFinish= true;
WinGame=false;
cout<<"It's a DRAW!"<<endl;
}
else {
if(player == 1)
player=2;
else
player=1;
}
} while(!GameFinish);
}
else(PlayerChoice == 2);{
cout<<"Ok, See you next time!"<<endl;
}
return 0;
}
|