Tic Tac Toe Problem in code

Write your question here.
Hi,i am trying to create Tic Tac Toe game for my scholl,but something in my code is bad,and dont know what.Please help my.
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
 #include <iostream>
#include <string>
using namespace std;
class TicTacToe
{
private:
	char table[3][3]; 
public: TicTacToe() {}
void setTable() 
{
	int n = 1;
	for(int i = 0; i < 3; i++)
	{ 
		for(int j = 0; j < 3; j++)
		{ table[i][j] = '0' + n;
		n++; } } }


void printTable()
{ 
	for(int i = 0; i < 3; i++)
	{ for(int j = 0; j < 3; j++) 
	if(j < 2) { cout << table[i][j] << "|"; }
	else { cout << table[i][j] << endl; }
	if(i < 2) { cout << "-+-+-\n"; } } } 
void playerMove(char num, char player)
{
	bool wrongMove = true;
	 for(int i = 0; i < 3; i++)
	 { for(int j = 0; j < 3; j++) 
	 {
		 if(table[i][j] == num)
		 {
			 table[i][j] = player; wrongMove = false; } } }
	 if(wrongMove == true) { cout << "Wrong move!\n"; } } 
bool checkWinner(char player, bool gameOver) {
	for(int i = 0; i < 3; i++)
		if(table[i][0] == table[i][1] && table[i][1] == table[i][2]) gameOver = true;
	for(int i = 0; i < 3; i++)
		if(table[0][i] == table[1][i] && table[1][i] == table[2][i]) gameOver = true;
	if(table[0][0] == table[1][1] && table[1][1] == table[2][2]) gameOver=true;
	if(table[0][2] == table[1][1] && table[1][1] == table[2][0]) gameOver = true;
	if(gameOver == true) { cout << "Player " << player << " wins!\n\n"; }
	return gameOver; }
bool checkDraw(bool gameOver)
{ 
	int n = 1, count = 0; 
	for(int i = 0; i < 3; i++) { 
		
	for(int j = 0; j < 3; j++) 
	{ 
		 if(table[i][j] == '0'+n) { count++; } n++; }

}
	if(count < 1) 
	{
		cout << "It's a draw!\n\n";
		gameOver = true; }
	return gameOver; 
};   
int main()
{ bool done = false, gameOver = false; 
char player = 'O', num; 
TicTacToe myGame;
myGame.setTable(); do { 
	if(player == 'X') 
	{
		player = 'O'; 
	} 
	else { player = 'X';
	}
	myGame.printTable();
		cout << "Player \"" << player << "\" turn or (q) to quit> ";
		cin >> num;
		cout << "\n";
		if(num == 'q')
		{
			cout << "Goodbye!...\n"; 
			break; } 
		myGame.playerMove(num, player);
		gameOver = myGame.checkWinner(player, gameOver); 
		gameOver = myGame.checkDraw(gameOver); 
		if(gameOver == true)
		{	
			myGame.setTable(); 
			gameOver = false; } }
while(!done);  
system("pause");
return 0; 
Line 59, you're missing a } to terminate the checkDraw function.
The }; at line 60 terminates your class.

Your use of indentation and alignment of {} is very inconsistent. A consistent style of matching { with } should have made a missing } easy to spot.
Thanks,but my game still don't work.
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
#include <iostream>
#include <string>
#include "stdafx.h"
using namespace std;
class TicTacToe
{
private:
	char table[3][3]; 
public: TicTacToe() {}
void setTable() 
{
	int n = 1;
	for(int i = 0; i < 3; i++)
	{ 
		for(int j = 0; j < 3; j++)
		{ table[i][j] = '0' + n;
		n++; } } }


void printTable()
{ 
	for(int i = 0; i < 3; i++)
	{ for(int j = 0; j < 3; j++) 
	if(j < 2) { cout << table[i][j] << "|"; }
	else { cout << table[i][j] << endl; }
	if(i < 2) { cout << "-+-+-\n"; } } } 
void playerMove(char num, char player)
{
	bool wrongMove = true;
	 for(int i = 0; i < 3; i++)
	 { for(int j = 0; j < 3; j++) 
	 {
		 if(table[i][j] == num)
		 {
			 table[i][j] = player; wrongMove = false; } } }
	 if(wrongMove == true) { cout << "Wrong move!\n"; } } 
bool checkWinner(char player, bool gameOver) {
	for(int i = 0; i < 3; i++)
		if(table[i][0] == table[i][1] && table[i][1] == table[i][2]) gameOver = true;
	for(int i = 0; i < 3; i++)
		if(table[0][i] == table[1][i] && table[1][i] == table[2][i]) gameOver = true;
	if(table[0][0] == table[1][1] && table[1][1] == table[2][2]) gameOver=true;
	if(table[0][2] == table[1][1] && table[1][1] == table[2][0]) gameOver = true;
	if(gameOver == true) { cout << "Player " << player << " wins!\n\n"; }
	return gameOver; }
bool checkDraw(bool gameOver)
{ 
	int n = 1, count = 0; 
	for(int i = 0; i < 3; i++) { 
		
	for(int j = 0; j < 3; j++) 
	{ 
		 if(table[i][j] == '0'+n) { count++; } n++; }

}
	if(count < 1) 
	{
		cout << "It's a draw!\n\n";
		gameOver = true; }
	return gameOver; }
};

