about summary refs log tree commit diff stats
path: root/js/games/nluqo.github.io/~bh/ssch9/bridge.html
blob: a523d27996f33ff8876d96b219a70e5b172df408 (plain) (blame)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<P>

<P><HTML>
<HEAD>
<TITLE>Simply Scheme:Project: Scoring Bridge Hands</TITLE>
</HEAD>
<BODY>
<CITE>Simply Scheme</CITE>:
<CITE>Introducing Computer Science</CITE> 2/e Copyright (C) 1999 MIT
<H1>Project: Scoring Bridge 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/ssch09.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="lambda.html"><STRONG>BACK</STRONG></A>
chapter thread <A HREF="../ssch10/ttt.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>
 
At the beginning of a game of bridge, each player assigns a value to his or
her hand by counting <EM>points.</EM>  Bridge players use these points in the
first part of the game, the &quot;bidding,&quot; to decide how high to bid.  (A
bid is a promise about how well you'll do in the rest of the game.  If you
succeed in meeting your bid you win, and if you don't meet the bid, you
lose.)  For example, if you have fewer than six points, you generally don't
bid anything at all.

<P>You're going to write a computer program to look at a bridge hand and decide
how many points it's worth.  You won't have to know anything about the rest
of the game; we'll tell you the rules for counting points.

<P>A bridge hand contains thirteen cards.  Each ace in the hand is worth four
<A NAME="g1"></A>
<A NAME="g2"></A>
points, each king is worth three points, each queen two points, and each
jack one.  The other cards, twos through tens, have no point value.
So if your hand has two aces, a king, two jacks, and eight other
cards, it's worth thirteen points.

<P>A bridge hand might also have some &quot;distribution&quot; points, which are points
having to do with the distribution of the thirteen cards among the four
suits.  If your hand has only two cards of a particular suit, then it is
worth an extra point.  If it has a &quot;singleton,&quot; only one card of a
particular suit, that's worth two extra points.  A &quot;void,&quot; no cards in a
particular suit, is worth three points.

<P>In our program, we'll represent a card by a word like <CODE>h5</CODE> (five of
hearts) or <CODE>dk</CODE> (king of diamonds).<A NAME="text1" HREF="bridge.html#ft1">[1]</A> A hand will be a
sentence of cards, like this:

<P><PRE>(sa s10 s7 s6 s2 hq hj h9 ck c4 dk d9 d3)
</PRE>

<P>This hand is worth 14 points: ace of spades (4), plus queen of hearts
(2), plus jack of hearts (1), plus king of clubs (3), plus king of diamonds
(3), plus one more for having only two clubs.

<P>To find the suit of a card, we take its <CODE>first</CODE>, and to find the rank, we
take the <CODE>butfirst</CODE>.  (Why not the <CODE>last</CODE>?)

<P>We have a particular program structure in mind.  We'll describe all of the
procedures you need to write; if you turn each description into a working
procedure, then you should have a complete program.  In writing each
procedure, take advantage of the ones you've already written.  Our
descriptions are ordered <EM>bottom-up</EM>, which means that for each
procedure you will already have written the helper procedures you need.
(This ordering will help you write the project, but it means that we're
beginning with small details.  If we were describing a project to help you
understand its structure, we'd do it in <EM>top-down</EM> order,
starting with the most general procedures.  We'll do that in the next
chapter, in which we present a tic-tac-toe program as a larger Scheme
programming example.)

<H3><CODE>Card-val</CODE></H3>

<P>Write a procedure <CODE><A NAME="g3"></A>card-val</CODE> that takes a single card as its
argument and returns the value of that card.

