Tic Tac Toe/Naughts and crosses

Hi guys, wrote a program for a basic console tic tac toe/ naughts and crosses game.

I thought it was ok but as soon as you input a move it prints the board out repeatedly.

if anybody can help me that would be really appreciated.

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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <iostream>

using namespace std;

int main(){

	char square1 = '1'; // creating grid
	char square2 = '2';
	char square3 = '3';
	char square4 = '4';
	char square5 = '5';
	char square6 = '6';
	char square7 = '7';
	char square8 = '8';
	char square9 = '9'; // finished grid

	int playerturn = 1;
	char playermark = 'X';
	int playermove;
	bool gameover = false;
	int gamewinner = 0;

	// all variables needed are made

	while(gameover == false){
		
		if (square1 == 'X' && square2 == 'X' && square3 == 'X'){ // checking for a victory of player X
			bool gameover = true;
			int gamewinner = 1;
			return 0;
		}else if (square1 == 'X' && square4 == 'X' && square7 == 'X'){
			bool gameover = true;
			int gamewinner = 1;
			return 0;
		}else if (square1 == 'X' && square5 == 'X' && square9 == 'X'){
			bool gameover = true;
			int gamewinner = 1;
			return 0;
		}else if (square2 == 'X' && square5 == 'X' && square8 == 'X'){
			bool gameover = true;
			int gamewinner = 1;
			return 0;
		}else if (square4 == 'X' && square5 == 'X' && square6 == 'X'){
			bool gameover = true;
			int gamewinner = 1;
			return 0;
		}else if (square7 == 'X' && square8 == 'X' && square9 == 'X'){
			bool gameover = true;
			int gamewinner = 1;
			return 0;
		}else if (square3 == 'X' && square5 == 'X' && square7 == 'X'){
			bool gameover = true;
			int gamewinner = 1;
			return 0;
		}else if (square3 == 'X' && square6 == 'X' && square9 == 'X'){
			bool gameover = true;
			int gamewinner = 1;
			return 0;
		}																// done

		if (square1 == 'O' && square2 == 'O' && square3 == 'O'){		// checking for victory of player O
			bool gameover = true;
			int gamewinner = 2;
			return 0;
		}else if (square1 == 'O' && square4 == 'O' && square7 == 'O'){
			bool gameover = true;
			int gamewinner = 2;
		}else if (square1 == 'O' && square5 == 'O' && square9 == 'O'){
			bool gameover = true;
			int gamewinner = 2;
		}else if (square2 == 'O' && square5 == 'O' && square8 == 'O'){
			bool gameover = true;
			int gamewinner = 2;
		}else if (square4 == 'O' && square5 == 'O' && square6 == 'O'){
			bool gameover = true;
			int gamewinner = 2;
		}else if (square7 == 'O' && square8 == 'O' && square9 == 'O'){
			bool gameover = true;
			int gamewinner = 2;
		}else if (square3 == 'O' && square5 == 'O' && square7 == 'O'){
			bool gameover = true;
			int gamewinner = 2;
		}else if (square3 == 'O' && square6 == 'O' && square9 == 'O'){
			bool gameover = true;
			int gamewinner = 2;
		}								// done

		cout << square1 << " | " << square2 << " | " << square3 << " | " << endl << endl; // drawing grid
		cout << square4 << " | " << square5 << " | " << square6 << " | " << endl << endl;
		cout << square7 << " | " << square8 << " | " << square9 << " | " << endl << endl; // drawn

		if (gameover == false && playerturn == 1){
			playermark = 'X';
			cout << "Player 1 enter your move: " << endl;
			cin >> playermove;
			if (playermove ==  1 && square1 == 1){
				square1 = playermark;
			}else if (playermove == 2 && square2 == 2){
				square2 = playermark;
			}else if (playermove == 3 && square3 == 3){
				square3 = playermark;
			}else if (playermove == 4 && square4 == 4){
				square4 = playermark;
			}else if (playermove == 5 && square5 == 5){
				square5 = playermark;
			}else if (playermove == 6 && square6 == 6){
				square6 = playermark;
			}else if (playermove == 7 && square7 == 7){
				square7 = playermark;
			}else if (playermove == 8 && square8 == 8){
				square8 = playermark;
			}else if (playermove == 9 && square9 == 9){
				square9 = playermark;
			}else{
				cout << "Invalid move." << endl;
			}
			playerturn = 2;
		}
		
		else if(gameover = false && playerturn == 2){
			playermark = 'O';
			cout << "Player 2 enter your move: " << endl;
			cin >> playermove;
			if (playermove ==  1 && square1 == 1){
				square1 = playermark;
			}else if (playermove == 2 && square2 == 2){
				square2 = playermark;
			}else if (playermove == 3 && square3 == 3){
				square3 = playermark;
			}else if (playermove == 4 && square4 == 4){
				square4 = playermark;
			}else if (playermove == 5 && square5 == 5){
				square5 = playermark;
			}else if (playermove == 6 && square6 == 6){
				square6 = playermark;
			}else if (playermove == 7 && square7 == 7){
				square7 = playermark;
			}else if (playermove == 8 && square8 == 8){
				square8 = playermark;
			}else if (playermove == 9 && square9 == 9){
				square9 = playermark;
			}else{
				cout << "Invalid move." << endl;
			}
			playerturn = 1;
		}								



	};

	cout << "Congratulations! Player " << gamewinner << " has won!" << endl;
			cout << "Press any key to exit the game . . ." << endl;
			char end;
			cin >> end;


}



of course there could be loads of problems with it, i just cant get past the first step...
Last edited on
there are two problems i notice with your code.
problem #1.
you have declareations like this
char square1 = '1'; notice the single quotes arround '1'
and then you have this
(playermove == 1 && square1 == 1){ notice no single quotes around '1' so you will get an invalid move output to the screen.

problem #2
on line 120 you have the statement
else if(gameover = false && playerturn == 2){
look closely at the = in gameover = false you are assigning false to gameover not checking to se if gameover == false it should look like this
else if(gameover == false && playerturn == 2){
ugh thanks. they're just stupid errors on my part, i'll fix them now
Consider having a 2d array, instead of 9 separate variables for your board.

Then you can write nested for loops to do things to the board.

There are lots of Tic tac toe games on this forum - check some of them out.

Hope all goes well :)
That's exactly my plan, I'm extremely talented at giving up, so I figured if I wrote a tic tac toe game as a complete beginner. Then write one with arrays, then using functions, objects and classes to check for victories etc until eventually I make a 3D game. This is how I'll check my progress :)
Topic archived. No new replies allowed.