MCS-177 Project: Nim with Strategies

Due: April 25, 2001


How do you modify the program for playing Nim so that the computer can play using different strategies? In this project, you should work through the exercises in section 6.5 (except for 6.20 and 6.21). Before coming to lab, be sure that you have read and understood sections 6.1-6.3 and the beginning of section 6.5, if you haven't already.

Project Report

As usual, write up a lab report that explains the project to an audience generally knowledgeable about Scheme and such, but ignorant of specifically what you did. Answer all of the questions posed, but do not simply write up your lab as a sequence of exercises. Instead, try to convey the big picture of what you accomplished. Remember, there are some tips in the document entitled Suggestions for Clear Lab Reports in Computer Science Courses.

There are many possible themes you could select for this particular report. Choose one, and structure your report to reflect that theme.

Computer Exercises

  1. Using your favorite method, save the file ~mc27/labs/nim/nim.scm somewhere under your home directory, and then evaluate everything in it in DrScheme. (Note: The easiest way to save the file is to go to the link, and then do a Save As... under the File menu in Netscape.) This file contains the Nim program and the game-state implementation from the book, except that computer-move and human-move are modified as indicated in exercise 6.13(c).

  2. In order to make the Nim program work, you will need an implementation of the move-instruction ADT (exercise 6.13(b)). For your convenience when doing the implementation of the move-instruction ADT, we have included ``stubs'' for the three ADT operations near the bottom of the file; the constructor is called make-move-instruction and the two selectors are called num-coins and pile-number. You should write them correctly by removing the error expressions which they currently contain and replacing them with the actual procedure bodies.

    Keep in mind that make-move-instruction just constructs a move-instruction object, while next-game-state actually performs the move.

    Once you have implemented the move-instruction ADT, you can test your implementation in two ways. First, you could test the ADT in isolation with expressions such as:

      (num-coins (make-move-instruction 4 2))
    Second, you can test the ADT in the context of the whole application by playing Nim games with the computer by evaluating, for example, the expression:
      (play-with-turns (make-game-state 5 8) 'human)
    Both kinds of tests are critical in any large application, so be sure to do both kinds of tests throughout this lab.

    Play a couple of games so that you get a hang of how the interaction works. (You needn't include transcripts of your game playing in the lab writeup.)

  3. Do exercises 6.14-6.19 in the text. When working with the random procedure, keep in mind that each time you use it you may get a different result; if you want to use the same randomly chosen integer twice, you should hold onto it, for example by naming it with a let, as you won't be able to regenerate it.

    Again, as with the game-state ADT, be sure to test each strategy in isolation as well as in the context of the whole application. If you are unsure about how to test the strategies in isolation, ask!

  4. Maintaining a modular program is critical to the success of any large programming project. A good way to test to be sure that your program is truly modular is to take out one working module, and swap it for a different, but also working, module. We'll do this, treating the move-instruction ADT as a module:

  5. (Optional) You are encouraged to do exercises 6.20 and 6.21 if you have time, but they are not required.

  6. (Optional) One other optional activity is to error-proof your Nim program, so that if the user enters an invalid move, the program handles the error appropriately. Ideally, you should have the program detect the invalid move, and then ask the user to enter a valid move instead. How can this be done most easily?