Исходник:
% This program is created by Rosica Panaiotova
% from University of Sofia
?-
G_White is brush(system_color(window)),
G_Red is brush(rgb(255,0,0)),
G_Yellow is brush(rgb(255,255,0)),
G_Blue is brush(rgb(0,0,255)),
G_Deep_Purple is brush(rgb(128,0,128)),
G_Orange is brush(rgb(255,128,64)),
G_Violet is brush(rgb(237,137,245)),
G_Green is brush(rgb(127,255,127)),
G_Pen is pen(3,rgb(0,0,0)),
G_PenW is pen(3,system_color(window)),
retractall(cell(_,_,_)),
retractall(selected(_,_)),
init_cells,
window(G_MainWindow,_,win_funk(_),"Five Balls",100,100,508,388).
win_funk(paint):-
pen(G_Pen),
for(I,0,9),
Y1 is 30+I*30,
line(100,Y1,370,Y1),
fail.
win_funk(paint):-
for(I,0,9),
Y2 is 100+I*30,
line(Y2,30,Y2,300),
fail.
win_funk(paint):-
for(I,0,8),
for(J,0,8),
draw_pull(I,J),
fail.
% we draw the pulls regarding the clauses cell
draw_pull(Xi,Yi):-
(selected(Xi,Yi),pen(G_Pen) % current selected
;
pen(G_PenW)
),
cell(Xi,Yi,CNum),
cell_colour(CNum,Brush),
brush(Brush),
X1 is (100 + Xi*30 + 4),X2 is X1 + 30 - 8,
Y1 is (30 + Yi*30 + 4),Y2 is Y1 + 30 - 8,
ellipse(X1,Y1,X2,Y2),!.
win_funk(mouse_click(X,Y)):-
Xi is (X - 100)//30,
Yi is (Y - 30)// 30,
Xi >= 0,Xi =< 8,Yi >= 0,Yi =< 8,
square_click(Xi,Yi). %za dv sled hoda koito sme napravili
init_cells:-
for(I,0,8),
for(J,0,8),
assert(cell(I,J,0)),
fail.
% place three arbitrary pulls on the board
init_cells:-
for(I,0,2),
put_random_cell(_,_),
fail.
%kato cveta im da ne e bql
init_cells:-
cell(X,Y,CNum),CNum >= 1,
assert(selected(X,Y)).
% colors of the pulls
cell_colour(0,G_White).
cell_colour(1,G_Red).
cell_colour(2,G_Yellow).
cell_colour(3,G_Blue).
cell_colour(4,G_Deep_Purple).
cell_colour(5,G_Orange).
cell_colour(6,G_Violet).
cell_colour(7,G_Green).
rmv_cfg([[1,0],[2,0],[3,0],[4,0]]).
rmv_cfg([[-1,0],[1,0],[2,0],[3,0]]).
rmv_cfg([[-2,0],[-1,0],[1,0],[2,0]]).
rmv_cfg([[-3,0],[-2,0],[-1,0],[1,0]]).
rmv_cfg([[-4,0],[-3,0],[-2,0],[-1,0]]).
rmv_cfg([[0,1],[0,2],[0,3],[0,4]]).
rmv_cfg([[0,-1],[0,1],[0,2],[0,3]]).
rmv_cfg([[0,-2],[0,-1],[0,1],[0,2]]).
rmv_cfg([[0,-3],[0,-2],[0,-1],[0,1]]).
rmv_cfg([[0,-4],[0,-3],[0,-2],[0,-1]]).
rmv_cfg([[1,1],[2,2],[3,3],[4,4]]).
rmv_cfg([[-1,-1],[1,1],[2,2],[3,3]]).
rmv_cfg([[-2,-2],[-1,-1],[1,1],[2,2]]).
rmv_cfg([[-3,-3],[-2,-2],[-1,-1],[1,1]]).
rmv_cfg([[-4,-4],[-3,-3],[-2,-2],[-1,-1]]).
rmv_cfg([[1,-1],[2,-2],[3,-3],[4,-4]]).
rmv_cfg([[-1,1],[1,-1],[2,-2],[3,-3]]).
rmv_cfg([[-2,2],[-1,1],[1,-1],[2,-2]]).
rmv_cfg([[-3,3],[-2,2],[-1,1],[1,-1]]).
rmv_cfg([[-4,4],[-3,3],[-2,2],[-1,1]]).
square_click(X,Y):- %this click is for moving after
%the move we should remove the needed cells
%and put three more cells
cell(X,Y,0),
selected(XFrom,YFrom),
cell(XFrom,YFrom,CFrom),
retractall(cell(X,Y,_)),
assert(cell(X,Y,CFrom)),
retractall(cell(XFrom,YFrom,_)),
assert(cell(XFrom,YFrom,0)),
retractall(selected(_,_)),
assert(selected(X,Y)),
draw_pull(XFrom,YFrom),
draw_pull(X,Y),
(
remove_cells(X,Y)
;
true
),
put_three_random_cells.
square_click(X,Y):-cell(X,Y,CNum),CNum > 0,
( selected(XOld,YOld)
;
XOld is -1,YOld is -1
),
retractall(selected(_,_)),
assert(selected(X,Y)),
draw_pull(X,Y),
draw_pull(XOld,YOld).
is_valid([],X,Y,_):-bRemoveMainCell() ; assert(bRemoveMainCell()) .
is_valid([[XHead,YHead]|Vector],X,Y,CNum):-
XE is X + XHead,YE is Y + YHead,
XE >= 0,XE =< 8,
YE >= 0,YE =< 8,
cell(XE,YE,CE),
CE > 0,
CE =:= CNum,
is_valid(Vector,X,Y,CNum),
retractall(cell(XE,YE,_)),
assert(cell(XE,YE,0)),
draw_pull(XE,YE).
remove_cells(X,Y):-
cell(X,Y,CNum),
CNum > 0,
rmv_cfg(Vector),
is_valid(Vector,X,Y,CNum),
fail.
remove_cells(X,Y):-
bRemoveMainCell(),
retractall(bRemoveMainCell()),
retractall(cell(X,Y,_)),
assert(cell(X,Y,0)),
retractall(selected(_,_)),
cell(Xs,Ys,CNum),CNum > 0,
assert(selected(Xs,Ys)),
draw_pull(Xs,Ys),
draw_pull(X,Y).
remove_cells(_,_).
put_three_random_cells:-
for(_,0,2),
put_random_cell(X,Y),
draw_pull(X,Y),
fail.
put_three_random_cells.
% for the pulls that are in the start of the game
put_random_cell(XR,YR):-
fill_random_list([XR,YR],9),% position of the random pull in the beginning
cell(XR,YR,0),
retractall(cell(XR,YR,_)),
CR1 is random(7),
CR is CR1 +1,
assert(cell(XR,YR,CR)), %slaga edin cell vyv nachaloto
!.
%draw_pull(XR,YR),
can_put.
%na proizv H=x da otgovarq y
fill_random_list(List,Limit):-random_list(List,Limit).
fill_random_list(List,Limit):-fill_random_list(List,Limit).
random_list([],_):-!.
random_list([H|T],Limit):- !,
H is random(Limit),random_list(T,Limit).
• |