Take the 2-minute tour ×

I am trying to code a poker game using Prolog. I understand how to code the royal flush but my four of a kind code won't work.

I have the following code:

member(card(V, T1), R),
member(card(V, T2), R),
member(card(V, T3), R),
member(card(V, T4), R).

where V is the rank which I want to be the same and T1,T2,T3,T4 are the suits. R is my list of cards.

Can anyone explain how to code the four of a kind in prolog please and explain what I am doing wrong please?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Your problem is that you search the whole hand all the times and T1, T2.. are free variables, so

member(card(V, T1), R),

unifies 4 times with the same card, and four_of_a_kind always returns true.

Solution: Just lock the suits.


is_card(X,Y) :-
    number(X), between(1,13,X),
    member(Y, [c,d,h,s]). /* clubs, diamonds, hearts and spades */

four_of_a_kind(R) :-
    member(card(V,c), R),
    member(card(V,d), R),
    member(card(V,h), R),
    member(card(V,s), R), !.

Some query:

?- four_of_a_kind([card(7,c), card(7,d), card(7,h), card(9,s), card(7,s)]).

?- four_of_a_kind([card(7,c), card(7,d), card(9,h), card(9,s), card(7,s)]).
share|improve this answer
Is it a good idea to have card both a compound term and a predicate? Maybe the predicate should be called is_card/2? –  aschepler Aug 5 '12 at 23:08
You are right, I'll edit. –  Haile Aug 5 '12 at 23:13
add comment

Your Answer


