Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 90

Artificial Intelligence (2180703)

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.

Rules of the Game:


 The game is to be played between two people (in this program between PLAYER and
COMPUTER or between PLAYER and PLAYER).
 One of the player chooses ‘O’ and the other ‘X’ to mark their respective cells.
 The game starts with one of the players and the game ends when one of the players has one
whole row/ column/ diagonal filled with his/her respective character (‘O’ or ‘X’).
 If no one wins, then the game is said to be draw.

Here is an example game won by the first player, X:

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' ||

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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:

printf("Enter player-1 name:");


scanf("%s",player1);
printf("Enter player-2 name:");
scanf("%s",player2);
printf("\n%s symbol : 0\n",player1);
printf("%s symbol : X\n",player2);
srand(time(0));
random_no=rand();
if(random_no%2==0)
{
index=1;
}
else
{
index=2;
}

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

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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:

printf("Enter player-1 name:");


scanf("%s",player1);
printf("\n\nComputer symbol : X");
printf("\n%s symbol : 0",player1);
srand(time(0));
random_no=rand();
if(random_no%2==0)
{
index=1;
}

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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';

else if(m[4]=='O'&&m[0]==' ')


m[0]='X';
//CENTER COMPUTER WIN
else if(m[4]=='X'&&m[1]=='X'&&m[7]==' ')
m[7]='X';
else if(m[4]=='X'&&m[7]=='X'&&m[1]==' ')
m[1]='X';
else if(m[4]=='X'&&m[5]=='X'&&m[3]==' ')
m[3]='X';
else if(m[4]=='X'&&m[3]=='X'&&m[5]==' ')
m[5]='X';
else if(m[4]=='X'&&m[2]=='X'&&m[6]==' ')

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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';

//SIDES COMPUTER WIN


else if(m[0]=='X'&&m[2]=='X'&&m[1]==' ')
m[1]='X';
else if(m[0]=='X'&&m[1]=='X'&&m[2]==' ')
m[2]='X';
else if(m[1]=='X'&&m[2]=='X'&&m[0]==' ')
m[0]='X';

else if(m[2]=='X'&&m[8]=='X'&&m[5]==' ')


m[5]='X';
else if(m[5]=='X'&&m[2]=='X'&&m[8]==' ')
m[8]='X';
else if(m[5]=='X'&&m[8]=='X'&&m[2]==' ')
m[2]='X';

else if(m[6]=='X'&&m[8]=='X'&&m[7]==' ')


m[7]='X';
else if(m[6]=='X'&&m[7]=='X'&&m[8]==' ')
m[8]='X';
else if(m[7]=='X'&&m[8]=='X'&&m[6]==' ')
m[6]='X';

else if(m[0]=='X'&&m[6]=='X'&&m[3]==' ')


m[3]='X';
else if(m[0]=='X'&&m[3]=='X'&&m[6]==' ')
m[6]='X';
else if(m[3]=='X'&&m[6]=='X'&&m[0]==' ')
m[0]='X';

//CENTER PLAYER NOT WIN


else if(m[4]=='O'&&m[1]=='O'&&m[7]==' ')
m[7]='X';
else if(m[4]=='O'&&m[7]=='O'&&m[1]==' ')
m[1]='X';
else if(m[4]=='O'&&m[5]=='O'&&m[3]==' ')
m[3]='X';
else if(m[4]=='O'&&m[3]=='O'&&m[5]==' ')
m[5]='X';

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

else if(m[4]=='O'&&m[2]=='O'&&m[6]==' ')


m[6]='X';
else if(m[4]=='O'&&m[0]=='O'&&m[8]==' ')
m[8]='X';
else if(m[4]=='O'&&m[8]=='O'&&m[0]==' ')
m[0]='X';
else if(m[4]=='O'&&m[6]=='O'&&m[2]==' ')
m[2]='X';

//SIDE PLAYER NOT WIN