<P><PRE>&gt; (card-val 'cq)
2

&gt; (card-val 's7)
0

&gt; (card-val 'ha)
4
</PRE>

<H3><CODE>High-card-points</CODE></H3>

<P>Write a procedure <CODE><A NAME="g4"></A>high-card-points</CODE> that takes a hand as its
argument and returns the total number of points from high cards in the
hand.  (This procedure does <EM>not</EM> count distribution points.)

<P><PRE>&gt; (high-card-points '(sa s10 hq ck c4))
9

&gt; (high-card-points '(sa s10 s7 s6 s2 hq hj h9 ck c4 dk d9 d3))
13
</PRE>

<H3><CODE>Count-suit</CODE></H3>

<P>Write a procedure <CODE><A NAME="g5"></A>count-suit</CODE> that takes a suit and a hand as
arguments and returns the number of cards in the hand with the given suit.

<P><PRE>&gt; (count-suit 's '(sa s10 hq ck c4))
2

&gt; (count-suit 'c '(sa s10 s7 s6 s2 hq hj h9 ck c4 dk d9 d3))
2

&gt; (count-suit 'd '(h3 d7 sk s3 c10 dq d8 s9 s4 d10 c7 d4 s2))
5
</PRE>

<H3><CODE>Suit-counts</CODE></H3>

<P>Write a procedure <CODE><A NAME="g6"></A>suit-counts</CODE> that takes a hand as its argument
and returns a sentence containing the number of spades, the number of hearts,
the number of clubs, and the number of diamonds in the hand.

<P><PRE>&gt; (suit-counts '(sa s10 hq ck c4))
(2 1 2 0)

&gt; (suit-counts '(sa s10 s7 s6 s2 hq hj h9 ck c4 dk d9 d3))
(5 3 2 3)

&gt; (suit-counts '(h3 d7 sk s3 c10 dq d8 s9 s4 d10 c7 d4 s2))
(5 1 2 5)
</PRE>

<H3><CODE>Suit-dist-points</CODE></H3>

<P>Write <CODE><A NAME="g7"></A>suit-dist-points</CODE> that takes a number as its argument,
interpreting it as the number of cards in a suit.  The procedure should
return the number of distribution points your hand gets for having that
number of cards in a particular suit.

<P><PRE>&gt; (suit-dist-points 2)
1

&gt; (suit-dist-points 7)
0

&gt; (suit-dist-points 0)
3
</PRE>

<H3><CODE>Hand-dist-points</CODE></H3>

<P>Write <CODE><A NAME="g8"></A>hand-dist-points</CODE>, which takes a hand as its argument and
returns the number of distribution points the hand is worth.

<P><PRE>&gt; (hand-dist-points '(sa s10 s7 s6 s2 hq hj h9 ck c4 dk d9 d3))
1

&gt; (hand-dist-points '(h3 d7 sk s3 c10 dq d8 s9 s4 d10 c7 d4 s2))
3
</PRE>

<H3><CODE>Bridge-val</CODE></H3>

<P>Write a procedure <CODE><A NAME="g9"></A>bridge-val</CODE> that takes a hand as its argument
and returns the total number of points that the hand is worth.

<P><PRE>&gt; (bridge-val '(sa s10 s7 s6 s2 hq hj h9 ck c4 dk d9 d3))
14

&gt; (bridge-val '(h3 d7 sk s3 c10 dq d8 s9 s4 d10 c7 d4 s2))
8
</PRE>

<P>

<HR>
<A NAME="ft1" HREF="bridge.html#text1">[1]</A> Why not <CODE>5h</CODE>?  Scheme
words that begin with a digit but aren't numbers have to be surrounded with
double-quote marks.  Putting the suit first avoids that.<P>
<P><A HREF="../ss-toc2.html">(back to Table of Contents)</A><P>
<A HREF="lambda.html"><STRONG>BACK</STRONG></A>
chapter thread <A HREF="../ssch10/ttt.html"><STRONG>NEXT</STRONG></A>

<P>
<ADDRESS>
<A HREF="../index.html">Brian Harvey</A>, 
<CODE>bh@cs.berkeley.edu</CODE>
</ADDRESS>
</BODY>
</HTML>