Homework Assignment 8

Assigned: Wednesday,March 27.
Due: Wednesday, April 10.

Read: Read 5.4 (about for loops), Appendix A and Appendix D.

Program: Write a program which plays the game of Marienbad with the user.

Rules of the game

  1. Setup: 16 sticks are arranged in 4 rows. 1 stick in the first row, 3 sticks in the second row, 5 sticks in the third row, 7 sticks in the fourth row.
  2. Play: There are two players, which alternate turns. In each turn, the player selects a row and takes from that row any number of sticks from 1 to the entire number of sticks in the row.
  3. Goal: The player taking the last stick loses.

Your program should display the sticks at the start of each player's turn. The user goes first, and the computer responds with an automatically generated response. The computer understands when the game has been won or lost, and responds appropriately. The computer also understands illegal moves, and responds appropriately. Bad input should not crash the program.

For extra credit: The computer, since it plays second, has a winning strategy - it can always win. Find and implement this strategy.

Download an SGI compatible binary for an example solution. (You will have to do a SAVE LINK as filename Marienbad and then chmod u+x Marienbad to set run-ability on the saved file.)


Here is a Java Applet with the same code as the binary. Click on a stick to select all sticks to the right. Click Take to take those sticks. The computer responds with it's selection of sticks. The player taking the last stick loses.
Your browser doesn't run Java Applets. On the Indy's, try Find->netscape2.0.