Шашки — игра для двух игроков на многоклеточной доске, подобной шахматной, специальными фишками-шашками. Подобно шахматам, шашки воспроизводят действия сражающихся по определённым правилам сил.
Исходник:
% This program is created by Borislav Rizov
% from University of Sofia
?-
set(pos(w,[[1,0],[5,0],[7,0],[3,0],
[0,1],[2,1],[4,1],[6,1],
[1,2],[3,2],[5,2],[7,2]],
[ [0,5],[2,5],[4,5],[6,5],
[1,6],[3,6],[5,6],[7,6],
[0,7],[2,7],[4,7],[6,7]])),
G_White is brush(rgb(0,120,255)),
G_Black is brush(rgb(255,0,70)),
G_NoColor is brush(rgb(0,0,0)),
G_Grey is brush(rgb(150,150,150)),
pen(0,rgb(0,0,0)),
window(_,_,myFunc(_),"Checkers",50,50,491,511).
%In The Beginning There Was Silence And Darkness All Across The Earth...
%********************* Initialization *********************************
newGame:-set(pos(w,[[1,0],[5,0],[7,0],[3,0],
[0,1],[2,1],[4,1],[6,1],
[1,2],[3,2],[5,2],[7,2]],
[ [0,5],[2,5],[4,5],[6,5],
[1,6],[3,6],[5,6],[7,6],
[0,7],[2,7],[4,7],[6,7]])),
G_Clicked:= 0,update_window(_).
myFunc(init):-
G_H = 60,G_S = 5,
G_Clicked = 0,G_B = [],G_W = [],
G_X = 1,G_Take = 0,G_BestVal=0,G_Depth = 4,
G_Y = 1.
%********************* Paint ******************************************
myFunc(paint):-drawTable,drawPos,fail.
drawTable:-
for(I,0,7),
for(J,0,7),
X1 is I*G_H,Y1 is J*G_H,
X2 is X1+G_H,Y2 is Y1+G_H,
draw(I,J),
rect(X1,Y1,X2,Y2),fail.
drawTable.
draw(I,J):- (I+J) mod 2=:=0 -> brush(G_Grey);brush(G_NoColor).
color(w):-brush(G_White).
color(b):-brush(G_Black).
color(t):-brush(G_NoColor).
%********************** Draws a pul *************************************
draw_Pul(C,X,Y):-color(C),
X1 is X*G_H+ G_S,Y1 is Y*G_H+ G_S,
X2 is X1 + G_H- 2*G_S,
Y2 is Y1 + G_H- 2*G_S,
ellipse(X1,Y1,X2,Y2).
%********************** Mouse Click **************************************
myFunc(mouse_click(X,Y)):-X
X1 is X // G_H,Y1 is Y // G_H,move(X1,Y1),!,fail.
move(X,Y):- G_Clicked = 0,pos(C,B,W),
elem([X,Y],W),++(G_Clicked),
G_X :=X,G_Y:=Y.
move(X,Y):- G_Clicked = 1,
pos(C,B,W),
hodW(C,G_X,G_Y,X,Y,B,W).
move(X,Y):- G_Clicked > 1,
pos(C,B,W),dir(C,D),
take(D,G_X,G_Y,X,Y,B,W).
%********************** Logic Part ***************************************
%*********** Can must check whether the player can take a pul(che) *******
can(D,[H|L],B,W):-elemOfelem(X,Y,H),Y1 is Y+2*D,X1 is X+2,X2 is X- 2,
(in(X1,Y1),t(D,X,Y,X1,Y1,B,W);in(X2,Y1),t(D,X,Y,X2,Y1,B,W)),G_Clicked := 0.
can(D,[H|L],B,W):-can(D,L,B,W).
%********************** HodW(hite) ***************************************
dir(w,-1).
dir(b,1).
%******************** checks whether a point [X,Y] is in table.***********
in(X,Y):-X>=0,X<8,Y>=0,Y<8.
%*************************************************************************
hodW(C,X1,Y1,X2,Y2,B,W):-dir(C,D),
(take(D,X1,Y1,X2,Y2,B,W);can(D,W,B,W);step(D,X1,Y1,X2,Y2,B,W);G_Clicked:=0).
t(D,X1,Y1,X2,Y2,B,W):-
Y2=:=Y1+2*D,abs(X2-X1)=:=2,
not(elem([X2,Y2],B)),not(elem([X2,Y2],W)),
Y is Y1+D,X is (X1+X2)//2,elem([X,Y],B).
take(D,X1,Y1,X2,Y2,B,W):-t(D,X1,Y1,X2,Y2,B,W),
Y is Y1+D,X is (X1+X2)//2,
remove([X1,Y1],W,W1),
addB([X2,Y2],W1,W2),
remove([X,Y],B,B1),
set(pos(w,B1,W2)),
draw_Pul(w,X2,Y2),draw_Pul(t,X1,Y1),draw_Pul(t,X,Y),cantake(X2,Y2).
%************************* Checkes whether the player can take again *****
cantake(X,Y):-pos(C,B,W),dir(C,D),
Y1 is Y +2*D,X1 is X+ 2,in(X1,Y1),t(D,X,Y,X1,Y1,B,W),++(G_Clicked),G_X := X,G_Y := Y.
cantake(X,Y):-pos(C,B,W),dir(C,D),
Y1 is Y +2*D,X1 is X- 2,in(X1,Y1),t(D,X,Y,X1,Y1,B,W),++(G_Clicked),G_X := X,G_Y := Y.
cantake(X,Y):-G_Clicked := 0,play.
%************************* Makes a single step ***************************
step(D,X1,Y1,X2,Y2,B,W):-
Y2=:=Y1+D,abs(X2-X1)=:=1,
not(elem([X2,Y2],B)),not(elem([X2,Y2],W)),
remove([X1,Y1],W,W1),
addB([X2,Y2],W1,W2),
set(pos(w,B,W2)),
draw_Pul(w,X2,Y2),draw_Pul(t,X1,Y1),G_Clicked:=0,play.
%***************** This must search a move and play it. ******************
play:-pos(C,B,W),(isFinal(B,W,X),end(w);
set_text("Please, wait!",_),
winB(G_Depth,B,W,X),update_window(_),
(pos(C,B1,W1),isFinal(B1,W1,X),end(b);
set_text("Your Turn, Sir",_))).
%**********************************
s(w,Y):-Y is "You win. ".
s(b,Y):-Y is "You lose. ".
end(X):-s(X,Y),
(yes_no("Game over",Y+"Play again?",!) ->
newGame; (!,G_Clicked:= -1,fail)).
%*************************
winB(Depth,B,W,X):- (Depth < 1;isFinal(B,W,X)),!,eval(B,W,X).
winB(Depth,B,W,X):-X := -32000,can(1,B,W,B),
( nextcanB(1,B1,W1,B,W),D is Depth- 1,winW(D,B1,W1,X1),
(X1>X -> X:=X1,( Depth=G_Depth->set(pos(w,B1,W1)))),fail;true),!.
winB(Depth,B,W,X):-X := -32000,
( nextstepB(B1,W1,B,W),D is Depth- 1,winW(D,B1,W1,X1),
(X1>X -> X:=X1,( Depth=G_Depth->set(pos(w,B1,W1)))),fail;true),!.
winW(Depth,B,W,X):-isFinal(B,W,X),!.
winW(Depth,B,W,X):- (Depth < 1;isFinal(B,W,X)),!,eval(B,W,X).
winW(Depth,B,W,X):-can(-1,W,B,W),X := 32000,
(nextcanB(-1,W1,B1,W,B),D is Depth- 1,winB(D,B1,W1,X1),
(X1 X:=X1),fail;true),!.
winW(Depth,B,W,X):-X := 32000,
(nextstepW(B1,W1,B,W),D is Depth- 1,winB(D,B1,W1,X1),
(X1 X:=X1),fail;true),!.
%********************** Next(s) ******************************************
nextcanB(D,P,Q,B,W):-elem(Z,B),elemOfelem(X,Y,Z),Y1 is Y+2*D,X1 is X- 2,X2 is X- 1,
Y2 is Y+D,
in(X1,Y1),t(D,X,Y,X1,Y1,W,B),
remove([X,Y],B,B1),
addB([X1,Y1],B1,B2),
remove([X2,Y2],W,W1),
cantakeB(D,P,Q,B2,W1,X1,Y1).
nextcanB(D,P,Q,B,W):-elem(Z,B),elemOfelem(X,Y,Z),Y1 is Y+2*D,X1 is X+ 2,X2 is X+ 1,
Y2 is Y+D,
in(X1,Y1),t(D,X,Y,X1,Y1,W,B),
remove([X,Y],B,B1),
addB([X1,Y1],B1,B2),
remove([X2,Y2],W,W1),
cantakeB(D,P,Q,B2,W1,X1,Y1).
cantakeB(D,P,Q,B,W,X,Y):-Y1 is Y+2*D,X1 is X- 2,X2 is X- 1,
Y2 is Y+D,
in(X1,Y1),t(D,X,Y,X1,Y1,W,B),
remove([X,Y],B,B1),
addB([X1,Y1],B1,B2),
remove([X2,Y2],W,W1),
cantakeB(D,P,Q,B2,W1,X1,Y1),!.
cantakeB(D,P,Q,B,W,X,Y):-Y1 is Y+2*D,X1 is X+ 2,X2 is X+ 1,
Y2 is Y+D,
in(X1,Y1),t(D,X,Y,X1,Y1,W,B),
remove([X,Y],B,B1),
addB([X1,Y1],B1,B2),
remove([X2,Y2],W,W1),
cantakeB(D,P,Q,B2,W1,X1,Y1),!.
cantakeB(D,B,W,B,W,X,Y).
%**********************
nextstepB(P,Q,B,W):-elem(Z,B),elemOfelem(X,Y,Z),Y1 is Y+1,X1 is X+ 1,
in(X1,Y1),not(elem([X1,Y1],B)),not(elem([X1,Y1],W)),
remove([X,Y],B,P1),Q is W,
addB([X1,Y1],P1,P).
nextstepB(P,Q,B,W):-elem(Z,B),elemOfelem(X,Y,Z),Y1 is Y+ 1,X1 is X- 1,
in(X1,Y1),not(elem([X1,Y1],B)),not(elem([X1,Y1],W)),
remove([X,Y],B,P1),Q is W,
addB([X1,Y1],P1,P).
nextstepW(Q,P,W,B):-elem(Z,B),elemOfelem(X,Y,Z),Y1 is Y- 1,X1 is X+ 1,
in(X1,Y1),not(elem([X1,Y1],B)),not(elem([X1,Y1],W)),
remove([X,Y],B,P1),Q is W,
addB([X1,Y1],P1,P).
nextstepW(Q,P,W,B):-elem(Z,B),elemOfelem(X,Y,Z),Y1 is Y- 1,X1 is X- 1,
in(X1,Y1),not(elem([X1,Y1],B)),not(elem([X1,Y1],W)),
remove([X,Y],B,P1),Q is W,
addB([X1,Y1],P1,P).
%********************** Draws Current Position ***************************
drawPos:-pos(K,B,W),drawColor(b,B),drawColor(w,W).
drawColor(C,[]).
drawColor(C,[H|B]):-drawColor(C,B),elemOfelem(X,Y,H),draw_Pul(C,X,Y).
%********************** elem(X,Y) X is element of Y***********************
elem(H,[H|L]).
elem(A,[H|L]):-elem(A,L).
%********************** Removes A element from list P: Result Q **********
remove(A,[A|P],P).
remove(A,[H|P],[H|Q]):-remove(A,P,Q).
%********************** Adds element in the begining of the list *********
addB(A,P,[A|P]).
%********************** Takes the Ordered pair****************************
elemOfelem(X,Y,[X,Y]).
%********************* Eval **********************************************
pr(0,1).
pr(1,2).
pr(2,4).
pr(3,8).
pr(4,16).
pr(5,32).
pr(6,64).
pr(7,2048).
evalB(0,[]):-!.
evalB(P,[H|L]):-elemOfelem(X,Y,H),
evalB(Q,L),pr(Y,D),P is Q+D.
evalW(0,[]):-!.
evalW(P,[H|L]):-elemOfelem(X,Y,H),Y1 is 7- Y,
evalW(Q,L),pr(Y1,D),P is Q+D.
eval(B,W,X):-evalB(P,B),P>=2048,X is 2048,!.
eval(B,W,X):-evalW(Q,W),Q>=2048,X is -2048,!.
eval(B,W,X):-evalB(P,B),evalW(Q,W),X is P- Q.
isFinal(B,W,X):-eval(B,W,X),abs(X) >= 2048.
%*************************************************************************
• |