View Full Version : prolog kayles
IronAlex
04-06-2007, 04:41 PM
Hey everyone,
I'm trying to implement a very simple prolog program (I'm just learning the language) to represent the game kayles. Basically, the game is like skittles, you have a horizontal row of bottles, either with space between the bottles or not.
So a set up of the game could be:
BB B BB B
A player can knock down a single bottle, or any two bottles that are directly next to each other.
And I'm not sure how to begin to represent this game as prolog terms. I understand I need to represent the state of each frame, but how would I do this in this case? Do I need a different state for every bottle, or do I need one state holding a list?
Please don't be too harsh, I'm just a beginner. I've been in web development for a while and this is a whole new ballgame!
Reedbeta
04-06-2007, 05:04 PM
May I ask why you want to learn prolog? If you're interested in game development, there are much better languages to do it in.
IronAlex
04-06-2007, 05:16 PM
No real reason, I just like learning new things. This is very different from web development and I'm finding it kinda fun.
IronAlex
04-06-2007, 05:21 PM
Incidentally, I think I should be representing a state of the game as follows:
state([1,1,0,1,0,1,1,0,1])
where 1 is a bottle and 0 is not? would I need to reference individual bottles?
Trap D
04-07-2007, 01:34 PM
I don't know enough of what you want to do to to say something.
You can do state(1,1,0,1,0,1,1,0,1) like that too.
But it's true that the shape "state([1,1,0,1,0,1,1,0,1])" maybe usefull for certains actions.
I think that a bottle can be "up" or "down".
So you can have à state like that : state([u,u,0,d,0,d,d,0,u]).
In SWI-Prolog you can do that :
% test if the #N bottle is up
is_bottle_up(state(St), N) :-
nth1(N, St, u).Hope it's usefull. :blush:
IronAlex
04-07-2007, 02:06 PM
Trap, thanks for your reply.
I have actually decided it would be better to do something simple like:
state(bottles, map)
where bottles is a list of the bottles in play, and map is a list of 'relations' between bottles, i.e. [ [b1,b2], [b2,b1] ] means that b2 and b1 are touching..
I think this is a good way to do things for the moment.
Trap D
04-07-2007, 03:58 PM
With a list, and in SWI-Prolog, you can define "near" like that
near(B1, B2, Lst) :-
nth1(N1,B1,Lst),
nth1(N2,B2,Lst),
abs(N1-N2) < 2.
IronAlex
04-07-2007, 04:09 PM
Trap, that's very interesting.
You mean if I had a list [1,1,0,1,0,1] right?
Not if I had two lists?
Trap D
04-08-2007, 02:29 AM
Not for two lists but it works for a list of lists or a list of anything else.
First I apologize, the good code for near is
near(B1, B2, Lst) :-
nth1(N1,Lst, B1),
nth1(N2,Lst, B2),
1 is abs(N1-N2).
Now you can have this kind of code wich is quite amazing when you come from imperative langage :
test :-
Lst = [bottle(red, full), bottle(pink, half), bottle(white, full), bottle(cognac, empty)],
% seeking for the bottles near white wine
bagof(B, near(bottle(white, _),B, Lst), LB),format('Bottles near white one ~w~n', [LB]),
% seeking for the color of wine near the pink one
bagof(C, near(bottle(pink, _),bottle(C, _), Lst), LC),format('Colors near pink wine ~w~n', [LC]).
the result is16 ?- test.
Bottles near white one [bottle(pink, half), bottle(cognac, empty)]
Colors near pink wine [red, white]
vBulletin, Copyright ©2000-2010, Jelsoft Enterprises Ltd.