DOC PREVIEW
Berkeley COMPSCI 61A - Programming Project 1 - Twenty-One

This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 61A Spring, 2010Programming Project 1: Twenty-One(Scratch version)For our purposes, the rules of twenty-one (“blackjack”) are as fol lows. There are twoplayers: the “customer” and the “dealer”. The object of the game is to be dealt a set ofcards that comes as close to 21 as possible without going over 21 (“busting”). A card isrepresented as a word, such as 10s for the ten of spades. (Ace, jack, queen, and king are a,j, q, and k.) Picture cards are worth 10 points; an ace is worth either 1 or 11 at the player’soption. We reshuffle the deck after each round, so strategies based on remembering whichcards were dealt earlier are not possible. Each player is dealt two cards, with one of thedealer’s cards face up. The dealer always takes another card (“hits”) if he has 16 or less,and always stops (“sta nds”) with 17 or more. The customer can play however s/he chooses,but must play before the dealer. I f t he customer exceeds 21, s/he immediately loses (andthe dealer doesn’t bother to take any cards). In case of a tie, neither player wins. (Theserules are simplified from real life. There is no “doubling down,” no “splitt ing,” etc.)The customer’s strategy of when to take another card is represented as a predicatefunction (the hexagonal-block kind that reports true or false). The function has two argu-ments: the customer’s hand so far, and t he dealer’s card that i s face up. The customer’shand i s represented as a list in which each element is a card in t he form of a word like 3Hfor the three of hearts, or QC for the queen of clubs. the dealer’s face-up card is a singleword (not a list). The strategy function should report a true or false output, which tellswhether or not the customer wants another card.We provide a library of functions to help in dealing with words (in t he Operationsmenu) and lists (in the Variables menu).Download http://byob.berkeley.edu/21.ypr which contains definitions for severalblocks needed in the project, most notably the Dealer sprite’s game block, which takes astrategy function as its argument. Cal ling game plays a game using the given strategy anda randomly shuffled deck, and reports 1, 0, or −1 according to whether the customer won,tied, o r lost.Don’t invent global variables. Each sprite has its own hand variable; make sure youare running each block in the appropriate sprite! Use block variables rather than globalsfor temporary storage.1. The program in the library is incomplete. It has a “stub” procedure best-total thatalways reports 0. It is supposed to take a hand (a list of card words) as argument, andreport t he total number of points in the hand. It’s called best-total because if a handcontains aces, it may have several different totals. The procedure should report the largest1possible total that’s less than or equal to 21, if possible. For example:Finish writing best-total.2. Define a strat egy procedure stop-at-17 that’s ident ical to the dealer’s, i.e., takes acard i f and only if the total so far is less than 17. (Not e: It’s okay to use best-total tocalculate the user’s point count, even though a serious blackjack player might use a morecomplicated rule to decide on the “best” value for a hand.)3. Write a procedure play-n such thatplays n games with a given strategy and reports the number of games that the customerwon minus the number that s/he lost. Use this to exercise your strat egy from problem 2,as well as strategies from the problems below.Don’t forget: a “strategy” is a procedure! We’re asking you to write a procedure thattakes another procedure as an argument. This comment is also relevant to parts 6 and 7below.24. Define a strategy named dealer-sensitive that “hits” (takes a card) if (and only i f)the dealer has an a ce, 7, 8, 9, 10, or picture card showing, and the customer has less t han17, or the dealer has a 2, 3, 4, 5, or 6 showing, and the customer has less than 12. (Theidea is that in the second case, the dealer is much more li kely t o “bust” (go over 21), sincethere are more 10-pointers than anything else.)5. Generalize part 2 above by defining a function stop-at.shouldreport a strategy that keeps hitting until a hand’s tota l is n or more. For example,is equivalent to the strategy in part 2.6. On Valentine’s Day, your local casino has a special deal: If you win a round of 21 witha heart in your hand, they pay double. You decide that if you have a heart in your hand,you should play more aggressivel y than usual. Write a valentine strategy that stops at17 unless you have a heart in your hand, in which case it stops at 19 .7. Generalize part 6 above by defining a function suit-strategy that takes three argu-ments: a suit (h, s, d, or c), a strategy to be used if your hand doesn’t include that suit,and a strategy to be used if your hand does include that suit. It should report a strategythat behaves accordingly. Show how you could use this function and the stop-at functionfrom part 5 to redefine the valentine strategy of part 6 .8. Define a function majority that takes three strategies as arguments and produces astrategy as a result, such that the result strategy always decides whether or not to “hit” byconsulting the three argument strategies, and going with the majority. That is, the resultstrategy should report true if and only if at least two of the three argument strategies do.Using the three strategies from parts 2, 4, a nd 6 as argument strategies, play a few gamesusing the “majority strategy” formed from these three.9. Some people just can’t resist taking one more card. Write a procedure reckless thattakes a strategy as its argument and reports another strategy. This new strategy shouldtake one more card than the original would. (In other words, the new strategy shouldstand if the old strategy would stand on the butlast of the customer’s hand.)10. That guy at the other end of the table isn’t doing anything. Write a strategy forhim that asks the user whether to hit or stand, after displaying the player’s hand andthe dealer’s up card. Modify the game procedure so that both players play, one with aprogrammed strategy and the other wi th this interactive strategy. Keep scores for


View Full Document

Berkeley COMPSCI 61A - Programming Project 1 - Twenty-One

Documents in this Course
Lecture 1

Lecture 1

68 pages

Midterm

Midterm

5 pages

Midterm

Midterm

6 pages

Lecture 35

Lecture 35

250 pages

Lecture 14

Lecture 14

125 pages

Lecture 2

Lecture 2

159 pages

Lecture 6

Lecture 6

113 pages

Lecture 3

Lecture 3

162 pages

Homework

Homework

25 pages

Lecture 13

Lecture 13

117 pages

Lecture 29

Lecture 29

104 pages

Lecture 11

Lecture 11

173 pages

Lecture 7

Lecture 7

104 pages

Midterm

Midterm

6 pages

Midterm

Midterm

6 pages

Lecture 8

Lecture 8

108 pages

Lab 4

Lab 4

4 pages

Lecture 7

Lecture 7

52 pages

Lecture 20

Lecture 20

129 pages

Lecture 15

Lecture 15

132 pages

Lecture 9

Lecture 9

95 pages

Lecture 30

Lecture 30

108 pages

Lecture 17

Lecture 17

106 pages

Load more
Download Programming Project 1 - Twenty-One
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Programming Project 1 - Twenty-One and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Programming Project 1 - Twenty-One 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?