Beginner trying rock, paper, scissors, shotgun and need some pointers

36 views
Skip to first unread message

Krakob

unread,
Jan 1, 2018, 5:35:00 PM1/1/18
to SWI-Prolog
Hello! I'm trying to learn Prolog so I figured rock paper scissors would be good practice, and it'd be a better learning experience with shotgun in order to broaden the scope and make it less linear, so to speak. Here's my code:
beats(rock, scissors).  % Basic rules of RPS
beats
(scissors, paper).
beats
(paper, rock).

beats
(shotgun, X) :- X \= shotgun.  % Nothing beats shotgun, but shotgun does not beat itself

winner
(X, Y) :- beats(X, Y) -> X; beats(Y, X) -> Y; neither.  % The winner must explicitly beat the other

I'm finding a few issues here:
1. While using stuff like beats(What, rock). works, beats(shotgun, What). should be giving me rock, paper, and scissors as alternatives. I suppose I could be hardcoding this i.e. beats(shotgun, rock). and so on, but that is not a very programmatic solution.
2. The winner logic seems to be correct, but it's trying to call the value I get as a function. I'm under the impression that a lower-case value is equivalent of an atom in Erlang. Returning true. or false. seems to be fine but neither. or rock. and so on does not.

Could y'all tell me why I'm wrong? :)


Barb Knox

unread,
Jan 2, 2018, 3:56:24 PM1/2/18
to SWI-Prolog
On 2/01/2018, at 11:35, Krakob <jako...@hotmail.com> wrote:

Hello! I'm trying to learn Prolog so I figured rock paper scissors would be good practice, and it'd be a better learning experience with shotgun in order to broaden the scope and make it less linear, so to speak. Here's my code:
beats(rock, scissors).  % Basic rules of RPS
beats
(scissors, paper).
beats
(paper, rock).

beats
(shotgun, X) :- X \= shotgun.  % Nothing beats shotgun, but shotgun does not beat itself

winner
(X, Y) :- beats(X, Y) -> X; beats(Y, X) -> Y; neither.  % The winner must explicitly beat the other

I'm finding a few issues here:

1. While using stuff like beats(What, rock). works, beats(shotgun, What). should be giving me rock, paper, and scissors as alternatives. I suppose I could be hardcoding this i.e. beats(shotgun, rock). and so on, but that is not a very programmatic solution.

Maybe, maybe not.  It *is* extendable, e.g. to beats(uzi, shotgun) etc.


2. The winner logic seems to be correct, but it's trying to call the value I get as a function. I'm under the impression that a lower-case value is equivalent of an atom in Erlang. Returning true. or false. seems to be fine but neither. or rock. and so on does not.

A term after a -> is a goal, meaning that it gets called.  If it’s a variable then the value of the variable is called.

What you want is to have it return the winner via an argument:  winner(X, Y, TheWinner), and if there is no winner then you can have it return neither or have it fail.


Could y'all tell me why I'm wrong? :)

HTH

-- 
---------------------------
|  BBB                b    \    Barbara at LivingHistory stop co stop uk
|  B  B   aa     rrr  b     |
|  BBB   a  a   r     bbb   |   ,008015L080180,022036,029037
|  B  B  a  a   r     b  b  |   ,047045,L014114L4.
|  BBB    aa a  r     bbb   |
-----------------------------

Krakob .

unread,
Jan 4, 2018, 7:42:42 AM1/4/18
to Barb Knox, SWI-Prolog
Yeah, I figured it out! Thanks a bunch! I never thought I'd be this impressed with digital rock paper scissors, haha :P

--
You received this message because you are subscribed to a topic in the Google Groups "SWI-Prolog" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/swi-prolog/I1JxClRTUAg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to swi-prolog+...@googlegroups.com.
Visit this group at https://groups.google.com/group/swi-prolog.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages