The void checkboard function is not working. This is a tic tac toe game. So that function checks after each move if someone has won the game or if it is a tie. After someone wins or there is a tie the board is to be reset and the game startover. I just cannot get the void checkboard function to work. Please help me solve this problem.
checkboard should check every users input to see if that play has won. So checkboard has to check to see if playerOne has Xs in a line or diagonally or if playerTwo has Os in that way either or if there is a tie. If the player wins or there is a draw it tells who won or that there is a tie and then ask the user if they want to play again or exit the program. From their response the board is reset or the game exits.
The problem i think is that bGameOver keeps coming out true no matter what. I put after the code before the gameover bGameOver = false and the proggram ran like it was suppose to. So some where in the if statements it keeps making bGameOver = true after 1 move.
After it is solved I would like suggestions on how to improve it only after the problem is fixed.
#include <iostream>
#include <string>
void checkboard(char** CurrentPlayer, char (&squares)[3][3], char &playerMarker, bool bGameOver,char playerOne[]);
using namespace std;
void main()
{
char board[3][3];
board[0][0] = ' ';
board[0][1] = ' ';
board[0][2] = ' ';
board[1][0] = ' ';
board[1][1] = ' ';
board[1][2] = ' ';
board[2][0] = ' ';
board[2][1] = ' ';
board[2][2] = ' ';
bool bGameOver(true);
char playerMarker;
// Main game loop
do
{
showboard(board);
boardMarker(playerTurn, playerOne, playerTwo, playerMarker, ¤tPlayer);
playerMove(currentPlayer, board, playerMarker);
checkboard(¤tPlayer, board, playerMarker, bGameOver, playerOne);
alternatePlayers(playerTurn);
}
while (!bGameOver);
}
void showboard(char (&board)[3][3])
{
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 3; ++j)
{
cout << board[i][j];
if(j < 2) cout << "|";
}
cout << "\n";
if(i < 2) cout << "-+-+-" << endl;;
}
}
void playerMove(char* CurrentPlayer, char (&board)[3][3], char &playerMarker)
{
bool bValidMove;
int x,y;
// Prompt the player for a move
cout << "Player " << CurrentPlayer << "'s move:" << endl;
// Loop until we get a valid move
do
{
cout << "Please enter the row number for the place you wish to mark: " << endl;
cin >> x;
cout << "Please enter the column number for the place you wish to mark" << endl;
cin >> y;
bValidMove = true;
// Check for a valid move
if (x == 1 && y == 1)
{
board[0][0] = playerMarker;
}
else if (x == 1 && y == 2)
{
board[0][1] = playerMarker;
}
else if (x == 1 && y == 3)
{
board[0][2] = playerMarker;
}
else if (x == 2 && y == 1)
{
board[1][0] = playerMarker;
}
else if (x == 2 && y == 2)
{
board[1][1] = playerMarker;
}
else if (x == 2 && y == 3)
{
board[1][2] = playerMarker;
}
else if (x == 3 && y == 1)
{
board[2][0] = playerMarker;
}
else if (x == 3 && y == 2)
{
board[2][1] = playerMarker;
}
else if (x == 3 && y == 3)
{
board[2][2] = playerMarker;
}
else
{
cout << "Invalid Move. Try again." << endl;
bValidMove = false;
}
}
while (!bValidMove);
}
void checkboard(char** CurrentPlayer, char (&board)[3][3], char &playerMarker, bool bGameOver, char playerOne[100])
{
bGameOver = false;
bool bWinGame = true;
// Check for end of game conditions
do
{
if (board[0][0] = playerMarker)
{
if (board[0][1] == board[0][0] && board[0][2] == board[0][0]) {
bGameOver = true;
}
if (board[1][0] == board[0][0] && board[2][0] == board[0][0]) {
bGameOver = true;
}
}
if (board[1][1] != playerMarker)
{
if (board[0][0] == board[1][1] && board[2][2] == board[1][1])
{
bGameOver = true;
}
if (board[0][1] == board[1][1] && board[2][1] == board[1][1])
{
bGameOver = true;
}
if (board[1][0] == board[1][1] && board[1][2] == board[1][1])
{
bGameOver = true;
}
if (board[0][2] == board[1][1] && board[2][0] == board[1][1])
{
bGameOver = true;
}
}
if (board[2][2] != playerMarker)
{
if (board[0][2] == board[2][2] && board[1][2] == board[2][2])
{
bGameOver = true;
}
if (board[2][0] == board[2][2] && board[2][1] == board[2][2])
{
bGameOver = true;
}
}
// Need to check the board full (no-win condition)
if (board[0][0] != playerMarker && board[0][1] != playerMarker && board[0][2] != playerMarker &&
board[1][0] != playerMarker && board[1][1] != playerMarker && board[1][2] != playerMarker &&
board[2][0] != playerMarker && board[2][1] != playerMarker && board[2][2] != playerMarker && !bGameOver)
{
bGameOver = true;
bWinGame = false;
}
system("cls");
if (bGameOver)
{
if (bWinGame)
{
cout << "Player " << CurrentPlayer << " wins!" << endl;
}
// Print ending board
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 3; ++j)
{
cout << board[i][j];
if(j < 2) cout << "|";
}
cout << "\n";
if(i < 2) cout << "-+-+-" << endl;;
}
cout << "Game Over!" << endl;
cout << "Play again (y/n)?" << endl;
char cPlayAgain;
cin >> cPlayAgain;
if (cPlayAgain == 'y')
{
bGameOver = false;
// Clear the board
board[0][0] = ' ';
board[0][1] = ' ';
board[0][2] = ' ';
board[1][0] = ' ';
board[1][1] = ' ';
board[1][2] = ' ';
board[2][0] = ' ';
board[2][1] = ' ';
board[2][2] = ' ';
}
*CurrentPlayer = playerOne;
}
} while (!bGameOver);
}