Program to Play Tic-Tac-Toe Game

Program to Play Tic-Tac-Toe Game

This program will simulate Tic-Tac-Toe game Between two Players .


Output :


Logic to generate the Game :

  • Get the Names of the Players for the Symbols O and X .
  • If player enters same Name then ask to Reenter the Second Player Name .
  • Display the Tic-Tac-Toe board using Print function .
  • Alternatively ask the players to Enter the Position to Insert the Symbols for 9 times .
  • After 5 iterations check whether a player finishes the Game or not By usingcheckFinish function .
  • If a player finishes the Game then change the flag variable finish to 1 and exit the loop .
  • Now the loop exits and games is finished .
  • If the flag variable is unchanged after 9 iterations then the Game is Tied .

Program :

#include<iostream>
using namespace std;
int checkFinish(string C,char P[3][3]);					//function for checking the winner
void print(char P[3][3]);								//function for printing the board
int main()
{
	string p1,p2,name;									//p1,p2=name of players
	cout<<"Enter Player Name for Symbol X : ";
	cin>>p1;
	cout<<"Enter Player Name for Symbol O : ";
	RENAME:												//label
	cin>>p2;
	if(p1==p2)											//if both names are same go to RENAME label
	{
		cout<<"Enter Different Player Name for Symbol O : ";
	    goto RENAME;
	}
	cout<<"Player Name for Symbol X : "<<p1;
	cout<<"\nPlayer Name for Symbol O : "<<p2;

	/*a[][] contains four rows with nine colums
	  row-1 : matches the input position to char array p
	  row-2 : row index of char array p
	  row-3 : column index of char array p
	  row-4 : used to store flags whether char array p is already have a symbol
	*/

	int a[4][9]={{1,2,3,4,5,6,7,8,9},{0,0,0,1,1,1,2,2,2},{0,1,2,0,1,2,0,1,2},{0}};

    char p[3][3]={{'1','2','3'},{'4','5','6'},{'7','8','9'}};	//p=tic tic toe symbols

	char set;											//temp varible to store X and O symbols
    int finish=0;    									//flag vriable to check game is finished or not
    cout<<"\n";
    print(p);                                           //function call to print the tic-tac-toe board
    for(int i=0;i<9&&finish!=1;i++)
    {
		REENTER:                                        //label
		if(i%2==0)
		{
			cout<<"\n"<<p1<<" Enter Position to Insert X : "; //at even numbers play one is to play and symbol is X
			set='X';
			name=p1;
		}
		else
		{
			cout<<"\n"<<p2<<" Enter Position to Insert O : "; //at odd numbers play two is to play and symbol is O
			set='O';
			name=p2;
		}
		int pos;
		char cpos;										//cpos=get the input position from the user as char
		cin>>cpos;
		pos=cpos-48;                                    //pos=converts char position to integer position
		if((pos>0&&pos<10)&&!(a[3][pos-1]))
		{
			p[(a[1][pos-1])][(a[2][pos-1])]=set;        //if input pos is <10 and >0 set the current symbol in tic-tac-toe board
			a[3][pos-1]=1;                              //after inserted the symbol change the flag to 1 to mention it is already having a symbol
		}
		else
		{
			cout<<"\nInvalid Position! Re-Enter\n";     //if user enters invalid position of already filled position go to REENTER label
			goto REENTER;
		}
		print(p);
		cout<<"\n";
		if(i>3)                                         //after 4 iteration we need to start check for winner of the game
		{
			finish=checkFinish(name,p);                 //function call to checkwinner function with current inserted player name and tic-tac-toe array
		}
	}
	if(finish==0)                                       //if finish variable is unchanged then game is tied
	{
		cout<<"\nGame Tied !";
	}
	return 0;
}
void print(char P[3][3])
{
  cout<<"\n-------------";
  for(int i=0;i<3;i++)
  {
    cout<<"\n";
    for(int j=0;j<3;j++)
    {
		cout<<"| "<<P[i][j]<<" ";
    }
    cout<<"|\n-------------";
  }
}
int checkFinish(string C,char P[3][3])
{
    for(int i=0;i<3;i++)
    {
        if((P[i][0]==P[i][1])&&(P[i][0]==P[i][2]))    //checks for horizontal match
        {
            cout<<"\n"<<C<<" Won the Game !";
            return 1;                                 //return 1 to finish to show that game is finished
		}
        if((P[0][i]==P[1][i])&&(P[0][i]==P[2][i]))    //checks for vertical match
        {
            cout<<"\n"<<C<<" Won the Game !";
            return 1;								  //return 1 to finish to show that game is finished
        }
    }
    if((P[0][0]==P[1][1])&&(P[1][1]==P[2][2]))        //checks for 1st diagnol match
    {
        cout<<"\n"<<C<<" Won the Game !";
        return 1;									  //return 1 to finish to show that game is finished
    }
    if((P[2][0]==P[1][1])&&(P[1][1]==P[0][2]))        //checks for 2nd diagonal match
    {
        cout<<"\n"<<C<<" Won the Game !";
        return 1;									  //return 1 to finish to show that game is finished
    }
    return 0;
}

Follow For Instant Updates

Join WhatsApp Group: link
Join our Telegram Channel: link
Like our Facebook Page:  link
Subscribe to our Youtube channel: link

Suriya27

A Programmer who have his programming skills on C,C++, Java and also interested in Android Development. He follows the famous quotes by Linus Torvalds ” Talk is cheap, Show me the code “ .
0 0 votes
Article Rating
Subscribe
Notify of
guest
5.4K Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
5.4K
0
Would love your thoughts, please comment.x
()
x