This Program is Tic Tac Toe game which contain following Variables and functions.
Variables
char space: It’s an array of type character and represent the positions on the table for game, the table consist of 9 position where user can input his choice.
int p: “p” represents the Player. This game is for two players.
int ch: “ch” represents choice that where the user wants to input his move, the possible inputs are from 1-9.
int i: “i” variable determines if a user won, or not!
char mark: “mark” convert the user input as “X” or “O”, based on the turn of user!
char m: “m” comes in play when a game is over, and takes user input, that whether he wanna play more or not!
Functions
void board(): This function initializes the board. Refreshes every time user enters his input.
void check(): This function runs on every input and checks to see if there is a Win, or all of the board is complete.
Program
#include<iostream.h>
#include<conio.h>
void board();
int check();
char space[10]={'0','1','2','3','4','5','6','7','8','9'};
void main(){
int p=1,ch,i;
char mark,m;
do{
clrscr();
for(int j=0;j<10;j++){
space[j]=j+ '0';
}
do{
clrscr();
board();
p=(p%2)?1:2;
cout<<"Player "<<p<<", enter your move: ";
cin>>ch;
mark=(p==1)?'X':'O';
if (ch == 1 && space[1] == '1')
space[1] = mark;
else if (ch == 2 && space[2] == '2')
space[2] = mark;
else if (ch == 3 && space[3] == '3')
space[3] = mark;
else if (ch == 4 && space[4] == '4')
space[4] = mark;
else if (ch == 5 && space[5] == '5')
space[5] = mark;
else if (ch == 6 && space[6] == '6')
space[6] = mark;
else if (ch == 7 && space[7] == '7')
space[7] = mark;
else if (ch == 8 && space[8] == '8')
space[8] = mark;
else if (ch == 9 && space[9] == '9')
space[9] = mark;
else
{
cout<<"Invalid move ";
p--;
cin.ignore();
cin.get();
}
i=check();
p++;
}while(i==-1);
p=(p%2)?2:1;
cout<<"\nCongratulations!!!Player "<<p<<" with mark \""<<mark<<"\" Won";
cout<<"\n\nWanna play more?? Enter \"y\" for Yes: ";
cin>>m;
}while(m=='y'||m=='Y');
getch();
}
/*Initialize the 3*3 board*/
void board(){
cout<<"\n\nTIC TAC TOE\n\n";
cout<<" "<<space[1]<<"|"<<space[2]<<"|"<<space[3]<<endl;
cout<<" "<<"------"<<endl;
cout<<" "<<space[4]<<"|"<<space[5]<<"|"<<space[6]<<endl;
cout<<" "<<"------"<<endl;
cout<<" "<<space[7]<<"|"<<space[8]<<"|"<<space[9]<<endl;
cout<<" "<<"------"<<endl;
}
/*Check to see if a user wins*/
int check()
{
if (space[1] == space[2] && space[2] == space[3])
return 1;
else if (space[4] == space[5] && space[5] == space[6])
return 1;
else if (space[7] == space[8] && space[8] == space[9])
return 1;
else if (space[1] == space[4] && space[4] == space[7])
return 1;
else if (space[2] == space[5] && space[5] == space[8])
return 1;
else if (space[3] == space[6] && space[6] == space[9])
return 1;
else if (space[1] == space[5] && space[5] == space[9])
return 1;
else if (space[3] == space[5] && space[5] == space[7])
return 1;
else if(space[1] != '1' && space[2] != '2' && space[3] != '3' &&
space[4] != '4' && space[5] != '5' && space[6] != '6' &&
space[7] != '7' && space[8] != '8' && space[9] != '9')
return 0;
else
return -1;
}
0 comments:
Post a Comment