Giving players names in a Tic-Tac-Toe Game

I am wanting to provide an option for players to input their names so that they are displayed at the top of the screen and are also prompted with their names when it is their turn to go.

Although with everything that I have tried I seem to always run into the problem that I cannot provide this option without messing up almost all of my code as I have given a global default player with which i have used throughout my code/functions.

I am wandering if there is a way for me to provide them with this option without messing up my code too much.
Last edited on
shouldn't be to hard. try something like this, just change "charName" in my example to whatever you used in your code

1
2
3
4
5
6
7
8
9
10
11

string charName = "Player"; //This give a default name of Player if you want a default name

int main ()

cout << "Player 1's name :\n";
cin >> charName

//than you can do things like display score and player name with

cout << charName << "'s score is 1,000";


this will ask the player for there name, then input there response to charName.
then it will print to the screen
"charName's score is 1,000" (where charName is what they input)
Last edited on
This worked, but how would I go about it rotating between two different names?

This is what I have for the 1 player

string X = "Player";

cout << "Player 1's name:\n";
cin >> X;

cout << "It's " << X << "'s turn. " << "Type the number of the field: ";
Make another variable for player 2. something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main ()

string charName = "Player1"
string charName2 = "Player2"



cout << "Player 1's name :\n";
cin >> charName
cout << "Player 2's name :\n";
cin >> charName2

cout << "Player 1's name is " << charName
cout << "Player 2's name is " << charName2


Now you can use either charName or charName2 depending on what player you are talking to.
Using a toggle function I was managing to switch between the names X and O as they played their turns e.g.

cout << "It's " << player << "'s turn. " << "Type the number of the field: ";

would display X's turn then O's turn, using:

void TogglePlayer()
{
if (player == 'X')
player = 'O';
else
player = 'X';
}

How would I go about changing this so that it outputted Player 1's name then switched to Player 2's name.

This is my full code:
http://cpp.sh/5zd7c
Last edited on
Your problem is in your display function. your code (as follows)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Display()
{
	//so that there is only one grid being displayed (clears previous display once an input is executed)
	system("cls");

	cout << "Welcome to Tic-Tac-Toe!" << endl;
	cout << "X goes first." << endl;
	//r = row c = column
	for (int r = 0; r < 3; r++)
	{
		for (int c = 0; c < 3; c++)
		{
			cout << grid[r][c] << " ";
		}
		cout << endl;
	}
}


should be
[code]
void Display()
{
//so that there is only one grid being displayed (clears previous display once an input is executed)
system("cls");

cout << "Welcome to Tic-Tac-Toe!" << endl;
cout << X << " goes first." << endl;
//r = row c = column
for (int r = 0; r < 3; r++)
{
for (int c = 0; c < 3; c++)
{
cout << grid[r][c] << " ";
}
cout << endl;
}
}
[\code]
Your problem was you had X as part of the literal string.
Hmm I've managed to work around this, thank you very much.

I also have another issue if your willing to help.
I am wanting to have a menu where is asks the player if they would like to play or quit the game.

I thought an if statement would suffice for this but are unsure on what to put for the coding e.g.

int p;
int Play;
int Quit;

cout << "Hello, and welcome to the Menu, " << endl;
cout << "If you would like to play, type 'Play'." << endl;
cout << "Or if you would like to quit, type 'Quit'." << endl;
cin >> p;

if (p == Play)
{
//Start the game
}
else if (p == Quit)
{
//Close window
}
Firstly, you have said Play is an int, but you want the play to type in a String, that wont work.
Same with Quit.

it might be easier using a char so for example

char p;
char play;
char quit;

cout << "Hello, and welcome to the Menu, " << endl;
cout << "If you would like to play, type 'p'." << endl;
cout << "Or if you would like to quit, type 'q'." << endl;
cin >> p;

if (p == 'p')
{
//Start the game
}
else if (p == 'q')
{
//Close window
}


still a beginner so not completely sure with this but hopefully that will help.
Topic archived. No new replies allowed.