Reputation: 194
I am writing a game in Prolog. The game has a "board" of the form shown below and it has two lists of "pawns". The size of the board is fixed to 16 positions. I want to make the board "dynamic" so when the game starts the user will define the size. I have thought of a way but the problem is that my way is more on a procedural way which for Prolog is not that correct. I thought of creating a predicate size/1 which will be initialized by the user in the beginning and then all the lists will be initialized according to that predicate using repeats and cuts and assert... Could someone propose any better implementation?
/*--------------------------------------------------------------------------
Initial armies (lists of 1 to 8)
--------------------------------------------------------------------------*/
black_army([1,2,3,4,5,6,7,8]).
white_army([1,2,3,4,5,6,7,8]).
/*--------------------------------------------------------------------------
Initial Board (all positions empty)
--------------------------------------------------------------------------*/
board([
(1,-,-),(2,-,-),(3,-,-),(4,-,-),
(5,-,-),(6,-,-),(7,-,-),(8,-,-),
(9,-,-),(10,-,-),(11,-,-),
(13,-,-),(14,-,-),(15,-,-),(16,-,-),(12,-,-)
]).
Upvotes: 2
Views: 302
Reputation: 60034
I think I would keep board size implicit in data declaration, just make it dynamic, and initialize when user request to change: so, keep your code as is, just add in front (note: untested code)
:- dynamic(black_army/1).
:- dynamic(white_army/1).
:- dynamic(board/1).
initialize_data(Size) :-
Size > 1, % this should be a sensible value, of course
retract(black_army(_)),
retract(white_army(_)),
retract(board(_)),
numlist(1,Size,B),
assertz(black_army(B)),
numlist(1,Size,W),
assertz(white_army(W)),
D is Size*2, findall((I,-,-), between(1,D,I), Board),
assertz(board(Board)).
Upvotes: 0