Tic Tac Toe Problem- Code Errors

MY CODE

Hey all, this is my first post on this forum. I have been trolling around for a while though. Anyways, I was wondering if anybody could correct my code or point me in the right direction. My code below results in the errors listed after my code. Any help would be appreciated.
Thankyou


//Include the following libraries:
#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <cstdlib>
#include <ctime>

//Using the standard namespace
using namespace std;

class TicTacToe
{

private:
char TheBoard [3][3]; //Variable
int board [3][3];

public:
TicTacToe(void);
void PlayOne (void);
void SwitchPlayer (char &);
void ShowBoard (void);
void PostMove (int, int, char);
char Winner (void);
};

int _tmain(int argc, _TCHAR* argv[])
{

TicTacToe Game1;
Game1.PlayOne ();

//Explain to user what this program will do, and prompt for first move.
cout<<"This program plays Tic-Tac-Toe, and determines whether the game has concluded."<<endl;
cout<<"Please select a location for your first X."<<endl;


//Instruct the console to remain open
_getch();
return 0;


}

//Beginning of new Void Fuction TicTacToe, which is the fuction at the heart of the tic-tac-toe game.
void TicTacToe ::PlayOne (void)
{

//Declare the following local variables within fuction TicTacToe
const int MaxMoves=9;
char CurrentPlayer='O';
int row= 0;
int clmn= 0;
char TheWinner= ' ';
int NumMoves= 0;

//Utilizing the do while repitition structure
do {
SwitchPlayer (CurrentPlayer);

ShowBoard ();

//Prompt the user to input their moves and input into the console via CIN
cout<<"\n\n Player "<<CurrentPlayer<<endl;
cout<<"Enter Your Row (0, 1, 2): ";
cin>>row;
cout<<"Enter Your Column (0, 1, 2): ";
cin>>clmn;

//Post the Move To The Board
PostMove (row, clmn, CurrentPlayer);

//Determine if the game has a winner yet
TheWinner=Winner ();

//Keep Track of the Number of Moves
NumMoves++;
}
while ((Winner=='D')&&(NumMoves < MaxMoves));

//Shows the Ending Board
ShowBoard ();

if (Winner != 'D')
cout<<"\n\nThe Winner is Player: "<<Winner;
else
cout<<"\n\nThe Game Was a Draw: "<<endl;

}

//Initialize the array contents
TicTacToe::TicTacToe (void)
{
TheBoard [3][3]=' ';
}

{
//Switches The Current Plater
void TicTacToe::SwitchPlayer(char ¤tPlayer){


CurrentPlayer= ' ';

if (CurrentPlayer =='X')

cout << "\nPlayer 1: Make your move " ;

CurrentPlayer = 1;
}
else
(CurrentPlayer == 'X');

}

//Displays the Board to the user
void TicTacToe::ShowBoard ();
{
cout<< "\n|"<<TheBoard [0][0]<<"|"<<TheBoard [0][1]<<"|"<<TheBoard [0][2]<<endl;
cout << "--------------------" << endl;
cout << "|" << TheBoard[1][0] << "|" << TheBoard[1][1] << "|" << TheBoard[1][2] << endl;
cout << "--------------------" << endl;
cout << "|" << TheBoard[2][0] << "|" << TheBoard[2][1] << "|" << TheBoard[2][2] << endl;
}

//Accepts move from the user and posts to the board
void TicTacToe::postMove(int row, int clmn, char value)
{
char CurrentPlayer = ' ';
if (TheBoard[row][clmn]== 1|| TheBoard[row][clmn]==2)
{
cout << "Space already taken. Please choose another. " << endl;
}
else
{
TheBoard[row][clmn]=CurrentPlayer;
}
for(row = 0; row <3; row ++)
{
if(row > 3)
{
cout << "Invalid choice" << endl;
}
for(clmn = 0; clmn <3; clmn++)
{
if(clmn > 3)
{
cout << "Invalid choice" << endl;
}

if(TheBoard[row][clmn]==0)
{
TheBoard[row][clmn]=' ';
}
if (TheBoard[row][clmn]== 1)
{
TheBoard[row][clmn] = 'X';
}
if (TheBoard[row][clmn]== 2)
{
TheBoard[row][clmn] = 'O';
}
}
}
}

