C++ Programming: How can I get rid of the global variable "char gamePlayer = 'X';

Microsoft Visual C#
7th Edition
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Joyce, Farrell.
Chapter8: Advanced Method Concepts
Section: Chapter Questions
Problem 8RQ
icon
Related questions
Question

C++ Programming: 

How can I get rid of the global variable "char gamePlayer = 'X';

 

This is my code:

Thanks

//This program creates a tic-tac-toe game for 2 users.
#include <iostream>
#include <iomanip>
using namespace std;

const int ROWS = 3; //Number of rows.
const int COLUMNS = 3; //Number of columns.
char gamePlayer = 'X';
char board[ROWS][COLUMNS] = {{'*','*','*'}, //Playboard array.
{'*','*','*'},
{'*','*','*'}
};
char checkWinner();
void drawBoard(); // Playboard.
void placeMove();
void playerChange();

int main()
{
int d = 0;
drawBoard();
while (1)
{
d++;
placeMove();
drawBoard();
playerChange();
if (checkWinner() == 'X')
{
cout << "Player X has won the game!\nThe game is now over, farewell!";
break;
}
else if (checkWinner() == 'O')
{
cout << "Player O has won the game!\nThe game is now over, farewell!";
break;
}
else if (checkWinner() == '/' && d == 9) //Check for tie.
{
cout << "This game has ended in a tie!\nThe game is now over, farewell!";
break;
}

}

return 0;
}

void drawBoard() //Function to draw board.
{
cout << " TIC_TAC_TOE\n" << endl;
cout << " COL1 COL2 COL3" << endl;
cout << "ROW 1 " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << endl;
cout << " ---|---|---" << endl;
cout << "ROW 2 " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << endl;
cout << " ---|---|---" << endl;
cout << "ROW 3 " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << endl;
}

void placeMove() //Input for player move on board as well as output.
{
int a,b;
cout << "Please enter the row you'd like to play:";
cin >> a;
cout << "Please enter the column you'd like to play:";
cin >> b;

if (a == 1 && b == 1 )
board[0][0] = gamePlayer;
else if (a == 1 && b == 2)
board[0][1] = gamePlayer;
else if (a == 1 && b == 3)
board[0][2] = gamePlayer;
else if (a == 2 && b == 1)
board[1][0] = gamePlayer;
else if (a == 2 && b == 2)
board[1][1] = gamePlayer;
else if (a == 2 && b == 3)
board[1][2] = gamePlayer;
else if (a == 3 && b == 1)
board[2][0] = gamePlayer;
else if (a == 3 && b == 2)
board[2][1] = gamePlayer;
else if (a == 3 && b == 3)
board[2][2] = gamePlayer;
}

void playerChange() //Changes the player turn.
{
if (gamePlayer == 'X')
gamePlayer = 'O';
else
gamePlayer = 'X';
}

char checkWinner()
{
//Player X Check
if (board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X')
return 'X';

if (board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X')
return 'X';

if (board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X')
return 'X';

if (board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X')
return 'X';

if (board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X')
return 'X';

if (board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X')
return 'X';

if (board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X')
return 'X';

if (board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X')
return 'X';

//Player O Check.
if (board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O')
return 'O';

if (board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O')
return 'O';

if (board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O')
return 'O';

if (board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O')
return 'O';

if (board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O')
return 'O';

if (board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O')
return 'O';

if (board[0][2] == 'O' && board[1][1] == 'O' && board[2][0] == 'O')
return 'O';

if (board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O')
return 'O';

return '/';

}

 

Expert Solution
steps

Step by step

Solved in 2 steps with 4 images

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,