else if(m[0]=='O'&&m[2]=='O'&&m[1]==' ')
m[1]='X';
else if(m[0]=='O'&&m[1]=='O'&&m[2]==' ')
m[2]='X';
else if(m[1]=='O'&&m[2]=='O'&&m[0]==' ')
m[0]='X';

else if(m[2]=='O'&&m[8]=='O'&&m[5]==' ')


m[5]='X';
else if(m[5]=='O'&&m[2]=='O'&&m[8]==' ')
m[8]='X';
else if(m[5]=='O'&&m[8]=='O'&&m[2]==' ')
m[2]='X';

else if(m[6]=='O'&&m[8]=='O'&&m[7]==' ')


m[7]='X';
else if(m[6]=='O'&&m[7]=='O'&&m[8]==' ')
m[8]='X';
else if(m[7]=='O'&&m[8]=='O'&&m[6]==' ')
m[6]='X';

else if(m[0]=='O'&&m[6]=='O'&&m[3]==' ')


m[3]='X';
else if(m[0]=='O'&&m[3]=='O'&&m[6]==' ')
m[6]='X';
else if(m[3]=='O'&&m[6]=='O'&&m[0]==' ')
m[0]='X';

//DRAW
else if(m[0]==' ')
m[0]='X';
else if(m[1]==' ')
m[1]='X';
else if(m[2]==' ')
m[2]='X';

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

else if(m[3]==' ')


m[3]='X';
else if(m[4]==' ')
m[4]='X';
else if(m[5]==' ')
m[5]='X';
else if(m[6]==' ')
m[6]='X';
else if(m[7]==' ')
m[7]='X';
else if(m[8]==' ')
m[8]='X';

display(m);
index++;
i=initcheck(m,"Computer");
}
}
break;

default:
printf("Please, enter correct choice!!!");
}
return 0;
}

Output: Player to Player ( Draw Condition )

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

Output: Player to Player ( Win Condition )

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

Output: Player to Computer ( Draw Condition )

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

Output: Player to Computer ( Win Condition )

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

PRACTICAL – 2

Write a program to implement Water Jug Problem with Breadth-First-Search


(BFS) in C.

About Water Jug Problem:


Water jug problem also called as water pouring puzzles or measuring puzzles. Water Jug problem
can be stated as follows: You are given a m liter jug and a n liter jug Both the jugs are initially
empty. The jugs don’t have markings to allow measuring smaller quantities. You have to use the
jugs to measure d liters of water where d is less than n.
(X, Y) corresponds to a state where X refers to amount of water in Jug1 and Y refers to amount of
water in Jug2 Determine the path from initial state (xi, yi) to final state (xf, yf), where
(xi, yi) is (0, 0) which indicates both Jugs are initially empty and (xf, yf) indicates a state which
could be (0, d) or (d, 0).
The operations you can perform are:
1. Empty a Jug, (X, Y)-> (0, Y) Empty Jug 1
2. Fill a Jug, (0, 0)-> (X, 0) Fill Jug 1
3. Pour water from one jug to the other until one of the jugs is either empty or full, (X, Y) ->
(X-d, Y+d)

BFS (Breath First Search):


Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures.
It starts at the tree root and explores the neighbor nodes first, before moving to the next level
neighbors.
It is more like brute forcing on a tree/graph checking all nodes at a certain depth and then moving
on deeper. This isn’t generally used in path finding or problems where there are a huge number of
nodes possible since it traverses to each and every node which consumes a lot of memory. This is
more used where there is no available heuristic to reach the goal and when the goal is more likely
to be non-leaf nodes or closer to the root.

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.

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

C Code:

#include<stdio.h>

int initial(int,int,int,int);

int BFS(int,int,int,int,int,int);

int min(int,int);

int add(int x,int y);

int remove();

int display();

int duplicate(int,int);

int gcd(int x,int y);

#define MAX 300

int q_array[MAX];

int rear = -1;

int front = - 1;

