TIC TAC TOE. HELP!!!

I am new to c++ and i just started working on a tic tac toe game.I put the playerturn variable before my primary loop but it still only inputs the 'X' character. Basically, i do not know how to get the playerturns to change. Obviously this is not a complete program, but it is the part that gets messed up.

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
 
		bool gameOn = false;
		bool validinput;
		int playerturn = 1;
		

		if (menuChoice == 'y') {

			do {
				
				cout << "       " << square1 << "  |  " << square2 << "  |  " << square3 << endl;
				cout << "     -----+-----+-----" << endl;
				cout << "       " << square4 << "  |  " << square5 << "  |  " << square6 << endl;
				cout << "     -----+-----+-----" << endl;
				cout << "       " << square7 << "  |  " << square8 << "  |  " << square9 << endl;
				cout << "     -----+-----+-----" << endl;

				
				

				char playermarker;
				if (playerturn == 1) {
					playermarker = 'X';
				}
				else {
					playermarker = 'O';
				}
				
				char CurrentMove;
				cout << " Enter the number of the square you want to take" << endl;
				cin >> CurrentMove;
				validinput = true;

				if (CurrentMove == '1' && square1 == '1') {
					square1 = playermarker;
				}
				else if (CurrentMove == '2' && square2 == '2') {
					square2 = playermarker;
				}
				else if (CurrentMove == '3' && square3 == '3') {
					square3 = playermarker;
				}
				else if (CurrentMove == '4' && square4 == '4') {
					square4 = playermarker;
				}
				else if (CurrentMove == '5' && square5 == '5') {
					square5 = playermarker;
				}
				else if (CurrentMove == '6' && square6 == '6') {
					square6 = playermarker;
				}
				else if (CurrentMove == '7' && square7 == '7') {
					square7 = playermarker;
				}
				else if (CurrentMove == '8' && square8 == '8') {
					square8 = playermarker;
				}
				else if (CurrentMove == '9' && square9 == '9') {
					square9 = playermarker;
				}
				else {
					cout << " Invalid Input, Try Again" << endl;
					validinput = false;
				}
				


			} while (validinput);
		
		
		}while (!gameOn);
		
	
	return 0;
} 
Last edited on
Maybe instead of this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
    char playermarker;

    if (playerturn == 1)
     {
	playermarker = 'X';
     }

    else 
     {
        playermarker = 'O';
 
     }
				


You could implement the bool playerturn variable instead of an integer
and switch directly in your first if statement

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

    bool playerturn = false;
    char playermarker;

    if (playerturn == false)
     {
	playermarker = 'X';
	playerturn = true; // Next turn It Will Be The ( O )
     }

    else 
     {
        playermarker = 'O';
        playerturn = false; // Next turn It Will Be The ( X )
     }
Last edited on
Thank you that worked. I did have to move the playerturn outside of the main loop but it did work in the end. Since i am still new to this, can you explain how your code works exactly. I dont want to just copy and paste, i actually want to understand the idea.
First of all ,before your main do/while loop , you have a declared variable of bool type

A bool variable can only contain two possible values , in this case false or true or it can also be 0 or 1 like machine language (binary) .

In this case changing your integer for a bool type variable optimized the space that you're using in memory to use your program, that's just an example because in a little program it won't even be noticeable but in bigger project it might.

Happy that it made it !!
Last edited on
Topic archived. No new replies allowed.