-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.pl
122 lines (102 loc) · 7.44 KB
/
main.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
:- use_module(library(lists)).
:- use_module(library(clpfd)).
:- use_module(library(random)).
:- consult('board.pl').
:- consult('logic.pl').
:- consult('display.pl').
:- consult('menus.pl').
/*reset_timer:-
statistics(total_runtime, _).
print_time(Msg):-
statistics(total_runtime,[_,T]),
TS is ((T//10)*10)/1000,
nl, write(Msg), write(TS), write('s'), nl, nl.*/
% ------------------------------------------------------------------------------------------------------------------------- %
% Solve Problem %
% Prototype: %
% solve(-Positions) %
% %
% Outputs: %
% Positions -> The positions of the chess pieces, after solving a Chess-Num problem, chosen by the user %
% ------------------------------------------------------------------------------------------------------------------------- %
solve(Positions) :-
display_menu,
input(N, 0, 12, 'Problem to solve? ', problems),
N \= exit,
predicate(N, PredicateName, Problem),
size_board(Problem, Size),
Predicate =.. [PredicateName, GameBoard, Size],
Predicate,
nl, nl, write('\t\t'), write(Problem),
nl, nl, display_board(GameBoard),
getCellsNumber(GameBoard, 1-1, Cells, Size), !,
nl, write('Cells: '), write(Cells), nl,
Positions = [_PawnX, _PawnY, _KnightX, _KnightY, _KingX, _KingY, _RookX, _RookY, _BishopX, _BishopY, _QueenX, _QueenY],
%reset_timer,
domain(Positions, 1, Size),
not_overlapping(Positions),
only_empty(Cells, Positions),
maplist(cell_attacks(Positions), Cells),
%print_time('PostingConstraints: '),
labeling([anti_first_fail, bisect], Positions),
%print_time('Labeling Time: '),
%fd_statistics,
%statistics,
nl, show_results(Positions, 1), nl,
display_solution(GameBoard, Positions).
% ------------------------------------------------------------------------------------------------------------------------- %
% Piece %
% Prototype: %
% piece(+Number, -Name) %
% %
% Inputs: %
% Number -> The number of the chess piece %
% %
% Outputs: %
% Name -> The name of the chess piece %
% ------------------------------------------------------------------------------------------------------------------------- %
piece(1, 'Pawn').
piece(2, 'Knight').
piece(3, 'King').
piece(4, 'Rook').
piece(5, 'Bishop').
piece(6, 'Queen').
% ------------------------------------------------------------------------------------------------------------------------- %
% Show Results %
% Prototype: %
% show_results(+Positions, +N) %
% %
% Inputs: %
% Positions -> The positions of the chess pieces, in the format [Row, Column] %
% N -> The number of the next chess piece to print on the screen %
% %
% Outputs: %
% Prints the chess pieces' positions to the screen, showing the solution for the Chess-Num problem %
% ------------------------------------------------------------------------------------------------------------------------- %
show_results([], _).
show_results([X, Y|Positions], N) :-
piece(N, Piece),
write(Piece), write(' is at cell ['), write(X), write(', '), write(Y), write(']'), nl,
N1 is N + 1,
show_results(Positions, N1).
% ------------------------------------------------------------------------------------------------------------------------- %
% No pieces on numbered cells %
% Prototype: %
% only_empty(+Cells, +Positions) %
% %
% Inputs: %
% Cells -> The numbered cells of the board, in the format Number-Row-Column %
% Positions -> The positions of the chess pieces, in the format [Row, Column] %
% %
% Outputs: %
% Adds restrictions so that no chess piece stands on a numbered cell %
% ------------------------------------------------------------------------------------------------------------------------- %
only_empty([], _).
only_empty([_-X-Y | Cells], [PawnX, PawnY, KnightX, KnightY, KingX, KingY, RookX, RookY, BishopX, BishopY, QueenX, QueenY]) :-
(PawnX #\= X #\/ PawnY #\= Y) #/\
(KnightX #\= X #\/ KnightY #\= Y) #/\
(KingX #\= X #\/ KingY #\= Y) #/\
(RookX #\= X #\/ RookY #\= Y) #/\
(BishopX #\= X #\/ BishopY #\= Y) #/\
(QueenX #\= X #\/ QueenY #\= Y),
only_empty(Cells, [PawnX, PawnY, KnightX, KnightY, KingX, KingY, RookX, RookY, BishopX, BishopY, QueenX, QueenY]).