Tic Tac Toe ending early

Okay so a few problems. Problem one: the program declares a winner when there is a marker in the middle, corner, and another corner, though it is not three in a row. Problem 2: when you chose to play again on the first move of the second game it just asks if you want to play again, heres the 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
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
#include <iostream>
#include <Windows.h>
#include <cstdlib>
using namespace std;
char slot[3][3]={'1','2','3','4','5','6','7','8','9'};
char *pointer[9]={&slot[0][0], &slot[0][1], &slot[0][2], &slot[1][0], &slot[1][1], &slot[1][2], &slot[2][0], &slot[2][1], &slot[2][2]};
char player_turn='2';
char X_or_O;
char Player_Move;
char restart_game;
int game_number=1;
bool valid_move=true;
bool game_over=false;
void print_board();
void Marker_Set();
void Switch_marker();

void Switch_marker()
{
	switch (player_turn)
	 {
	 case '1': player_turn='2';
		 break;
	 
	 case '2': player_turn='1';
		 break;
	 }
}
void print_board()
{
	cout<<slot[0][0]<<"   "<<slot[0][1]<<"   "<<slot[0][2]<<endl;
	cout<<slot[1][0]<<"   "<<slot[1][1]<<"   "<<slot[1][2]<<endl;
	cout<<slot[2][0]<<"   "<<slot[2][1]<<"   "<<slot[2][2]<<endl;
}
void Marker_Set()
{
	cout<<"player "<<player_turn<<"'s turn:   ";
	if (player_turn=='1')
		X_or_O='X';
	else 
	X_or_O='O';
}
int main()
{
	do
	{
	do
	{
	game_over==false;
		do
		{		
		print_board(); // function prints out the board
		Switch_marker(); //switches marker from X to O
		Marker_Set(); //sets player 1 to X and player two to O
		
		cin>>Player_Move; //input and checks to see if the move is valid or not
			if (Player_Move=='1' && slot[0][0]=='1')
				slot[0][0]=X_or_O;
			else if (Player_Move=='2' && slot[0][1]=='2')
				slot[0][1]=X_or_O;
			else if (Player_Move=='3'  && slot[0][2]=='3')
				slot[0][2]=X_or_O;
			else if (Player_Move=='4' && slot[1][0]=='4')
				slot[1][0]=X_or_O;
			else if (Player_Move=='5' && slot[1][1]=='5')
				slot[1][1]=X_or_O;
			else if (Player_Move=='6' && slot[1][2]=='6')
				slot[1][2]=X_or_O;
			else if (Player_Move=='7' && slot[2][0]=='7')
				slot[2][0]=X_or_O;
			else if (Player_Move=='8' && slot[2][1]=='8')
				slot[2][1]=X_or_O;
			else if (Player_Move=='9' && slot[2][2]=='9')
				slot[2][2]=X_or_O;
			else 
			{	cout<<"Invalid move, have another go."<<endl;
				valid_move=false; }
			}  
			while (valid_move==false); // repeat input and checks to see if its valid 


	 if (slot[0][0]==slot[0][1] && slot[0][2]==slot[0][1]) //checks to see if someone has won the game
		game_over=true;
	 if (slot[1][0]==slot[1][1] && slot[1][2]==slot[1][1]) 
		game_over=true;
	 if (slot[2][0]==slot[2][1] && slot[2][2]==slot[2][1])
		game_over=true;
	 if (slot[0][0]==slot[1][0] && slot[2][0]==slot[1][0])
		game_over=true;
	 if (slot[0][1]==slot[1][1] && slot[2][1]==slot[1][1])
		game_over=true;
	 if (slot[0][2]==slot[1][2] && slot[2][2]==slot[1][2])
		game_over=true;
	 if (slot[0][0]==slot[1][1] && slot[2][2]==slot[1][1])
		game_over=true;
	 if (slot[0][2]==slot[1][1] && slot[0][2]==slot[1][1])
		game_over=true;
	 	if (game_over==true)
			cout<<"player "<<player_turn<<" wins!!"<<endl;
	
		if (slot[0][0]!='1' && slot[0][1]!='2' && slot[0][2]!='3' 
		 && slot[1][0]!='4' && slot[1][1]!='5' && slot[1][2]!='6' &&
		 slot[2][0]!='7' && slot[2][1]!='8' && slot[2][2]!='9')
		 game_over==true;
	}
	while(game_over==false);
	print_board();
	cout<<"would you like to play again?"<<"y / n"<<endl;
	cin>>restart_game;
	if (restart_game=='y')
	{
		game_number+=1;
		cout<<"okay tic tac toe game number:  "<<game_number<<endl;
		if(player_turn=='1')
		player_turn++; 
	slot[0][0]='1';
	slot[0][1]='2';
	slot[0][2]='3';
	slot[1][0]='4';
	slot[1][1]='5';
	slot[1][2]='6';
	slot[2][0]='7';
	slot[2][1]='8';
	slot[2][2]='9';
		
	}
	else cout<<"okay goodbye, come back later";
	Sleep(1000);
	}
	while (restart_game=='y');
} 
Last edited on
The reason it is choosing a winner incorrectly in certain cases is due to your last check for gameover on the game board.
1
2
if (slot[0][2]==slot[1][1] && slot[0][2]==slot[1][1])
game_over=true;


It looks like you made a typo here (it is checking the same 2 squares twice)

As for it instantly asking you "if you want to play again" after you start a new game, that is because you are not setting game_over back to false after you start again, so the code always thinks the player has finished.

Finally it looks like your "tie" code isn't going to work, change the == to =.
1
2
3
4
if (slot[0][0]!='1' && slot[0][1]!='2' && slot[0][2]!='3' 
&& slot[1][0]!='4' && slot[1][1]!='5' && slot[1][2]!='6' &&
slot[2][0]!='7' && slot[2][1]!='8' && slot[2][2]!='9')
game_over==true;

you could use
 
system("cls");

to clear the screen before reprinting the 3*3 grid
Alright thanks a ton guys. didnt realize that typo either
My cats game (tie) code still isnt working. Any ideas?

1
2
3
4
if (slot[0][0]!='1' && slot[0][1]!='2' && slot[0][2]!='3' 
		 && slot[1][0]!='4' && slot[1][1]!='5' && slot[1][2]!='6' &&
		 slot[2][0]!='7' && slot[2][1]!='8' && slot[2][2]!='9')
		 game_over==true;
Last edited on
game_over = true;
Woo program tic tac toe complete thank you. But I'm wondering why its = and not == . anyone know?
= is to directly assign something to a variable. == is used to compare 2 different variables.

In this case, when there are no spaces left for players to pick, you want to set game_over to true, not compare it to true;
Ah = is used to make something equal. == is used to see if conditions are true and compare things yea gotcha thanks bro.
Topic archived. No new replies allowed.