Re: programming a poker game in Prolog



"Alok" <alokjariwala@xxxxxxxxx> writes:

The exact task description is,

I suggest a different representation: Let a card be represented as a
term "card" of arity 2: first argument denotes rank, second its suit.

hand(Hs) :-
length(Hs, 5),
cards(Hs).

cards([]).
cards([card(Rank,Suit)|Cs]) :-
rank(Rank),
suit(Suit),
cards(Cs).


rank(R) :- member(R, [ace,king,queen,jack,10,9,8,7,6,5,4,3,2]).

suit(S) :- member(S, [spades,hearts,diamonds,clubs]).


We can use this for type checking as well as for generating hands
(with duplicates).


A royal flush:

royal_flush(RF) :-
memberchk(card(ace,S), RF),
memberchk(card(king,S), RF),
memberchk(card(queen,S), RF),
memberchk(card(jack,S), RF),
memberchk(card(10,S), RF).

Four of a kind:

all_dif([]).
all_dif([D|Ds]) :-
dif_from(Ds, D),
all_dif(Ds).

dif_from([], _).
dif_from([A|As], D) :-
dif(A, D),
dif_from(As, D).


four_of_a_kind(FOAK) :-
all_dif([S1,S2,S3,S4]),
member(card(R,S1), FOAK),
member(card(R,S2), FOAK),
member(card(R,S3), FOAK),
member(card(R,S4), FOAK).


We can generate a royal flush:

?- hand(H), royal_flush(H).

H = [card(ace, spades), card(king, spades), card(queen, spades),
card(jack, spades), card(10, spades)]

and four of a kind:

?- hand(H), four_of_a_kind(H).

H = [card(ace, spades), card(ace, spades), card(ace, hearts),
card(ace, diamonds), card(ace, clubs)]



Best wishes,
Markus Triska
.



Relevant Pages

  • Re: cards out of wack
    ... in main, not in the card class, and pass a reference to it to the card class ... > case ACE: ... >ace of spades ...
    (comp.lang.java.programmer)
  • Re: I dont believe what I just read
    ... definitely have bid more than two spades on the second round and North ... double or Michael's qbid surely limits the spade support he can hold. ... wwith three card support and a hand worth the three level ...
    (rec.games.bridge)
  • Re: What does this bid show?
    ... my spades were very good so I wanted to highlight the spade ... my final bid of 4H would never be on a four card heart suit. ... know if partner had a forcing game raise in spades or diamonds? ...
    (rec.games.bridge)
  • Re: probability question
    ... >> already discovered that one opponent has ... > 1 card difference....exact toss-up ... If RHO had 3 spades, ... I'd play for the drop. ...
    (rec.games.bridge)
  • Re: probability question
    ... >> already discovered that one opponent has ... > 1 card difference....exact toss-up ... On the long spades opponenta discard red ... I'd play for the drop. ...
    (rec.games.bridge)