Hi. I was wondering if someone could help me with this program.. the purpose of this program is to play tic tac toe, and I'm having difficulty getting it to compile. I have gone through it several times. I would most appericate it if anyone could look at it and tell what I am doing wrong.. :)

here is the program of what i have so far:

#include <cmath>
#include <iostream>
#include <fstream>
#include <cctype>
#include <conio.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <time.h>
using namespace std;

char p1=' ', p2=' ',p3=' ',p4=' ',p5=' ',p6=' ',p7=' ',p8=' ',p9=' ';
//ofstream out;
//out.open ("data.txt");

void DISPLAY_BOARD();
void Play ();
void CLEAR_BOARD ();
void COMPUTER_MOVE(int guess);
void HUMAN_MOVE(int guess);
bool Check_WIN ();
bool Check (int guess);

void main ()

{
cout<<"Tic Tac Toe"<<endl;
//out<<"Tic Tac Toe"<<endl;

cout<<" 1 | 2 | 3 "<<endl;
cout<<"- - - - - -"<<endl;
cout<<" 4 | 5 | 6 "<<endl;
cout<<"- - - - - -"<<endl;
cout<<" 7 | 8 | 9 "<<endl;


/*out<<" 1 | 2 | 3 "<<endl;
out<<"- - - - - -"<<endl;
out<<" 4 | 5 | 6 "<<endl;
out<<"- - - - - -"<<endl;
out<<" 7 | 8 | 9 "<<endl;
*/
srand (time(unsigned));
Play();
}

void DISPLAY_BOARD()

{

cout<<" "<<p1<<" | "<<p2<<" | "<<p3<<endl;
cout<<"- - - - - -"<<endl;
cout<<" "<<p4<<" | "<<p5<<" | "<<p6<<endl;
cout<<"- - - - - -"<<endl;
cout<<" "<<p7<<" | "<<p8<<" | "<<p9<<endl;

/*out<<" "<<p1<<" | "<<p2<<" | "<<p3<<endl;
out<<"- - - - - -"<<endl;
out<<" "<<p4<<" | "<<p5<<" | "<<p6<<endl;
out<<"- - - - - -"<<endl;
out<<" "<<p7<<" | "<<p8<<" | "<<p9<<endl;
*/
}

void Play()
{
	int guess=0;
	int counter=0;
	char again='\0';
	
	while (counter<9)
	{
		cout<<"Human:";
		cin>>guess;
		while (Check (guess))
		{
			cout<<"Already been played. Please Try again.";
			cin>>guess;
		}
		HUMAN_MOVE(guess);
		DISPLAY_BOARD();
		if (Check_WIN ())
		{
			cout<<"Human Wins!!"<<endl;
//			out<<"Human Wins!!"<<endl;
			break;
		}
		counter++;	
		if (counter>8)
		{
			cout<<"Tie Game!"<<endl;
//			out<<"Tie Game!"<<endl;
			break;
		}
		guess=(rand()%9)+1;
			while (Check (guess));
		{
			guess= (rand()%9)+1;
		}
		cout<<"Computer: "<<guess<<endl;
		COMPUTER_MOVE(guess);	
		DISPLAY_BOARD();
		if (Check_WIN())
		{
			cout<<"Computer Wins!!"<<endl;
//			out<<"Computer Wins!!"<<endl;
			break;
		}
		counter++;
		cout<<"c= "<<counter<<endl;
		if (counter>8)
		{
			cout<<"Tie Game!"<<endl;
//			out<<"Tie Game!"<<endl;
			break;
		}
	}
	cout<<"Would you like to play again? (y/n): ";
	cin>>again;
	if (again=='y'||again=='Y')
	{
		CLEAR_BOARD();
		DISPLAY_BOARD();
		Play();
	}
	else
	{
		cout<<"Game Over"<<endl;
			getch();
	}
}

