Sadar Patel College of Engineering, Bakrol
Sadar Patel College of Engineering, Bakrol
Sadar Patel College of Engineering, Bakrol
PRACTICAL: 1
#include <stdio.h>
#include <conio.h>
char square[10] = { 'o', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
int checkwin();
void board();
int main()
{
int player = 1, i, choice;
char mark;
do
{
board();
player = (player % 2) ? 1 : 2;
else
{
printf("Invalid move ");
player--;
getch();
}
i = checkwin();
player++;
}while (i == - 1);
board();
if (i == 1)
printf("==>\aPlayer %d win ", --player);
else
printf("==>\aGame draw");
getch();
return 0;
}
int checkwin()
{
if (square[1] == square[2] && square[2] == square[3])
return 1;
return 1;
else if (square[1] != '1' && square[2] != '2' && square[3] != '3' &&
square[4] != '4' && square[5] != '5' && square[6] != '6' && square[7]
!= '7' && square[8] != '8' && square[9] != '9')
return 0;
else
return - 1;
}
void board()
{
system("cls");
printf("\n\n\tTic Tac Toe\n\n");
printf(" | | \n");
printf(" %c | %c | %c \n", square[1], square[2], square[3]);
printf("_____|_____|_____\n");
printf(" | | \n");
printf("_____|_____|_____\n");
printf(" | | \n");
printf(" | | \n\n");
}
OUTPUT:
3 Artificial Intelligence | 2180703 171243107011
SADAR PATEL COLLEGE OF ENGINEERING, BAKROL
PRACTICAL: 2
4 Artificial Intelligence | 2180703 171243107011
SADAR PATEL COLLEGE OF ENGINEERING, BAKROL
struct node
{
int x, y;
struct node *next;
}*root, *left, *right;
int isNodePresent(struct node *nextState, int maxJug1, int maxJug2, int reqJug1)
{
struct node *temp;
if((nextState->x == reqJug1) )
return(0);
if((nextState->x == maxJug1) && (nextState->y == maxJug2))
return(1);
if((nextState->x == 0) && (nextState->y == 0))
return(1);
temp = left;
while(1)
{
break;
else
temp = temp->next;
}
temp = right;
while(1)
{
if((temp->x == nextState->x) && (temp->y == nextState->y))
return(1);
else if(temp->next == NULL)
break;
temp = temp->next;
}
return(0);
}
struct node* genNewState(struct node *crntState, int maxJug1, int maxJug2, int reqJug1)
{
int d;
struct node *nextState;
nextState = (struct node*)malloc(sizeof(struct node));
nextState->x = maxJug1;
nextState->y = crntState->y;
if(isNodePresent(nextState, maxJug1, maxJug2, reqJug1) != 1)
return(nextState);
nextState->x = crntState->x;
nextState->y = maxJug2;
nextState->x = 0;
nextState->y = crntState->y;
if(isNodePresent(nextState, maxJug1, maxJug2, reqJug1) != 1)
return(nextState);
nextState->y = 0;
nextState->x = crntState->x;
if(isNodePresent(nextState, maxJug1, maxJug2, reqJug1) != 1)
return(nextState);
{
nextState->x = 0;
nextState->y = crntState->y + crntState->x;
}
else
{
nextState->x = crntState->x-d;
nextState->y = crntState->y+d;
}
if(isNodePresent(nextState, maxJug1, maxJug2, reqJug1) != 1)
return(nextState);
}
{
flag1 = 0; flag2 = 0;
if((tempLeft->x != reqJug1))
{
tempLeft->next = genNewState(tempLeft, maxJug1, maxJug2, reqJug1);
tempLeft = tempLeft->next;
tempLeft->next = NULL;
flag1 = 1;
}
if((tempRight->x != reqJug1))
{
tempRight->next = genNewState(tempRight, maxJug1, maxJug2, reqJug1);
tempRight = tempRight->next;
tempRight->next = NULL;
flag2 = 1;
}
if((flag1 == 0) && (flag2 == 0))
break;
}
}
void main()
{
int maxJug1, maxJug2, reqJug1;
BFS(reqJug1);
}
OUTPUT:
PRACTICAL: 3
int xcapacity;
int ycapacity;
void display(int a, int b);
int min(int d, int f)
{
if (d < f)
return d;
else
return f;
}
int solution1(int n)
{
int x = 0, y = 0, step = 0;
int temp;
printf("\n\t\t\t Jug X Jug Y \n");
printf("\n\t\t\t _____ ______ \n");
while (x != n )
{
if (x == 0)
{
x = xcapacity;
step += 1;
printf("\nFill X ");
display(x, y);
}
else if (y == ycapacity)
{
y = 0;
step++;
printf("\nEmpty Y ");
display(x, y);
}
else
{
temp = min(ycapacity - y, x);
y = y + temp;
x = x - temp;
step++;
printf("\nPour X in Y");
display(x, y);
10 Artificial Intelligence | 2180703 171243107011
SADAR PATEL COLLEGE OF ENGINEERING, BAKROL
}
}
return step;
}
int solution2(int n)
{
int x = 0, y = 0, step = 0;
int temp;
printf("\n\t\t\t Jug X Jug Y \n");
printf("\n\t\t\t _____ ______ \n");
while (x != n )
{
if (y == 0)
{
y = ycapacity;
step += 1;
printf("\nFill Y ");
display(x, y);
}
else if (x == xcapacity)
{
x = 0;
step++;
printf("\nEmpty X ");
display(x, y);
}
else
{
temp = min(y, xcapacity-x);
y = y - temp;
x = x + temp;
step++;
printf("\nPour Y in X");
display(x, y);
}
}
return step;
}
void display(int a, int b)
{
printf("\n\t\t\t %d \t\t\t\t %d \n", a, b);
}
int main()
{
int n, ans;
11 Artificial Intelligence | 2180703 171243107011
SADAR PATEL COLLEGE OF ENGINEERING, BAKROL
OUTPUT:
PRACTICAL: 4
void disp_matrix(void);
int main(void)
{
char done;
printf("This is the game of Tic Tac Toe.\n");
printf("You will be playing against the computer.\n");
done = ' ';
init_matrix();
do {
disp_matrix();
get_player_move();
done = check(); /* see if winner */
if(done!= ' ') break; /* winner!*/
get_computer_move();
done = check(); /* see if winner */
} while(done== ' ');
if(done=='X') printf("You won!\n");
else printf("I won!!!!\n");
disp_matrix(); /* show final positions */
return 0;
}
/* Initialize the matrix. */
void init_matrix(void)
{
int i, j;
int x, y;
printf("Enter X,Y coordinates for your move: "); scanf("%d%*c%d", &x, &y); x--; y--;
if(i*j==9) {
printf("draw\n");
exit(0);
}
else
matrix[i][j] = 'O';
}
OUTPUT:
PRACTICAL: 5
#define map_size_rows 10
#define map_size_cols 10
char map[map_size_rows][map_size_cols] = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 1, 1, 1, 0, 1},
{1, 0, 0, 1, 0, 0, 0, 1, 0, 1},
{1, 0, 0, 1, 0, 0, 0, 1, 0, 1},
{1, 0, 0, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
int ind[map_size_rows][map_size_cols] = {
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
};
int main() {
int i, j, k, l, b, found;
int p_len = 0;
int * path = NULL;
int c_len = 0;
int * closed = NULL;
int o_len = 1;
int * open = (int*)calloc(o_len, sizeof(int));
double min, tempg;
int s;
int e;
int current;
int s_len = 0;
struct stop * stops = NULL;
int r_len = 0;
struct route * routes = NULL;
stops[t].n = NULL;
ind[i][j] = t;
}
}
18 Artificial Intelligence | 2180703 171243107011
SADAR PATEL COLLEGE OF ENGINEERING, BAKROL
open[0] = s;
stops[s].g = 0;
stops[s].f = stops[s].g + stops[s].h;
found = 0;
if (current == e) {
found = 1;
++p_len;
path = (int*)realloc(path, p_len * sizeof(int));
path[p_len - 1] = current;
while (stops[current].from >= 0) {
current = stops[current].from;
++p_len;
path = (int*)realloc(path, p_len * sizeof(int));
path[p_len - 1] = current;
}
}
++c_len;
closed = (int*)realloc(closed, c_len * sizeof(int));
closed[c_len - 1] = current;
if (routes[stops[current].n[i]].y == closed[j]) {
b = 1;
}
}
if (b) {
continue;
}
b = 1;
if (o_len > 0) {
for (j = 0; j < o_len; j++) {
if (routes[stops[current].n[i]].y == open[j]) {
b = 0;
}
}
}
if (b) {
++o_len;
open = (int*)realloc(open, o_len * sizeof(int));
open[o_len - 1] = routes[stops[current].n[i]].y;
}
}
}
}
if (map[i][j]) {
putchar(0xdb);
} else {
b = 0;
for (k = 0; k < p_len; k++) {
if (ind[i][j] == path[k]) {
++b;
}
}
if (b) {
putchar('x');
} else {
putchar('.');
}
}
}
putchar('\n');
}
if (not found) {
puts("IMPOSSIBLE");
} else {
printf("path cost is %d:\n", p_len);
for (i = p_len - 1; i >= 0; i--) {
printf("(%1.0f, %1.0f)\n", stops[path[i]].col, stops[path[i]].row);
}
}
return 0;
}
OUTPUT:
PRACTICAL: 6
constraints(0, _, _, _) :- !.
constraints(N, Row, [R|Rs], [C|Cs]) :-
arg(N, Row, R-C),
M is N-1,
constraints(M, Row, Rs, Cs).
queens([], _, []).
queens([C|Cs], Row0, [Col|Solution]) :-
Row is Row0+1,
select(Col-Vars, [C|Cs], Board),
arg(Row, Vars, Row-Row),
queens(Board, Row, Solution).
OUTPUT:
PRACTICAL: 7
:- op(400,yfx,'#').
solve(Start,Soln) :- f_function(Start,0,F),
search([Start#0#F#[]],S),
reverse(S,Soln).
expand(State#D#_#A, All_My_Children) :-
bagof(Child#D1#F#[Move|A],
( D1 is D + 1,
move(State,Child,Move),
f_function(Child,D1,F) ) ,
All_My_Children).
insert_all([F|R],Open1,Open3) :- insert(F,Open1,Open2),
insert_all(R,Open2,Open3).
insert_all([],Open,Open).
insert(B,Open,Open) :- repeat_node(B,Open), ! .
insert(B,[C|R],[B,C|R]) :- cheaper(B,C), ! .
insert(B,[B1|R],[B1|S]) :- insert(B,R,S), !.
insert(B,[],[B]).
repeat_node(P#_#_#_, [P#_#_#_|_]).
goal(1/2/3/8/0/4/7/6/5).
move(P,C,left) :- left(P,C).
move(P,C,up) :- up(P,C).
move(P,C,right) :- right(P,C).
move(P,C,down) :- down(P,C).
f_function(State,D,F) :- h_function(State,H), F is D + H.
p_fcn(A/B/C/D/E/F/G/H/I, P) :-
a(A,Pa), b(B,Pb), c(C,Pc),
d(D,Pd), e(E,Pe), f(F,Pf),
g(G,Pg), h(H,Ph), i(I,Pi),
P is Pa+Pb+Pc+Pd+Pe+Pf+Pg+Ph+Pg+Pi.
s_fcn(A/B/C/D/E/F/G/H/I, S) :-
s_aux(A,B,S1), s_aux(B,C,S2), s_aux(C,F,S3),
s_aux(F,I,S4), s_aux(I,H,S5), s_aux(H,G,S6),
s_aux(G,D,S7), s_aux(D,A,S8), s_aux(E,S9),
S is S1+S2+S3+S4+S5+S6+S7+S8+S9.
s_aux(0,0) :- !.
s_aux(_,1).
s_aux(X,Y,0) :- Y is X+1, !.
s_aux(8,1,0) :- !.
s_aux(_,_,2).
OUTPUT:
PRACTICAL: 8
edge(a, d, 2).
edge(a, e, 7).
edge(b, c, 4).
edge(b, d, 6).
edge(b, e, 3).
edge(c, d, 5).
edge(c, e, 8).
edge(d, e, 6).
edge(b, a, 3).
edge(c, a, 4).
edge(d, a, 2).
edge(e, a, 7).
edge(c, b, 4).
edge(d, b, 6).
edge(e, b, 3).
edge(d, c, 5).
edge(e, c, 8).
edge(e, d, 6).
edge(a, h, 2).
edge(h, d, 1).
/* Finds the length of a list, while there is something in the list it increments N
when there is nothing left it returns.*/
len([], LenResult):-
LenResult is 0.
len([X|Y], LenResult):-
len(Y, L),
LenResult is L + 1.
/*This adds the stopping location to the visited list, adds the distance and then calls recursive
to the next stopping location along the path */
/*When we find a path back to the starting point, make that the total distance and make
sure the graph has touch every node*/
/*This is called to find the shortest path, takes all the paths, collects them in holder.
Then calls pick on that holder which picks the shortest path and returns it*/
/* Is called, compares 2 distances. If cost is smaller than bcost, no need to go on. Cut it.*/
best(Cost-Holder,Bcost-_,Cost-Holder):- Cost<Bcost,!.
best(_,X,X).
/*Takes the top path and distance off of the holder and recursively calls it.*/
pick([Cost-Holder|R],X):- pick(R,Bcost-Bholder),best(Cost-Holder,Bcost-Bholder,X),!.
pick([X],X).
OUTPUT:
PRACTICAL: 9
mat(mat1).
sat_on (cat1, mat1).
bird(bird1).
Caught (tom, bird1).
like(X,cream) :– cat(X).
mammal(X): – cat(X).
has(X,fur) :– mammal(X).
animal(X): – mammal(X).
animal(X): – bird(X).
owns(john,tom).
is_coloured(tom,ginger).
PRACTICAL: 10
Answer (a):
Answer (b):