CS 61AProgramming Project 1: Twenty-OneThis is a one-week project.For our purposes, the rules of twenty-one (“blackjack”) are as follows. There are twoplayers: the “customer” and the “dealer”. The object of the ga me is to be dealt a set ofcards that comes as close t o 21 as possible without going over 21 (“busting”). A card isrepresented as a word, such as 10s for the ten of spades. (Ace, jack, q ueen, and king are a,j, q, and k.) Picture cards are worth 10 points; an ace is wort h 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, wi th one of thedealer’s cards face up. The dealer always takes another card (“hits”) i f he has 16 or less,and always stops (“stands”) with 17 or more. The customer can play however s/he chooses,but must play before the dealer. If 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 function.The function has two arguments: the customer’s hand so far, and the dealer’s card that isface up. The customer’s hand i s represented as a sentence in which each word is a card;the dealer’s face-up card is a single wo rd (not a sentence). The strategy function shouldreturn a true or false output, which tells whether or not the customer wants another card.(The true value can be represented in a program as #t, while false is represented as #f.)The file ~cs61a/lib/twenty-one.scm contains a definition of function twenty-one.Invoking (twenty-one strategy) plays a game using the given strategy and a randomlyshuffled deck , and returns 1, 0, o r −1 according to whether the customer won, tied, or lost.For each of the steps below, you must provide a transcript indicat i ngenough testing of your procedure to convince the readers that you are re-ally sure your procedure works. These transcripts should include trace outputwhere appropriate .1. The program in the library is incomplete. It la cks a procedure best-total that takes ahand (a sentence of card words) as argument, and returns the total number of points in thehand. It’ s called best-total because if a hand contains aces, it may have several differenttotals. The procedure should return t he largest possible total that’s less than or equal to21, if possible. For example:29> (best-total ’(ad 8s)) ; in this hand the ace counts as 1119> (best-total ’(ad 8s 5h)) ; here it must count as 1 to avoid busting14> (best-total ’(ad as 9h)) ; here one counts as 11 and the other as 121Write best-total.2. Define a strategy procedure stop-at-17 that’s identical to the dealer’s, i.e., takes acard if and only if the total so far is less than 17.3. Write a procedure play-n such that(play-n strategy n)plays n games with a given strategy and returns the number of games that the customerwon minus the number that s/he lost. Use this to exercise your strategy from problem 2,as well as strategies from the problems below. To make sure your strategies do what youthink they do, trace them when possible.Don’t forget: a “strategy” is a procedure! We’re aski ng you to write a procedure thattakes another procedure as an argument. This comment is also relevant to parts 6 and 7below.4. Define a strategy named dealer-sensitive t hat “hits” (takes a card) if (a nd only if)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, o r 6 showing, and t he customer has less than 12. (Theidea is that in the second case, the dealer is much more likely to “bust” (go over 21), sincethere are more 10-pointers than anything else.)5. Generalize part 2 above by defining a function stop-at. (stop-at n) should return astrategy that keeps hitting until a hand’s total is n or more. For example, (stop-at 17)is equivalent to the strategy in part 2.6. On Valentine’s Day, your local casino has a special deal: If you wi n 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 a ggressively 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 ta kes 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 return 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 .308. 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 t o “hit”by consulting t he three argument strategies, and going with the majority. That is, theresult strategy should return #t i f and only if at l east two o f the three arg ument strategiesdo. Using the three strategies from parts 2, 4 , and 6 as argument strategies, play a fewgames using 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 returns another strategy. This new strategy shouldtake one mo re 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. Copy your Scheme file to a new fi le, named joker.scm, before you beginthis problem. We are going to change the rules by adding two jokers to the deck. A jokercan be worth any number of points from 1 to 11. Modify whatever has to be modified tomake this work. (The main point of this exercise is precisely for you to figure out whichprocedures must be modified.) You will submit both this new file and the original, non-joker version for grading. You don’t have t o worry about making strategies optimal; justbe sure nothing blows up and the hands are totalled
View Full Document