AI Lab.. File...
AI Lab.. File...
“TIC-TAC-TOE PROBLEM”
#include <stdio.h>
#include<conio.h>
void main()
{
int i = 0,player = 0,go = 0,row = 0,column = 0,line = 0,winner = 0;
char board[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
clrscr();
for( i = 0; i<9 && winner==0; i++)
{
printf("\n\n");
printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
printf("---+---+---\n");
printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
printf("---+---+---\n");
printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);
player = i%2 + 1;
do
{
printf("\nPlayer %d, please enter the number of the square "
"where you want to place your %c: ", player,(player==1)?'X':'O');
scanf("%d", &go);
row = --go/3;
column = go%3;
}while(go<0 || go>9 || board[row][column]>'9');
board[row][column] = (player == 1) ? 'X' : 'O';
if((board[0][0] == board[1][1] && board[0][0] == board[2][2]) ||
(board[0][2] == board[1][1] && board[0][2] == board[2][0]))
winner = player;
else
for(line = 0; line <= 2; line ++)
if((board[line][0] == board[line][1] && board[line][0] == board[line][2])||
(board[0][line] == board[1][line] && board[0][line] == board[2][line]))
winner = player;
}
printf("\n\n");
printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
printf("---+---+---\n");
printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
printf("---+---+---\n");
printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);
if(winner == 0)
printf("\n How boring, it is a draw\n");
else
printf("\n Congratulations, player %d, YOU ARE THE WINNER!\n", winner);
getch();
}
OUTPUT :
X|2|3
---+---+---
4|5|6
---+---+---
7|8|9
Player 2, please enter the number of the square where you want to place your O : 4
X|2|3
---+---+---
O|5|6
---+---+---
7|8|9
Player 1, please enter the number of the square where you want to place your X : 5
X|2|3
---+---+---
O|X|6
---+---+---
7|8|9
Player 2, please enter the number of the square where you want to place your O : 6
X|2|3
---+---+---
O|X|O
---+---+---
7|8|9
Player 1, please enter the number of the square where you want to place your X : 9
X|2|3
---+---+---
O|X|O
---+---+---
7|8|X
Congratulations, player 1, YOU ARE THE WINNER!
C-IMPLEMENTATION OF
“WATER-JUG PROBLEM”
#include<stdio.h>
#include<conio.h>
#define MAX 20
void main()
{
int a[MAX][3],b[MAX][3],i,p,temp1,temp2,j,n;
char c;
clrscr();
printf("\n\n x -> 4 litre jug, y -> 3 litre jug.");
printf("\n\n Enter the initial condition of Jugs (x,y) : ");
scanf("%d%d",&a[0][0],&a[0][1]);
a[0][2]=-1;
i=0;
p=1;
while(i!=p)
{
printf("\n\n Are choice for (%d,%d) available (y/n) : ",a[i][0],a[i][1]);
scanf("%c",&c);
while(c=='y')
{
printf("\n Enter the available options for (%d,%d) : ",a[i][0],a[i][1]);
scanf("%d%d",&temp1,&temp2);
for(j=0;(j<p) && !(a[j][0]==temp1 && a[j][1]==temp2);j++);
if(j==p)
{
a[p][0]=temp1;
a[p][1]=temp2;
a[p][2]=i;
p++;
}
c='n';
printf("\n Do you want to enter more choice (y/n) : ");
scanf("%c",&c);
}
i++;
}
printf("\n\n Enter the terminating condition for the problem (x,y) : ");
scanf("%d%d",&temp1,&temp2);
printf("\n\n Solution sequence is :\n");
for(j=0;j<=i;j++)
if(a[j][0]==temp1 && a[j][1]==temp2)
p=j;
j=0;
while(p!=-1)
{
b[j][0]=a[p][0];
b[j][1]=a[p][1];
p=a[p][2];
j++;
}
for(i=(j-1);i>=0;i--)
printf("\n\t\t(%d,%d)",b[i][0],b[i][1]);
getch();
}
OUTPUT :
x -> 4 litre jug, y -> 3 litre jug.
Enter the initial condition of Jugs (x,y) : 0 0
Are choice for (0,0) available (y/n) : y
Enter the available options for (0,0) : 4 0
Do you want to enter more choice (y/n) : y
Enter the available options for (0,0) : 0 3
Do you want to enter more choice (y/n) : n
Are choice for (4,0) available (y/n) : y
Enter the available options for (4,0) : 1 3
Do you want to enter more choice (y/n) : y
Enter the available options for (4,0) : 4 3
Do you want to enter more choice (y/n) : y
Enter the available options for (4,0) : 0 0
Do you want to enter more choice (y/n) : n
Are choice for (0,3) available (y/n) : y
Enter the available options for (0,3) : 3 0
Do you want to enter more choice (y/n) : y
Enter the available options for (0,3) : 4 3
Do you want to enter more choice (y/n) : n
Are choice for (1,3) available (y/n) : y
Enter the available options for (1,3) : 1 0
Do you want to enter more choice (y/n) : y
Enter the available options for (1,3) : 0 3
Do you want to enter more choice (y/n) : n
Are choice for (4,3) available (y/n) : y
Enter the available options for (4,3) : 4 0
Do you want to enter more choice (y/n) : y
Enter the available options for (4,3) : 0 3
Do you want to enter more choice (y/n) : n
Are choice for (3,0) available (y/n) : y
Enter the available options for (3,0) : 3 3
Do you want to enter more choice (y/n) : n
Are choice for (1,0) available (y/n) : y
Enter the available options for (1,0) : 0 1
Do you want to enter more choice (y/n) : n
Are choice for (3,3) available (y/n) : y
Enter the available options for (3,3) : 4 2
Do you want to enter more choice (y/n) : n
Are choice for (0,1) available (y/n) : n
Are choice for (4,2) available (y/n) : y
Enter the available options for (4,2) : 0 2
Do you want to enter more choice (y/n) : n
Are choice for (0,2) available (y/n) : y
Enter the available options for (0,2) : 2 0
Do you want to enter more choice (y/n) : y
Enter the available options for (0,2) : 4 2
Do you want to enter more choice (y/n) : n
Are choice for (2,0) available (y/n) : n
Enter the terminating condition for the problem (x,y) : 2 0
Solution sequence is :
(0,0)
(0,3)
(3,0)
(3,3)
(4,2)
(0,2)
(2,0)
Getting Started in SWI prolog for Windows
Objective : To get oriented with the SWI-interface, learn how to perform basic tasks of
opening prolog files, querying knowledge databases, and editing prolog files.
1. Install SWI-prolog by going to
http://www.swi-prolog.org/dl-stable.html
Download and run the self-extracting Windows executable.
2. Open SWI-prolog by clicking Start_All Programs_SWI-Prolog_Prolog
You should see the following Command window.
Q2. Run the following query, by typing it exactly into the prompt:
chinese(X).
Press ENTER after the result appears.
Run the query again, by typing it exactly into the prompt:
chinese(X).
But press semi-colon repeatedly.
In your own words, when running a query how does pressing ; differ from
pressing ENTER?
Q6. Add a new ethnic food category of your choosing (e.g. mexican) to the knowledge
database likes.pl with at least 3 food types (e.g. nachos) by adding simple clauses
of the form mexican(nachos). to likes.pl. Construct a rule (in likes.pl) to
determine when sam likes that ethnic food. Perform a query to see if sam
likes your ethnic food. Write down your changes to likes.pl (both assertions and
your rule) along with your query(ies) and its results.
Q7. For those of you who would like some additional PROLOG programming
challenges, here are two more problems you can cut your teeth on:
A. Suppose we are given a knowledge base with the following facts:
tran(eins,one).
tran(zwei,two).
tran(drei,three).
tran(vier,four).
tran(fuenf,five).
tran(sechs,six).
tran(sieben,seven).
tran(acht,eight).
tran(neun,nine).
Write a predicate listtran(G,E) which translates a list of German number words to
the corresponding list of English number words. For example:
listtran([eins,neun,zwei],X).
should give:
X = [one,nine,two].
Your program should also work in the other direction. For example, if you give it the
query
listtran(X,[one,seven,six,two]).
it should return:
X = [eins,sieben,sechs,zwei].
Hint: to answer this question, first ask yourself `How do I translate the empty list of
number words?'. That's the base case. For non-empty lists, first translate the head of the list, then
use recursion to translate the tail.
KNOWLEDGE BASE
File likes.pl
likes(sam,Food) :-
indian(Food),
mild(Food).
likes(sam,Food) :-
chinese(Food).
likes(sam,Food) :-
italian(Food).
likes(sam,chips).
indian(curry).
indian(dahl).
indian(tandoori).
indian(kurma).
mild(dahl).
mild(tandoori).
mild(kurma).
chinese(chow_mein).
chinese(chop_suey).
chinese(sweet_and_sour).
italian(pizza).
italian(spaghetti).
QUERIES :
Q.1
likes(sam,tandoori).
Yes
a) Sam likes tandoori.
likes(sam,lasagna).
No
b) Sam doesn’t like lasagna.
?- chinese(X).
X = chow_mein ;
X = chop_suey ;
X = sweet_and_sour ;
No
?- indian(X).
X = curry ;
X = dahl ;
Yes
c) Dahl is an Indian food.
d)
Q.2
?- chinese(X).
X = chow_mein
Yes
?- chinese(X).
X = chow_mein ;
X = chop_suey ;
X = sweet_and_sour ;
No
When ENTER is pressed the query terminates after showing only single result. The ‘Yes’ word shows that
other result also exists. While, When semi-colon is pressed, the query terminates after showing all the
results. The ‘No’ word show that no more result exist for this query.
Q.3
?- indian(X).
X = curry ;
X = dahl ;
X = tandoori ;
X = kurma ;
No
a) Available Indian foods are : curry, dahl, tandoori, kurma.
italian(X).
X = pizza ;
X = spaghetti ;
No
?- chinese(X).
X = chow_mein ;
X = chop_suey ;
X = sweet_and_sour ;
No
b) Thus Indian food has most foods in the database.
?- likes(sam,X).
X = dahl ;
X = tandoori ;
X = kurma ;
X = chow_mein ;
X = chop_suey ;
X = sweet_and_sour ;
X = pizza ;
X = spaghetti ;
X = chips ;
c) Sam likes curry, dahl, tandoori, kurma, pizza, spaghetti, chow_mein, chop_suey, sweet_and_sour
foods.
Q.4
likes(sam,tandoori).
Yes
a) Sam likes lasagna.
b) When Prolog returns ‘No’ for a query, it means that the record doesn’t exists in the database.
c)
likes(sam,Food) :- italian(Food).
italian(lasagna).
Q.5
likes(sam,naan).
No
Sam doesn’t like naan because Sam likes only mild Indian foods & naan is not a mild food.
Water Jug Solution
;;; dfs2 recurses on each sibling from a single node, calling dfs1
(defun dfs2 (states depth)
(cond
((null states) nil)
((dfs1 (car states) depth))
((dfs2 (cdr states) depth))))
Sample Run
;;; Sun Common Lisp, Development Environment 4.0.0 , 6 July 1990
;;; Sun-4 Version for SunOS 4.0.x and sunOS 4.1
> *start*
(0 0)
Theory
The 8-puzzle is a square tray in which are placed eight square tiles. 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.
2 8 3
1 6 4 1 2 3
7 5 8 4
7 6 5
Program Code
dif( A, B, D) :-
D is A-B, D >=0, !;
D is B-A.
h( [Empty | Tiles}, H) :-
goal( [Empty1 | GoalSquares] ),
totdist( Tiles, GoalSquares, D),
seq( Tiles S),
H is D + 3*S.
showsol( [] )
showsol( [P | L] ) :-
showsol( L ),
nl, write( ‘---‘)
showpos( P).
showpos( [S0,S1,S2,S3,S4,S5,S6,S7,S8] ) :-
member( Y, [3,2,1] ), % Order of Y-coordinates
nl, member( X, [1,23] ) % Order of X-coordinates
member( Tile-X/Y, ‘’-S0,1-S1,2-S2,3-S3,4-S4,5-S5,6-S6,7-S7,8-S8] ),
% Tile on square X/Y
write( Tile),
fail %Backtrack to next square ;
true. % All squares done
start1( [2/2,1/3,3/2,2/3,3/3,3/1,2/1,1/1,1/2] ).
start2( [2/1,1/2,1/3,3/3,3/2,3/1,2/2,1/1,2/3] ).
start3( [2/2,2/3,1/3,3/1,1/2,2/1,3/31/1,3/2] ).
?- start1( Pos), bestfirst( Pos, Sol), showsol( Sol).