Tic-Tac-Toe

I'm currently attempting to make a two player Tic-Tac-Toe Game for 2 players I am currently very happy with the results I am having. Except I cannot get the CHECK_X() or the CHECK_O() function to work properly(my way to determine if someone has won) could someone please explain to me why I am having issues?

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
#include<iostream>
#include<string>
using namespace std;

void CS()
{
	cout<<string(100,'\n');
}

string GAP1;
string GAP2;
string GAP3;
string track="123456789";//keeps track of all spaces
void STR_GAP1()
{
	GAP1[1]=track[0];
	GAP1[4]=track[1];
	GAP1[7]=track[2];
}
void STR_GAP2()
{
	GAP2[1]=track[3];
	GAP2[4]=track[4];
	GAP2[7]=track[5];
}
void STR_GAP3()
{
	GAP3[1]=track[6];
	GAP3[4]=track[7];
	GAP3[7]=track[8];
}
int CHECK_X()
{
	     if(track[0]==track[1]==track[2]=='X') return 1;
	else if(track[3]==track[4]==track[5]=='X') return 1;
	else if(track[6]==track[7]==track[8]=='X') return 1;
	else if(track[0]==track[3]==track[6]=='X') return 1;
	else if(track[1]==track[4]==track[7]=='X') return 1;
	else if(track[2]==track[5]==track[8]=='X') return 1;
	else if(track[0]==track[4]==track[8]=='X') return 1;
	else if(track[2]==track[4]==track[6]=='X') return 1;
	return 0;
}
int CHECK_O()
{	
	     if(track[0]==track[1]==track[2]==O[0]) return 1;
	else if(track[3]==track[4]==track[5]==O[0]) return 1;
	else if(track[6]==track[7]==track[8]==O[0]) return 1;
	else if(track[0]==track[3]==track[6]==O[0]) return 1;
	else if(track[1]==track[4]==track[7]==O[0]) return 1;
	else if(track[2]==track[5]==track[8]==O[0]) return 1;
	else if(track[0]==track[4]==track[8]==O[0]) return 1;
	else if(track[2]==track[4]==track[6]==O[0]) return 1;
	return 0;
}

void main()
{
	CS();//lines up the game so between frame one and two it doesnt jar
	cout<<"This is a Tic-Tac-Toe game for 2 players :)"<<endl;
	string Line="+--+--+--+\n";
	GAP1 ="|1 |2 |3 |\n";
	GAP2 ="|4 |5 |6 |\n";
	GAP3 ="|7 |8 |9 |\n";
	int k=1;
	int j=0, l=0;
	int x=0;//breaks while statement and also indicates who has won
	
	while(k<10&&x==0)
	{
		cout<<Line<<GAP1<<Line<<GAP2<<Line<<GAP3<<Line;
		if(k%2==0)
			{
				cout<<"It is O's turn\nWhere would you like to go?\n";
				for(;l==0;)
					{
					cin>>j;
					cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
					if(j!=0&&j<10&&track[j-1]!='O'&&track[j-1]!='X')
						{
							track[j-1]='O';//places an O in the proper place in track
							l=1;
						}
					else cout<<"ERROR Please enter a proper number";
					}
				l=0;
			}
		else 
			{
				cout<<"It is X's turn\nWhere would you like to go?\n";
				for(;l==0;)
					{
					cin>>j;
					cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
					if(j!=0&&j<10&&track[j-1]!='O'&&track[j-1]!='X')
						{
							track[j-1]='X';//places an X int he proper place in track
							l=1;
						}
					else cout<<"ERROR Please enter a proper number";
					}
				l=0;
			}
		STR_GAP1();//reinitalizes STR_GAP 1-3 to have proper X's and O's
		STR_GAP2();
		STR_GAP3();
		CS();//clears the screen
		k++;
		if     (CHECK_X()==1)     x=1;//checks if X won
		else if(CHECK_O()==1)     x=2;//checks if O won

	}
	cout<<Line<<GAP1<<Line<<GAP2<<Line<<GAP3<<Line;//prints the board one more time
	if (x==1)
		cout<<"Congratulations X you won the game!!!";
	else if(x==2)
		cout<<"Congratulations O you won the game!!!";
	else
		cout<<"CATS GAME!!!!";
	cout<<endl;
	cin>>j;
}

It took a while for me to see why that wasn't working, at a quick glance those functions should return the values correctly.

It isn't working because of how C++ is reading the order of them. For example this line
if(track[0]==track[1]==track[2]=='X')

It first checks to see if track[0] is equal to track[1], if they are equal then it makes that value 1 (for true) then compares it to track[2] etc...which of course wont work.

To fix this problem rewrite them so they look something like
if( (track[0] == track[1]) && ( track[1] == track[2]) && (track[2] == 'X') ) return 1;

Then it should check each one of them individually.
Last edited on
Thank you very much that was driving me crazy. Now that I have it working does anyone see a way to optimize it, I know my code is sloppy at best. I still have only a basic understanding of C++.
Last edited on
I don't know if this counts, but at line 42 and 54, always end else-if ladders with an else... like:

1
2
3
4
5
6
7
8
9
if(track[0]==track[1]==track[2]=='X') return 1;
	else if(track[3]==track[4]==track[5]=='X') return 1;
	else if(track[6]==track[7]==track[8]=='X') return 1;
	else if(track[0]==track[3]==track[6]=='X') return 1;
	else if(track[1]==track[4]==track[7]=='X') return 1;
	else if(track[2]==track[5]==track[8]=='X') return 1;
	else if(track[0]==track[4]==track[8]=='X') return 1;
	else if(track[2]==track[4]==track[6]=='X') return 1;
	else return 0;


I don't know if its a strict convention, but I was taught it was a good habit...
Topic archived. No new replies allowed.