[KAPLAN]
|
Cevap : Prolog Örnekleri | Prolog Examples
Lecture 10
Program 10 1 - a Prolog program for solving a puzzle
/* ************************************************ */
/* */
/* board/3 */
/* Arg 1: board as a structured object */
/* Arg 2: board as a list */
/* Arg 3: board as a list of rows, columns */
/* and groups of squares */
/* Summary: Represents the board in several ways */
/* to aid processing Note the use of */
/* shared variables to ensure that the */
/* instantiation of a variable in one */
/* argument of board/3 instantiates the */
/* all instances of the variable */
/* Author: P J Hancox */
/* Date: 27 November 2004 */
/* */
/* ************************************************ */
board(square(A1, A2, A3, A4, A5, A6,
B1, B2, B3, B4, B5, B6,
C1, C2, C3, C4, C5, C6,
D1, D2, D3, D4, D5, D6,
E1, E2, E3, E4, E5, E6,
F1, F2, F3, F4, F5, F6),
square([A1, A2, A3, A4, A5, A6,
B1, B2, B3, B4, B5, B6,
C1, C2, C3, C4, C5, C6,
D1, D2, D3, D4, D5, D6,
E1, E2, E3, E4, E5, E6,
F1, F2, F3, F4, F5, F6]),
sizes([ % rows
six(A1, A2, A3, A4, A5, A6),
six(B1, B2, B3, B4, B5, B6),
six(C1, C2, C3, C4, C5, C6),
six(D1, D2, D3, D4, D5, D6),
six(E1, E2, E3, E4, E5, E6),
six(F1, F2, F3, F4, F5, F6),
% columns
six(A1, B1, C1, D1, E1, F1),
six(A2, B2, C2, D2, E2, F2),
six(A3, B3, C3, D3, E3, F3),
six(A4, B4, C4, D4, E4, F4),
six(A5, B5, C5, D5, E5, F5),
six(A6, B6, C6, D6, E6, F6d),
% groups
six(A1, A2, A3, A4, B2, C2),
six(A5, A6, B3, B4, B5, C3),
six(B1, C1, D1, D2, D3, E2),
six(B6, C4, C5, C6, D6, E6),
six(D4, D5, E5, F4, F5, F6),
six(E1, E3, E4, F1, F2, F3)]))
/* ************************************************ */
/* */
/* candidates/1 */
/* Arg 1: list of candidates */
/* Summary: Candidates may be used to fill */
/* squares */
/* Author: P J Hancox */
/* Date: 27 November 2004 */
/* */
/* ************************************************ */
candidates([1,2,3,4,5,6])
/* ************************************************ */
/* */
/* solve/0 */
/* Summary: Generates and tests solutions to the */
/* puzzle, outputing successful */
/* solutions */
/* Author: P J Hancox */
/* Date: 27 November 2004 */
/* */
/* ************************************************ */
solve :-
board(Display_Square, square(Square), sizes(Sixes)),
candidates(Candidates),
generate(Square, Candidates),
display_board(Display_Square),
test(Sixes),
display_board(Display_Square)
/* ************************************************ */
/* */
/* generate/2 */
/* Arg 1: list of squares on the board */
/* Arg 2: list of candidates */
/* Summary: Fills squares on the board to */
/* generate possible solutions */
/* Author: P J Hancox */
/* Date: 27 November 2004 */
/* */
/* ************************************************ */
% 1 - terminating
generate([], _Candiates)
% 2 - recursive
generate([Square|Squares], Candiates) :-
member(Square, Candiates),
generate(Squares, Candiates)
/* ************************************************ */
/* */
/* test/1 */
/* Arg 1: list of rows, columns and groups of */
/* squares on the board */
/* Summary: Tests lines of six squares to ensure */
/* the squares are unique */
/* Author: P J Hancox */
/* Date: 27 November 2004 */
/* */
/* ************************************************ */
% 1 - terminating
test([])
% 2 - recursive
test([Six|Sixes]) :-
test_six(Six),
test(Sixes)
/* ************************************************ */
/* */
/* test/1 */
/* Arg 1: a row, column or group of squares */
/* Summary: Tests line of six squares to ensure */
/* the squares are unique */
/* Author: P J Hancox */
/* Date: 27 November 2004 */
/* */
/* ************************************************ */
test_six(six(Sq1, Sq2, Sq3, Sq4, Sq5, Sq6)) :-
Sq1 =\= Sq2,
Sq1 =\= Sq3,
Sq1 =\= Sq4,
Sq1 =\= Sq5,
Sq1 =\= Sq6,
Sq2 =\= Sq3,
Sq2 =\= Sq4,
Sq2 =\= Sq5,
Sq2 =\= Sq6,
Sq3 =\= Sq4,
Sq3 =\= Sq5,
Sq3 =\= Sq6,
Sq4 =\= Sq5,
Sq4 =\= Sq6,
Sq5 =\= Sq6
/* ************************************************ */
/* */
/* display_board/1 */
/* Arg 1: board as a structured object */
/* Summary: Displays the board */
/* Author: P J Hancox */
/* Date: 27 November 2004 */
/* */
/* ************************************************ */
display_board(Board) :-
display_board(0, Board)
/* ************************************************ */
/* */
/* display_board/1 */
/* Arg 1: counter */
/* Arg 2: board as a structured object */
/* Summary: Displays the board */
/* Author: P J Hancox */
/* Date: 27 November 2004 */
/* */
/* ************************************************ */
% 1 - terminating
display_board(36, _Board) :-
nl, nl
% 2 - recursive
display_board(Index, Board) :-
Index < 36,
Index1 is Index + 1,
arg(Index1, Board, Cell),
display_square(Index1, Cell),
display_board(Index1, Board)
/* ************************************************ */
/* */
/* display_square/1 */
/* Arg 1: counter */
/* Arg 2: individual square of the board */
/* Summary: Displays an individual square and, if */
/* it is the 6th square of a row, moves */
/* to a new line */
/* Author: P J Hancox */
/* Date: 27 November 2004 */
/* */
/* ************************************************ */
display_square(Index, Square) :-
( nonvar(Square) ->
write(' '), write(Square), write(' ')
;
write(' ')
),
( (Index) // 6 =:= (Index)/6 ->
nl
;
true
)
/* ************************************************ */
/* */
/* member/2 */
/* */
/* ************************************************ */
% 1 - terminating
member(Term, [Term|_])
% 2 - recursive
member(Term, [_|Tail]) :-
member(Term, Tail)
/* ************************************************ */
/* */
/* End of program */
/* */
/* ************************************************ */
|