int initial(int jug1,int jug2,int goal_jug,int goal_cap)

if(goal_jug==1 && goal_cap>jug1 || goal_jug==2 && goal_cap>jug2)

printf("\n you cannot fill the jug");

else if(goal_cap==0)

printf("\n no need to fill jug");

else if(goal_jug==1 && goal_cap==jug1 || goal_jug==2 && goal_cap==jug2)

printf("\n you can directly fill the jug");

else if(jug1<1 || jug2<1)

printf("\n enter value grater than 0");

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

else if(jug1==jug2 && goal_cap!=jug1)

printf("\n Solution is not possible.\n");

else if(goal_cap%(gcd(jug1,jug2))!=0)

printf("\n Solution is not possible.\n");

else

return 0;

int min(int x,int y)

if(x<y)

return x;

else

return y;

int add(int x,int y)

if (rear == MAX - 1)

printf("Queue Overflow \n");

else

if (front == - 1)

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

front = 0;

rear = rear + 1;

q_array[rear] = x;

rear = rear + 1;

q_array[rear]=y;

int duplicate(int x,int y)

int i;

for(i=front;i<=rear;i=i+2)

if(i!=(rear-1) && q_array[i]==x && q_array[i+1]==y)

return 1;

return 0;

int BFS(int f,int s,int goal_jug,int goal_cap,int jug1,int jug2)

int w,step=0,temp,count=0,j1fill=0,j2fill=0;

if(f==0 && s==0)

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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);

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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);

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

if(s==jug2)

j2fill=0;

j1fill=f;

step++;

if(duplicate(j1fill,j2fill)==0)

printf("\n(%d,%d)",j1fill,j2fill);

add(j1fill,j2fill);

if(f==0 && s==jug2)

j1fill=s;

j2fill=f;

step++;

if(duplicate(j1fill,j2fill)==0 && j1fill<=jug1 && j2fill<=jug2)

printf("\n(%d,%d)",j1fill,j2fill);

add(j1fill,j2fill);

if(f<jug1 && s==jug2 && jug1>jug2 && f>0)

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

j1fill=jug1;

j2fill=s-(j1fill-f);

step++;

if(duplicate(j1fill,j2fill)==0 && j2fill>0)

printf("\n(%d,%d)",j1fill,j2fill);

add(j1fill,j2fill);

if(f==jug1 && s<jug2 && jug1<jug2 && s>0)

j2fill=jug2;

j1fill=jug1-(jug2-j2fill);

step++;

if(duplicate(j1fill,j2fill)==0 && j1fill>0)

printf("\n(%d,%d)",j1fill,j2fill);

add(j1fill,j2fill);

if(f==0 && s==jug2 && jug1<jug2)

j1fill=jug1;

j2fill=jug2-j1fill;

if(duplicate(j1fill,j2fill)==0)

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

printf("\n(%d,%d)",j1fill,j2fill);

add(j1fill,j2fill);

if(f==jug1 && s==0 && jug2>jug1)

j1fill=s;

j2fill=f;

if(duplicate(j1fill,j2fill)==0)

printf("\n(%d,%d)",j1fill,j2fill);

add(j1fill,j2fill);

if(f!=0 && s!=jug2 && jug1>jug2)

temp=min(jug2-s,f);

j2fill=s+temp;

j1fill=f-temp;

step++;

if(duplicate(j1fill,j2fill)==0)

printf("\n(%d,%d)",j1fill,j2fill);

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

add(j1fill,j2fill);

if(f!=jug1 && s!=jug2 && jug1<jug2)

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);

if(f==jug1 && s<jug2 && jug1<jug2)

temp=min(f,jug2-s);

j2fill=s+temp;

j1fill=f-temp;

step++;

if(duplicate(j1fill,j2fill)==0 && j1fill>0)

printf("\n(%d,%d)",j1fill,j2fill);

add(j1fill,j2fill);

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

if(s==0 && jug1<jug2)

j1fill=s;

j2fill=f;

step++;

if(duplicate(j1fill,j2fill)==0)

printf("\n(%d,%d)",j1fill,j2fill);

add(j1fill,j2fill);

if(f==0 && jug1<jug2)

j1fill=s;

j2fill=f;

step++;

if(duplicate(j1fill,j2fill)==0 && j1fill<=jug1)

printf("\n(%d,%d)",j1fill,j2fill);

add(j1fill,j2fill);

count++;

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

return step;

int gcd(int x,int y)

while(x!=y)

if(x>y)

return gcd(x-y , y);

else

return gcd(x , y-x);

return x;

int main()

int x=0,y=0,ans,d,c1fill,c2fill,f,s,i,c;

int jug1,jug2,goal_cap,goal_jug;

printf("\n Capacity of jug 1 is:");

scanf("%d",&jug1);

printf("\n Capacity of jug 2 is:");

scanf("%d",&jug2);

printf("\n Enter the goal jug:");

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

scanf("%d",&goal_jug);

printf("\n Required water in jug%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("\n Invalid Data");

printf("(0,0)");

add(0,0);

for(i = front; i <= rear; i=i+2)

f=q_array[i];

s=q_array[i+1];

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

if(f==c1fill && s==c2fill)

return 0;

BFS(f,s,goal_jug,goal_cap,jug1,jug2);

Input/output:

1) If both the jugs have same capacity:

2) If the required water in any jug is more than the given capacity:

3) If jugs capacity is equal to 0:

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

4) Not Possible Solution:

5) Otherwise:

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

PRACTICAL – 3

Write a program to implement Water Jug Problem with Depth-First-Search


(DFS) in C.

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

In which jug you want to fill:1

Water required:2L

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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
{

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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

int DFS(int jug1, int jug2, intsel_jug, int water)


{
Int jug1fill=0,jug2fill=0, count=0, step=0, step1=0,
temp,i,j1f1[50]={0},j2f1[50]={0},j1f2[50]={0},j2f2[50]={0},flag1=0,flag2=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

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

{
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)
{

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

if(flag1==1 && flag2==1)


{
if(step<step1)
{
jug2fill=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
{
jug2fill=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)
{
jug2fill=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
{
jug2fill=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);
}
}

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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;

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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;

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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:-

1. Require amount of water in jug1.

2. Require amount of water in jug2.

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

3. For solution is not possible.

4. Jug capacity is equal.

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

PRACTICAL – 4

Implement Blocks World Game in C.


About Blocks World Problem:
The blocks world is one of the most famous planning domains in artificial intelligence. Imagine a
set of wooden blocks of various articles and colours sitting on a table. The goal is to build one or
more vertical stacks of blocks. The catch is that only one block may be moved at a time: it may
either be placed on the table or placed atop another block. Because of this, any blocks that are, at
a given time, under another block cannot be moved. Moreover, some kinds of blocks cannot have
other blocks stacked on top of them.

Example:
Here is a simple blocks world problem:

Start State Goal State

Step 1: move ‘C’ to column 3 Step 2: move ‘D’ to column 2

Step 3: move ‘A’ to column 2

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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]);

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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();
}

void position(int posi[],char b[],int x)


{
int i,h=-1;
for(i=0;i<3;i++)
{
if(b[i]!='|')
{
h++;
}
}
posi[x]=h;
}

void get_value(int b[],char a[])


{
int A,B,C,i;
A=b[0];
B=b[1];
C=b[2];
for(i=0;i<3;i++)
{
if(a[i]=='A')
{
A=1;
}
if(a[i]=='B')
{
B=1;
}
if(a[i]=='C')

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

{
C=1;
}
}
b[0]=A;
b[1]=B;
b[2]=C;
}

void column_fill(char col1[],int a,int A,int B,int C)


{
int h=0,i=0;
if(a==0)
{
for(i=0;i<3;i++)
{
col1[i]='|';
}
}
if(a==1)
{
while(1)
{
h=rand()%3;
if(h==0)
{
//possibility for A
if(A==0)
{
col1[0]='A';
A=1;
break;
}
}
if(h==1)
{
//possibility for B
if(B==0)
{
col1[0]='B';
B=1;
break;
}
}
if(h==2)
{

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

//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;

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

}
}

void play(char a[],char b[],char c[],int d[],char e[])


{
int ch,i,j,k=0;
char temp;

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++;

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

break;
}

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

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

}
}

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

Input/Ouput:

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

PRACTICAL – 5

Implement 8-puzzle problem in C.

About 8-Puzzle Problem:


The 8-puzzle is a square tray in which eight square tiles are placed. The remaining ninth square is
uncovered. Each tile has a number on it. A tile that is adjacent to the blank space can be slid into
that space. A game consists of a starting position and a specified goal position. The goal is to
transform the starting position into the goal position by sliding the tiles around.
Rules for solving the puzzle: Instead of moving the tiles in the empty space we can visualize
moving the empty space in place of the tile, basically swapping the tile with the empty space. The
empty space can only move in four directions viz.,
1. Up
2.Down
3. Right or
4. Left
The empty space cannot move diagonally and can take only one step at a time (i.e. move the empty
space one position at a time).
Example:

1 2 3 1 2 3 1 2 3

4 6 4 6 4 5 6

7 5 8 7 5 8 7 8

Initial state Goal state

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

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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;

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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)

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

