Code:
#include <iostream>
char gameBoard [3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};
/*
1,2,3
4,5,6
7,8,9
[0][0], [0][1], [0][2]
[1][0], [1][1], [1][2]
[2][0], [2][1], [2][2]
*/
void printGameboard(); //no need to pass global variables as arguments to this function - info thanks to killme!
void resetGameboard();
int main()
{
//variable to hold the 'X' for player 1, and 'O' for player 2
char playerMarker;
//the square a player chooses
int squareChoice;
//after game is finished, a choice for another go
bool bAnotherGo = true;
//main game loop - exits program if anotherGo is false
do
{
bool bGameOver = false;
//variable to switch whos turn it is (starts with player 1 == true)
int playerTurn = 1;
do //exits loop when the game is over i.e. true, then asks user if wants another go
{
//set markers for each player
if (playerTurn == 1)
{
playerMarker = 'X';
}
else
{
playerMarker = 'O';
}
//prints game board with updated variables depending on player move
printGameboard();
//get player input
std::cout << "Player " << playerTurn << ", your go: ";
std::cin >> squareChoice;
//check move is valid
bool bIsValid;
do
{
bIsValid = true;
if (squareChoice == 1 && gameBoard[0][0] == '1')
{
gameBoard[0][0] = playerMarker;
}
else if (squareChoice == 2 && gameBoard[0][1] == '2')
{
gameBoard[0][1] = playerMarker;
}
else if (squareChoice == 3 && gameBoard[0][2] == '3')
{
gameBoard[0][2] = playerMarker;
}
else if (squareChoice == 4 && gameBoard[1][0] == '4')
{
gameBoard[1][0] = playerMarker;
}
else if (squareChoice == 5 && gameBoard[1][1] == '5')
{
gameBoard[1][1] = playerMarker;
}
else if (squareChoice == 6 && gameBoard[1][2] == '6')
{
gameBoard[1][2] = playerMarker;
}
else if (squareChoice == 7 && gameBoard[2][0] == '7')
{
gameBoard[2][0] = playerMarker;
}
else if (squareChoice == 8 && gameBoard[2][1] == '8')
{
gameBoard[2][1] = playerMarker;
}
else if (squareChoice == 9 && gameBoard[2][2] == '9')
{
gameBoard[2][2] = playerMarker;
}
else
{
std::cout << "Invalid Move. Try again: ";
std::cin >> squareChoice;
bIsValid = false;
}
} while (!bIsValid);
//check to see if the previous move is a winning one
if (!(gameBoard[0][0] == '1')) //if square 1 is not a number...
{
if (gameBoard[0][0] == gameBoard[0][1] && gameBoard[0][0] == gameBoard[0][2]) //3 in a row 1,2,3
{
bGameOver = true;
}
if (gameBoard[0][0] == gameBoard[1][0] && gameBoard[0][0] == gameBoard[2][0]) //3 in a row 1,4,7
{
bGameOver = true;
}
}
if (!(gameBoard[1][1] == '5')) //if square 5 is not a number...
{
if (gameBoard[1][1] == gameBoard[0][1] && gameBoard[1][1] == gameBoard[2][1]) //3 in a row 2,5,8
{
bGameOver = true;
}
if (gameBoard[1][1] == gameBoard[1][0] && gameBoard[1][1] == gameBoard[1][2]) //3 in a row 4,5,6
{
bGameOver = true;
}
if (gameBoard[1][1] == gameBoard[0][2] && gameBoard[1][1] == gameBoard[2][0]) //3 in a row 7,5,3
{
bGameOver = true;
}
if (gameBoard[1][1] == gameBoard[0][0] && gameBoard[1][1] == gameBoard[2][2]) //3 in a row 1,4,9
{
bGameOver = true;
}
}
if (!(gameBoard[2][2] == '9')) //if square 9 is not a number...
{
if (gameBoard[2][2] == gameBoard[2][1] && gameBoard[2][2] == gameBoard[2][0]) //3 in a row 7,8,9
{
bGameOver = true;
}
if (gameBoard[2][2] == gameBoard[1][2] && gameBoard[2][2] == gameBoard[0][2]) //3 in a row 3,6,9
{
bGameOver = true;
}
}
//check for a tie
// ??????????? //
//congratulate player when game is over
if (bGameOver == true)
{
std::cout << "Well done! Player " << playerTurn << " wins!" << std::endl;
}
//move is valid, switch players
if (playerTurn == 1)
{
playerTurn = 2;
}
else
{
playerTurn = 1;
}
} while (bGameOver == false);
//ask if they want another go? using while (bAnotherGo == 1) to exit loop and end game;
std::cout << "Do you want another go? 1 for yes, 0 for no: ";
std::cin >> bAnotherGo;
//if they do want another go, reset the board, somehow...
if (bAnotherGo == true)
{
resetGameboard();
}
} while (bAnotherGo == true);
std::cout << "I hope you enjoyed your game :)";
}
void printGameboard()
{
for (int i = 0; i < 3; i++)
{
std::cout << "|" << gameBoard[i][0] << "|" << gameBoard[i][1] << "|" << gameBoard[i][2] << "|" << std::endl;
if (i < 2)
{
std::cout << "+" << "-" << "+" << "-" << "+" << "-" << "+" << std::endl;
}
}
}
void resetGameboard()
{
gameBoard [3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}}; //excess elements in scalar initializer??
}
My problems now are: