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------