Re: programming a poker game in Prolog
- From: Markus Triska <triska@xxxxxx>
- Date: Fri, 29 Sep 2006 18:21:40 +0200
"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
.
- References:
- Re: programming a poker game in Prolog
- From: Duncan Patton
- Re: programming a poker game in Prolog
- From: Alok
- Re: programming a poker game in Prolog
- Prev by Date: Re: programming a poker game in Prolog
- Next by Date: Re: no backtrack
- Previous by thread: Re: programming a poker game in Prolog
- Next by thread: Re: programming a poker game in Prolog
- Index(es):
Relevant Pages
|
|