For Programmers: Free Programming Magazines  


Home > Archive > Prolog > October 2006 > programming a poker game in Prolog









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author programming a poker game in Prolog
Alok

2006-09-28, 7:00 pm


I have an assignment t program a poker game in Prolog can anyone help
me ??

Alok

A.L.

2006-09-28, 7:00 pm

On 28 Sep 2006 10:53:33 -0700, "Alok" <alokjariwala@gmail.com>
wrote:

>
>I have an assignment t program a poker game in Prolog can anyone help
>me ??
>
>Alok


Yes. Learn to play poker.

A.L.
russell kym horsell

2006-09-29, 4:02 am

Alok <alokjariwala@gmail.com> wrote:
> I have an assignment t program a poker game in Prolog can anyone help
> me ??



(1) Learn to play poker. Watch what you do when you play.
Any variety of poker is apparently ok. :)
If you know what you're doing, you can write a program to do it, too.
Has interesting corollary. :)

Playing games with incomplete or (as in this case) deliberately and
maximally misleading info, or perhaps needing
the maniuplation of things like "I know X knows I may know Y" is not
really introductory Prolog stuff.

The theoretical underpins are things like probability and (shudder)
inductive logic. Setting up simple game theory-type tableaux is
also likely to be involved.

"If I do X and player A does Y and player B does Z,
then what is my expected/optimistic/pessimistic payoff?".

Then do what seems to maximise the metric chosen.

Some intro stuff on mathematical thy in poker was written by
"Johnny" von Neuman back in the 30s or 40s. It's still relevant, despite being
for "toy" draw poker.


The method I've used in one dark period in the past is
(i) build a computer cluster, (*)
(ii) write some distributed s/w to simulate intelligent agents
playing poker; add in some adaptive capability,
(iii) run for several 1000 hours and gather some statistically
significant data on strategy and (simulated) player populations.

Much of this is implicit in books written c 1980 by significant players. :)

Haven't looked at bridge(s), but played with 500, draw, Omaha & Texas.
It's all much the same kinds of techniques, both to play (if seriously --
I like more to just obviously cheat at 500) and to program.


===
(*) It had one or 2 other uses. Not just a poker cluster. :)
Duncan Patton

2006-09-29, 4:02 am

On Thu, 28 Sep 2006 20:03:59 +0200
Markus Triska <triska@gmx.at> wrote:

> "Alok" <alokjariwala@gmail.com> writes:
>
>
> Please post the exact task description.
>
> Best wishes, Markus Triska.


Is the prof's name Ada?

Dhu

--
???????????????????????????????????????

Open Systems Integration

Contact Fubar the Hack: fubar AT neotext.ca

Area code seven eight zero, Exchange four six six, Local zero one zero nine

Highland terms, Canadian workmanship.

All persons named herein are purely fictional victims
of the Canidian Bagle Breeder's Association.

Save the Bagle!

Sun Dhu


???????????????????????????????????????

Alok

2006-09-29, 4:02 am

The exact task description is,
Prolog Cards
Overview
Help me convince my children's grandfather that gambling is a waste of
time, by creating a prolog program to play cards. For this assignment
you will implement a prolog predicate,

wins(Hand1, Hand2).

that succeeds when Hand1 is stronger than (wins over) Hand2, according
to Texas Hold'em Poker Rules.

Representation
A hand is represented by a list of 5 cards. A card is represented by a
2 character list (a string) where the first character is one of
akqjt98765432 (ace, king, queen, jack, ten, nine, eight, ...) and the
second character is one of cdhs (clubs, diamonds, hearts, spades). So,
for example, "8d" is the eight of diamonds and "as" is the ace of
spades.

So a royal flush could be represented by,

["ts", "js", "qs", "ks", "as"]

Note that the order of the cards within a hand is not important, so
this is an equivalent hand,

["as", "ks", "js", "ts", "qs"]


A ranking of Poker hands from royal flush to high card
Standard five-card Poker hands are ranked here in order of strength,
from the strongest Poker hand to the weakest.

Royal Flush
This is the best possible hand in standard five-card Poker. Ace, King,
Queen, Jack and 10, all of the same suit.

Straight Flush
Any five-card sequence in the same suit (e.g.: 8, 9, 10, Jack and Queen
of clubs; or 2, 3, 4, 5 and 6 of diamonds).

Four of a Kind
All four cards of the same value (e.g.: 8, 8, 8, 8; or Queen, Queen,
Queen, Queen).

Full House
Three of a kind combined with a pair (e.g.: 10, 10, 10 with 6, 6; or
King, King, King with 5, 5).

Flush
Any five cards of the same suit, but not in sequence (e.g.: 4, 5, 7, 10
and King of spades).

Straight
Five cards in sequence, but not in the same suit (e.g.: 7 of clubs, 8
of clubs, 9 of diamonds, 10 of spades and Jack of diamonds).

Three of a Kind
Three cards of the same value (e.g.: 3, 3, 3; or Jack, Jack, Jack).

Two Pair
Two separate pairs (e.g.: 2, 2, Queen, Queen).

Pair
Two cards of the same value (e.g.: 7, 7).

High Card
If a Poker hand contains none of the above combinations, it's valued by
the highest card in it.

Texas hold'em poker hand ranking rules state that if the highest cards
are a tie then the tie is broken by the second highest card. Suits are
not used to break ties.

Hints
Try this simple query to learn how gprolog encodes strings,

?- [H|T]="abc".


You may want to implement your own predicate to "sort" cards in a hand.
Since it may be easier to write rules that compare sorted hands instead
of arbitrary hands.

Friends I dont have experience in programming it is just that i cannot
run away from te situation i have to take the course and get it done.it
someone knows it thoroughly pls give some time on this and help me. it
will be a great help

