\documentclass[]{article}

%\usepackage{graphicx}
%\usepackage{rotating}
% \usepackage{ifthen}
%%%%%%
%%%%%%
%%%%%% Modified by A. Coates
%%%%%%
%%%%%% 15 June 01
%%%%%%
%%%%%%



%\setlength{\textwidth}{9.0in}
%\setlength{\textheight}{6.50in}
%\setlength{\oddsidemargin}{0in}
%\setlength{\evensidemargin}{0in}

\setlength{\textwidth}{6.5in}
\setlength{\textheight}{9.00in}
\setlength{\oddsidemargin}{0in}
\setlength{\topmargin}{-.5in}
\setlength{\evensidemargin}{0.0in}

\setcounter{page}{58}


\def\prgex{\smallskip\aftergroup\smallskip
\vskip 1pt
\tt\obeyspaces %
\def\par{\leavevmode\endgraf} %
\obeylines\parindent=0pt\parskip=0pt}
{\obeyspaces\global\let =\ }


\begin{document}
\begin{center}
 UNIVERSITY of CALIFORNIA at Berkeley\\
Department of Electrical Engineering and Computer Sciences\\
Computer Sciences Division\\
\vspace{2mm}
 \framebox{
      \vbox{\vspace{2mm}
    \hbox to 6in { {\bf CS61A \hfill Kurt Meinz}}
       \hbox to 6in { Structure and Interpretation of Computer Programs \hfill Summer 2002}
       \vspace{1mm}
   }
}
   \end{center}
\centerline{\bf Programming Project 1: Twenty-One} \vskip 12pt
\medskip
This project must be done INDIVIDUALLY and is due Monday 7/8 at
3:00am. Please check the course website for updates and errata.
\medskip

For   our   purposes,   the   rules    of    twenty-one
(``blackjack'')  are  as follows.  There are two players:\  the
``customer'' and the ``dealer''.  The object of the game  is
to be dealt a set of cards that comes as close to 21 as
possible without  going  over  21  (``busting'').  A card is
represented as a word, such as {\tt 10s} for the ten of spades.
(Ace, jack, queen, and king are {\tt a}, {\tt j}, {\tt q}, and
{\tt k}.)  Picture cards are worth 10 points; an ace is worth
either 1 or 11 at the player's option.  We reshuffle the deck
after each round, so strategies based on remembering which
cards were dealt earlier are not possible.
Each player is dealt two cards, with one of the  dealer's  cards
face up.  The dealer always takes another card (``hits'') if
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  the  customer  exceeds  21,  s/he
immediately loses (and the dealer doesn't bother to take any
cards).  In case of a  tie,  neither  player  wins.   (These
rules are simplified from real life.  There is no ``doubling
down,'' no ``splitting,'' etc.)

