# Design a game based on 5*5 matrix and two players moving in spiral way to the center given the dice value

```%trace
domains

startx,starty = integer
dicevalx,dicevaly = integer
totalx,totaly = integer

database

state(startx,starty,dicevalx,dicevaly)

predicates

play(startx,starty)
rand(startx,starty)
turnx(startx,starty,dicevalx,totalx,totaly)
turny(startx,starty,dicevaly,totalx,totaly)
checkx(startx,starty,dicevalx,totalx,totaly)
checky(startx,starty,dicevaly,totalx,totaly)

clauses

play(Startx,StartY):-
Startx = 25,
Starty <= 25,
write(\"X is Winner\").

play(Startx,StartY):-
Starty = 25,
Startx <= 25,
write(\"Y is winner\").

play(Startx,Starty):-
Startx = 0,
Starty = 0,
rand(6,Dicevalx),
Newvalx = Dicevalx,
turnx(Startx,Starty,Newvalx,Totalx,Totaly),
rand(6,Dicevaly),
Newvaly = Dicevaly,
Totalxx = Totalx,
Totalxy = Totaly,
turny(Totalxx,Totalxy,Newvaly,Totalyx,Totalyy),
Newx = Totalyx,
Newy = Totalyy,
assert(state(Newx,Newy,Newvalx,Newvaly)),
write(\"X : \",Newx,\"  Y : \",Newy,\"  Diecevalx : \",Newvalx,\"  Dicevaly :\",Newvaly),nl,
play(Newx,Newy).

play(Startx,Starty):-
Startx <= 19,
Starty <= 19,
rand(6,Dicevalx),
Newvalx = Dicevalx,
turnx(Startx,Starty,Newvalx,Totalx,Totaly),
rand(6,Dicevaly),
Newvaly = Dicevaly,
Totalxx = Totalx,
Totalxy = Totaly,
turny(Totalxx,Totalxy,Newvaly,Totalyx,Totalyy),
Newx = Totalyx,
Newy = Totalyy,
assert(state(Newx,Newy,Newvalx,Newvaly)),
write(\"X : \",Newx,\"  Y : \",Newy,\"  Dicevalx : \",Newvalx,\"  Dicevaly : \",Newvaly),nl,
play(Newx,Newy).

play(Startx,Starty):-
Startx < 26,
Starty < 26,
rand(6,Dicevalx),
Newvalx=Dicevalx,
checkx(Startx,Starty,Newvalx,Totalx,Totaly),
Totalxx = Totalx,
Totalxy = Totaly,
rand(6,Dicevaly),
Newvaly=Dicevaly,
checky(Totalxx,Totalxy,Newvaly,Totalyx,Totalyy),
Newx = Totalyx,
Newy = Totalyy,
assert(state(Newx,Newy,Newvalx,Newvaly)),
write(\"X : \",Newx,\"  Y : \",Newy,\"  Dicevalx : \",Newvalx,\"  Dicevaly : \",Newvaly),nl,
play(Newx,Newy).

rand(X,Y):-
random(Z),
Y = (X - 1) * Z + 1.

turnx(Startx,Starty,Dicevalx,Totalx,Totaly):-
Startx + Dicevalx <> Starty,
free(Totalx),
Totalx = Startx + Dicevalx,
free(Totaly),
Totaly = Starty.

turnx(Startx,Starty,Dicevalx,Totalx,Totaly):-
Startx + Dicevalx = Starty,
free(Totalx),
Totalx = Startx + Dicevalx,
free(Totaly),
Totaly = 0.

turny(Startx,Starty,Dicevaly,Totalx,Totaly):-
Starty + Dicevaly <> Startx,
free(Totaly),
Totaly = Starty + Dicevaly,
free(Totalx),
Totalx = Startx.

turny(Startx,Starty,Dicevaly,Totalx,Totaly):-
Starty + Dicevaly = Startx,
free(Totaly),
Totaly = Starty + Dicevaly,
free(Totalx),
Totalx = 0.

checkx(Startx,Starty,Dicevalx,Totalx,Totaly):-
25 - Startx >= Dicevalx,
turnx(Startx,Starty,Dicevalx,Totalx,Totaly).

checkx(Startx,Starty,Dicevalx,Totalx,Totaly):-
25 - Startx <= Dicevalx,
free(Totalx),
Totalx = Startx,
Totaly = Starty.

checky(Startx,Starty,Dicevaly,Totalx,Totaly):-
25 - Starty >= Dicevaly,
turny(Startx,Starty,Dicevaly,Totalx,Totaly).

checky(Startx,Starty,Dicevaly,Totalx,Totaly):-
25 - Starty <= Dicevaly,
free(Totaly),
Totaly = Starty,
Totalx = Startx.

goal
makewindow(1,7,7,\"Spriral Move Game (2 Player)\",0,0,25,80),
assert(state(0,0,0,0)),
play(0,0),
save(\"spiral_out.txt\").

Output :

+-------------------------Spriral Move Game (2 Player)-------------------------+
¦X : 5  Y : 4  Diecevalx : 5  Dicevaly :4                                      ¦
¦X : 10  Y : 9  Dicevalx : 5  Dicevaly : 5                                     ¦
¦X : 0  Y : 13  Dicevalx : 3  Dicevaly : 4                                     ¦
¦X : 6  Y : 18  Dicevalx : 6  Dicevaly : 5                                     ¦
¦X : 12  Y : 21  Dicevalx : 6  Dicevaly : 3                                    ¦
¦X : 18  Y : 25  Dicevalx : 6  Dicevaly : 4                                    ¦
¦Y is winner                                                                   ¦
¦Press the SPACE bar                                                           ¦
¦                                                                              ¦
¦
----------------------------------------------------------------
spiral_out.txt

state(0,0,0,0)
state(5,4,5,4)
state(10,9,5,5)
state(0,13,3,4)
state(6,18,6,5)
state(12,21,6,3)
state(18,25,6,4)
```