//Analyzes the board to determine if there is a winner, and returns X, O, or D (Draw)
char TicTacToe::determineWinner(void)
{

//Checks the rows
for (int i = 0; i < 3; i++)
{
if (TheBoard[i][0] == TheBoard[i][1]
&& TheBoard[i][1] == TheBoard[i][2]
&& TheBoard[i][0] != ' ')
{
return TheBoard[i][0];
}
}

//Checks the Columns
for (int i = 0; i < 3; i++)
{
if (TheBoard[0][i] == TheBoard[1][i]
&& TheBoard[1][i] == TheBoard[2][i]
&& TheBoard[0][i] != ' ')
{
return TheBoard[0][i];
}
}

//Check to see if diagonal moves indicate a winner
if (TheBoard[0][0] == TheBoard[1][1]
&& TheBoard[1][1] == TheBoard[2][2]
&& TheBoard[0][0] != ' ')
{
return TheBoard[0][0];
}
if (TheBoard[2][0] == TheBoard[1][1]
&& TheBoard[1][1] == TheBoard[0][2]
&& TheBoard[2][0] != ' ')
{
return TheBoard[2][0];
}

return 'D';
}






MY LIST OF ERRORS PER MICROSOFT VISUAL COMPILER

1>------ Build started: Project: Final_Project, Configuration: Debug Win32 -----
1> Final_Project.cpp
1>e:\final_project\final_project\final_project.cpp(88): error C3867: 'TicTacToe::Winner': function call missing argument list; use '&TicTacToe::Winner' to create a pointer to member
1>e:\final_project\final_project\final_project.cpp(88): error C2446: '==' : no conversion from 'int' to 'char (__thiscall TicTacToe::* )(void)'
1> There are no conversions from integral values to pointer-to-member values
1>e:(88): error C2040: '==' : 'char (__thiscall TicTacToe::* )(void)' differs in levels of indirection from 'int'
1>e:(93): error C3867: 'TicTacToe::Winner': function call missing argument list; use '&TicTacToe::Winner' to create a pointer to member
1>e:(93): error C2446: '!=' : no conversion from 'int' to 'char (__thiscall TicTacToe::* )(void)'
1> There are no conversions from integral values to pointer-to-member values
1>e:(93): error C2040: '!=' : 'char (__thiscall TicTacToe::* )(void)' differs in levels of indirection from 'int'
1>e:(94): error C3867: 'TicTacToe::Winner': function call missing argument list; use '&TicTacToe::Winner' to create a pointer to member
1>e:(94): error C3867: 'TicTacToe::Winner': function call missing argument list; use '&TicTacToe::Winner' to create a pointer to member
1>e:(106): error C2447: '{' : missing function header (old-style formal list?)
1>e:(125): error C2761: 'void TicTacToe::ShowBoard(void)' : member function redeclaration not allowed
1>e:(126): error C2447: '{' : missing function header (old-style formal list?)
1>e:(135): error C2039: 'postMove' : is not a member of 'TicTacToe'
1>e:(18) : see declaration of 'TicTacToe'
1>e:(138): error C2065: 'TheBoard' : undeclared identifier
1>e:(138): error C2065: 'TheBoard' : undeclared identifier
1>e:(144): error C2065: 'TheBoard' : undeclared identifier
1>e:(159): error C2065: 'TheBoard' : undeclared identifier
1>e:(161): error C2065: 'TheBoard' : undeclared identifier
1>e:(163): error C2065: 'TheBoard' : undeclared identifier
1>(165): error C2065: 'TheBoard' : undeclared identifier
1>e:(167): error C2065: 'TheBoard' : undeclared identifier
1>e:(169): error C2065: 'TheBoard' : undeclared identifier
1>e:(176): error C2039: 'determineWinner' : is not a member of 'TicTacToe'
1>(18) : see declaration of 'TicTacToe'
1>e:(182): error C2065: 'TheBoard' : undeclared identifier
1>e:(182): error C2065: 'TheBoard' : undeclared identifier
1>(183): error C2065: 'TheBoard' : undeclared identifier
1>(183): error C2065: 'TheBoard' : undeclared identifier
1>e:(184): error C2065: 'TheBoard' : undeclared identifier
1>e:(186): error C2065: 'TheBoard' : undeclared identifier
1>e:(193): error C2065: 'TheBoard' : undeclared identifier
1>e:(194): error C2065: 'TheBoard' : undeclared identifier
1>e:(195): error C2065: 'TheBoard' : undeclared identifier
1>e:(197): error C2065: 'TheBoard' : undeclared identifier
1>e:(202): error C2065: 'TheBoard' : undeclared identifier
1>e:(203): error C2065: 'TheBoard' : undeclared identifier
1>(204): error C2065: 'TheBoard' : undeclared identifier
1>e:(206): error C2065: 'TheBoard' : undeclared identifier
1>e:(208): error C2065: 'TheBoard' : undeclared identifier
1>(208): error C2065: 'TheBoard' : undeclared identifier
1>(209): error C2065: 'TheBoard' : undeclared identifier
1>e:(209): error C2065: 'TheBoard' : undeclared identifier
1>e:(210): error C2065: 'TheBoard' : undeclared identifier
1>e:(212): error C2065: 'TheBoard' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Rewrite it from scratch, every time you add a line of code run and test it, then fix any errors that come up because of that line of code.
dont listen to zephilinox, when you get this many errors, there is ALWAYS a quick fix that doesnt require you re-write