{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]) //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==0)
{
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]) //down to up move

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

{
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];

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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;

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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;

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

}
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

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

{
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;

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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
{

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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++)

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

{
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:

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

PRACTICAL – 6

Write a program to implement single player game (Blocks-World game) using


Global Heuristic Function.

About Heuristic Function:


In computer science, heuristic is a technique designed for solving a problem more quickly when
classic methods are too slow or when classic methods fail to find any exact solution.

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:

Start State Heuristic Value(HV) = -2 Goal State Heuristic Value(HV) = +4

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

Step 1: move ‘C’ to column 3 HV= 0 Step 2: move ‘D’ to column 2 HV= +2

Step 3: move ‘A’ to column 2 HV= +4

Global heuristic:

Start State Heuristic Value(HV) = -2 Goal State Heuristic Value(HV) = +3

Step 1: move ‘C’ to column 3 HV= -1 Step 2: move ‘D’ to column 2 HV= +1

Step 3: move ‘A’ to column 2 HV= +3

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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();
}

void position(int posi[],char b[],int x)


{
int i,h=-1;
for(i=0;i<3;i++)
{
if(b[i]!='|')
{
h++;
}
}
posi[x]=h;
}

void get_value(int b[],char a[])


{
int A,B,C,i;
A=b[0];
B=b[1];
C=b[2];
for(i=0;i<3;i++)
{
if(a[i]=='A')
{
A=1;
}
if(a[i]=='B')

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

{
B=1;
}
if(a[i]=='C')
{
C=1;
}
}
b[0]=A;
b[1]=B;
b[2]=C;
}

void column_fill(char col1[],int a,int A,int B,int C)


{
int h=0,i=0;
if(a==0)
{
for(i=0;i<3;i++)
{
col1[i]='|';
}
}
if(a==1)
{
while(1)
{
h=rand()%3;
if(h==0)
{
//possibility for A
if(A==0)
{
col1[0]='A';
A=1;
break;
}
}
if(h==1)
{
//possibility for B
if(B==0)
{
col1[0]='B';
B=1;
break;

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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;

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

}
}
}
}
}
}

void play(char a[],char b[],char c[],int d[],char e[])


{
int ch,i,l,j,key,t,k=0,temp4;
char temp;
//intial heuristic value.....
i=check_heuristic(a,b,c,d,e);
printf("\n\t\t\t Current heuristic value is:%d\n",i);
while(1)
{
printf("\n\t\t\t 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\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;

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

}
}
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();

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

printf("\n\t\t\t Best OPTIMAL Option is %d...",ch);


printf("\n\t\t\t COMPUTER Select %d Option....\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++;
}
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++;
}
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++;
}
d[1]=i;
d[0]=j;
break;

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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);

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]);

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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

int check_heuristic(char a[],char b[],char c[],int d[],char e[])


{
int heu1=-3,heu2=-3,heu3=-3;
if(d[0]!=-1)
{
if(d[0]==0)
{
heu1=0;
}
if(d[0]==1)
{
if(a[0]==e[0] && a[1]==e[1])
{
heu1=1;
}
else
{
heu1=-1;
}
}
if(d[0]==2)
{
if(a[0]==e[0] && a[1]==e[1] && a[2]==e[2])
{
heu1=3;
}
else if(a[0]==e[0] && a[1]==e[1])
{
heu1=1;
}
else
{

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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

int select_option(char a[],char b[],char c[],int d[],char e[])


{
int large,temp1[6]={'-9','-9','-9','-9','-9','-9'},m=1,i,j,temp,p,temp3=1,flag=-1;
while(m!=7)
{

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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];

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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;

default: printf("\t\t\t Please Enter The Valid Choice \n");


break;
}
if(flag==0)
{
temp1[m]=check_heuristic(a,b,c,d,e);
switch(m)
{
case 1: i=d[0];
j=d[1];
i++;
temp=b[j];
b[j]=a[i];
a[i]=temp;
j--;
d[0]=i;
d[1]=j;
break;
case 2: i=d[0];
j=d[2];
i++;
temp=c[j];
c[j]=a[i];
a[i]=temp;

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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;

default: printf("\t\t\t Please Enter The Valid Choice \n");

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

Input/Output:

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

PRACTICAL – 7

Write a program to implement A* Algorithm for 8-puzzle problem.

About A* Algorithm for 8-puzzle problem:

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)

Where n is the next node on the path


g(n) is the cost of the path from the start node to n
h(n) is a heuristic function
f(n) estimates the cost of the cheapest path from n to the goal.

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.

Steps of solves the 8-Puzzle problem using A* algorithm:

• 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.

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

• 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:

//C Program for 8-puzzle game using A* search algorithm


#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])


{

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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)

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

{
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)

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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

int up(int random[3][3])


{
int i,j,temp,np=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(random[i][j]==0)

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

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

int left(int random[3][3])


{
int i,j,temp,np=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(random[i][j]==0)
{
if(j!=0)
j=j-1;
if(j<0)
np=1;
else
temp=random[i][j];
random[i][j]=random[i][j+1];
random[i][j+1]=temp;
else
np=1;
}
}

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

}
return np;
}

int right(int random[3][3])


{
int i,j,temp,np=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(random[i][j]==0)
{
j=j+1;
if(j>2)
np=1;
else
temp=random[i][j];
random[i][j]=random[i][j-1];
random[i][j-1]=temp;
}
}
}
return np;
}

int down(int random[3][3])


{
int i,j,temp,np=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(random[i][j]==0)
{
i=i+1;
if(i>2)
np=1;
else
temp=random[i][j];

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

random[i][j]=random[i-1][j];
random[i-1][j]=temp;
}
}
}
return np;
}

int diff(int final[3][3], int random[3][3])


{
int i,j,md=0;
for(i=0;i<3;++i){
for(j=0;j<3;++j)
{
if(final[i][j] != random[i][j])
md+=random[i][j];
} }
return md;
}

int min(int diff_D,int diff_U,int diff_L,int diff_R)


{
int Min = diff_D;
if(Min>diff_U)
Min=diff_U;
if(Min>diff_L)
Min=diff_L;
if(Min>diff_R)
Min=diff_R;
return Min;
}

BE-IV (Semester VIII) CE 160450107020 5


Artificial Intelligence (2180703)

Input/Output:

BE-IV (Semester VIII) CE 160450107020 5

You might also like