Reputation: 9
In the past few days I have tried doing the game Adugo in a prolog program, but Logic programming is a bit hard for me. I'm trying do it simple as possible, the initial matrix is :
[[1,1,1,0,0,0,0],
[1,1,1,0,0,0,0],
[1,1,2,0,0,0,0],
[1,1,1,0,0,0,0],
[1,1,1,0,0,0,0]]).
The user will only control the dogs and the jaguar will be a bot.I have done this game in C++, but in Prolog I don't know how to do.
Upvotes: 0
Views: 746
Reputation:
The game is quite fun. I have not yet completed the development,
but the basic idea is to use a predicate set_arg/4
to change the board
state. A predicate arg/3 is already found in the ISO core standard, it simply picks an argument from a compound. If it were not already predefined one could bootstrap via univ (=..)/2 as follows:
arg(K, C, A) :-
C =.. [_|L],
nth1(K, L, A).
Now the predicate set_arg/3 works similarly but its aim is not to access an argument, but instead to modify an argument. But since in Prolog terms are immutable, we simply return a new modified term and thus retain declarativity. It can be bootstrapped as follows:
set_arg(K, C, A, D) :-
C =.. [F|L],
nth1(K, L, _, H),
nth1(K, R, A, H),
D =.. [F|R].
Prolog implementors will not like such a bootstrapping, the univ (=..)/2 is notoriously a predicate that slows down the Prolog interpreter. In Jekejeke Prolog we have native support for a built-in set_arg/4 which makes it less a burden for the Prolog interpreter.
With set_arg/4 we can then implement the Adugo game moves relatively simple. Here is an example move, when a Jaguar (*) eats a Dog (o) by jumping over it. The link predicate represents the board topology including "streets":
move(S, K, *, T, K-I) :-
link(K, J, M),
arg(J, S, o),
N is M+1,
link(J, I, N),
arg(I, S, +),
set_arg(J, S, +, H),
set_arg(K, H, +, L),
set_arg(I, L, *, T).
Here is a screenshot of the midst of our game development. A game session can be started by the predicate game/2. The game/2 predicate cannot yet correctly detect an end of a game. But we are working on it:
Efficiency is not so bad. Since the Prolog interpreter allows just in time multi-argument indexing the move search via the link/3 topology database is quite fast. Other areas could be maybe improved, like a counter for the dogs and some alpha/beta pruning.
Upvotes: 1