Professor Simonson

How Computers Work

CS 195

Assignment 5 -  30 points  

Due:  midnight, February 28, Thursday


Write definitions for all the procedures that are in bold and use the skeleton 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.  The main theme here is appreciating the notion of top-down design, and trying to figure out how the program outline works and how to make your own outlines.
 

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 asks the player if
     Play_the_Game                   ; he/she wants to play again
                                              ; and outputs “TRUE or “FALSE
End

To single_game
     printsticks
     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
                                                                                                   ; printwinc prints a congratuatory message to the computer
     player_move   ;  This asks the player how many
                          ;   sticks he/she wants to remove,
                          ;   checks that a legal answer is
                          ;   provided, 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 what it did
                              ;  modifies "sticks, and prints out a new picture.
     game_loop
End
 
 

back
 


shai@stonehill.edu

http://www.stonehill.edu/compsci/shai.htm