The customer's {\it 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 is face up.  The customer's hand is
represented as a sentence in which each word is a card;
the dealer's face-up card is a single word (not a sentence).
The strategy function should return 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 {\tt \#t},
while false is represented as {\tt \#f}.)

The   file {\tt \~{}cs61a/lib/twenty-one.scm} contains a definition
of function {\tt twenty-one}.  Invoking $${\tt (twenty-one~strategy)}$$ plays  a
game  using the given strategy and a randomly shuffled deck,
and returns 1, 0, or $-1$ according to  whether  the  customer
won,  tied,  or lost.

{\bf For each of the steps below, you must provide a transcript indicating
enough testing of your procedure to convince the readers that you are
really sure your procedure works.  These transcripts should include
trace output where appropriate.}
\smallskip
\begin{enumerate}
\item  The program in the library is incomplete.  It lacks
a procedure {\tt best-total} that takes a hand (a sentence of card
words) as argument, and returns the total number of points in
the hand.  It's called {\it best\/}-total because if a hand
contains aces, it may have several different totals.  The
procedure should return the largest possible total that's
less than or equal to 21, if possible.  For example:

{\prgex
\hfuzz=1.3in%
> (best-total '(ad 8s))    ; in this hand the ace counts as 11
19
> (best-total '(ad 8s 5h)) ; here it must count as 1 to avoid busting
14
> (best-total '(ad as 9h)) ; here one counts as 11 and the other as 1
21
\prgex}

\noindent Write {\tt best-total}.
\smallskip
\item  Define a strategy procedure
{\tt stop-at-17} that's  identical  to  the  dealer's,
i.e., takes  a  card if and only if the total so far is
less than 17.
\smallskip
\item  Write a procedure {\tt play-n} such that

{\prgex%
(play-n strategy n)
}

\noindent plays {\tt n} games with a given strategy and
returns the number of games that the customer won 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
you think they do, {\tt trace} them  when  possible.

Don't forget: a ``strategy'' is a procedure!  We're asking
you to write a procedure that takes another procedure as
an argument.  This comment is also relevant to parts 6
and 7 below.
\smallskip
\item   Define a strategy that ``hits'' (takes a card) if  (and
only  if)  the  dealer has an ace, 7, 8, 9, 10, or picture card showing,
and the customer has less than 17, or the dealer has  a
2,  3,  4,  5,  or 6 showing, and the customer has less
than 12.  (The idea is that in  the  second  case,  the
dealer  is  much  more likely to ``bust'' (go over 21),
since there are more 10-pointers than anything else.)
\smallskip
\item   Define the best strategy that you can think  of.   (No,
we won't grade you on how mathematically optimal it is.
Just try to have fun.)
\smallskip
\item   Generalize part 2 above by defining a function {\tt stop-at}.
{\tt (stop-at~n)} should return a strategy that keeps hitting
until a hand's  total  is  {\tt n}  or  more.   For  example,
{\tt (stop-at~17)} is equivalent to the strategy in part 2.
\smallskip
\item    Define a function {\tt majority} that takes
three  strategies  as
arguments  and  produces a  strategy as a result, such
that the result strategy always decides whether or  not
to ``hit'' by consulting the three argument strategies, and
going with the majority.  That is, the result  strategy
should  return {\tt \#t} if and only if at least two of the three
argument strategies do.  Using the  three  strategies  from
parts  2,  4,  and 5 as argument strategies, play a few
games using the ``majority strategy'' formed from these
three.

% \noindent 8.  The procedure {\tt play-dealer} in the library file
% is inefficient because it computes the point counts of each hand
% more than once on each invocation.  Use {\tt let} to rewrite it
% avoiding the extra computations.
\smallskip
\item   Some people just can't resist taking one more
card.  Write a procedure {\tt reckless} that takes a strategy as
its argument and returns another strategy.  This new strategy should
take one more card than the original would.  (In other words, the
new strategy should stand if the old strategy would stand on the
{\tt butlast} of the customer's hand.)
\smallskip
%\item We would now like to add two additional pieces to this
%project: the first is the notion of betting, and the second is the
%notion of ``blackjack''.
%\smallskip
%9a. Write a new projecture  {\tt bet-on-21} which takes two
%argument: one strategy and one number $\geq 1$ (the amount of
%money being bet) and returns the amount of money won. (Lost money
%is represented by a negative number.)
%
%9b. Now we add the notion of ``blackjack''.
%A ``Blackjack `` is a 2 card hand, where one card is an Ace,
%and the other card is worth 10 points.
%If you are dealt a Blackjack (notice that you must be Dealt a Blackjack),
%gambling establishments will pay you 3:2: meaning, that you win 3 if you bet 2.
%In other words, if you bet 1 and receive a Blackjack, you win 1.5.

%Write a predicate {\tt Blackjack?} that takes a hand as an argument
%and returns 1 is the player's hand is a Blackjack and 0 if not.
%Using the above predicate

%Modify {\tt twenty-one  } so that on a Blackjack, the payout is 1.5.
%\smallskip
\item   {\bf Print out a complete listing of your procedures
before you begin this problem.}  We are going to change the rules
by adding two jokers to the deck.  A joker can be worth any number
of points from 1 to 11.  Modify whatever has to be modified to make
this work.  (The main point of this exercise is precisely for you to
figure out which procedures must be modified.)  The reason you
printed everything first is that otherwise the readers would have
trouble grading the earlier steps, since you might mess up earlier
work in attempting this one.

\end{enumerate}

\bigskip
{\bf Submission instructions:} In a directory named 'proj1',
create files called 'twenty-one.scm' (which should contain your
solutions for questions 1 through 8) and 'twenty-one-joker.scm'
(which should contain your solutions for questions 1 through 9).
Please make sure that each file is a fully playable copy of the
project (not just a listing of the procedures that you changed).
In addition, you must create a file called 'testing.txt' which
contains an explanation of your testing and your transcripts --
please surround your transcripts with real English words so that
the readers can figure out what you are trying to do. Make sure
that every file has your name and login on it and type 'submit
proj1' at the shell prompt from within this directory. Also,
please submit paper copies of both these files to the homework
box.

By the way, the online submissions system is built so that you can
submit as many times as you like with no penalty. Beware, however,
that only your last submission will be graded, so don't do
anything silly like re-submitting after the deadline.

\end{document}
