Professor Simonson

How Computers Work

CSC 195

Assignment 5 - 20 points

Due: Midnight, Thursday, February 29

Write definitions for all the procedures that are in bold
and use the skeleton (or outline) below to make the program NIM
work. The computer should play perfectly, and win whenever
possible. The game starts with some number of sticks between
1 and 20, and the player and computer alternate turns taking 1, 2
or 3 sticks until all the sticks are gone. The one to take
the last stick wins. The details of the game strategy will
be discovered in class. Your program need not be very fancy
graphically, but it should do all input/output on the Graphics
window rather than on the "listener" screen.

The outline is carefully designed:
a. Every method exits back to the method
that called it, (there is no "winding up" of procedure
calls) and
b. Each loop is clear and simple tail
recursion.
Stray from the outline at your own risk. The main theme here
is appreciating the notion of top-down design, understanding the
program outline, and in the future, using this outline to learn
how to make the outline for your project.

To NIM
make "player_win
0 ; variable to hold
number of times user wins
make "comp_win
0 ; variable counting
number of times computer wins Explain_Rules
;
explains
rules
until
user
clicks
the
mouse or types ok.
Play_the_Game Bye_Bye
;
prints
out
summary
of
wins
and
losses
End

To Play_the_Game
make "sticks random 20 ; Game can
have up to 20 sticks
single_game
if not playagain? then
stop ; this prints a summary of wins and losses so far and
asks the player if
Play_the_Game
; he/she wants to play again
; and outputs “true or “false
End

To single_game printsticks
;
This prints a picture of the sticks and the number of sticks
if not gofirst? then
comp_move ;
gofirst? should ask the player if
;
he/she
wants
to
go
first,
and
output
; “true or “false appropriately
game_loop
End

To game_loop
if win? then printwinc
make "comp_win (+ :comp_win 1) stop

;win? checks if the number of
sticks is 0 and outputs "true or "false
; printwinc prints a congratulatory message to
the computer

player_move ;
This uses a GUI to let the player choose 1, 2, or 3 sticks
; modifies “sticks, and
;
prints
out
a
new
picture.

if win? then printwinp
make "player_win (+ :player_win 1) stop

; printwinp prints a
congratulatory message to the player

comp_move
; The computer makes its move, prints the number of sticks it
chose
;
modifies
"sticks,
and
prints
out
a
new
picture.
; Before the computer moves, it is nice to ask the user to
hit
; any key when he/she is
ready for the computer to move
; This lets the
user see the sticks after his/her move before the computer changes
it.