Pls help me.

Alok jariwala

Duncan Patton wrote:
> On Thu, 28 Sep 2006 20:03:59 +0200
> Markus Triska <triska@gmx.at> wrote:
>
>
> Is the prof's name Ada?
>
> Dhu
>
> --
> ???????????????????????????????????????
>
> Open Systems Integration
>
> Contact Fubar the Hack: fubar AT neotext.ca
>
> Area code seven eight zero, Exchange four six six, Local zero one zero nine
>
> Highland terms, Canadian workmanship.
>
> All persons named herein are purely fictional victims
> of the Canidian Bagle Breeder's Association.
>
> Save the Bagle!
>
> Sun Dhu
>
>
> ???????????????????????????????????????


Matthew Huntbach

2006-09-29, 4:02 am



On Fri, 29 Sep 2006, Alok wrote:

> The exact task description is,
> Prolog Cards
> Overview
> Help me convince my children's grandfather that gambling is a waste of
> time, by creating a prolog program to play cards. For this assignment
> you will implement a prolog predicate,
>
> wins(Hand1, Hand2).
>
> that succeeds when Hand1 is stronger than (wins over) Hand2, according
> to Texas Hold'em Poker Rules.


Right, so first lesson is that if you have a problem, you need to
provide a good specification for it.

What you originally wrote suggested you were being asked to write
a program which played poker i.e. made the tactical decisions which
are part of playing the game. This would be a big research problem,
there are people who have worked on it, papers you can read about
their work, but the actual language used to program the algorithms
and represent the data would be a minor issue.

Now you have made it clear all you want is a program which takes two
poker hands and say which one wins over the other. Fine - so why didn't
you say this in the first place?

> Friends I dont have experience in programming it is just that i cannot
> run away from te situation i have to take the course and get it done.it
> someone knows it thoroughly pls give some time on this and help me. it
> will be a great help


You have been set this exercise because it will help you learn Prolog
by doing it. If someone gives you the answer, you will have learnt
nothing. The person who gave yuou the answer will have been guilty of
aiding and abetting fraud - you will have fraudulently gained a
qualification saying you had a certain level of Prolog programming
ability when you hadn't.

Matthew Huntbach

Markus Triska

2006-09-29, 7:01 pm

"Alok" <alokjariwala@gmail.com> 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
russell kym horsell

2006-09-30, 3:59 am

Alok <alokjariwala@gmail.com> wrote:
> The exact task description is,
> Prolog Cards
> Overview
> Help me convince my children's grandfather that gambling is a waste of
> time, by creating a prolog program to play cards. For this assignment
> you will implement a prolog predicate,
> wins(Hand1, Hand2).
> that succeeds when Hand1 is stronger than (wins over) Hand2, according
> to Texas Hold'em Poker Rules.

[...]

Not exactly "playing poker", is it?

Since you're expecting a "yes/no" answer rather than a probability
this is a pretty simple problem. Just classify the hand into
the usual types -- 2-of-kind, 3-of-kind, 4-of-kind, full-house, etc,
along with a "paramter" that describes over cards, and then you know
4-of-kind beats 3-of-kind, etc.

Pretty much the way you'd write down the rules in English can be
translated line-to-line into Prolog.

Convincing your gradpa (a charming story :) will take more, I'm sure.

You might like to then drive your "wins" predicate to show how often
hand H wins, or generalise the "wins" to just work from lists of 2..7 cards.
Maybe wins should take 2 sets of hole cards, plus a list of 0..5 community cards.
Bill Spight

2006-09-30, 7:01 pm

Dear Alok,

Assignment:

> Help me convince my children's grandfather that gambling is a waste of
> time, by creating a prolog program to play cards. For this assignment
> you will implement a prolog predicate,
>
> wins(Hand1, Hand2).
>
> that succeeds when Hand1 is stronger than (wins over) Hand2, according
> to Texas Hold'em Poker Rules.
>
> Representation
> A hand is represented by a list of 5 cards.


Forget Texas Hold'em. A Texas Hold'em hand consists of 7 cards, 2 in
hand plus 5 shared cards.

Get out any old poker manual and translate the definitions of various
hands into Prolog. Certain types of hands beat other types. E. g., three
of a kind beats two pair. Within each type of hand some hands beat others.
E. g., Ace high beats King high. Translate the comparison rules into
Prolog.

Best wishes,

Bill
Alok

2006-10-01, 4:00 am

Hey Thank you friends,

Thank you for helping a novice like me. Ok i have a question how can i
call a txt file where i wrote my code in GNU Prolog.

If anyone can guide me how can learn this Prolog would be helpful . As
i am not able to figure out.

I have to learn anyways for my curriculam.
Loook forward for some good eteacher to teach me this . hehehe..... :)

The time constraints is killin me pls lend me some of ur time, guidance
and support. I wil do it myself.

Thank you,
Alok


Bill Spight wrote:
> Dear Alok,
>
> Assignment:
>
>
> Forget Texas Hold'em. A Texas Hold'em hand consists of 7 cards, 2 in
> hand plus 5 shared cards.
>
> Get out any old poker manual and translate the definitions of various
> hands into Prolog. Certain types of hands beat other types. E. g., three
> of a kind beats two pair. Within each type of hand some hands beat others.
> E. g., Ace high beats King high. Translate the comparison rules into
> Prolog.
>
> Best wishes,
>
> Bill


Joseph Hansen

2006-10-01, 4:00 am

> how can i call a txt file where i wrote my code in GNU Prolog.

most prologs will accept either of

?- consult('/path/to/file').

or

?- ['/path/to/file']

cheers
jeh
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com