📄 aufgabe2.pl
字号:
:-dynamic db/1.
look:-
not(clean),
packen(Qs),
sort(Qs,QsNow),
not(db(QsNow)),
assert(db(QsNow)),
% nl,write(Qs), fail.
schachbrett(QsNow).
clean:-
clause(db(X), true),
retract(db(X)),
fail.
packen(Qs):-
member(A,[11,12,13,14,15,16,17,18,21,22,23,24,25,26,27,28,31,32,33,34,35,36,37,38,41,42,43,44,45,46,47,48,51,52,53,54,55,56,57,58,61,62,63,64,65,66,67,68,71,72,73,74,75,76,77,78,81,82,83,84,85,86,87,88]),
select(A,[11,12,13,14,15,16,17,18,21,22,23,24,25,26,27,28,31,32,33,34,35,36,37,38,41,42,43,44,45,46,47,48,51,52,53,54,55,56,57,58,61,62,63,64,65,66,67,68,71,72,73,74,75,76,77,78,81,82,83,84,85,86,87,88],Ns),
member(B,Ns),
select(B,Ns,Ns1),
not(attackDS(A,B)),
flatten([A,-B],Qs1),
member(C,Ns1),
select(C,Ns1,Ns2),
not(attackDD(A,C)),not(attackDS(C,B)),
flatten([Qs1,C],Qs2),
member(D,Ns2),
select(D,Ns2,Ns3),
not(attackDS(A,D)), not(attackSS(B,D)),
not(attackDS(C,D)),
flatten([Qs2,-D],Qs3),
member(E,Ns3),
select(E,Ns3,Ns4),
not(attackDD(A,E)), not(attackSD(B,E)),
not(attackDD(C,E)), not(attackSD(D,E)),
flatten([Qs3,E],Qs4),
member(F,Ns4),
select(F,Ns4,Ns5),
not(attackDS(A,F)), not(attackSS(B,F)),
not(attackDS(C,F)), not(attackSS(D,F)),
not(attackDS(E,F)),
flatten([Qs4,-F],Qs5),
member(G,Ns5),
select(G,Ns5,Ns6),
not(attackDD(A,G)), not(attackSD(B,G)),
not(attackDD(C,G)), not(attackSD(D,G)),
not(attackDD(E,G)), not(attackSD(F,G)),
flatten([Qs5,G],Qs6),
member(H,Ns6),
not(attackDS(A,H)), not(attackSS(B,H)),
not(attackDS(C,H)), not(attackSS(D,H)),
not(attackDS(E,H)), not(attackSS(F,H)),
not(attackDS(G,H)),
flatten([Qs6,-H],Qs).
schachbrett(Qs):-
nl,
write('------------------'),
nl,
write(' 1 2 3 4 5 6 7 8'),
member(X,[1,2,3,4,5,6,7,8]),
nl,
write(X),write(' '),
member(Y,[1,2,3,4,5,6,7,8]),
Po is X*10+Y,
(
aaa(Po,Qs)
),fail.
aaa(W,Qs):-
(member(W,Qs),write('D '),!;
(member(-W,Qs),write('S '),!;
write('. ')
)
).
attackDS(X,Xs):-
Y1 is X mod 10,
X1 is (X - Y1)/10,
Y2 is Xs mod 10,
X2 is (Xs - Y2)/10,
((Xslist = [((X2-2)*10 + (Y2-1)), % Attack zwischen Springen zu Damen.%
((X2-2)*10 + (Y2+1)),
((X2-1)*10 + (Y2+2)),
((X2+1)*10 + (Y2+2)),
((X2+2)*10 + (Y2+1)),
((X2+2)*10 + (Y2-1)),
((X2+1)*10 + (Y2-2)),
((X2-1)*10 + (Y2-2))],
member(Xslist1,Xslist),
0 =:= X - Xslist1);
(P1 = (Y1-Y2)+X1,
0 =:= X2-P1);
(P2 = (Y1-Y2)-X1,
0 =:= X2+P2);
0 =:= X1 - X2;
0 =:= Y1 - Y2).
attackSD(X,Xs):-
Y1 is X mod 10,
X1 is (X - Y1)/10,
Y2 is Xs mod 10,
X2 is (Xs - Y2)/10,
((Xlist = [((X1-2)*10 + (Y1-1)), % Attack zwischen Springen gegen Damen%
((X1-2)*10 + (Y1+1)),
((X1-1)*10 + (Y1+2)),
((X1+1)*10 + (Y1+2)),
((X1+2)*10 + (Y1+1)),
((X1+2)*10 + (Y1-1)),
((X1+1)*10 + (Y1-2)),
((X1-1)*10 + (Y1-2))],
member(Xlist1,Xlist),
0 =:= Xs - Xlist1);
(P1 = (Y2-Y1)+X2,
0 =:= X1-P1);
(P2 = (Y2-Y1)-X2,
0 =:= X1+P2);
0 =:= X1 - X2;
0 =:= Y1 - Y2).
attackSS(X,Xs):-
Y1 is X mod 10,
X1 is (X - Y1)/10,
Y2 is Xs mod 10,
X2 is (Xs - Y2)/10,
((Xlist = [((X1 - 2)*10 + (Y1 - 1)), % Attack zwischen Springer1 zu Springer2%
((X1 - 2)*10 + (Y1 + 1)),
((X1 - 1)*10 + (Y1 + 2)),
((X1 + 1)*10 + (Y1 + 2)),
((X1 + 2)*10 + (Y1 + 1)),
((X1 + 2)*10 + (Y1 - 1)),
((X1 + 1)*10 + (Y1 - 2)),
((X1 - 1)*10 + (Y1 - 2))],
member(Xlist1,Xlist),
0 =:= Xs - Xlist1);
(Xslist = [((X2 - 2)*10 + (Y2 - 1)), % Attack zwischen Springer2 zu Springer1%
((X2 - 2)*10 + (Y2 + 1)),
((X2 - 1)*10 + (Y2 + 2)),
((X2 + 1)*10 + (Y2 + 2)),
((X2 + 2)*10 + (Y2 + 1)),
((X2 + 2)*10 + (Y2 - 1)),
((X2 + 1)*10 + (Y2 - 2)),
((X2 - 1)*10 + (Y2 - 2))],
member(Xslist1, Xslist),
0 =:= X - Xslist1)).
attackDD(X,Xs):-
Y1 is X mod 10,
X1 is (X - Y1)/10,
Y2 is Xs mod 10,
X2 is (Xs - Y2)/10,
((P1 = (Y2-Y1)+X2,
0 =:= X1-P1);
(P2 = (Y2-Y1)-X2,
0 =:= X1+P2);
(P3 = (Y1-Y2)+X1,
0 =:= X2-P3);
(P4 = (Y1-Y2)-X1,
0 =:= X2+P4);
0 =:= X1 - X2;
0 =:= Y1 - Y2).
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -