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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
#include <iostream>
using namespace std;
void displayBoard(char b[][3], int size);
void grabInput(char player);
void nextPlayer (char activePlayer, char player, char player1);
void updateBoard(char b[3][3], int /*size*/, char player, char currentMove);
void checkWin();
void checkDraw();
bool isPlaying = true; //Not necessary but I'll keep it for future changes
bool hasWon = false;
bool hasDrawn = false;
char player = 'X';
char player2 = 'O';
char activePlayer = player;
char currentMove;
char b[3][3] =
{
{'1','2','3'},
{'4','5','6'},
{'7','8','9'}
};
int main()
{
displayBoard(b,3);
while (isPlaying && !hasWon && !hasDrawn)
{
grabInput(activePlayer);
updateBoard(b,3,activePlayer, currentMove);
checkWin();
checkDraw();
nextPlayer (activePlayer, player, player2);
}
if (hasWon)
cout << "Congrats!";
else
displayBoard(b,3);
}
void displayBoard(char b[3][3], int size)
{
cout<<"::Tic Tac Toe::\n\n";
for (int x = 0; x < size; x++)
{
for (int y = 0; y < size; y++)
{
cout<<b[x][y];
cout<<" ";
}
cout<<endl;
}
cout<<endl;
}
void grabInput(char player)
{
cout<<"Enter your move player "<<player<<": ";
cin>>currentMove;
}
void nextPlayer (char activePlayer, char player, char player2) {
if (activePlayer == player)
activePlayer = player2;
else activePlayer = player;
}
void updateBoard(char b[3][3], int /*size*/, char player, char currentMove)
{
if (currentMove > '0' && currentMove <= '9')
b[0][currentMove - '1'] = player;
}
void checkWin()
{
if (b[0][0] == 'X' && b[0][1] == 'X' && b[0][2] == 'X')
{
cout<<"\nPlayer 1 Wins!\n\n";
hasWon = true;
}
if (b[1][0] == 'X' && b[1][1] == 'X' && b[1][2] == 'X')
{
cout<<"\nPlayer 1 Wins!\n\n";
hasWon = true;
}
if (b[2][0] == 'X' && b[2][1] == 'X' && b[2][2] == 'X')
{
cout<<"\nPlayer 1 Wins!\n\n";
hasWon = true;
}
if (b[0][0] == 'X' && b[1][0] == 'X' && b[2][0] == 'X')
{
cout<<"\nPlayer 1 Wins!\n\n";
hasWon = true;
}
if (b[0][1] == 'X' && b[1][1] == 'X' && b[2][1] == 'X')
{
cout<<"\nPlayer 1 Wins!\n\n";
hasWon = true;
}
if (b[0][2] == 'X' && b[1][2] == 'X' && b[2][2] == 'X')
{
cout<<"\nPlayer 1 Wins!\n\n";
hasWon = true;
}
if (b[0][0] == 'X' && b[1][1] == 'X' && b[2][2] == 'X')
{
cout<<"\nPlayer 1 Wins!\n\n";
hasWon = true;
}
if (b[0][2] == 'X' && b[1][1] == 'X' && b[2][0] == 'X')
{
cout<<"\nPlayer 1 Wins!\n\n";
hasWon = true;
}
if (b[0][0] == 'O' && b[0][1] == 'O' && b[0][2] == 'O')
{
cout<<"\nPlayer Wins!\n\n";
hasWon = true;
}
if (b[1][0] == 'O' && b[1][1] == 'O' && b[1][2] == 'O')
{
cout<<"\nPlayer Wins!\n\n";
hasWon = true;
}
if (b[2][0] == 'O' && b[2][1] == 'O' && b[2][2] == 'O')
{
cout<<"\nPlayer Wins!\n\n";
hasWon = true;
}
if (b[0][0] == 'O' && b[1][0] == 'O' && b[2][0] == 'O')
{
cout<<"\nPlayer Wins!\n\n";
hasWon = true;
}
if (b[0][1] == 'O' && b[1][1] == 'O' && b[2][1] == 'O')
{
cout<<"\nPlayer Wins!\n\n";
hasWon = true;
}
if (b[0][2] == 'O' && b[1][2] == 'O' && b[2][2] == 'O')
{
cout<<"\nPlayer Wins!\n\n";
hasWon = true;
}
if (b[0][0] == 'O' && b[1][1] == 'O' && b[2][2] == 'O')
{
cout<<"\nPlayer Wins!\n\n";
hasWon = true;
}
if (b[0][2] == 'O' && b[1][1] == 'O' && b[2][0] == 'O')
{
cout<<"\nPlayer Wins!\n\n";
hasWon = true;
}
}
void checkDraw()
{
if (b[0][0] != '1' && b[0][1] != '2' && b[0][2] != '3' && b[1][0] != '4' && b[1][1] != '5' && b[1][2] != '6' && b[2][0] != '7' && b[2][1] != '8' && b[2][2] != '9' && !hasWon)
hasDrawn = true;
if (hasDrawn)
{
cout<<"\nThe game is a draw!\n\n";
}
}
|