Want to play again?

Right now, I'm working on tic tac toe programming. I already got the code in and the game work fine, it's just that when I try to ask players if they want to play again, it keep looping back "Want to play it again". Here my code:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
int main()
{

const int NUM_ROWS = 3;
const int NUM_COLS = 3;



/**********************************************************************
 * VARIABLES
 *********************************************************************/

char boardAr[NUM_ROWS][NUM_COLS];
string player1;
string player2;
int maxMove;
int move;
char decision;

char token;
char user;
char again;
char victor;

move = 0;
maxMove = 9;
token = 'X';


cout << "WELCOME to a classic game TIC-TAC-TOE";
OutputInstruct();

cout << "\nWould you like to play? (select 'Y' or 'N'): ";
cin >> decision;
cin.ignore(1000, '\n');
decision = toupper(decision);

while(decision == 'Y')
{

//Grab the user name
	GetPlayers(player1, player2);

	InitBoard(boardAr);
	DisplayBoard(boardAr);


//Call for the move of the players
	/*
	do
	{
		GetAndCheckInp(boardAr, token, player1, player2);
		system("cls");
		DisplayBoard(boardAr);
		token = SwitchToken(token);
		move++;

		victor = CheckWin(boardAr);

	}while ((victor == 'T') && (move < maxMove));

	OutputWinner(victor, player1, player2);

	move = 0;
	*/
	do
	{
		cout << "\nWould you like to play again? (select 'Y' or 'N'): ";
		cin >> decision;
		cin.ignore(1000, '\n');
		decision = toupper(decision);

		if (decision == 'Y')
		{
			cout << "Would you like to keep the same players? ";
			cin >> decision;
			cin.ignore(1000, '\n');
			decision = toupper(decision);

				if(decision == 'N')
				{
					GetPlayers(player1, player2);
				}
				else
				{
					cout << "Did not change.";
					cout << endl;
				}
		}

		else
		{
			decision = 'N';
		}

		decision = 'N';
	}while (decision == 'N');


}
cout <<"\nThank you for visiting, please come again";
cout << endl;

	return 0;

}


It keep saying "Wanna play again?" without looping back to the beginning.
Last edited on
closed account (owbkoG1T)
Your play again loop concept is almost right, the problems that I see are that:

You use the same variable for two different decisions (to play again and to use the same players), so if I want to play again, but with different players, my play again decision is overwritten by my player decision. This is on lines 69 and 76, on line 69, the user decided if he/ she wants to play again, on line 76, the user overwrites that value to decided if he/ she wants different players.

Line 96 is the big problem, no matter what use enters into decision, this line will execute anyways and enter the value 'N', which is the value that the do while loop checks for. Get rid of that line.

On line 97, should you not check to see if decision is 'Y' to play again? If I were to enter 'Y', I would think that that means that I DO want to play again, if I entered 'N', I would be thinking that I DON'T want to play again.

Edit:

Just realized that I confused the do while loop from 66 to 97 with the while loop from line 38 to 100.
Last edited on
For line 97, what should I put to get out of the loop? Should I put
while(decision != 'Y');

Or should I use different variable?

Here I have so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
	do
	{
		cout << "\nWould you like to play again? (select 'Y' or 'N'): ";
		cin >> decision;
		cin.ignore(1000, '\n');
		decision = toupper(decision);

		if (decision == 'Y')
		{
			cout << "Would you like to keep the same players? ";
			cin >> user;
			cin.ignore(1000, '\n');
			decision = toupper(user);

				if(user == 'N')
				{
					GetPlayers(player1, player2);
				}
				else
				{
					cout << "Did not change.";
					cout << endl;
				}
				user = 'Y';
				user = decision;
		}

		else
		{
			decision = 'N';
		}


	}while (decision == 'Y');
}
cout <<"\nThank you for visiting, please come again";
cout << endl;

	return 0;

}


The "No" part of the decision was successful when putting yes, its still loop.

Only the yes part still haven't loop. Instead it keep looping "Do you want to play again?". I"m so close!!
Last edited on
closed account (owbkoG1T)
Sorry, I just made a correction to my first post. I was looking at the right loop in the wrong sense.

Or should I use different variable?


Yes, use a different variable for each decision. The problem is still that you overwrite the user's decision to play again with the decision to use different players.

Try using different variables for the two decisions, and while you try that I'm going to try and understand how I messed up.

Again, sorry for the confusion.
On line 97, change from decision == 'N' to decision !='N'. Every time 'N' is pressed, it'll loop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
cout << "\nWould you like to play this game? (select 'Y' or 'N'): ";
cin >> decision;
cin.ignore(1000, '\n');
decision = toupper(decision);

while(decision == 'Y')
{

	InitBoard(boardAr);
	DisplayBoard(boardAr);


//Call for the move of the players
	/*
	do
	{
		GetAndCheckInp(boardAr, token, player1, player2);
		system("cls");
		DisplayBoard(boardAr);
		token = SwitchToken(token);
		move++;

		victor = CheckWin(boardAr);

	}while ((victor == 'T') && (move < maxMove));

	OutputWinner(victor, player1, player2);

	move = 0;
	*/
	do
	{
		cout << "\nWould you like to play again? (select 'Y' or 'N'): ";
		cin >> decision;
		cin.ignore(1000, '\n');
		decision = toupper(decision);

		if (decision == 'Y')
		{
			cout << "Would you like to keep the same players? ";
			cin >> user;
			cin.ignore(1000, '\n');
			user = toupper(user);

				if(user == 'N')
				{
					GetPlayers(player1, player2);
				}
				else
				{
					cout << "Did not change.";
					cout << endl;
				}
				decision = user;

		}

		else
		{
			decision = 'N';
		}


	}while (decision != 'N');
}
cout <<"\nThank you for visiting, please come again";
cout << endl;

	return 0;

}


I try everything and I still couldn't get it work. Still the loop for yes have not loop at all back to the beginning. Any idea? Everytime I push yes, it's reloop back to do you want to play again.
Last edited on
In the else statement on line 60 I am guessing you are trying to end the game?? If you are put break; in place of decision != 'N';
Topic archived. No new replies allowed.