void HUMAN_MOVE(int guess)
{

switch (guess)
{
case 1: p1 = 'x'; break;
case 2: p2 = 'x'; break;
case 3: p3 = 'x'; break;
case 4: p4 = 'x'; break;
case 5: p5 = 'x'; break;
case 6: p6 = 'x'; break;
case 7: p7 = 'x'; break;
case 8: p8 = 'x'; break;
case 9: p9 = 'x'; break;
}	
}

void COMPUTER_MOVE(int guess)
{
	{switch (guess)
{
case 1: p1 = 'o'; break;
case 2: p2 = 'o'; break;
case 3: p3 = 'o'; break;
case 4: p4 = 'o'; break;
case 5: p5 = 'o'; break;
case 6: p6 = 'o'; break;
case 7: p7 = 'o'; break;
case 8: p8 = 'o'; break;
case 9: p9 = 'o'; break;
}
}

bool Check (int guess)
{

switch (guess)
{
case 1: if (p1 != ' ') return true; 
		else {return false;} break;
case 2: if (p2 != ' ') return true; 
		else {return false;} break;
case 3: if (p3 != ' ') return true;
		else {return false;} break;
case 4: if (p4 != ' ') return true; 
		else {return false;} break;
case 5: if (p5 != ' ') return true; 
		else {return false;} break;
case 6: if (p6 != ' ') return true; 
		else {return false;} break;
case 7: if (p7 != ' ') return true; 
		else {return false;} break;
case 8: if (p8 != ' ') return true; 
		else {return false;} break;
case 9: if (p9 != ' ') {return true;} 
		else {return false;} break;
}

}
bool Check_WIN ()
{

	if ((p1=='x'&&p4=='x'&&p7=='x')||(p1=='o'&&p4=='o'&&p7=='o'))		return true;
	else if ((p2=='x'&&p5=='x'&&p8=='x')||(p2=='o'&&p5=='o'&&p8=='o'))	return true;
	else if ((p3=='x'&&p6=='x'&&p9=='x')||(p3=='o'&&p6=='o'&&p9=='o'))	return true;
	else if ((p1=='x'&&p5=='x'&&p9=='x')||(p1=='o'&&p5=='o'&&p9=='o'))	return true;
	else if ((p3=='x'&&p5=='x'&&p7=='x')||(p3=='o'&&p5=='o'&&p7=='o'))	return true;
	else if ((p4=='x'&&p5=='x'&&p6=='x')||(p4=='o'&&p5=='o'&&p6=='o'))	return true;
	else if ((p7=='x'&&p8=='x'&&p9=='x')||(p7=='o'&&p8=='o'&&p9=='o'))	return true;
	else if ((p1=='x'&&p2=='x'&&p3=='x')||(p1=='o'&&p2=='o'&&p3=='o'))	return true;

	else return(false);

}

void CLEAR_BOARD()
{
	p1=' ', p2=' ',p3=' ',p4=' ',p5=' ',p6=' ',p7=' ',p8=' ',p9=' ';
}

//------End of Program------

Recommended Answers

All 13 Replies

A. Indent
B. Are you up to 2D arrays yet?
C. Use of global variables is generally frowned upon - that's where a 2D array would help in being a single item to pass as parameter to functions.
D. If it doesn't compile, what error message do you get? Where does it point to?

Val

Your program has some mismatched { and } bracket. Proper code alignment should show these up right away. The function at line 150 has this problem.

line 43 is syntax error. You did not specify a parameter for the time() function.

Yes, but this program was given to us before he taught Arrays.. and the error messages i have been getting were about Check and Check_WIN Function.. oh well..
And Thank You Ancient Dragon!!

You have many libraries included that are not needed/used. You are missing two libraries that you do need for the randomizing functions.
<time.h> should be <ctime>
<stdio.h> should be <cstdio>

Look again at all your error messages - You should have several not related to Check and Check_win.

I redid it with the change you have given to me. It can compile, but will not build. Here is the error: ( error PRJ0003 : Error spawning 'cmd.exe'.)
and I had at least 2 other computer people look over this and they could not figure out the problem as well..

here the full program once again with many changes since yesturday..

#include <cmath>
#include <iostream>
#include <fstream>
#include <cctype>
#include <conio.h>
#include <iomanip>
#include <cstdio>
#include <string>
#include <ctime>

using namespace std;

void DISPLAY_BOARD();
void Play ();
void clear_board ();
void COMPUTER_MOVE(int guess);
void HUMAN_MOVE(int guess);
bool check_win ();
bool checkPlayed(int guess);

char p1=' ', p2=' ',p3=' ',p4=' ',p5=' ',p6=' ',p7=' ',p8=' ',p9=' ';
int couter = 0;

void main ()

{
ofstream fout;
fout.open ("game.txt");
cout<<"Tic Tac Toe"<<endl;

cout<<" 1 | 2 | 3 "<<endl;
cout<<"- - - - - -"<<endl;
cout<<" 4 | 5 | 6 "<<endl;
cout<<"- - - - - -"<<endl;
cout<<" 7 | 8 | 9 "<<endl<<endl;

fout<<"Tic Tac Toe"<<endl;
fout<<" 1 | 2 | 3 "<<endl;
fout<<"- - - - - -"<<endl;
fout<<" 4 | 5 | 6 "<<endl;
fout<<"- - - - - -"<<endl;
fout<<" 7 | 8 | 9 "<<endl<<endl;

//void srand (time(unsigned));
Play();
}

void DISPLAY_BOARD()

{
ofstream fout;
fout.open ("game.txt", ios::app);
cout<<" "<<p1<<" | "<<p2<<" | "<<p3<<endl;
cout<<"- - - - - -"<<endl;
cout<<" "<<p4<<" | "<<p5<<" | "<<p6<<endl;
cout<<"- - - - - -"<<endl;
cout<<" "<<p7<<" | "<<p8<<" | "<<p9<<endl<<endl;

fout<<" "<<p1<<" | "<<p2<<" | "<<p3<<endl;
fout<<"- - - - - -"<<endl;
fout<<" "<<p4<<" | "<<p5<<" | "<<p6<<endl;
fout<<"- - - - - -"<<endl;
fout<<" "<<p7<<" | "<<p8<<" | "<<p9<<endl<<endl;
}

void Play()
{
	int guess=0;
	int counter=0;
	char again='\0';
	
	while (counter<9)
	{
		cout<<"Human:";
		cin>>guess;
		while (checkPlayed (guess))
		{
			cout<<"Already been played. Please Try again.";
			cin>>guess;
		}
		HUMAN_MOVE(guess);
		DISPLAY_BOARD();
		if (check_win ())
		{
			cout<<"Human Wins!!"<<endl;
//			out<<"Human Wins!!"<<endl;
			break;
		}
		counter++;	
		if (counter>8)
		{
			cout<<"Tie Game!"<<endl;
//			out<<"Tie Game!"<<endl;
			break;
		}
		guess=(rand()%9)+1;
			while (checkPlayed (guess));
		{
			guess= (rand()%9)+1;
		}
		cout<<"Computer: "<<guess<<endl;
		COMPUTER_MOVE(guess);	
		DISPLAY_BOARD();
		if (check_win())
		{
			cout<<"Computer Wins!!"<<endl;
//			out<<"Computer Wins!!"<<endl;
			break;
		}
		counter++;
		cout<<"c= "<<counter<<endl;
		if (counter>8)
		{
			cout<<"Tie Game!"<<endl;
//			out<<"Tie Game!"<<endl;
			break;
		}
	}
	cout<<"Would you like to play again? (y/n): ";
	cin>>again;
	if (again=='y'||again=='Y')
	{
		clear_board();
		DISPLAY_BOARD();
		Play();
	}
	else
	{
		cout<<"Game Over"<<endl;
		
	}
}

void HUMAN_MOVE(int guess)
{

switch (guess)
{
case 1: p1 = 'x'; break;
case 2: p2 = 'x'; break;
case 3: p3 = 'x'; break;
case 4: p4 = 'x'; break;
case 5: p5 = 'x'; break;
case 6: p6 = 'x'; break;
case 7: p7 = 'x'; break;
case 8: p8 = 'x'; break;
case 9: p9 = 'x'; break;
}	
}

void COMPUTER_MOVE(int guess)

	{switch (guess)
{
case 1: p1 = 'o'; break;
case 2: p2 = 'o'; break;
case 3: p3 = 'o'; break;
case 4: p4 = 'o'; break;
case 5: p5 = 'o'; break;
case 6: p6 = 'o'; break;
case 7: p7 = 'o'; break;
case 8: p8 = 'o'; break;
case 9: p9 = 'o'; break;
}
}

bool checkPlayed(int guess)
{
	switch(guess)
	{
case 1: if (p1 != ' ')return true;else{return false;}break;
case 2: if (p2 != ' ')return true;else{return false;}break;
case 3: if (p3 != ' ')return true;else{return false;}break;
case 4: if (p4 != ' ')return true;else{return false;}break;
case 5: if (p5 != ' ')return true;else{return false;}break;
case 6: if (p6 != ' ')return true;else{return false;}break;
case 7: if (p7 != ' ')return true;else{return false;}break;
case 8: if (p8 != ' ')return true;else{return false;}break;
case 9: if (p9 != ' ')return true;else{return false;}break;
	default: cout<<"not an available play space"<<endl;
		return false;
	}
}
bool check_win ()
{

	if ((p1=='x'&& p4=='x'&& p7=='x')||(p1=='o'&& p4=='o'&& p7=='o'))	{return true;}
	else if ((p2=='x'&& p5=='x'&& p8=='x')||(p2=='o'&& p5=='o'&& p8=='o'))	{return true;}
	else if ((p3=='x'&& p6=='x'&& p9=='x')||(p3=='o'&& p6=='o'&& p9=='o'))	{return true;}
	else if ((p1=='x'&& p5=='x'&& p9=='x')||(p1=='o'&& p5=='o'&& p9=='o'))	{return true;}
	else if ((p3=='x'&& p5=='x'&& p7=='x')||(p3=='o'&& p5=='o'&& p7=='o'))	{return true;}
	else if ((p4=='x'&& p5=='x'&& p6=='x')||(p4=='o'&& p5=='o'&& p6=='o'))	{return true;}
	else if ((p7=='x'&& p8=='x'&& p9=='x')||(p7=='o'&& p8=='o'&& p9=='o'))	{return true;}
	else if ((p1=='x'&& p2=='x'&& p3=='x')||(p1=='o'&& p2=='o'&& p3=='o'))	{return true;}

	else return false;

}

void clear_board()
{
	p1=' ', p2=' ',p3=' ',p4=' ',p5=' ',p6=' ',p7=' ',p8=' ',p9=' ';
}

I compiled with VC++ 2005 Express and it ran ok. Here is the output

Tic Tac Toe
1 | 2 | 3
- - - - - -
4 | 5 | 6
- - - - - -
7 | 8 | 9

Human:1
x | |
- - - - - -
| |
- - - - - -
| |

Computer: 9
x | |
- - - - - -
| |
- - - - - -
| | o

c= 2
Human:

but did it stop in the middle of the game?

yes, I continued and after the 3d entry it stopped. I'm going to run the debugger to see what is causing it, my guess is the program is trashing memory.

Thank You!!!!
It has been kicking my butt for the past 2 weeks..
Thank You again!!! ^.^

because the program was given 2 weeks ago as well.. sorry, i forgot to mention that part.. sorry... and thank you again.. ^.^

line 97: >>while (checkPlayed (guess));
It gets suck here sometimes. In my testing it was after I entered the 3d number. Very probably because of that semicolon at the end of the line.

[edit]See now, if you had learned to use your compiler's debugger you could have found this error two weeks ago. It took me all of about 10 minutes to find the problem.[/edit]

That fixed it!! Thank You!!

Glad to have helped.

AD

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.