I want to pass a boolean value, bool gameover(false), into the function check_winning_condition, and then pass it to function
ask_rematch. Then in ask_rematch, if the user's input is "no", then return the boolean gameover as true to stop the game.
How do I do this? Or is there a better alternative to this? Am I making things harder?
Code:
#include <iostream>
using namespace std;
void show_board(char board[][3]);
int ask_rematch(char board[][3], bool gameover)
{ string rematch;
std::cout << "Would you like a rematch? yes/no \n";
std::cin >> rematch;
if ( rematch == "yes")
{
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';
}
else if ( rematch == "no")
{
gameover = true;
return gameover;
}
}
int check_winning_condition(char board[][3],string player[],bool gameover)
{
if ((board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X')||
(board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X')||
(board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X')||
(board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X')||
(board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X')||
(board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X')||
(board[2][0] == 'X' && board[1][1] == 'X' && board[0][2] == 'X')||
(board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X'))
{
std::cout << player[0] << " WINS!\n";
show_board(board);
ask_rematch(board,gameover);
return gameover;
}
else if
((board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O')||
(board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O')||
(board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O')||
(board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O')||
(board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O')||
(board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O')||
(board[2][0] == 'O' && board[1][1] == 'O' && board[0][2] == 'O')||
(board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O'))
{
std::cout << player[1] << " WINS!\n";
show_board(board);
ask_rematch(board,gameover);
return gameover;
}
}
void get_player_names(string player[])
{ string player1, player2;
std::cout << "Player 1's name: ";
std::getline(cin, player1);
std::cout << "\n";
std::cout << "Player 2's name: ";
std::getline(cin, player2);
std::cout << "\n";
player[0] = player1;
player[1] = player2;
}
int game_start(char board[][3],string player[])
{
bool gameover(false);
int turn = 0; //players' turn
do{
char mark;
show_board(board);
std::cout << player[turn] << "'s move.\n";
std::cout << "Pick the number to mark. \n";
std::cin >> mark;
for ( int row=0;row<3;row++ )
{
for (int column=0;column<3;column++)
{
if(turn == 0 && mark == board[row][column] && mark != 'O')
{ board[row][column] = 'X';
turn = 1;
}
else if (turn == 1 && mark == board[row][column] && mark != 'X')
{ board[row][column] = 'O';
turn = 0;
}
}
}
check_winning_condition(board,player,gameover);
}
while(!gameover);
}
void show_board(char board[][3])
{
for (int row=0;row<3;row++)
{
for (int column=0;column<3;column++)
{
std::cout << board[row][column] << " ";
}
std::cout << endl << endl;
}
}
int main()
{
char board[3][3] = { {'1','2','3'} ,
{'4','5','6'} ,
{'7','8','9'} };
string player[2];
get_player_names(player);
game_start(board,player);
return 0;
}