Jump to content


prolog kayles


8 replies to this topic

#1 IronAlex

    New Member

  • Members
  • Pip
  • 7 posts

Posted 06 April 2007 - 11: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!

#2 Reedbeta

    DevMaster Staff

  • Administrators
  • 4782 posts
  • LocationBellevue, WA

Posted 07 April 2007 - 12:04 AM

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.
reedbeta.com - developer blog, OpenGL demos, and other projects

#3 IronAlex

    New Member

  • Members
  • Pip
  • 7 posts

Posted 07 April 2007 - 12:16 AM

No real reason, I just like learning new things. This is very different from web development and I'm finding it kinda fun.

#4 IronAlex

    New Member

  • Members
  • Pip
  • 7 posts

Posted 07 April 2007 - 12:21 AM

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?

#5 Trap D

    Member

  • Members
  • PipPip
  • 55 posts

Posted 07 April 2007 - 08: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:

#6 IronAlex

    New Member

  • Members
  • Pip
  • 7 posts

Posted 07 April 2007 - 09: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.

#7 Trap D

    Member

  • Members
  • PipPip
  • 55 posts

Posted 07 April 2007 - 10: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.



#8 IronAlex

    New Member

  • Members
  • Pip
  • 7 posts

Posted 07 April 2007 - 11: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?

#9 Trap D

    Member

  • Members
  • PipPip
  • 55 posts

Posted 08 April 2007 - 09: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 is

Quote

16 ?- test.
Bottles near white one [bottle(pink, half), bottle(cognac, empty)]
Colors near pink wine [red, white]






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users