1 Replies - 1969 Views - Last Post: 12 April 2009 - 07:47 PM Rate Topic: -----

#1 RBSprogram101  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 38
  • Joined: 28-March 09

tic tac toe valid cell check

Posted 12 April 2009 - 07:00 PM

hi this is a on the side project tic tac toe without arrays! I know it would be easy with them.

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";
		
}	 


Is This A Good Question/Topic? 0
  • +

Replies To: tic tac toe valid cell check

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: tic tac toe valid cell check

Posted 12 April 2009 - 07:47 PM

If( (s1 == 'X') || (s1 == 'O') ) {
	// s1 has been taken
} 


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1