your issue is that you did not create a declaration inside the class for determineWinner so it is not recognized as a class method, ie:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class TicTacToe 
{

private:
    char TheBoard [3][3]; //Variable
    int board [3][3];

public:
    TicTacToe(void);
    void PlayOne (void);
    void SwitchPlayer (char &);
    void ShowBoard (void);
    void PostMove (int, int, char);
    char Winner (void);
};


->

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class TicTacToe 
{

private:
    char TheBoard [3][3]; //Variable
    int board [3][3];

public:
    TicTacToe();
    void PlayOne ();
    void SwitchPlayer (char &);
    void ShowBoard ();
    void PostMove (int, int, char);
    char Winner ();
    //char determineWinner (); <- need to add this function or change the name to Winner
};


That should fix all of the "error C2065: 'TheBoard' : undeclared identifier" errors, after you fix that post the errors you have left

also use [code] tags or i wont reply next time, its hard as hell to read that code
Last edited on
This is the class implementation file for the Tic Tac Toe i did, maybe this will help you.
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <iostream>
#include "TicTacToe.h"

//Return Type: void
//Parameters: int for player
//Allows the player to place an x or an o

void TicTacToe::ChoosePosition(int player)
{
	int x,y;
	int choice=0;

	while(choice>9 || choice<1)
	{
	cout << "Choose a position: ";
	cin >> choice;

	//set Co-ords
	if(choice==1)
	{x=0;
	y=0;}
	if(choice==2)
	{x=1;
	y=0;}
	if(choice==3)
	{x=2;
	y=0;}
	if(choice==4)
	{x=0;
	y=1;}
	if(choice==5)
	{x=1;
	y=1;}
	if(choice==6)
	{x=2;
	y=1;}
	if(choice==7)
	{x=0;
	y=2;}
	if(choice==8)
	{x=1;
	y=2;}
	if(choice==9)
	{x=2;
	y=2;}
	if(choice>9 || choice<1)
	{x=0;
	y=0;}

	if (choice>9 || choice<1 || board[y][x]=='X' || board[y][x]=='O')
		{cout << "Invalid choice"
			<< endl;
		//Make sure loop continues if occupied square was selected
		choice=10;}
	}

	board[y][x]=players[player].symbol;
}

//Return Type: integer for winner
//Parameters: none
//Checks the board to see who won

int TicTacToe::CheckForWinner()
{
	char line[3];
	char winner='t';
	int win;
	
	//Left to right diagonal
	if(board[0][0]==board[1][1] && board[1][1]==board[2][2])
		winner=board[0][0];
	//Right to left diagonal
	if(board[2][0]==board[1][1] && board[1][1]==board[0][2])
		winner=board[0][0];

	//Vertical
	for (int i = 0; i<3; i++)
		if(board[0][i]==board[1][i] && board[1][i]==board[2][i])
			winner=board[0][i];
	//Horizontal
	for (int i = 0; i<3; i++)
		if(board[i][0]==board[i][1] && board[i][1]==board[i][2])
			winner=board[i][0];

	if(winner=='t')
		win = 0; //tie

	else if(winner=='X')
		win = 1; //player 1
	else //(winner=='O')
		win = 2; //player 2

	return win;
}