int main()
{ bool done = false, gameOver = false; 
char player = 'O', num; 
TicTacToe myGame;
myGame.setTable(); do { 
	if(player == 'X') 
	{
		player = 'O'; 
	} 
	else { player = 'X';
	}
	myGame.printTable();
		cout << "Player \"" << player << "\" turn or (q) to quit> ";
		cin >> num;
		cout << "\n";
		if(num == 'q')
		{
			cout << "Goodbye!...\n"; 
			break; } 
		myGame.playerMove(num, player);
		gameOver = myGame.checkWinner(player, gameOver); 
		gameOver = myGame.checkDraw(gameOver); 
		if(gameOver == true)
		{	
			myGame.setTable(); 
			gameOver = false; } }
while(!done);  
system("pause");
return 0; 
};
What exactly doesn't work?
I played one game and it appeared to work.
I have Visual Studio 2010 and it will not start.I have errors like:

1> Add directive to 'StdAfx.h' or rebuild precompiled header
1>c:\users\pc\documents\visual studio 2010\projects\pajdicka\pajdicka\tictactoe.cpp(2): warning C4627: '#include <string>': skipped when looking for precompiled header use
1> Add directive to 'StdAfx.h' or rebuild precompiled header
1>c:\users\pc\documents\visual studio 2010\projects\pajdicka\pajdicka\tictactoe.cpp(24): error C2065: 'cout' : undeclared identifier
1>c:\users\pc\documents\visual studio 2010\projects\pajdicka\pajdicka\tictactoe.cpp(25): error C2065: 'cout' : undeclared identifier
1>c:\users\pc\documents\visual studio 2010\projects\pajdicka\pajdicka\tictactoe.cpp(25): error C2065: 'endl' : undeclared identifier
1>c:\users\pc\documents\visual studio 2010\projects\pajdicka\pajdicka\tictactoe.cpp(26): error C2065: 'cout' : undeclared identifier
1>c:\users\pc\documents\visual studio 2010\projects\pajdicka\pajdicka\tictactoe.cpp(36): error C2065: 'cout' : undeclared identifier
1>c:\users\pc\documents\visual studio 2010\projects\pajdicka\pajdicka\tictactoe.cpp(44): error C2065: 'cout' : undeclared identifier
1>c:\users\pc\documents\visual studio 2010\projects\pajdicka\pajdicka\tictactoe.cpp(58): error C2065: 'cout' : undeclared identifier
1>c:\users\pc\documents\visual studio 2010\projects\pajdicka\pajdicka\tictactoe.cpp(75): error C2065: 'cout' : undeclared identifier
1>c:\users\pc\documents\visual studio 2010\projects\pajdicka\pajdicka\tictactoe.cpp(76): error C2065: 'cin' : undeclared identifier
1>c:\users\pc\documents\visual studio 2010\projects\pajdicka\pajdicka\tictactoe.cpp(77): error C2065: 'cout' : undeclared identifier
1>c:\users\pc\documents\visual studio 2010\projects\pajdicka\pajdicka\tictactoe.cpp(80): error C2065: 'cout' : undeclared identifier
1>c:\users\pc\documents\visual studio 2010\projects\pajdicka\pajdicka\tictactoe.cpp(90): error C3861: 'system': identifier not found
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.81
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
1> Add directive to 'StdAfx.h' or rebuild precompiled header
You have precompiled headers enabled. You can either disable them or #include "stdafx.h" Actually on your second set of code you added the header. Though it needs to be first as it is precompiled. So put it on line one.
Last edited on
Now it's work,Thanks!
how to play?

I can run the program, but how to play the game?
You enter a number from 1-9. Anyways the op should change it so that when it is a "wrong move" it doesn't change player.
Thanks. It works now. I can play the game now.
Topic archived. No new replies allowed.