Modification(adding AI) tic_tac_tic game
#include<stdio.h>
#include<conio.h>
/*
WORKS TO DO FOR IMPROVING THIS GAME TO (HUMAN VS HUMAN) TO (HUMAN VS AI)
To do task
* make a function
1. to give random number for AI move
2. to check whether AI move is overwrite other player moves
a. if overwriring then make a new random number
b. if not leave it
3. check the patterns and if any 2 elements of same type in way
example :
--|--|--
1 |2 |0
--|--|--
4 |0 |6
--|--|--
7 |8 |9
--|--|--
a. it will decide to win and place "0" at 7
--|--|--
1 |2 |x
--|--|--
4 |x |6
--|--|--
7 |8 |9
--|--|--
b. it will decide to protect and place "0" at 7
4. ai attachment finish so remove player2 objects
*/
int gameover=1;
char a[9] = {'1','2','3','4','5','6','7','8','9'};
char p1name[20],p2name[20];
char symbol1='x',symbol2 = '0';
int player1,player2,ch = 0;
void show();
void input();
void win();
int main(){
// clrscr();
printf("enter player 1 name : ");
scanf("%s",p1name);
printf("enter player 2 name : ");
scanf("%s",p2name);
while(gameover!= 0){
clrscr();
show();
input();
win();
}
clrscr();
show();
win();
getc();
return 0;
}
void show(){
// it will show the layout of game
printf("\t\t\t\t TIC TAC TIC\n");
printf("\n\n");
printf("\t\t\t--|--|--\n");
printf("\t\t\t%c |%c |%c \n",a[0],a[1],a[2]);
printf("\t\t\t--|--|--\n");
printf("\t\t\t%c |%c |%c \n",a[3],a[4],a[5]);
printf("\t\t\t--|--|--\n");
printf("\t\t\t%c |%c |%c \n",a[6],a[7],a[8]);
printf("\t\t\t--|--|--\n");
printf("\n%s: x",p1name);
printf("\n%s : 0",p2name);
}
void input()
{
/*
1. it will see who will play now
2. and take input from the user
3. update game layout according to given data
*/
int i ;
if (ch == 0)
{
printf("\n%s enter your choice",p1name);
scanf("%d",&player1);
ch = 1;
}
else
{
printf("\n%s enter your choice",p2name);
scanf("%d",&player2);
ch = 0;
}
if (ch == 1)
{
for(i=0;i<9;i++)
{
if(i== player1-1)
{
a[i]=symbol1;
}
}
}
else
{
for(i=0;i<9;i++)
{
if(i== player2-1)
{
a[i]=symbol2;
}
}
}
}
void win(){
/*
-> 012,345,678,036,147,258,048,246 -> according to computer
(has array strats from 0 )
-> 123,456,789,147,258,369,159,357 -> according to user
-> it will declare win if any player meet above case's
-> check which player has done strick and announce who wins
*/
// player 1
if ( a[0] == 'x'&&a[1] == 'x'&&a[2]=='x')
{
printf("\n%s win the math",p1name);
gameover = 0;
}
else if(a[3] == 'x'&&a[4] == 'x'&&a[5]=='x' ){
printf("\n%s win the math",p1name);
gameover = 0 ;
}
else if ( a[6] == 'x'&&a[7] == 'x'&&a[8]=='x')
{
printf("\n%s win the math",p1name);
gameover = 0;
}
else if ( a[0] == 'x'&&a[3] == 'x'&&a[6]=='x')
{
printf("\n%s win the math",p1name);
gameover = 0;
}
else if ( a[1] == 'x'&&a[4] == 'x'&&a[7]=='x')
{
printf("\n%s win the math",p1name);
gameover = 0;
}
else if ( a[2] == 'x'&&a[5] == 'x'&&a[8]=='x')
{
printf("\n%s win the math",p1name);
gameover = 0;
}
else if ( a[0] == 'x'&&a[4] == 'x'&&a[8]=='x')
{
printf("\n%s win the math",p1name);
gameover = 0;
}
else if ( a[2] == 'x'&&a[4] == 'x'&&a[6]=='x')
{
printf("\n%s win the mathh",p1name);
gameover = 0;
}
//player2
if ( a[0] == '0'&&a[1] == '0'&&a[2]=='0')
{
printf("\n%s win the math",p2name);
gameover = 0;
}
else if(a[3] == '0'&&a[4] == '0'&&a[5]=='0' ){
printf("\n%s win the math",p2name);
gameover = 0 ;
}
else if ( a[6] == '0'&&a[7] == '0'&&a[8]=='0')
{
printf("\n%s win the math",p2name);
gameover = 0 ;
}
else if ( a[0] == '0'&&a[3] == '0'&&a[6]=='0')
{
printf("\n%s win the math",p2name);
gameover = 0 ;
}
else if ( a[1] == '0'&&a[4] == '0'&&a[7]=='0')
{
printf("\n%s win the math",p2name);
gameover = 0 ;
}
else if ( a[2] == '0'&&a[5] == '0'&&a[8]=='0')
{
printf("\n%s win the math",p2name);
gameover = 0 ;
}
else if ( a[0] == '0'&&a[4] == '0'&&a[8]=='0')
{
printf("\n%s win the math",p2name);
gameover = 0 ;
}
else if ( a[2] == '0'&&a[4] == '0'&&a[6]=='0')
{
printf("\n%s win the math",p2name);
gameover = 0;
}
//123,456,789,147,258,369,159,357 -> according to user
//012,345,678,036,147,258,048,246 -> according to computer
// (array strats with 0)
}
Comments
Post a Comment