//Return Type: void
//Parameters: none
//Displays score on screen
void TicTacToe::DisplayScore()
{
cout << players[0].name<<"\t"<< players[1].name << "\t"<<"Tie"
	<< endl << "_____________________________" << endl
	<< players[0].CurrentScore << "\t" << players[1].CurrentScore
	<< "\t" << ties << endl << endl;
}


//Default Constructor

TicTacToe::TicTacToe()

{

  players[0].name = "John Doe";

  players[0].symbol = 'X';

  players[1].name = "Kevin James";

  players[1].symbol = 'O';

  players[0].CurrentScore = 0;

  players[1].CurrentScore = 0;

  currentWinner = 0;

  ties = 0;

}


//Return Type: Void
//Parameters: none
//Resets Board

void TicTacToe::Reset()

{	

	  board[0][0] = '1';
	  board[0][1] = '2';
	  board[0][2] = '3';
	  board[1][0] = '4';
	  board[1][1] = '5';
	  board[1][2] = '6';
	  board[2][0] = '7';
	  board[2][1] = '8';
	  board[2][2] = '9';



}


//Return Type: Voidif(board[0][i]==board[1][i] && board[1][i]==board[2][i])
//Parameters: none
//Displays board on screen

void TicTacToe::DisplayBoard()

{

  for(int x = 0; x < 3; x++)

    {

      cout << endl;

      cout << "|";

      for(int y = 0; y < 3; y++)

	{

	  cout << "_";

	  cout << board[x][y];

	}

    }
	cout << endl;





}


//Return Type: Void
//Parameters: string for name, int for player
//Sets Name
void TicTacToe::SetName(string name, int player)
{
	players[player].name = name;
}



//Return Type: String
//Parameters: int for player
//Returns name of specified player

string TicTacToe::GetName(int player)
{
	return players[player].name;
}


//Return Type: Void
//Parameters: none
//Adds a tie

void TicTacToe::TieGame()
{
ties++;
}

//Return Type: Void
//Parameters: int for player
//Adds a win for the given player

void TicTacToe::AddWin(int player)
{
players[player].CurrentScore++;
}
Hey all thanks for the replies. I decided to go get rid of the classes and utilize global and local variables to simplify the code a bit.

One issue I am still having is what functions to call within main, and initializing the array within the alloted space.

I figured out how to use the code tags also, thanks guys.

My code is listed below.


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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//Using the standard namespace
using namespace std;

//Declare the following variables
	char TheBoard [3][3]; 
	int board [3][3];
	void PlayOne ();
	void SwitchPlayer (char &);
	void ShowBoard ();
	void PostMove (int, int, char);
	char Winner ();
	char determineWinner ();
	char CurrentPlayer='O';

//Beginning of new Void Fuction TicTacToe, which is the fuction at the heart of the tic-tac-toe game.
void PlayOne ()
{

//Declare the following local variables 
	const int MaxMoves=9;
	int row= 0;
	int clmn= 0;
	char TheWinner= ' ';
	int NumMoves= 0;

	//Utilizing the do while repitition structure
	do 
	{
		SwitchPlayer (CurrentPlayer);

		ShowBoard ();

		//Prompt the user to input their moves and input into the console via CIN
		cout<<"\n\n Player "<<CurrentPlayer<<endl;
		cout<<"Enter Your Row (0, 1, 2): ";
		cin>>row;
		cout<<"Enter Your Column (0, 1, 2): ";
		cin>>clmn;

		//Post the Move To The Board
		PostMove (row, clmn, CurrentPlayer);

		//Determine if the game has a winner yet
		TheWinner=Winner() ;

		//Keep Track of the Number of Moves
		NumMoves++;
	}
	while ((Winner()=='D')&&(NumMoves < MaxMoves));

	//Shows the Ending Board
	ShowBoard ();

	if (Winner() != 'D')
		cout<<"\n\nThe Winner is Player: "<<Winner;
	else 
		cout<<"\n\nThe Game Was a Draw: "<<endl;

}

//Initialize the array contents
void	TicTacToe ()
{
	TheBoard [3][3]=' ';
}


    //Switches The Current Plater
