role(red)
role(blue)

row(1)
row(2)
row(3)
row(4)

col(1)
col(2)
col(3)
col(4)

base(cell(R,C,empty)) :- row(R) & col(C)
base(cell(R,C,W)) :- role(W)

base(control(red))
base(control(blue))

action(place(R1,C1,R2,C2)) :- adjacent(R1,C1,R2,C2)

init(cell(R,C,empty)) :- row(R) & col(C)
init(control(red))

adjacent(1,1,1,2)
adjacent(1,2,1,3)
adjacent(1,3,1,4)

adjacent(2,1,2,2)
adjacent(2,2,2,3)
adjacent(2,3,2,4)

adjacent(3,1,3,2)
adjacent(3,2,3,3)
adjacent(3,3,3,4)

adjacent(4,1,4,2)
adjacent(4,2,4,3)
adjacent(4,3,4,4)

adjacent(1,1,2,1)
adjacent(2,1,3,1)
adjacent(3,1,4,1)

adjacent(1,2,2,2)
adjacent(2,2,3,2)
adjacent(3,2,4,2)

adjacent(1,3,2,3)
adjacent(2,3,3,3)
adjacent(3,3,4,3)

adjacent(1,4,2,4)
adjacent(2,4,3,4)
adjacent(3,4,4,4)

legal(place(R1,C1,R2,C2)) :-
  adjacent(R1,C1,R2,C2) &
  cell(R1,C1,empty) &
  cell(R2,C2,empty)

place(R1,C1,R2,C2) :: control(red) ==> cell(R1,C1,red) & cell(R2,C2,red)
place(R1,C1,R2,C2) :: control(blue) ==> cell(R1,C1,blue) & cell(R2,C2,blue)

place(R1,C1,R2,C2) :: ~cell(R1,C1,empty)
place(R1,C1,R2,C2) :: ~cell(R2,C2,empty)

place(R1,C1,R2,C2) :: control(red) ==> ~control(red) & control(blue)
place(R1,C1,R2,C2) :: control(blue) ==> ~control(blue) & control(red)

openpair :- adjacent(R1,C1,R2,C2) & cell(R1,C1,empty) & cell(R2,C2,empty)

terminal :- ~openpair

goal(red,0) :- terminal & control(red)
goal(blue,100) :- terminal & control(red)

goal(blue,0) :- terminal & control(blue)
goal(red,100) :- terminal & control(blue)

goal(red,50) :- ~terminal
goal(blue,50) :- ~terminal