0% found this document useful (0 votes)
15 views6 pages

Ai 5 - 6

The document outlines two experiments involving Prolog programming. Experiment 5 focuses on implementing the Water-Jug Problem with various state transitions, while Experiment 6 involves creating a Tic-Tac-Toe game with win condition checks and move validation. Both experiments include Prolog predicates and example queries for testing functionality.

Uploaded by

rohitgoyal8178
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views6 pages

Ai 5 - 6

The document outlines two experiments involving Prolog programming. Experiment 5 focuses on implementing the Water-Jug Problem with various state transitions, while Experiment 6 involves creating a Tic-Tac-Toe game with win condition checks and move validation. Both experiments include Prolog predicates and example queries for testing functionality.

Uploaded by

rohitgoyal8178
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Date -

Experiment-5

Aim: Write a program to implement Water-Jug Problem.

Prolog Predicate:
% Define the possible state transitions move((X, Y), (4, Y), 'Fill the 4 gallon jug') :- X < 4.

move((X, Y), (X, 3), 'Fill the 3 gallon jug') :- Y < 3. move((X, Y), (X1, Y), 'Pour some water

out of the 4 gallon jug') :- X > 0, X1 is X - min(X, 4). move((X, Y), (X, Y1), 'Pour some water

out of the 3 gallon jug') :- Y > 0, Y1 is Y - min(Y, 3). move((X, Y), (0, Y), 'Empty the 4 gallon

jug') :- X > 0. move((X, Y), (X, 0), 'Empty the 3 gallon jug') :- Y > 0.

move((X, Y), (4, Y1), 'Pour water from the 3 gallon jug into the 4 gallon jug until the 4 gallon
jug is full') :-

X + Y >= 4, Y > 0, Y1 is Y - (4 - X).

move((X, Y), (X1, 3), 'Pour water from the 4 gallon jug into the 3-gallon jug until the 3 gallon
jug is full') :-

X + Y >= 3, X > 0, X1 is X - (3 - Y).

move((X, Y), (X1, 0), 'Pour all the water from the 3 gallon jug into the 4 gallon jug') :-

X + Y =< 4, Y > 0, X1 is X + Y.

move((X, Y), (0, Y1), 'Pour all the water from the 4 gallon jug into the 3 gallon jug') :-

X + Y =< 3, X > 0, Y1 is X + Y.

move((0, 2), (2, 0), 'Pour the 2 gallons from 3 gallon jug into the 4 gallon jug'). move((2,

Y), (0, Y), 'Empty the 2 gallons in the 4 gallon jug on the ground').

% Query example to find all possible moves from a given state

% ?- move((2, 2), NextState, Description).

Output:
Date -
Experiment-6
Aim: Write a program to generate Tic-Tac Toe problem.

Prolog Predicate:
% Tic-Tac-Toe Win Case Checker in Prolog

% Winning conditions

win(Player, Board) :-

Board = [Player, Player, Player, _, _, _, _, _, _]; % Row 1

Board = [_, _, _, Player, Player, Player, _, _, _]; % Row 2

Board = [_, _, _, _, _, _, Player, Player, Player]; % Row 3

Board = [Player, _, _, Player, _, _, Player, _, _]; % Column 1

Board = [_, Player, _, _, Player, _, _, Player, _]; % Column 2

Board = [_, _, Player, _, _, Player, _, _, Player]; % Column 3

Board = [Player, _, _, _, Player, _, _, _, Player]; % Diagonal 1

Board = [_, _, Player, _, Player, _, Player, _, _]. % Diagonal 2

% Replace an element at index in a list replace([_|T], 0, X,

[X|T]). replace([H|T], I, X, [H|R]) :- I > 0, I1 is I - 1,

replace(T, I1, X, R).

% Display the board display_board([A, B, C, D, E, F,

G, H, I]) :- write(A), write(' | '), write(B), write(' | '),

write(C), nl, write('---------'), nl, write(D), write(' |

'), write(E), write(' | '), write(F), nl, write('---------'),

nl,

write(G), write(' | '), write(H), write(' | '), write(I), nl, nl.


% Make a move move(Player, Board, Index, NewBoard) :-

nth0(Index, Board, e), % Ensure the position is empty

replace(Board, Index, Player, NewBoard), format('Move ~w is

valid on board: ~w~n', [Index, NewBoard]).

% Play test cases play_win_cases :- write('Horizontal Win

Case (X wins):'), nl, Board1 = [e, e, e, e, e, e, e, e, e],

move(x, Board1, 0, B1), move(x, B1, 3, B2), move(x, B2,

1, B3), move(x, B3, 4, B4), move(x, B4, 2, B5), move(x,

B5, 5, FinalBoard1), write('Solution verified. Final board: '),

write(FinalBoard1), nl, display_board(FinalBoard1),

write('Vertical Win Case (O wins):'),

nl, Board2 = [e, e, e, e, e, e, e, e, e],

move(x, Board2, 0, B6), move(o, B6,

1, B7), move(x, B7, 3, B8),

move(o, B8, 4, B9), move(x, B9, 6, B10), move(o, B10,

7, FinalBoard2), write('Solution verified. Final board: '),

write(FinalBoard2), nl, display_board(FinalBoard2),

write('Diagonal Win Case (X wins):'), nl, Board3 = [e, e, e,

e, e, e, e, e, e], move(x, Board3, 0, B11), move(o, B11, 1,

B12), move(x, B12, 4, B13), move(o, B13, 2, B14),

move(x, B14, 8, FinalBoard3), write('Solution verified. Final

board: '), write(FinalBoard3), nl,

display_board(FinalBoard3),
write('Draw Case (Game Draw):'),

nl, Board4 = [e, e, e, e, e, e, e, e, e],

move(x, Board4, 0, B15), move(o,

B15, 1, B16), move(x, B16, 2, B17),

move(o, B17, 6, B18), move(x, B18,

3, B19), move(o, B19, 8, B20),

move(x, B20, 4, B21), move(o, B21,

7, B22),

move(x, B22, 5, FinalBoard4), write('Solution verified.

Final board: '), write(FinalBoard4), nl,

display_board(FinalBoard4).

Output:

You might also like