1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
<P>
<P><HTML>
<HEAD>
<TITLE>Simply Scheme:Project: Scoring Poker Hands</TITLE>
</HEAD>
<BODY>
<CITE>Simply Scheme</CITE>:
<CITE>Introducing Computer Science</CITE> 2/e Copyright (C) 1999 MIT
<H1>Project: Scoring Poker Hands</H1>
<TABLE width="100%"><TR><TD>
<IMG SRC="../simply.jpg" ALT="cover photo">
<TD><TABLE>
<TR><TD align="right"><CITE><A HREF="http://www.cs.berkeley.edu/~bh/">Brian
Harvey</A><BR>University of California, Berkeley</CITE>
<TR><TD align="right"><CITE><A HREF="http://ccrma.stanford.edu/~matt">Matthew
Wright</A><BR>University of California, Santa Barbara</CITE>
<TR><TD align="right"><BR>
<TR><TD align="right"><A HREF="../pdf/ssch15.pdf">Download PDF version</A>
<TR><TD align="right"><A HREF="../ss-toc2.html">Back to Table of Contents</A>
<TR><TD align="right"><A HREF="adv-recur.html"><STRONG>BACK</STRONG></A>
chapter thread <A HREF="../ssch16/match.html"><STRONG>NEXT</STRONG></A>
<TR><TD align="right"><A HREF="http://mitpress.mit.edu/0262082810">MIT
Press web page for <CITE>Simply Scheme</CITE></A>
</TABLE></TABLE>
<HR>
<P>The idea of this project is to invent a procedure <CODE>poker-value</CODE>
that works like this:
<P><PRE>> (<A NAME="g19"></A>poker-value '(h4 s4 c6 s6 c4))
(FULL HOUSE - FOURS OVER SIXES)
> (poker-value '(h7 s3 c5 c4 d6))
(SEVEN-HIGH STRAIGHT)
> (poker-value '(dq d10 dj da dk))
(ROYAL FLUSH - DIAMONDS)
> (poker-value '(da d6 d3 c9 h6))
(PAIR OF SIXES)
</PRE>
<P>As you can see, we are representing cards and hands just as in the
Bridge project, except that poker hands have only five
cards.<A NAME="text1" HREF="poker.html#ft1">[1]</A>
<P>Here are the various kinds of poker hands, in decreasing order of value:
<P><P><TABLE><TR><TH align="right" valign="top">•<TD> <TD valign="top">Royal flush: ten, jack, queen, king, and ace, all of the same suit
</TABLE><TABLE><TR><TH align="right" valign="top">•<TD> <TD valign="top">Straight flush: five cards of sequential rank, all of the same suit
</TABLE><TABLE><TR><TH align="right" valign="top">•<TD> <TD valign="top">Four of a kind: four cards of the same rank
</TABLE><TABLE><TR><TH align="right" valign="top">•<TD> <TD valign="top">Full house: three cards of the same rank, and two of a second rank
</TABLE><TABLE><TR><TH align="right" valign="top">•<TD> <TD valign="top">Flush: five cards of the same suit, not sequential rank
</TABLE><TABLE><TR><TH align="right" valign="top">•<TD> <TD valign="top">Straight: five cards of sequential rank, not all of the same suit
</TABLE><TABLE><TR><TH align="right" valign="top">•<TD> <TD valign="top">Three of a kind: three cards of the same rank, no other matches
</TABLE><TABLE><TR><TH align="right" valign="top">•<TD> <TD valign="top">Two pair: two pairs of cards, of two different ranks
</TABLE><TABLE><TR><TH align="right" valign="top">•<TD> <TD valign="top">Pair: two cards of the same rank, no other matches
</TABLE><TABLE><TR><TH align="right" valign="top">•<TD> <TD valign="top">Nothing: none of the above
</TABLE>
<P>
An ace can be the lowest card of a straight (ace, 2, 3, 4, 5) or
the highest card of a straight (ten, jack, queen, king, ace), but a straight
can't "wrap around"; a hand with queen, king, ace, 2, 3 would be worthless
(unless it's a flush).
<P>Notice that most of the hand categories are either entirely about the ranks
of the cards (pairs, straight, full house, etc.) or entirely about the
suits (flush). It's a good idea to begin your program by separating the
rank information and the suit information. To check for a straight flush or
royal flush, you'll have to consider both kinds of information.
<P>In what form do you want the suit information? Really, all you need is a
true or false value indicating whether or not the hand is a flush, because
there aren't any poker categories like "three of one suit and two of
another."
<P>What about ranks? There are two kinds of hand categories involving ranks:
the ones about equal ranks (pairs, full house) and the ones about sequential
ranks (straight). You might therefore want the rank information in two
forms. A sentence containing all of the ranks in the hand, in sorted order,
will make it easier to find a straight. (You still have to be careful about
aces.)
<P>For the equal-rank categories, what you want is some data structure that
will let you ask questions like "are there three cards of the same rank
in this hand?" We ended up using a representation like this:
<P><PRE>> (compute-ranks '(q 3 4 3 4))
(ONE Q TWO 3 TWO 4)
</PRE>
<P>One slightly tricky aspect of this solution is that we spelled
out the numbers of cards, <CODE>one</CODE> to <CODE>four</CODE>, instead of using the more
obvious <CODE>(1 Q 2 3 2 4)</CODE>. The reason, as you can probably tell just by
looking at the latter version, is that it would lead to confusion between
the names of the ranks, most of which are digits, and the numbers of
occurrences, which are also digits. More specifically, by spelling out the
numbers of occurrences, we can use <CODE>member?</CODE> to ask easily if there is
a three-of-a-kind rank in the hand.
<P>You may find it easier to begin by writing a version that returns only the
name of a category, such as <CODE>three of a kind</CODE>, and only after you get
that to work, revise it to give more specific results such as <CODE>three
sixes</CODE>.
<P><H2>Extra Work for Hotshots</H2>
<P>In some versions of poker, each player gets seven cards and can choose any
five of the seven to make a hand. How would it change your program if the
argument were a sentence of seven cards? (For example, in five-card poker
there is only one possible category for a hand, but in seven-card you have
to pick the best category that can be made from your cards.) Fix your
program so that it works for both five-card and seven-card hands.
<P>Another possible modification to the program is to allow for playing with
"wild" cards. If you play with "threes wild," it means that if there is
a three in your hand you're allowed to pretend it's whatever card you like.
For this modification, your program will require a second argument indicating
which cards are wild. (When you play with wild cards, there's the
possibility of having five of a kind. This beats a straight flush.)
<P>
<HR>
<A NAME="ft1" HREF="poker.html#text1">[1]</A> Later on we'll think about seven-card variants of poker.<P>
<P><A HREF="../ss-toc2.html">(back to Table of Contents)</A><P>
<A HREF="adv-recur.html"><STRONG>BACK</STRONG></A>
chapter thread <A HREF="../ssch16/match.html"><STRONG>NEXT</STRONG></A>
<P>
<ADDRESS>
<A HREF="../index.html">Brian Harvey</A>,
<CODE>bh@cs.berkeley.edu</CODE>
</ADDRESS>
</BODY>
</HTML>
|