C TIC TOC game using a multidimensional array It should allo
C++ TIC TOC game using a multidimensional array. It should allow two humans to play together.
Your game must prevent illegal moves and have an option to erase the board to allow for multiple games.
I already craeated the program but im having problem testing the WINNER diagonally and option to ERASE the board and start fresh new game.
Heres my code:
#include <iostream>
using namespace std;
void eraseGameBoard();
void displayBoard();
void playerTurn();
bool gameover();
char turn;
bool draw = false;
char board[3][3] = { { \'1\',\'2\',\'3\'},{ \'4\',\'5\',\'6\'},{ \'7\',\'8\',\'9\'} };
int main()
{
cout << \"Tic Toc Toe Game\ \";
cout << \"Player 1 [X] ------ Player 2 [O]\ \";
turn = \'X\';
while (!gameover())
{
displayBoard();
playerTurn();
gameover();
}
if (turn == \'O\' && !draw)
{
displayBoard();
cout << endl << endl << \"player 1 [X] Wins! Game Over!\ \";
}
else if (turn == \'X\' && !draw)
{
displayBoard();
cout << endl << endl << \"player 2 [O] Wins! Game Over!\ \";
}
else
{
displayBoard();
cout << endl << endl << \"It\'s a draw! Game Over!\ \";
}
return 0;
}
void eraseGameBoard()
{
cout << endl;
cout << \" | | \" << endl;
cout << \" \" << board[0][0] << \" | \" << board[0][1] << \" | \" << board[0][2] << endl;
cout << \"____|____|____\" << endl;
cout << \" | | \" << endl;
cout << \" \" << board[1][0] << \" | \" << board[1][1] << \" | \" << board[1][2] << endl;
cout << \"____|____|____\" << endl;
cout << \" | | \" << endl;
cout << \" \" << board[2][0] << \" | \" << board[2][1] << \" | \" << board[2][2] << endl;
cout << \" | | \" << endl;
}
void displayBoard()
{
cout << endl;
cout << \" | | \" << endl;
cout << \" \" << board[0][0] << \" | \" << board[0][1] << \" | \" << board[0][2] << endl;
cout << \"____|____|____\" << endl;
cout << \" | | \" << endl;
cout << \" \" << board[1][0] << \" | \" << board[1][1] << \" | \" << board[1][2] << endl;
cout << \"____|____|____\" << endl;
cout << \" | | \" << endl;
cout << \" \" << board[2][0] << \" | \" << board[2][1] << \" | \" << board[2][2] << endl;
cout << \" | | \" << endl;
}
void playerTurn()
{
int choice;
int row = 0, column = 0;
if (turn == \'X\')
{
cout << \"Player 1 turn [X]: \";
}
else if (turn == \'O\')
{
cout << \"Player 2 turn [O]: \";
}
cin >> choice;
switch (choice)
{
case 1: row = 0; column = 0; break;
case 2: row = 0; column = 1; break;
case 3: row = 0; column = 2; break;
case 4: row = 1; column = 0; break;
case 5: row = 1; column = 1; break;
case 6: row = 1; column = 2; break;
case 7: row = 2; column = 0; break;
case 8: row = 2; column = 1; break;
case 9: row = 2; column = 2; break;
default:
cout << \"Illegal move! Try again\ \";
playerTurn();
}
if (turn == \'X\' && board[row][column] != \'X\' && board[row][column] != \'O\')
{
board[row][column] = \'X\';
turn = \'O\';
}
else if(turn == \'O\' && board[row][column] != \'X\' && board[row][column] != \'O\')
{
board[row][column] = \'O\';
turn = \'X\';
}
else
{
cout << \"The cell you chose is used! Choose another one\ \";
playerTurn();
}
}
bool gameover()
{
for (int i = 0; i < 3; i++)
{
if ((board[i][0] == board[i][1] && board[i][1] == board[i][2]) || (board[0][i] == board[1][i] && board[1][i] == board[2][i]))
{
return true;
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (board[i][j] != \'X\' && board[i][j] != \'O\')
{
return false;
}
}
}
draw = true;
return true;
}
Solution
#include <iostream>
using namespace std;
void eraseGameBoard();
void displayBoard();
void playerTurn();
bool gameover();
char turn;
bool draw = false;
char board[3][3] = { { \'1\',\'2\',\'3\'},{ \'4\',\'5\',\'6\'},{ \'7\',\'8\',\'9\'} };
int main()
{
cout << \"Tic Toc Toe Game\ \";
cout << \"Player 1 [X] ------ Player 2 [O]\ \";
turn = \'X\';
while (!gameover())
{
displayBoard();
playerTurn();
gameover();
}
if (turn == \'O\' && !draw)
{
displayBoard();
cout << endl << endl << \"player 1 [X] Wins! Game Over!\ \";
}
else if (turn == \'X\' && !draw)
{
displayBoard();
cout << endl << endl << \"player 2 [O] Wins! Game Over!\ \";
}
else
{
displayBoard();
cout << endl << endl << \"It\'s a draw! Game Over!\ \";
}
return 0;
}
void eraseGameBoard()
{
cout << endl;
cout << \" | | \" << endl;
cout << \" \" << board[0][0] << \" | \" << board[0][1] << \" | \" << board[0][2] << endl;
cout << \"____|____|____\" << endl;
cout << \" | | \" << endl;
cout << \" \" << board[1][0] << \" | \" << board[1][1] << \" | \" << board[1][2] << endl;
cout << \"____|____|____\" << endl;
cout << \" | | \" << endl;
cout << \" \" << board[2][0] << \" | \" << board[2][1] << \" | \" << board[2][2] << endl;
cout << \" | | \" << endl;
}
void displayBoard()
{
cout << endl;
cout << \" | | \" << endl;
cout << \" \" << board[0][0] << \" | \" << board[0][1] << \" | \" << board[0][2] << endl;
cout << \"____|____|____\" << endl;
cout << \" | | \" << endl;
cout << \" \" << board[1][0] << \" | \" << board[1][1] << \" | \" << board[1][2] << endl;
cout << \"____|____|____\" << endl;
cout << \" | | \" << endl;
cout << \" \" << board[2][0] << \" | \" << board[2][1] << \" | \" << board[2][2] << endl;
cout << \" | | \" << endl;
}
void playerTurn()
{
int choice;
int row = 0, column = 0;
if (turn == \'X\')
{
cout << \"Player 1 turn [X]: \";
}
else if (turn == \'O\')
{
cout << \"Player 2 turn [O]: \";
}
cin >> choice;
switch (choice)
{
case 1: row = 0; column = 0; break;
case 2: row = 0; column = 1; break;
case 3: row = 0; column = 2; break;
case 4: row = 1; column = 0; break;
case 5: row = 1; column = 1; break;
case 6: row = 1; column = 2; break;
case 7: row = 2; column = 0; break;
case 8: row = 2; column = 1; break;
case 9: row = 2; column = 2; break;
default:
cout << \"Illegal move! Try again\ \";
playerTurn();
}
if (turn == \'X\' && board[row][column] != \'X\' && board[row][column] != \'O\')
{
board[row][column] = \'X\';
turn = \'O\';
}
else if(turn == \'O\' && board[row][column] != \'X\' && board[row][column] != \'O\')
{
board[row][column] = \'O\';
turn = \'X\';
}
else
{
cout << \"The cell you chose is used! Choose another one\ \";
playerTurn();
}
}
bool gameover()
{
for (int i = 0; i < 3; i++)
{
if ((board[i][0] == board[i][1] && board[i][1] == board[i][2]) || (board[0][i] == board[1][i] && board[1][i] == board[2][i]))
{
return true;
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (board[i][j] != \'X\' && board[i][j] != \'O\')
{
return false;
}
}
}
draw = true;
return true;
}




