Reputation: 4091
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:
four_of_a_kind(R):-
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?
Upvotes: 1
Views: 386
Reputation: 3180
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.
Code:
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)]).
true.
?- four_of_a_kind([card(7,c), card(7,d), card(9,h), card(9,s), card(7,s)]).
false.
Upvotes: 2