void SwitchPlayer(char &tPlayer)
{
		     
	    CurrentPlayer= ' ';
	         
	    if (CurrentPlayer =='X')
		{
        cout << "\nPlayer 1: Make your move " ;

       CurrentPlayer = 1;
	    }
    else
      (CurrentPlayer == 'X');
        
}

//Displays the Board to the user
void ShowBoard ()
{
	cout<< "\n|"<<TheBoard [0][0]<<"|"<<TheBoard [0][1]<<"|"<<TheBoard [0][2]<<endl;
	cout << "--------------------" << endl;
	cout << "|" << TheBoard[1][0] << "|" << TheBoard[1][1] << "|" << TheBoard[1][2] << endl;
	cout << "--------------------" << endl;
	cout << "|" << TheBoard[2][0] << "|" << TheBoard[2][1] << "|" << TheBoard[2][2] << endl;
 }

//Accepts move from the user and posts to the board
void postMove(int row, int clmn, char value)
{
	char CurrentPlayer = ' ';
	if (TheBoard[row][clmn]== 1|| TheBoard[row][clmn]==2)
{
	cout << "Space already taken. Please choose another. " << endl;
}
	else
{
	TheBoard[row][clmn]=CurrentPlayer;
}
	for(row = 0; row <3; row ++)
{
	if(row > 3)
{
	cout << "Invalid choice" << endl;
}
	for(clmn = 0; clmn <3; clmn++)
{
	if(clmn > 3)
	{
		cout << "Invalid choice" << endl;
	}
	
	if(TheBoard[row][clmn]==0)
	{
		TheBoard[row][clmn]=' ';
	}
	if (TheBoard[row][clmn]== 1)
	{
		TheBoard[row][clmn] = 'X';
	}
	if (TheBoard[row][clmn]== 2)
	{
		TheBoard[row][clmn] = 'O';
	}
 }
}
}

//Analyzes the board to determine if there is a winner, and returns X, O, or D (Draw)
char determineWinner()
{

//Checks the rows
	for (int i = 0; i < 3; i++)
	{
		if (TheBoard[i][0] == TheBoard[i][1] 
		&& TheBoard[i][1] == TheBoard[i][2]
		&& TheBoard[i][0] != ' ')
			{
				return TheBoard[i][0];
			}
	}

//Checks the Columns
	for (int i = 0; i < 3; i++)
	{
		if (TheBoard[0][i] == TheBoard[1][i]
			&& TheBoard[1][i] == TheBoard[2][i]
			&& TheBoard[0][i] != ' ')
			{
				return TheBoard[0][i];
			}
	}

//Check to see if diagonal moves indicate a winner
	if (TheBoard[0][0] == TheBoard[1][1]
		&& TheBoard[1][1] == TheBoard[2][2]
		&& TheBoard[0][0] != ' ')
		{
			return TheBoard[0][0];
		}
	if (TheBoard[2][0] == TheBoard[1][1]
		&& TheBoard[1][1] == TheBoard[0][2]
		&& TheBoard[2][0] != ' ')
		{
			return TheBoard[2][0];
		}

	return 'D';
}

int _tmain(int argc, _TCHAR* argv[])
{

//Call the following functions to run
PlayOne ();

//Explain to user what this program will do, and prompt for first move.
cout<<"This program plays Tic-Tac-Toe, and determines whether the game has concluded."<<endl;
cout<<"Please select a location for your first X."<<endl;


//Instruct the console to remain open
	_getch();
	return 0;


}


When run, I encounter the following 3 errors :

Error1 error LNK2019: unresolved external symbol "char __cdecl Winner(void)" (?Winner@@YADXZ) referenced in function "void __cdecl PlayOne(void)" (?PlayOne@@YAXXZ) E:\Final_Project\Final_Project\Final_Project.obj Final_Project

Error 2 error LNK2019: unresolved external symbol "void __cdecl PostMove(int,int,char)" (?PostMove@@YAXHHD@Z) referenced in function "void __cdecl PlayOne(void)" (?PlayOne@@YAXXZ) E:\Final_Project\Final_Project\Final_Project.obj Final_Project

Error 3 error LNK1120: 2 unresolved externals E:\Final_Project\Debug\Final_Project.exe 1 1 Final_Project


Thankyou for all the help.

-Jeff
Topic archived. No new replies allowed.