Im wondering what r some good ways to to check if a cell has been taken or not![attachment=11337:attachment]
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
void pb(char s1, char s2, char s3, char s4, char s5, char s6, char s7, char s8, char s9);
void check (char s1, char s2, char s3, char s4, char s5, char s6, char s7, char s8, char s9,int *t);
void openFile(ifstream &inf);
void openFile(ofstream &inf);
int header();
void playerInput();
void getData(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7, char *s8, char *s9,int *t);
void sendData(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7, char *s8, char *s9,int *t);
int main (void)
{
system("TITLE TIC TAC TOE");
system("COLOR C");
char s1 = '1';
char s2 = '2';
char s3 = '3';
char s4 = '4';
char s5 = '5';
char s6 = '6';
char s7 = '7';
char s8 = '8';
char s9 = '9';
int p1move,i;
int p2move;
int t = 0;
string p1enter = "Player 1 please enter your move ";
string p2enter = "Player 2 please enter your move ";
i = header();
if(i == 1)
{
getData(&s1,&s2,&s3,&s4,&s5,&s6,&s7,&s8,&s9,&t);
}
else
cout<<"A new will start!\n";
pb(s1, s2, s3, s4, s5, s6, s7, s8, s9);
while (t < 9 && t != -1)
{
if (t % 2 == 0)
{
cout << p1enter;
cin >> p1move;
switch (p1move)
{
case 1:
s1 = 'X';
break;
case 2:
s2 = 'X';
break;
case 3:
s3 = 'X';
break;
case 4:
s4 = 'X';
break;
case 5:
s5 = 'X';
break;
case 6:
s6 = 'X';
break;
case 7:
s7 = 'X';
break;
case 8:
s8 = 'X';
break;
case 9:
s9 = 'X';
break;
case -1:
sendData(&s1,&s2,&s3,&s4,&s5,&s6,&s7,&s8,&s9,&t);
break;
default:
cout << "This is an invalid move";
}
pb(s1, s2, s3, s4, s5, s6, s7, s8, s9);
}
if (t % 2 != 0)
{
cout << p2enter;
cin >> p2move;
switch (p2move)
{
case 1:
s1 = 'O';
break;
case 2:
s2 = 'O';
break;
case 3:
s3 = 'O';
break;
case 4:
s4 = 'O';
break;
case 5:
s5 = 'O';
break;
case 6:
s6 = 'O';
break;
case 7:
s7 = 'O';
break;
case 8:
s8 = 'O';
break;
case 9:
s9 = 'O';
break;
case -1:
sendData(&s1,&s2,&s3,&s4,&s5,&s6,&s7,&s8,&s9,&t);
break;
default:
cout << "This is an invalid move";
}
pb(s1, s2, s3, s4, s5, s6, s7, s8, s9);
}
check (s1, s2, s3, s4, s5, s6, s7, s8, s9, &t);
t++;
}
system("PAUSE");
return 0;
}
void playerInput()
{
}
//gets information from file
void getData(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7, char *s8, char *s9,int *t)
{
ifstream gamesave;
openFile(gamesave);
gamesave >> *s1;
gamesave >> *s2;
gamesave >> *s3;
gamesave >> *s4;
gamesave >> *s5;
gamesave >> *s6;
gamesave >> *s7;
gamesave >> *s8;
gamesave >> *s9;
gamesave >> *t;
gamesave.close();
}
//gets information from file
void sendData(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7, char *s8, char *s9,int *t)
{
ofstream gamesave;
openFile(gamesave);
gamesave << *s1 <<" ";
gamesave << *s2 <<" ";
gamesave << *s3 <<" \n";
gamesave << *s4 <<" ";
gamesave << *s5 <<" ";
gamesave << *s6 <<" \n";
gamesave << *s7 <<" ";
gamesave << *s8 <<" ";
gamesave << *s9 <<" \n";
gamesave << *t;
gamesave.close();
system("PAUSE");
exit (EXIT_FAILURE);
}
int header()
{
int i = 0;
cout <<"Welcome to TIC TAC TOE\a\n"
<<"This is a two player game\n"
<<"Player 1 always goes first!\n"
<<"you can save a game by entering -1 for a input during the game\n"
<<"you can load a game now by entering 1 for a input or 2 for a new game\n";
cout<<"Enter here --> ";
cin >> i;
return i;
system("PAUSE");
system("CLS");
}
void pb( char s1, char s2, char s3, char s4, char s5, char s6, char s7, char s8, char s9)
{
cout << endl;
cout <<" " <<" " << s1 << " | " << s2 << " | " << s3 << endl
<<" "<< "---+---+---" << endl
<<" "<< " " << s4 << " | " << s5 << " | " << s6 << endl
<<" "<< "---+---+---" << endl
<<" "<< " " << s7 << " | " << s8 << " | " << s9 << endl
<< endl;
}
void check (char s1, char s2, char s3, char s4, char s5, char s6, char s7, char s8, char s9,int *t)
{
// checks if player1 wins
if (s1 == 'X' && s2 == 'X' && s3 == 'X')
{
*t = 9;
cout << "Player 1 is the Winner!!!!!";
cout << endl;
}
if (s4 == 'X' && s5 == 'X' && s6 == 'X')
{
*t = 9;
cout << "Player 1 is the Winner!!!!!";
cout << endl;
}
if (s7 == 'X' && s8 == 'X' && s9 == 'X')
{
*t = 9;
cout << "Player 1 is the Winner!!!!!";
cout << endl;
}
if (s1 == 'X' && s4 == 'X' && s7 == 'X')
{
*t = 9;
cout << "Player 1 is the Winner!!!!!";
cout << endl;
}
if (s2 == 'X' && s5 == 'X' && s8 == 'X')
{
*t = 9;
cout << "Player 1 is the Winner!!!!!";
cout << endl;
}
if (s3 == 'X' && s6 == 'X' && s9 == 'X')
{
*t = 9;
cout << "Player 1 is the Winner!!!!!";
cout << endl;
}
if (s1 == 'X' && s5 == 'X' && s9 == 'X')
{
*t = 9;
cout << "Player 1 is the Winner!!!!!";
cout << endl;
}
if (s3 == 'X' && s5 == 'X' && s7 == 'X')
{
*t = 9;
cout << "Player 1 is the Winner!!!!!";
cout << endl;
}
// checks if player2 wins
if (s1 == 'O' && s2 == 'O' && s3 == 'O')
{
*t = 9;
cout << "Player 2 is the Winner!!!!!";
cout << endl;
}
if (s4 == 'O' && s5 == 'O' && s6 == 'O')
{
*t = 9;
cout << "Player 2 is the Winner!!!!!";
cout << endl;
}
if (s7 == 'O' && s8 == 'O' && s9 == 'O')
{
*t = 9;
cout << "Player 2 is the Winner!!!!!";
cout << endl;
}
if (s1 == 'O' && s4 == 'O' && s7 == 'O')
{
*t = 9;
cout << "Player 2 is the Winner!!!!!";
cout << endl;
}
if (s2 == 'O' && s5 == 'O' && s8 == 'O')
{
*t = 9;
cout << "Player 2 is the Winner!!!!!";
cout << endl;
}
if (s3 == 'O' && s6 == 'O' && s9 == 'O')
{
*t = 9;
cout << "Player 2 is the Winner!!!!!";
cout << endl;
}
if (s1 == 'O' && s5 == 'O' && s9 == 'O')
{
*t = 9;
cout << "Player 2 is the Winner!!!!!";
cout << endl;
}
if (s3 == 'O' && s5 == 'O' && s7 == 'O')
{
*t = 9;
cout << "Player 2 is the Winner!!!!!";
cout << endl;
}
}
//opens the file
void openFile(ofstream &inf)
{
inf.open("save.txt");
if(!inf)
{
cout << "Error opening file..." << endl;
system("PAUSE");
exit (EXIT_FAILURE);
}
else
cout <<"saving game\a\n";
}
//opens the file
void openFile(ifstream &inf)
{
inf.open("save.txt");
if(!inf)
{
cout << "Error opening file..." << endl;
system("PAUSE");
exit (EXIT_FAILURE);
}
else
cout <<"File found and open!\a\n";
}

New Topic/Question
Reply




MultiQuote




|