-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfig24_10.pl
244 lines (177 loc) · 5.53 KB
/
fig24_10.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
% Figure 24.10: Predicate library for king and rook vs king.
% Position is represented by: Side..Wx:Wy..Rx:Ry..Bx:By..Depth
% Side is side to move ('w' or 'b')
% Wx, Wy are X and Y-coordinates of White king
% Rx, Ry are X and Y-coordinates of White rook
% Bx, By are coordinates of Black king
% Depth is depth of position in search tree
% Selector relations
side( Side.._, Side).
wk( _..WK.._, WK).
wr( _.._..WR.._, WR).
bk( _.._.._..BK.._, BK).
depth( _.._.._.._..Depth, Depth).
resetdepth( S..W..R..B..D, S..W..R..B..0). % Copy of position with depth 0
% Some relations between squares
n( N, N1) :- % Neighbour integers 'within board'
( N1 is N+1;
N1 is N-1),
in( N1).
in( N) :-
N > 0, N < 9.
diagngb( X:Y, X1:Y1) :- % Diagonal neighbour squares
n( X, X1), n( Y, Y1).
verngb( X:Y, X:Y1) :- % Vertical neighbour squares
n( Y, Y1).
horngb( X:Y, X1:Y) :- % Horizontal neighbour squares
n( X, X1).
ngb( S, S1) :- % Neighbour squares, first diagonal
diagngb( S, S1);
horngb( S, S1);
verngb( S, S1).
end_of_game( Pos) :-
mate( Pos).
% Move constraints predicates
% These are specialised move-generators:
%
% move( MoveConstr, Pos, Move, NewPos)
move( depth < Max, Pos, Move, Pos1) :-
depth( Pos, D),
D < Max, !.
move( depth = D, Pos, Move, Pos1) :-
depth( Pos, D), !.
move( kingdiagfirst, w..W..R..B..D, W-W1, b..W1..R..B..D1) :-
D1 is D+1,
ngb( W, W1), % 'ngb' generates diagonal moves first
\+ ngb( W1, B), % Must not move into check
W1 \== R. % Must not collide with rook
move( rookmove, w..W..Rx:Ry..B..D, Rx:Ry-R, b..W..R..B..D1) :-
D1 is D+1,
coord( I), % Integer between 1 and 8
( R = Rx:I; R = I:Ry), % Move vertically or horizonatlly
R \== Rx:Ry, % Must have moved
\+ inway( Rx:Ry, W, R). % White king not in way
move( checkmove, Pos, R-Rx:Ry, Pos1) :-
wr( Pos, R),
bk( Pos, Bx:By),
(Rx = Bx; Ry = By), % Rook and black king in line
move( rookmove, Pos, R-Rx:Ry, Pos1).
move( legal, w..P, M, P1) :-
( MC = kingdiagfirst; MC = rookmove; MC = checkfirst),
move( MC, w..P, M, P1).
move( legal, b..W..R..B..D, B-B1, w..W..R..B1..D1) :-
D1 is D+1,
ngb( B, B1),
\+ check( w..W..R..B1..D1).
legalmove( Pos, Move, Pos1) :-
move( legal, Pos, Move, Pos1).
check( _..W..Rx:Ry..Bx:By.._) :-
ngb( W, Bx:By); % King's too close
( Rx = Bx; Ry = By),
Rx:Ry \== Bx:By, % Not rook captured
\+ inway( Rx:Ry, W, Bx:By).
inway( S, S1, S1) :- !.
inway( X1:Y, X2:Y, X3:Y) :-
ordered( X1, X2, X3), !.
inway( X:Y1, X:Y2, X:Y3) :-
ordered( Y1, Y2, Y3).
ordered( N1, N2, N3) :-
N1 < N2, N2 < N3;
N3 < N2, N2 < N1.
coord(1). coord(2). coord(3). coord(4).
coord(5). coord(6). coord(7). coord(8).
% Goal predicates
true( Pos).
themtomove( b.._). % Black = 'them' to move
mate( Pos) :-
side( Pos, b),
check( Pos),
\+ legalmove( Pos, _, _).
stalemate( Pos) :-
side( Pos, b),
\+ check( Pos),
\+ legalmove( Pos, _, _).
newroomsmaller( Pos, RootPos) :-
room( Pos, Room),
room( RootPos, RootRoom),
Room < RootRoom.
rookexposed( Side..W..R..B.._) :-
dist( W, R, D1),
dist( B, R, D2),
( Side = w, !, D1 > D2+1;
Side = b, !, D1 > D2 ).
okapproachedcsquare( Pos, RootPos) :-
okcsquaremdist( Pos, D1),
okcsquaremdist( RootPos, D2),
D1 < D2.
okcsquaremdist( Pos, Mdist) :- % Manh. dist. between WK and critical square
wk( Pos, WK),
cs( Pos, CS), % Critical square
manhdist( WK, CS, Mdist).
rookdivides( _..Wx:Wy..Rx:Ry..Bx:By.._) :-
ordered( Wx, Rx, Bx), !;
ordered( Wy, Ry, By).
lpatt( _..W..R..B.._) :- % L-pattern
manhdist( W, B, 2),
manhdist( R, B, 3).
okorndle( _..W..R.._, _..W1..R1.._) :-
dist( W, R, D),
dist( W1, R1, D1),
D =< D1.
roomgt2( Pos) :-
room( Pos, Room),
Room > 2.
our_king_edge( _..X:Y.._) :- % White king on edge
( X=1, !; X=8, !; Y=1, !; Y=8).
their_king_edge( _..W..R..X:Y.._) :- % Black king on edge
( X=1, !; X=8, !; Y=1, !; Y=8).
kings_close( Pos) :- % Dist. between kings < 4
wk( Pos, WK), bk( Pos, BK),
dist( WK, BK, D),
D < 4.
rooklost( _..W..B..B.._). % Rook has been captured
rooklost( b..W..R..B.._) :-
ngb( B, R), % B. king attacks rook
\+ ngb( W, R). % W. king does not defend
dist( X:Y, X1:Y1, D) :- % Distance in king moves
absdiff( X, X1, Dx),
absdiff( Y, Y1, Dy),
max( Dx, Dy, D).
absdiff( A, B, D) :-
A > B, !, D is A-B;
D is B-A.
max( A, B, M) :-
A >= B, !, M = A;
M = B.
manhdist( X:Y, X1:Y1, D) :- % Manhattan distance
absdiff( X, X1, Dx),
absdiff( Y, Y1, Dy),
D is Dx + Dy.
room( Pos, Room) :- % Area to which B. king is confined
wr( Pos, Rx:Ry),
bk( Pos, Bx:By),
( Bx < Rx, SideX is Rx - 1; Bx > Rx, SideX is 8 - Rx),
( By < Ry, SideY is Ry - 1; By > Ry, SideY is 8 - Ry),
Room is SideX * SideY, !;
Room is 64. % Rook in line with Black king
cs( _..W..Rx:Ry..Bx:By.._, Cx:Cy) :- % 'Critical square'
( Bx < Rx, !, Cx is Rx-1; Cx is Rx+1),
( By < Ry, !, Cy is Ry-1; Cy is Ry+1).
% Display procedures
show( Pos) :-
nl,
coord( Y), nl,
coord( X),
writepiece( X:Y, Pos),
fail.
show( Pos) :-
side( Pos, S), depth( Pos, D),
nl, write('Side='), write(S),
write(' Depth='), write(D), nl.
writepiece( Square, Pos) :-
wk( Pos, Square), !, write('W');
wr( Pos, Square), !, write('R');
bk( Pos, Square), !, write('B');
write('.').
showmove( Move) :-
nl, write(Move), nl.