%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% reversum
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% metadata
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
role(red)
role(blue)
base(cell(X,Y,R)) :- index(X) & index(Y) & role(R)
base(control(R)) :- role(R)
action(move(X,Y)) :- index(X) & index(Y)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% init
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
init(cell(3,3,blue))
init(cell(3,4,red))
init(cell(4,3,red))
init(cell(4,4,blue))
init(control(red))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% legal
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
legal(move(X,Y)) :-
cell(U,V,R) & nextcellindir(U,V,Dir,X,Y) &
~cell(X,Y,red) & ~cell(X,Y,blue)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% operations
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
move(X,Y) :: control(R) ==> cell(X,Y,R)
move(U,V) :: control(R) & affectedcell(R,U,V,X,Y) & cell(X,Y,S)
==> ~cell(X,Y,S) & cell(X,Y,R)
move(X,Y) :: control(red) ==> ~control(red) & control(blue)
move(X,Y) :: control(blue) ==> ~control(blue) & control(red)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% goal
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
goal(R,N) :- ~terminal & score(R,N)
goal(R,N) :- terminal & score(R,M) & scoremap(M,N)
score(R,N) :- countofall(cell(X,Y),cell(X,Y,R),N)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% terminal
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
terminal :- ~playable
playable :- empty(X,Y)
empty(X,Y) :- index(X) & index(Y) & ~occupied(X,Y)
occupied(X,Y) :- cell(X,Y,R)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Views
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
affectedcell(Color,U,V,X2,Y2) :-
affecteddir(Color,U,V,Dir) &
opponent(Color,Other) &
nextcellindir(U,V,Dir,X1,Y1) &
affectedcellindir(Other,X1,Y1,Dir,X2,Y2)
affecteddir(Color,U,V,Dir) :-
nextcellindir(U,V,Dir,X,Y) &
captureindir(Other,X,Y,Color,Dir)
nextcellindir(X1,Y1,Dir,X2,Y2) :-
dir(Dir,Xdelta,Ydelta) &
add(X1,Xdelta,X2) &
add(Y1,Ydelta,Y2)
affectedcellindir(Other,X1,Y1,Dir,X1,Y1) :- cell(X1,Y1,Other)
affectedcellindir(Other,X1,Y1,Dir,X3,Y3) :-
cell(X1,Y1,Other) &
nextcellindir(X1,Y1,Dir,X2,Y2) &
affectedcellindir(Other,X2,Y2,Dir,X3,Y3)
captureindir(Other,X1,Y1,Color,Dir) :-
cell(X1,Y1,Other) &
nextcellindir(X1,Y1,Dir,X2,Y2) &
cell(X2,Y2,Color)
captureindir(Other,X1,Y1,Color,Dir) :-
cell(X1,Y1,Other) &
nextcellindir(X1,Y1,Dir,X2,Y2) &
captureindir(Other,X2,Y2,Color,Dir)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Facts
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
opponent(blue,red)
opponent(red,blue)
oppdir(nw,se)
oppdir(ne,sw)
oppdir(se,nw)
oppdir(sw,ne)
oppdir(n,s)
oppdir(e,w)
oppdir(s,n)
oppdir(w,e)
dir(n,0,p1)
dir(s,0,n1)
dir(e,p1,0)
dir(w,n1,0)
dir(nw,n1,p1)
dir(ne,p1,p1)
dir(se,p1,n1)
dir(sw,n1,n1)
add(1,p1,2)
add(2,p1,3)
add(3,p1,4)
add(4,p1,5)
add(5,p1,6)
add(1,0,1)
add(2,0,2)
add(3,0,3)
add(4,0,4)
add(5,0,5)
add(6,0,6)
add(6,n1,5)
add(5,n1,4)
add(4,n1,3)
add(3,n1,2)
add(2,n1,1)
index(1)
index(2)
index(3)
index(4)
index(5)
index(6)
nextcell(1,1,1,2)
nextcell(1,2,1,3)
nextcell(1,3,1,4)
nextcell(1,4,1,5)
nextcell(1,5,1,6)
nextcell(1,6,2,1)
nextcell(2,1,2,2)
nextcell(2,2,2,3)
nextcell(2,3,2,4)
nextcell(2,4,2,5)
nextcell(2,5,2,6)
nextcell(2,6,3,1)
nextcell(3,1,3,2)
nextcell(3,2,3,3)
nextcell(3,3,3,4)
nextcell(3,4,3,5)
nextcell(3,5,3,6)
nextcell(3,6,4,1)
nextcell(4,1,4,2)
nextcell(4,2,4,3)
nextcell(4,3,4,4)
nextcell(4,4,4,5)
nextcell(4,5,4,6)
nextcell(4,6,5,1)
nextcell(5,1,5,2)
nextcell(5,2,5,3)
nextcell(5,3,5,4)
nextcell(5,4,5,5)
nextcell(5,5,5,6)
nextcell(5,6,6,1)
nextcell(6,1,6,2)
nextcell(6,2,6,3)
nextcell(6,3,6,4)
nextcell(6,4,6,5)
nextcell(6,5,6,6)
scoremap(0,0)
scoremap(1,0)
scoremap(2,0)
scoremap(3,0)
scoremap(4,0)
scoremap(5,0)
scoremap(6,0)
scoremap(7,0)
scoremap(8,0)
scoremap(9,0)
scoremap(10,0)
scoremap(11,0)
scoremap(12,0)
scoremap(13,0)
scoremap(14,0)
scoremap(15,0)
scoremap(16,0)
scoremap(17,0)
scoremap(18,50)
scoremap(19,100)
scoremap(20,100)
scoremap(21,100)
scoremap(22,100)
scoremap(23,100)
scoremap(24,100)
scoremap(25,100)
scoremap(26,100)
scoremap(27,100)
scoremap(28,100)
scoremap(29,100)
scoremap(30,100)
scoremap(31,100)
scoremap(32,100)
scoremap(33,100)
scoremap(34,100)
scoremap(35,100)
scoremap(36,100)
succ(0,1)
succ(1,2)
succ(2,3)
succ(3,4)
succ(4,5)
succ(5,6)
succ(6,7)
succ(7,8)
succ(8,9)
succ(9,10)
succ(10,11)
succ(11,12)
succ(12,13)
succ(13,14)
succ(14,15)
succ(15,16)
succ(16,17)
succ(17,18)
succ(18,19)
succ(19,20)
succ(20,21)
succ(21,22)
succ(22,23)
succ(23,24)
succ(24,25)
succ(25,26)
succ(26,27)
succ(27,28)
succ(28,29)
succ(29,30)
succ(30,31)
succ(31,32)
succ(32,33)
succ(33,34)
succ(34,35)
succ(35,36)
succ(36,37)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%