Professional Documents
Culture Documents
AI File
AI File
PRACTICAL – 1
Write a program to implement Tic-Tac-Toe game in C.
About Tic-Tac-Toe game:
The Tic Tac Toe is also known as Noughts and Crosses or X’s and 0’s, the player needs to take
turns making the space in a 3x3 grid with their own marks, if 3 condecutive marks (Horizontal,
Vertical, Diagonal) are formed thenthe player who owns these moves get won.
C Code:
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include<time.h>
int initcheck(char x[],char str[])
{
if(x[0]=='O'&&x[1]=='O'&&x[2]=='O' ||
x[3]=='O'&&x[4]=='O'&&x[5]=='O' ||
x[6]=='O'&&x[7]=='O'&&x[8]=='O' ||
x[0]=='O'&&x[3]=='O'&&x[6]=='O' ||
x[1]=='O'&&x[4]=='O'&&x[7]=='O' ||
x[2]=='O'&&x[5]=='O'&&x[8]=='O' ||
x[0]=='O'&&x[4]=='O'&&x[8]=='O' ||
x[2]=='O'&&x[4]=='O'&&x[6]=='O')
{
printf("\n%s won the game.",str);
return 1;
}
else if(x[0]=='X'&&x[1]=='X'&&x[2]=='X' ||
x[3]=='X'&&x[4]=='X'&&x[5]=='X' ||
x[6]=='X'&&x[7]=='X'&&x[8]=='X' ||
x[0]=='X'&&x[3]=='X'&&x[6]=='X' ||
x[1]=='X'&&x[4]=='X'&&x[7]=='X' ||
x[2]=='X'&&x[5]=='X'&&x[8]=='X' ||
x[0]=='X'&&x[4]=='X'&&x[8]=='X' ||
x[2]=='X'&&x[4]=='X'&&x[6]=='X')
{
printf("\n%s won the game.",str);
return 2;
}
else if(x[0]!=' '&& x[1]!=' '&& x[2]!=' '&&x[3]!=' '&&x[4]!=' '&&x[5]!=' '&&x[6]!='
'&&x[7]!=' '&&x[8]!=' ')
{
printf("\nMatch Draw...");
return 3;
}
else
{
return 0;
}
}
char array(char x[],int n,char i)
{
if(x[n]==' ')
{
x[n]=i;
return x[n];
}
else
{
printf("cell already filled...\n");
return x[n];
}
}
void display(char m[])
{
printf(" %c | %c | %c \n-----------\n %c | %c | %c \n-----------\n %c | %c | %c
",m[0],m[1],m[2],m[3],m[4],m[5],m[6],m[7],m[8]);
}
int main()
{
char m[]={' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
int i=0,cell,index,n,random_no;
char player1[20],player2[20];
printf(" 0 | 1 | 2 \n-----------\n 3 | 4 | 5 \n-----------\n 6 | 7 | 8 \n");
printf("---------------Tic-tac-toe---------------");
printf("\n.........................................\n");
printf("\nLet's start to play game...\n");
printf("\nChoice any one...\n 1. Player to Player\n 2. Player to Computer\n");
printf("\nEnter your choice: ");
scanf("%d",&n);
switch(n)
{
case 1:
ABC:while(i==0)
{
if(index%2!=0)
{
printf("\n%s choose cell: ",player1);
scanf("%d",&cell);
if(cell>=9 || cell<0)
{
printf("Enter proper cell...");
goto ABC;
}
if(m[cell]=='O')
{
printf("cell already filled...\n");
goto ABC;
}
if(m[cell]=='X')
{
printf("cell already filled...\n");
goto ABC;
}
m[cell]=array(m,cell,'O');
display(m);
index++;
i=initcheck(m,player1);
}
else
{
printf("\n%s choose cell: ",player2);
scanf("%d",&cell);
if(cell>=9 || cell<0)
{
printf("Enter proper cell...");
goto ABC;
}
if(m[cell]=='X')
{
printf("cell already filled...\n");
goto ABC;
}
if(m[cell]=='O')
{
printf("cell already filled...\n");
goto ABC;
}
m[cell]=array(m,cell,'X');
display(m);
index++;
i=initcheck(m,player2);
}
}
break;
case 2:
else
{
index=2;
}
XYZ:while(i==0)
{
if(index%2!=0)
{
printf("\n%s choose cell: ",player1);
scanf("%d",&cell);
if(cell>=9 || cell<0)
{
printf("Enter proper cell...");
goto XYZ;
}
m[cell]=array(m,cell,'O');
if(m[cell]=='X')
{
goto XYZ;
}
display(m);
index++;
i=initcheck(m,player1);
}
else
{
printf("\nComputer moves :\n ");
if(m[4]==' ')
m[4]='X';
m[6]='X';
else if(m[4]=='X'&&m[0]=='X'&&m[8]==' ')
m[8]='X';
else if(m[4]=='X'&&m[8]=='X'&&m[0]==' ')
m[0]='X';
else if(m[4]=='X'&&m[6]=='X'&&m[2]==' ')
m[2]='X';
//DRAW
else if(m[0]==' ')
m[0]='X';
else if(m[1]==' ')
m[1]='X';
else if(m[2]==' ')
m[2]='X';
display(m);
index++;
i=initcheck(m,"Computer");
}
}
break;
default:
printf("Please, enter correct choice!!!");
}
return 0;
}
PRACTICAL – 2
Example:
In this particular example we have given 2 jugs with capacity 4 litter & 3 litter respectively. There
is no marker in jug. Your goal is to fill 2 litter water in jug 1.
C Code:
#include<stdio.h>
int initial(int,int,int,int);
int BFS(int,int,int,int,int,int);
int min(int,int);
int remove();
int display();
int duplicate(int,int);
int q_array[MAX];
int front = - 1;
else if(goal_cap==0)
else if(goal_cap%(gcd(jug1,jug2))!=0)
else
return 0;
if(x<y)
return x;
else
return y;
if (rear == MAX - 1)
else
if (front == - 1)
front = 0;
rear = rear + 1;
q_array[rear] = x;
rear = rear + 1;
q_array[rear]=y;
int i;
for(i=front;i<=rear;i=i+2)
return 1;
return 0;
int w,step=0,temp,count=0,j1fill=0,j2fill=0;
j1fill=jug1;
j2fill=0;
if(duplicate(j1fill,j2fill)==0)
printf("\n(%d,%d)",j1fill,j2fill);
add(j1fill,j2fill);
j1fill=0;
j2fill=jug2;
if(duplicate(j1fill,j2fill)==0)
printf("\n(%d,%d)",j1fill,j2fill);
add(j1fill,j2fill);
return 0;
if(f<jug1)
j1fill=jug1;
j2fill=s;
step++;
if(duplicate(j1fill,j2fill)==0)
printf("\n(%d,%d)",j1fill,j2fill);
add(j1fill,j2fill);
if(s<jug2)
j1fill=f;
j2fill=jug2;
step++;
if(duplicate(j1fill,j2fill)==0)
printf("\n(%d,%d)",j1fill,j2fill);
add(j1fill, j2fill);
if(f==jug1)
j1fill=0;
j2fill=s;
step++;
if(duplicate(j1fill,j2fill)==0)
printf("\n(%d,%d)",j1fill,j2fill);
add(j1fill,j2fill);
if(s==jug2)
j2fill=0;
j1fill=f;
step++;
if(duplicate(j1fill,j2fill)==0)
printf("\n(%d,%d)",j1fill,j2fill);
add(j1fill,j2fill);
j1fill=s;
j2fill=f;
step++;
printf("\n(%d,%d)",j1fill,j2fill);
add(j1fill,j2fill);
j1fill=jug1;
j2fill=s-(j1fill-f);
step++;
printf("\n(%d,%d)",j1fill,j2fill);
add(j1fill,j2fill);
j2fill=jug2;
j1fill=jug1-(jug2-j2fill);
step++;
printf("\n(%d,%d)",j1fill,j2fill);
add(j1fill,j2fill);
j1fill=jug1;
j2fill=jug2-j1fill;
if(duplicate(j1fill,j2fill)==0)
printf("\n(%d,%d)",j1fill,j2fill);
add(j1fill,j2fill);
j1fill=s;
j2fill=f;
if(duplicate(j1fill,j2fill)==0)
printf("\n(%d,%d)",j1fill,j2fill);
add(j1fill,j2fill);
temp=min(jug2-s,f);
j2fill=s+temp;
j1fill=f-temp;
step++;
if(duplicate(j1fill,j2fill)==0)
printf("\n(%d,%d)",j1fill,j2fill);
add(j1fill,j2fill);
temp=min(jug1-f,s);
j1fill=f+temp;
j2fill=s-temp;
step++;
if(duplicate(j1fill,j2fill)==0)
printf("\n(%d,%d)",j1fill,j2fill);
add(j1fill,j2fill);
temp=min(f,jug2-s);
j2fill=s+temp;
j1fill=f-temp;
step++;
printf("\n(%d,%d)",j1fill,j2fill);
add(j1fill,j2fill);
j1fill=s;
j2fill=f;
step++;
if(duplicate(j1fill,j2fill)==0)
printf("\n(%d,%d)",j1fill,j2fill);
add(j1fill,j2fill);
j1fill=s;
j2fill=f;
step++;
printf("\n(%d,%d)",j1fill,j2fill);
add(j1fill,j2fill);
count++;
return step;
while(x!=y)
if(x>y)
else
return x;
int main()
int x=0,y=0,ans,d,c1fill,c2fill,f,s,i,c;
int jug1,jug2,goal_cap,goal_jug;
scanf("%d",&jug1);
scanf("%d",&jug2);
scanf("%d",&goal_jug);
scanf("%d",&goal_cap);
//c = check(jug1,jug2,goal_jug,goal_cap)
// if(c==0)
if(!(initial(jug1,jug2,goal_jug,goal_cap))==0)
return 0;
if(goal_jug==1)
c1fill=goal_cap;
c2fill=0;
else if(goal_jug==2)
c1fill=0;
c2fill=goal_cap;
else
printf("(0,0)");
add(0,0);
f=q_array[i];
s=q_array[i+1];
return 0;
BFS(f,s,goal_jug,goal_cap,jug1,jug2);
Input/output:
2) If the required water in any jug is more than the given capacity:
5) Otherwise:
PRACTICAL – 3
Assume that you have two jugs, Jug-A and Jug-B each of which holds a certain number of gallons.
Initially, both gallons are full, but we have an infinite supply of water. Our task is to measure
exactly X gallons. The problem is solvable only when t is a multiple of gcd(a, b) and can be
modelled as search through a state space. The state space for this problem can be described as the
set of ordered pair of integers (x, y) such that x ∈ {0, 1, 2, …, a} and y ∈ {0, 1, 2, …, b}.
The initial state is (0, 0) and the goal states are (t, y) and (x, t) ∀ x, y.
All we need now for a search procedure to work is a way to generate new states (successors) from
a given state. This is captured by production rules that specify how and when a new state can be
generated from a given state. For the water jug problem, the following production rules are
sufficient:
1. (x, y) -> (a, y) if x < a i.e., Fill the first jug if it is not already full
2. (x, y) -> (x, b) if y < b i.e., Fill the second jug if it is not already full
3. (x, y) -> (0, y) if x > 0 i.e., Empty the first jug
4. (x, y) -> (x, 0) if y > 0 i.e., Empty the second jug
5. (x, y) -> (min(x + y, a), max(0, x + y – a)) if y > 0 i.e., Pour from second jug into first jug
until the first jug is full or the second jug is empty
6. (x, y) -> (max(0, x + y – b), min(x + y, b)) if x > 0 i.e., Pour from first jug into second jug
until the second jug is full or the first jug is empty
The conditions in the production rules are not required e.g., we could fill an already full jug except
that it won’t lead us anywhere and would be wasteful in a tree search procedure where the visited
states are not saved to prevent revisiting.
Example:
Enter jug1:4L
Enter jug2:5L
Water required:2L
C code:
#include<stdio.h>
int DFS(int jug1, int jug2, intsel_jug, int water);
int min(int x ,int y)
{
if(x<y)
return x;
else
return y;
}
int main()
{
int jug1,jug2,sel_jug,water,value=0;
printf("Enter The Capacity of Jug1: ");
scanf("%d",&jug1);
printf("Enterthe Capacity of jug2: ");
scanf("%d",&jug2);
printf("In which jug you want to Fill Water:");
scanf("%d",&sel_jug);
printf("How much water you want in jug%d:",sel_jug);
scanf("%d",&water);
if(sel_jug==1)
{
value=jug1;
}
else
{
value=jug2;
}
if(jug1==jug2)
{
printf("\nERROR: Take both Jug's capacity different\n");
}
else if(water>value)
{
printf("\nERROR:Inputed more water than the capacity\n");
}
else if(sel_jug>3)
{
printf("\nERROR:jug number is only 1 or 2\n");
}
else
{
DFS(jug1,jug2,sel_jug,water);
}
return 0;
}
printf("step 1:(0,0)\n");
if(sel_jug==1)
{
while(!(jug1fill==water || count>25))
{
if(jug1fill==0)
{
jug1fill=jug1;
j1f1[step]=jug1fill;
j2f1[step]=jug2fill;
step++;
}
else if(jug2fill==jug2)
{
jug2fill=0;
j1f1[step]=jug1fill;
j2f1[step]=jug2fill;
step++;
}
else
{
temp=min(jug2-jug2fill,jug1fill);
jug2fill=jug2fill+temp;
jug1fill=jug1fill-temp;
j1f1[step]=jug1fill;
j2f1[step]=jug2fill;
step++;
}
count++;
}
if(j1f1[step-1]==water)
flag1=1;
count=0;
jug1fill=0;
jug2fill=0;
while(!(jug1fill==water || count>25))
{
if(jug2fill==0)
{
jug2fill=jug2;
j1f2[step1]=jug1fill;
j2f2[step1]=jug2fill;
step1++;
}
else if(jug1fill==jug1)
{
jug1fill=0;
j1f2[step1]=jug1fill;
j2f2[step1]=jug2fill;
step1++;
}
else
{
temp=min(jug1-jug1fill,jug2fill);
jug1fill=jug1fill+temp;
jug2fill=jug2fill-temp;
j1f2[step1]=jug1fill;
j2f2[step1]=jug2fill;
step1++;
}
count++;
}
if(j1f2[step1-1]==water)
flag2=1;
if(jug1fill==water)
{
if(sel_jug==2)
{
while(!(jug2fill==water || count>25))
{
if(jug1fill==0)
{
jug1fill=jug1;
j1f1[step]=jug1fill;
j2f1[step]=jug2fill;
step++;
}
else if(jug2fill==jug2)
{
jug2fill=0;
j1f1[step]=jug1fill;
j2f1[step]=jug2fill;
step++;
}
else
{
temp=min(jug2-jug2fill,jug1fill);
jug2fill=jug2fill+temp;
jug1fill=jug1fill-temp;
j1f1[step]=jug1fill;
j2f1[step]=jug2fill;
step++;
}
count++;
}
if(j2f1[step-1]==water)
flag1=1;
count=0;
jug1fill=0;
jug2fill=0;
while(!(jug2fill==water || count>25))
{
if(jug2fill==0)
{
jug2fill=jug2;
j1f2[step1]=jug1fill;
j2f2[step1]=jug2fill;
step1++;
}
else if(jug1fill==jug1)
{
jug1fill=0;
j1f2[step1]=jug1fill;
j2f2[step1]=jug2fill;
step1++;
}
else
{
temp=min(jug1-jug1fill,jug2fill);
jug1fill=jug1fill+temp;
jug2fill=jug2fill-temp;
j1f2[step1]=jug1fill;
j2f2[step1]=jug2fill;
step1++;
}
count++;
}
if(j2f2[step1-1]==water)
flag2=1;
if(jug2fill==water)
{
if(flag1==1 && flag2==1)
{
if(step<step1)
{
jug1fill=0;
j1f1[step]=jug1fill;
j2f1[step]=jug2fill;
for(i=0;i<step;i++)
{
printf("step %d:(%d,%d)\n",i+2,j1f1[i],j2f1[i]);
}
}
else
{
jug1fill=0;
j1f2[step1]=jug1fill;
j2f2[step1]=jug2fill;
for(i=0;i<step1;i++)
{
printf("step %d:(%d,%d)\n",i+2,j1f2[i],j2f2[i]);
}
}
}
else if(flag1==1 && flag2==0)
{
jug1fill=0;
j1f1[step]=jug1fill;
j2f1[step]=jug2fill;
for(i=0;i<step;i++)
{
printf("step %d:(%d,%d)\n",i+2,j1f1[i],j2f1[i]);
}
}
else
{
jug1fill=0;
j1f2[step1]=jug1fill;
j2f2[step1]=jug2fill;
for(i=0;i<step1;i++)
{
printf("step %d:(%d,%d)\n",i+2,j1f2[i],j2f2[i]);
}
}
printf("\nFinalAns(%d,%d)\n",jug1fill,jug2fill);
}
}
if(jug1fill!=water && jug2fill!=water)
printf("\nSOLUTION DOES NOT EXIST....\n");
}
OUTPUT:-
PRACTICAL – 4
Example:
Here is a simple blocks world problem:
C Code:
//C program for blocks world game
#include<stdio.h>
#include<stdlib.h>
void get_value(int b[],char a[]);
void position(int posi[],char b[],int x);
void column_fill(char col1[],int a,int A,int B,int C);
void play(char a[],char b[],char c[],int d[],char e[]);
void main()
{
int i,j,blocks=3,posi[3]={-1,-1,-1};
int a[3],sum,A=0,B=0,C=0,h,b[3]={0,0,0};
char col1[3],col2[3],col3[3],ans[3];
clrscr();
j=blocks;
printf("\t\t\t ******BLOCKS WORLD GAME***** \n");
randomize();
while(sum!=j)
{
sum=0;
for(i=0;i<j;i++)
{
a[i]=rand()%4;
sum=sum+a[i];
}
}
column_fill(col1,a[0],A,B,C);
get_value(b,col1);
A=b[0];
B=b[1];
C=b[2];
column_fill(col2,a[1],A,B,C);
get_value(b,col2);
A=b[0];
B=b[1];
C=b[2];
column_fill(col3,a[2],A,B,C);
get_value(b,col3);
A=b[0];
B=b[1];
C=b[2];
printf("\t\t\t The Given Blocks Is..: \n");
printf("\t\t\t\t\t%c %c %c \n",col1[2],col2[2],col3[2]);
printf("\t\t\t\t\t%c %c %c \n",col1[1],col2[1],col3[1]);
printf("\t\t\t\t\t%c %c %c \n",col1[0],col2[0],col3[0]);
printf("\t\t\t\t\t^ ^ ^ \n");
column_fill(ans,3,0,0,0);
printf("\t Make the Pattern Given Below in Any One of the Column To WIN \n");
printf("\t\t\t\t\t%c \n",ans[2]);
printf("\t\t\t\t\t%c \n",ans[1]);
printf("\t\t\t\t\t%c \n",ans[0]);
printf("\t\t\t\t\t^ \n");
position(posi,col1,0);
position(posi,col2,1);
position(posi,col3,2);
printf("\t\t\t Let The Game Begin \n");
play(col1,col2,col3,posi,ans);
printf("\t\t\t Thanks For Playing The Game. \n");
getch();
}
{
C=1;
}
}
b[0]=A;
b[1]=B;
b[2]=C;
}
//possibility for C
if(C==0)
{
col1[0]='C';
C=1;
break;
}
}
}
col1[1]='|';
col1[2]='|';
}
if(a==2)
{
for(i=0;i<2;i++)
{
while(1)
{
h=rand()%3;
if(h==0)
{
//possibility for A
if(A==0)
{
col1[i]='A';
A=1;
break;
}
}
if(h==1)
{
//possibility for B
if(B==0)
{
col1[i]='B';
B=1;
break;
}
}
if(h==2)
{
//possibility for C
if(C==0)
{
col1[i]='C';
C=1;
break;
}
}
}
}
col1[2]='|';
}
if(a==3)
{
for(i=0;i<3;i++)
{
while(1)
{
h=rand()%3;
if(h==0)
{
//possibility for A
if(A==0)
{
col1[i]='A';
A=1;
break;
}
}
if(h==1)
{
//possibility for B
if(B==0)
{
col1[i]='B';
B=1;
break;
}
}
if(h==2)
{
//possibility for C
if(C==0)
{
col1[i]='C';
C=1;
break;
}
}
}
}
}
}
while(1)
{
printf("\t\t\t Choose your option for Swapping \n");
printf("\t\t\t 1>1-2\t\t 2>1-3\n\t\t\t 3>2-1\t\t 4>2-3\n\t\t\t 5>3-1\t\t 6>3-
2\n\t\t\t 7>Display Result 8>EXIT \n\t\t\t\t\t");
scanf("\%d",&ch);
clrscr();
printf("\n\n\n\n\t\t\t Option %d selected...\n",ch);
switch(ch)
{
case 1: i=d[0];
j=d[1];
if(i!=-1)
{
j++;
temp=a[i];
a[i]=b[j];
b[j]=temp;
i--;
k++;
}
else
{
printf("\t\t\t Invalid Move.....\n");
}
d[0]=i;
d[1]=j;
break;
case 2: i=d[0];
j=d[2];
if(i!=-1)
{
j++;
temp=a[i];
a[i]=c[j];
c[j]=temp;
i--;
k++;
}
else
{
printf("\t\t\t Invalid Move..... \n");
}
d[0]=i;
d[2]=j;
break;
case 3: i=d[1];
j=d[0];
if(i!=-1)
{
j++;
temp=b[i];
b[i]=a[j];
a[j]=temp;
i--;
k++;
}
else
{
printf("\t\t\t Invalid Move..... \n");
}
d[1]=i;
d[0]=j;
break;
case 4: i=d[1];
j=d[2];
if(i!=-1)
{
j++;
temp=b[i];
b[i]=c[j];
c[j]=temp;
i--;
k++;
}
else
{
printf("\t\t\t Invalid Move..... \n");
}
d[1]=i;
d[2]=j;
break;
case 5: i=d[2];
j=d[0];
if(i!=-1)
{
j++;
temp=c[i];
c[i]=a[j];
a[j]=temp;
i--;
k++;
}
else
{
printf("\t\t\t Invalid Move..... \n");
}
d[2]=i;
d[0]=j;
break;
case 6: i=d[2];
j=d[1];
if(i!=-1)
{
j++;
temp=c[i];
c[i]=b[j];
b[j]=temp;
i--;
k++;
}
else
{
printf("\t\t\t Invalid Move..... \n");
}
d[2]=i;
d[1]=j;
break;
case 7: printf("%c \n",e[2]);
printf("%c \n",e[1]);
printf("%c \n",e[0]);
break;
case 8: exit(1);
break;
default: printf("Please Enter The Valid Choice \n");
break;
}
if(d[0]==2)
{
if(a[0]==e[0] && a[1]==e[1] && a[2]==e[2])
{
printf("\n\t\t\t BRAVO! You won the game in %d moves.....
\n",k);
break;
}
}
if(d[1]==2)
{
if(b[0]==e[0] && b[1]==e[1] && b[2]==e[2])
{
printf("\n\t\t\t BRAVO! You won the game in %d moves.....
\n",k);
break;
}
}
if(d[2]==2)
{
if(c[0]==e[0] && c[1]==e[1] && c[2]==e[2])
{
printf("\n\t\t\t BRAVO! You won the game in %d moves.....
\n",k);
break;
}
}
}
}
Input/Ouput:
PRACTICAL – 5
1 2 3 1 2 3 1 2 3
4 6 4 6 4 5 6
7 5 8 7 5 8 7 8
C Code:
#include<stdio.h>
#include<stdlib.h>
int final[3][3]={{1,2,3},{4,5,6},{7,8,0}};
int Print(int one[3][3])
{
int i=0,j=0;
printf("\n\t\t\tFor Quit Press 0\n");
printf("\n\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(one[i][j]==0)
{printf("\t[ ]");}
else
printf("\t[%d]",one[i][j]);
}
printf("\n");
}
}
int Random(int a[], int n, int high)
{
int i,j,again;
for(i=0; i<n;i++)
{
do{
again =0;
a[i]=rand()%9;
for(j=i-1; j>=0; j--)
{
if(a[j]==a[i])
{
again =1;
break;
}
}
} while(again);
}
}
int main()
{
int i=0,j=0;
int k=0,temp=0;
int one[3][3];
int check[9];
int m=0;
int r=10,t=0,n=9,high=9;
printf("GOAL State:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(final[i][j]==0)
{
printf("\t[ ]");
}
else
printf("\t[%d]",final[i][j]);
}
printf("\n");
}
srand(time(NULL));
Random(check,n,high);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
one[i][j]=check[m];
m++;
}
}
Print(one);
while(1)
{
if(r==0)
{
printf("\t\t\t----------You Quit----------");
break;
}
else
{for(i=0;i<3;i++)
{for(j=0;j<3;j++)
{if(one[i][j]==0)
{if(j==0 && i==0)
{
temp=one[i+1][j];
one[i+1][j]=one[i][j];
one[i][j]=temp;
Print(one);
goto l1;
}
else if(r==one[i][j-1]) //left to right move
{
temp=one[i][j-1];
one[i][j-1]=one[i][j];
one[i][j]=temp;
Print(one);
goto l1;
}
else if(r==0)
{
break;
}
else
{
printf("\n\n\t\t\t\t!!!!!Wrong Choice\n");
printf("\t\t\t----------Movement Cannot Possible----------");
}
}
else if(j==2 && i==0)
{
printf("\n\n\t\tEnter Your Choice to move::");
scanf("%d",&r);
if(r==one[i+1][j]) //down to up move
{
temp=one[i+1][j];
one[i+1][j]=one[i][j];
one[i][j]=temp;
Print(one);
goto l1;
}
else if(r==one[i][j-1]) //left to right movie
{
temp=one[i][j-1];
one[i][j-1]=one[i][j];
one[i][j]=temp;
Print(one);
goto l1;
}
else if(r==0)
{
break;
}
else
{
printf("\n\n\t\t\t\t!!!!!Wrong Choice\n");
printf("\t\t\t----------Movement Cannot Possible----------");
}
}
else if(j==0 && i==1)
{
printf("\n\n\t\tEnter Your Choice to move::");
scanf("%d",&r);
if(r==one[i][j+1]) //right to left move
{
temp=one[i][j+1];
one[i][j+1]=one[i][j];
one[i][j]=temp;
Print(one);
goto l1;
}
else if(r==one[i-1][j]) // up to down
{
temp=one[i-1][j];
one[i-1][j]=one[i][j];
one[i][j]=temp;
Print(one);
goto l1;
}
else if(r==one[i+1][j]) //down to up move
{
temp=one[i+1][j];
one[i+1][j]=one[i][j];
one[i][j]=temp;
Print(one);
goto l1;
}
else if(r==0)
{
break;
}
else
{
printf("\n\n\t\t\t\t!!!!!Wrong Choice\n");
printf("\t\t\t----------Movement Cannot Possible----------");
}
}
else if(j==1 && i==1)
{
printf("\n\n\t\tEnter Your Choice to move::");
scanf("%d",&r);
if(r==one[i][j+1]) //right to left move
{
temp=one[i][j+1];
one[i][j+1]=one[i][j];
one[i][j]=temp;
Print(one);
goto l1;
}
else if(r==one[i][j-1]) //left to right move
{
temp=one[i][j-1];
one[i][j-1]=one[i][j];
one[i][j]=temp;
Print(one);
goto l1;
}
else if(r==one[i+1][j]) //down to up mpve
{
temp=one[i+1][j];
one[i+1][j]=one[i][j];
one[i][j]=temp;
Print(one);
goto l1;
}
else if(r==one[i-1][j]) ///up to down move
{
temp=one[i-1][j];
one[i-1][j]=one[i][j];
one[i][j]=temp;
Print(one);
goto l1;
}
else if(r==0)
{
break;
}
else
{
printf("\n\n\t\t\t\t!!!!!Wrong Choice\n");
printf("\t\t\t----------Movement Cannot Possible----------");
}
}
else if(j==2 && i==1)
{
printf("\n\n\t\tEnter Your Choice to move::");
scanf("%d",&r);
if(r==one[i][j-1]) //left to right move
{
temp=one[i][j-1];
one[i][j-1]=one[i][j];
one[i][j]=temp;
Print(one);
goto l1;
}
else if(r==one[i-1][j]) //up to down move
{
temp=one[i-1][j];
one[i-1][j]=one[i][j];
one[i][j]=temp;
Print(one);
goto l1;
}
else if(r==one[i+1][j]) //down to up move
{
temp=one[i+1][j];
one[i+1][j]=one[i][j];
one[i][j]=temp;
Print(one);
goto l1;
}
else if(r==0)
{
break;
}
else
{
printf("\n\n\t\t\t\t!!!!!Wrong Choice\n");
printf("\t\t\t----------Movement Cannot Possible----------");
}
}
else if(j==0 && i==2)
{
printf("\n\n\t\tEnter Your Choice to move::");
scanf("%d",&r);
if(r==one[i-1][j]) //up to down move
{
temp=one[i-1][j];
one[i-1][j]=one[i][j];
one[i][j]=temp;
Print(one);
goto l1;
}
else if(r==one[i][j+1]) //right to left move
{
temp=one[i][j+1];
one[i][j+1]=one[i][j];
one[i][j]=temp;
Print(one);
goto l1;
}
else if(r==0)
break;
else
{
printf("\n\n\t\t\t\t!!!!!Wrong Choice\n");
printf("\t\t\t----------Movement Cannot Possible----------");
}
}
else if(j==1 && i==2)
{
printf("\n\n\t\tEnter Your Choice to move::");
scanf("%d",&r);
if(r==one[i-1][j]) //up to down move
{
temp=one[i-1][j];
one[i-1][j]=one[i][j];
one[i][j]=temp;
Print(one);
goto l1;
}
else if(r==one[i][j+1]) //right to left move
{
temp=one[i][j+1];
one[i][j+1]=one[i][j];
one[i][j]=temp;
Print(one);
goto l1;
}
else if(r==one[i][j-1]) //left to right move
{
temp=one[i][j-1];
one[i][j-1]=one[i][j];
one[i][j]=temp;
Print(one);
goto l1;
}
else if(r==0)
{
break;
}
else
{
printf("\n\n\t\t\t\t!!!!!Wrong Choice\n");
printf("\t\t\t----------Movement Cannot Possible----------");
}
}
else if(j==2 && i==2)
{
printf("\n\n\t\tEnter Your Choice to move::");
scanf("%d",&r);
if(r==one[i-1][j]) //up to down move
{
temp=one[i-1][j];
one[i-1][j]=one[i][j];
one[i][j]=temp;
Print(one);
goto l1;
}
else if(r==one[i][j-1]) //left to right move
{
temp=one[i][j-1];
one[i][j-1]=one[i][j];
one[i][j]=temp;
Print(one);
goto l1;
}
else if(r==0)
{
break;
}
else
{
printf("\n\n\t\t\t\t!!!!!Wrong Choice\n");
printf("\t\t\t----------Movement Cannot Possible----------");
}
}}
}
}
l1: t=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(one[i][j]==final[i][j])
t++;
}
}
//printf("\n%d\n",t);
if(t==9)
{
//Print(one);
printf("\n\t\t\t----------Congratulation You Win--------\n");
break;
}
}
}
return 0;
}
Input/Output:
PRACTICAL – 6
The heuristic function is a way to inform the search about the direction to a goal. It provides an
informed way to guess which neighbor of a node will lead to a goal.
Local search algorithms will not always find the correct or optimal solution, if one exists.
Global search algorithms will always find the correct or optimal solution if there is one, given
enough time. An algorithm like A* uses global heuristics.
Rules:
Local heuristic:
– Add one point for every block that is resting on the thing it is supposed to be resting on.
– Subtract one point for every block that is sitting on a wrong thing.
Global heuristic:
– For each block that has the correct support structure add one point for every block in the
support structure.
– For each block that has an incorrect support structure subtract one point for every block in
the existing support structure.
Examples:
Local heuristic:
Here is a simple blocks world problem:
Step 1: move ‘C’ to column 3 HV= 0 Step 2: move ‘D’ to column 2 HV= +2
Global heuristic:
Step 1: move ‘C’ to column 3 HV= -1 Step 2: move ‘D’ to column 2 HV= +1
C Code:
//C program for blocks world game using global heuristic search
#include<stdio.h>
#include<stdlib.h>
void get_value(int b[],char a[]);
void position(int posi[],char b[],int x);
void column_fill(char col1[],int a, int A, int B, int C);
void play(char a[],char b[],char c[],int d[],char e[]);
int check_heuristic(char a[],char b[],char c[],int d[],char e[]);
int select_option(char a[],char b[],char c[],int d[],char e[]);
void main()
{
int i,j,blocks=3,posi[3]={-1,-1,-1};
int a[3],sum,A=0,B=0,C=0,h,b[3]={0,0,0};
char col1[3],col2[3],col3[3],ans[3];
clrscr();
j=blocks;
printf("\t\t\t ******BLOCKS WORLD GAME***** \n");
randomize();//select random value....
while(sum!=j)
{
sum=0;
for(i=0;i<j;i++)
{
a[i]=rand()%4;
sum=sum+a[i];
}
}
column_fill(col1,a[0],A,B,C);
get_value(b,col1);
A=b[0];
B=b[1];
C=b[2];
column_fill(col2,a[1],A,B,C);
get_value(b,col2);
A=b[0];
B=b[1];
C=b[2];
column_fill(col3,a[2],A,B,C);
get_value(b,col3);
A=b[0];
B=b[1];
C=b[2];
printf("\t\t\t The Given Blocks Is..: \n");
printf("\t\t\t\t\t%c %c %c \n",col1[2],col2[2],col3[2]);
printf("\t\t\t\t\t%c %c %c \n",col1[1],col2[1],col3[1]);
printf("\t\t\t\t\t%c %c %c \n",col1[0],col2[0],col3[0]);
printf("\t\t\t\t\t^ ^ ^ \n");
column_fill(ans,3,0,0,0);
printf("\t Make the Pattern Given Below in Any One of the Column To WIN \n");
printf("\t\t\t\t\t%c \n",ans[2]);
printf("\t\t\t\t\t%c \n",ans[1]);
printf("\t\t\t\t\t%c \n",ans[0]);
printf("\t\t\t\t\t^ \n");
//indicates column position.....
position(posi,col1,0);
position(posi,col2,1);
position(posi,col3,2);
printf("\t\t\t Let The Game Begin \n");
play(col1,col2,col3,posi,ans);
printf("\t\t\t Thanks For Playing The Game. \n");
getch();
}
{
B=1;
}
if(a[i]=='C')
{
C=1;
}
}
b[0]=A;
b[1]=B;
b[2]=C;
}
}
}
if(h==2)
{
//possibility for C
if(C==0)
{
col1[0]='C';
C=1;
break;
}
}
}
col1[1]='|';
col1[2]='|';
}
if(a==2)
{
for(i=0;i<2;i++)
{
while(1)
{
h=rand()%3;
if(h==0)
{
//possibility for A
if(A==0)
{
col1[i]='A';
A=1;
break;
}
}
if(h==1)
{
//possibility for B
if(B==0)
{
col1[i]='B';
B=1;
break;
}
}
if(h==2)
{
//possibility for C
if(C==0)
{
col1[i]='C';
C=1;
break;
}
}
}
}
col1[2]='|';
}
if(a==3)
{
for(i=0;i<3;i++)
{
while(1)
{
h=rand()%3;
if(h==0)
{
//possibility for A
if(A==0)
{
col1[i]='A';
A=1;
break;
}
}
if(h==1)
{
//possibility for B
if(B==0)
{
col1[i]='B';
B=1;
break;
}
}
if(h==2)
{
//possibility for C
if(C==0)
{
col1[i]='C';
C=1;
break;
}
}
}
}
}
}
if(d[1]==2)
{
if(b[0]==e[0] && b[1]==e[1] && b[2]==e[2])
{
printf("\n\t\t\t BRAVO! You won the game in
%d moves..... \n",k);
break;
}
}
if(d[2]==2)
{
if(c[0]==e[0] && c[1]==e[1] && c[2]==e[2])
{
printf("\n\t\t\t BRAVO! You won the game in
%d moves..... \n",k);
break;
}
}
printf("\t\t\t Press 1 for Check Optimal Option\n\t\t\t press 2 COMPUTER'S
MOVE\n\t\t\t Press 3 Display Current State\n\t\t\t Press 4 for
EXIT..\n\t\t\t\t\t");
scanf("%d",&key);
switch(key)
{
case 1: clrscr();//best optimal option...
if(d[0]==2)
{
if(a[0]==e[0] && a[1]==e[1] && a[2]==e[2])
{
printf("\n\t\t\t BRAVO! You won the game in %d
moves..... \n",k);
break;
}
}
if(d[1]==2)
{
if(b[0]==e[0] && b[1]==e[1] && b[2]==e[2])
{
printf("\n\t\t\t BRAVO! You won the game in %d
moves..... \n",k);
break;
}
}
if(d[2]==2)
{
if(c[0]==e[0] && c[1]==e[1] && c[2]==e[2])
{
printf("\n\t\t\t BRAVO! You won the game in %d
moves..... \n",k);
break;
}
}
ch=select_option(a,b,c,d,e);
printf("\n\t\t\t Best OPTIMAL Option is %d...\n",ch);
break;
case 2:
clrscr();//computer's move...
ch=select_option(a,b,c,d,e);
clrscr();
case 4: i=d[1];
j=d[2];
if(i!=-1)
{
j++;
temp=b[i];
b[i]=c[j];
c[j]=temp;
i--;
k++;
}
d[1]=i;
d[2]=j;
break;
case 5: i=d[2];
j=d[0];
if(i!=-1)
{
j++;
temp=c[i];
c[i]=a[j];
a[j]=temp;
i--;
k++;
}
d[2]=i;
d[0]=j;
break;
case 6: i=d[2];
j=d[1];
if(i!=-1)
{
j++;
temp=c[i];
c[i]=b[j];
b[j]=temp;
i--;
k++;
}
d[2]=i;
d[1]=j;
break;
default: printf("\t\t\t Please Enter The Valid Choice \n");
break;
}
printf("\n\t\t\t Current state\t Goal state\n");
if(d[0]==2)
{
if(a[0]==e[0] && a[1]==e[1] && a[2]==e[2])
{
printf("\n\t\t\t BRAVO! You won the game in %d moves..... \
n",k);
break;
}
}
if(d[1]==2)
{
if(b[0]==e[0] && b[1]==e[1] && b[2]==e[2])
{
printf("\n\t\t\t BRAVO! You won the game in %d moves.....
\n",k);
break;
}
}
if(d[2]==2)
{
if(c[0]==e[0] && c[1]==e[1] && c[2]==e[2])
{
printf("\n\t\t\t BRAVO! You won the game in %d moves.....
\n",k);
break;
}
}
break;
case 3: clrscr();
printf("\n\t\t\t Current state\t Goal state\n");
printf("\t\t\t %c %c %c\t ",a[2],b[2],c[2]);
printf("\t%c\n",e[2]);
printf("\t\t\t %c %c %c\t ",a[1],b[1],c[1]);
printf("\t%c\n",e[1]);
printf("\t\t\t %c %c %c\t ",a[0],b[0],c[0]);
printf("\t%c\n",e[0]);
printf("\t\t\t ^ ^ ^ \t\t^\n");
l=check_heuristic(a,b,c,d,e);
printf("\n\t\t\t current heuristic value is:%d\n",l);
break;
case 4: exit(1);
break;
}
}
}
heu1=-3;
}
}
}
else
{
heu1=0;
}
if(d[1]!=-1)
{
if(d[1]==0)
{
heu2=0;
}
if(d[1]==1)
{
if(b[0]==e[0] && b[1]==e[1])
{
heu2=1;
}
else
{
heu2=-1;
}
}
if(d[1]==2)
{
if(b[0]==e[0] && b[1]==e[1] && b[2]==e[2])
{
heu2=3;
}
else if(b[0]==e[0] && b[1]==e[1])
{
heu2=1;
}
else
{
heu2=-3;
}
}
}
else
{
heu2=0;
}
if(d[2]!=-1)
{
if(d[2]==0)
{
heu3=0;
}
if(d[2]==1)
{
if(c[0]==e[0] && c[1]==e[1])
{
heu3=1;
}
else
{
heu3=-1;
}
}
if(d[2]==2)
{
if(c[0]==e[0] && c[1]==e[1] && c[2]==e[2])
{
heu3=3;
}
else if(c[0]==e[0] && c[1]==e[1])
{
heu3=1;
}
else
{
heu3=-3;
}
}
}
else
{
heu3=0;
}
return heu1+heu2+heu3;
}
switch(m)
{
case 1: i=d[0];
j=d[1];
if(i!=-1)
{
j++;
temp=a[i];
a[i]=b[j];
b[j]=temp;
i--;
flag=0;
}
else
{
flag=1;
}
d[0]=i;
d[1]=j;
break;
case 2: i=d[0];
j=d[2];
if(i!=-1)
{
j++;
temp=a[i];
a[i]=c[j];
c[j]=temp;
i--;
flag=0;
}
else
{
flag=1;
}
d[0]=i;
d[2]=j;
break;
case 3: i=d[1];
j=d[0];
if(i!=-1)
{
j++;
temp=b[i];
b[i]=a[j];
a[j]=temp;
i--;
flag=0;
}
else
{
flag=1;
}
d[1]=i;
d[0]=j;
break;
case 4: i=d[1];
j=d[2];
if(i!=-1)
{
j++;
temp=b[i];
b[i]=c[j];
c[j]=temp;
i--;
flag=0;
}
else
{
flag=1;
}
d[1]=i;
d[2]=j;
break;
case 5: i=d[2];
j=d[0];
if(i!=-1)
{
j++;
temp=c[i];
c[i]=a[j];
a[j]=temp;
i--;
flag=0;
}
else
{
flag=1;
}
d[2]=i;
d[0]=j;
break;
case 6: i=d[2];
j=d[1];
if(i!=-1)
{
j++;
temp=c[i];
c[i]=b[j];
b[j]=temp;
i--;
flag=0;
}
else
{
flag=1;
}
d[2]=i;
d[1]=j;
break;
j--;
d[0]=i;
d[2]=j;
break;
case 3: i=d[1];
j=d[0];
i++;
temp=a[j];
a[j]=b[i];
b[i]=temp;
j--;
d[1]=i;
d[0]=j;
break;
case 4: i=d[1];
j=d[2];
i++;
temp=c[j];
c[j]=b[i];
b[i]=temp;
j--;
d[1]=i;
d[2]=j;
break;
case 5: i=d[2];
j=d[0];
i++;
temp=a[j];
a[j]=c[i];
c[i]=temp;
j--;
d[2]=i;
d[0]=j;
break;
case 6: i=d[2];
j=d[1];
i++;
temp=b[j];
b[j]=c[i];
c[i]=temp;
j--;
d[2]=i;
d[1]=j;
break;
break;
}
}
if(flag==1)
{
temp1[m]=-99;
}
m++;
}
large=temp1[1];
for(p=2;p<=6;p++)
{
if(temp1[p]>large)
{
large=temp1[p];
temp3=p;
}
}
printf("\n\t\t\t Current state\t Goal state\n");
printf("\t\t\t %c %c %c\t ",a[2],b[2],c[2]);
printf("\t%c\n",e[2]);
printf("\t\t\t %c %c %c\t ",a[1],b[1],c[1]);
printf("\t%c\n",e[1]);
printf("\t\t\t %c %c %c\t ",a[0],b[0],c[0]);
printf("\t%c\n",e[0]);
printf("\t\t\t ^ ^ ^ \t\t^\n");
return temp3;
}
Input/Output:
PRACTICAL – 7
A* (A star) is a search algorithm that is used for finding path from one node to another. So it can
be compared with Breadth First Search, or Dijkstra’s algorithm, or Depth First Search, or
Best First Search. A* algorithm is widely used in graph search for being better in efficiency and
accuracy, where graph pre-processing is not an option.
At each iteration of its main loop, A* needs to determine which of its paths to extend. It does so
based on the cost of the path and an estimate of the cost required to extend the path all the way to
the goal. Specifically, A* selects the path that minimizes
f(n)=g(n)+h(n)
A* terminates when the path it chooses to extend is a path from start to goal or if there are no paths
eligible to be extended. The heuristic function is problem-specific. If the heuristic function is
admissible, meaning that it never overestimates the actual cost to get to the goal, A* is guaranteed
to return a least-cost path from start to goal.
• We first move the empty space in all the possible directions in the start state and calculate
f-score for each state. This is called expanding the current state.
• After expanding the current state, it is pushed into the closed list and the newly generated
states are pushed into the open list.
• A state with the least f-score is selected and expanded again. This process continues until
the goal state occurs as the current state.
• Basically, here we are providing the algorithm a measure to choose its actions. The
algorithm chooses the best possible action and proceeds in that path.
• This solves the issue of generating redundant child states, as the algorithm will expand the
node with the least f-score.
• h-score as the number of misplaced tiles by comparing the current state and the goal state
or summation of the Manhattan distance between misplaced nodes.
• g-score will remain as the number of nodes traversed from start node to get to the current
node.
Example:
C Code:
int i=0,j=0;
//printf("\n\t\t\tFor Quit Press 0\n");
printf("\n\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(one[i][j]==0)
printf("\t[ ]");
else
printf("\t[%d]",one[i][j]);
}
printf("\n");
}
}
int up(int random[3][3]);
int down(int random[3][3]);
int left(int random[3][3]);
int right(int random[3][3]);
int diff(int final[3][3],int random[3][3]);
int min(int diff_D, int diff_U, int diff_L, int diff_R);
int main()
{
int i=0,j=0,temp=0,count=0,flag=0,diff_D,diff_U,diff_L,diff_R,Min;
int check[9], random[3][3]={{1,2,3},{4,5,0},{7,8,6}};
printf("GOAL State:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(final[i][j]==0)
printf("\t[ ]");
else
printf("\t[%d]",final[i][j]);
}
printf("\n");
}
printf("\n Random State:");
Print(random);
while(1)
{
diff_D=50, diff_U=50, diff_L=50, diff_R=50;
flag=up(random);
if(flag != 1)
{
printf("\n Possible state when number shifted down:");
Print(random);
diff_U = diff(final,random);
printf("\n Value of f = %d",diff_U);
down(random);
}
flag=down(random);
if(flag != 1)
{
printf("\n Possible state when number shifted up:");
Print(random);
diff_D = diff(final,random);
printf("\n Value of f = %d",diff_D);
up(random);
}
flag=left(random);
if(flag != 1)
{
printf("\n Possible state when number shifted right:");
Print(random);
diff_L = diff(final,random);
printf("\n Value of f = %d",diff_L);
right(random);
}
flag=right(random);
if(flag != 1)
{
printf("\n Possible state when number shifted left:");
Print(random);
diff_R = diff(final,random);
printf("\n Value of f = %d",diff_R);
left(random);
}
Min=min(diff_D,diff_U,diff_L,diff_R);
if(Min == diff_D)
{
down(random);
printf("\n Move Up");
Print(random);
printf("\n");
}
else if(Min == diff_U)
{
up(random);
printf("\n Move Down");
Print(random);
printf("\n");
}
else if(Min == diff_L)
{
left(random);
printf("\n Move Right");
Print(random);
printf("\n");
}
else
right(random);
printf("\n Move Left");
Print(random);
printf("\n");
if(Min == 0)
printf("\n Congratulations You Win !!!");
break;
}
return 0;
}
{
if(i!=0)
{
i=i-1;
if(i<0)
np=1;
else
temp=random[i][j];
random[i][j]=random[i+1][j];
random[i+1][j]=temp;
}
else
np=1;
}
}
}
return np;
}
}
return np;
}
random[i][j]=random[i-1][j];
random[i-1][j]=temp;
}
}
}
return np;
}
Input/Output: