Tic Tac Toe Problems

Can someone take a look at this and point me in the right direction. I have been having trouble with this for a couple of days now and can't figure it out. I'ts not correctly recognizing when someone wins.

Thanks,

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
//Tic Tac Toe

#include <iostream>
#include <string>

using namespace std;

//Declare funtions
bool CheckBoardForO(char x[]);
bool CheckBoardForX(char x[]);
void DrawBoard(char x[]);	

//Main loop -->
int main(){

	char myArray[9];
	bool winner = false;
	string theWinner = "";

	int p1;
	int p2;

	//Set up board
	char t = '0';
	for (int a = 0; a < 9; a++){
		t = t + 1;
		myArray[a] = t;
	}

	//Main Game Loop
	do{

		while (true){ //Player_1 LOOP--------------------
			
			system("CLS");
			DrawBoard(myArray);

			cout << "Player 1 - Make your move(1-9): ";
			cin >> p1;

			if (p1 <= 9 && p1 >= 1){ //If input is in correct range
				
				if (myArray[p1 - 1] != 'X' && myArray[p1 - 1] != 'O'){ //If the spot is not already taken					
					myArray[p1 - 1] = 'X';

					if (CheckBoardForX(myArray) == true){ //Did 'X' win?
						theWinner = "Player 1";
						winner = true;
						break;

					}else{
						break; //if there is no winner yet
					}
							
				}else{
					cout << "Error, please pick an empty space please!" << endl;
					system("pause");
					}
				
			}else{
				cout << "Please enter a number in range!" << endl;
				system("pause");
			}
		}//whileloop



		while (true){ //Player 2 LOOP-------------------------

			system("CLS");
			DrawBoard(myArray);

			cout << "Player 2 - Make your move(1-9): ";
			cin >> p2;

			if (p2 <= 9 && p2 >= 1){

				if (myArray[p2 - 1] != 'X' && myArray[p2 - 1] != 'O'){
					myArray[p2 - 1] = 'O';

					if (CheckBoardForO(myArray) == true){
					theWinner = "Player 2";
					winner = true;
					break;

				}else{ //if input is accepted
					break; 
					}
					
				
				}else{ //if number not in range
					cout << "Error, please pick an empty space!" << endl;
					system("pause");
				}

			}else{ //Num check error
				cout << "Please enter a number in range!" << endl;
				system("pause");
				
			
			}
		}//1st if
	

	}while (winner == false);


	//End game, display the winner//
	system("CLS");
	cout << theWinner << " win's the game!!!" << endl;
	
	system("pause");
	return 0;
}


void DrawBoard(char x[]){
	cout << x[0] << " | " << x[1] << " | " << x[2] << endl;
	cout << "---------" << endl;
	cout << x[3] << " | " << x[4] << " | " << x[5] << endl;
	cout << "---------" << endl;
	cout << x[6] << " | " << x[7] << " | " << x[8] << endl;
}


bool CheckBoardForX(char x[]){
	if (x[0] && x[1] && x[2] == 'X'){return true;}
	if (x[3] && x[4] && x[5] == 'X'){return true;}
	if (x[6] && x[7] && x[8] == 'X'){return true;}

	if (x[0] && x[3] && x[6] == 'X'){return true;}
	if (x[1] && x[4] && x[7] == 'X'){return true;}
	if (x[2] && x[5] && x[8] == 'X'){return true;}

	if (x[0] && x[4] && x[8] == 'X'){return true;}
	if (x[2] && x[4] && x[6] == 'X'){return true;}
	return false;
	}
		
bool CheckBoardForO(char x[]){
	if (x[0] && x[1] && x[2] == 'O'){return true;}
	if (x[3] && x[4] && x[5] == 'O'){return true;}
	if (x[6] && x[7] && x[8] == 'O'){return true;}

	if (x[0] && x[3] && x[6] == 'O'){return true;}
	if (x[1] && x[4] && x[7] == 'O'){return true;}
	if (x[2] && x[5] && x[8] == 'O'){return true;}

	if (x[0] && x[4] && x[8] == 'O'){return true;}
	if (x[2] && x[4] && x[6] == 'O'){return true;}
	return false;
	}

In C++ you are not able to check a bunch of variables against 1 thing the way you are doing it. (As the logic doesn't work the same as a person would look at it).

Change stuff like
if (x[0] && x[1] && x[2] == 'X'){return true;}
to
if (x[0] == x[1] && x[1] == x[2] && x[2] == 'X'){return true;}
so that it can be read correctly.
i have made your basic screen a bit nicer by changing drawboard:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void DrawBoard(char x[]){

	char a=179;
	char b=196;
	char c=197;
	char space=0x20;// see ascii tables

	cout<<endl;
	cout << space<< x[0] << " "<< a <<" " << x[1] << " "<< a <<" " << x[2] << endl;
	cout << space<<b <<b<<c<<b<<b<<b<<c<<b<<b << endl;
	cout << space<< x[3] << " "<< a <<" " << x[4] << " "<< a <<" " << x[5] << endl;
	cout << space<<b <<b<<c<<b<<b<<b<<c<<b<<b << endl;
	cout << space<< x[6] << " "<< a <<" " << x[7] << " "<< a <<" " << x[8] << endl;
	cout<<endl;
}
Last edited on
This got my thinking...can't this be done gracefully with variadic templates? The below works in gcc 4.7. Note this is my first foray into variadic templates:

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
template<typename T, typename T2>
bool are_equal(const T & _v1, const T2 & _v2)
{
	return _v1 == _v2;
}

template<typename T, typename T2, typename... Args>
bool are_equal(const T & _v1, const T2 & _v2, Args... args)
{
	return _v1 == _v2 && are_equal(_v1, args...);
}

struct foo
{
	foo(int _bar){ m_bar = _bar; }
	bool operator==(int _rhs){ return m_bar == _rhs; }	
	int m_bar;
};

bool operator==(int _lhs, const foo & _rhs){ return _rhs.m_bar == _lhs; }
	
int main()
{	
	foo myFoo(2);
	cout << are_equal(1, 1, 1, myFoo, 2, 4, 1) << endl;
	cout << are_equal(2, 2, 2, 2) << endl;
	cout << are_equal(2.0f, 2, myFoo, 2.0) << endl;
	return 0;
}
Thank you guys for your help.
Also you should edit checkBoard so that is general for BOTH players. For example change this
bool CheckBoardForO(char x[]){
to this
bool CheckBoard(char x[], char p){
Where p is player to checked for... Implementing should be easy.
Topic archived. No new replies allowed.