MONT 112G, Fall 2011

    Home | | Course Schedule | | Assignments | | Lecture Notes

    MONT 112G Homework 5

    The Game of Nim

    Due Wednesday, October 19, at the beginning of class.

    Introduction
    The game of Nim is a simple board game played between two players. The game board consists of some number of squares in a line. Each player is allowed to place 1, 2, or 3 markers on the board in turn. Player 1 plays with X's and player 2 plays with O's. The winner is the player who places their marker in the last square. The following figure shows a decision tree for this game when the board has 7 squares.

    In this project, you will create and implement a decision tree for a 6 square game of Nim. The rules are exactly the same as for the 7 square version, except there are only 6 squares on the board. The winner is the player who places a piece on the 6th square.

    Decision Tree
    First you will create a decision tree for the 6-square game of Nim. The user makes the first move, placing 1, 2 or 3 X's on the board. If the user plays 1 or 2 X's, the computer should respond with a single O. If the user plays 3 X's, the computer should play three O's (to win and end the game). In the first 2 cases, the user makes a second move of 1, 2, or 3 X's. The computer should then respond with an appropriate move to win the game or report that the user has won. Note that there should be only one condition under which the user wins. Draw the decision tree for this 6-square game of Nim.

    Program Description
    The program will implement the decision tree you develop above. The user will be prompted for their move, and they should enter 1, 2 or 3 X's. Depending on the user's entry, the program will make a move of 1,2 or 3 O's as shown in your decision tree. When the last (6th) box is filled, the program reports the winner.

    Project Specifications
    Place all your code in a file named <username>_nim.py, where <username> refers to your own username. For example, Professor Royden's file would be named croyden_nim.py.

    1. Prompt the user to enter his/her first move, and read in the response.
    2. Based on the response, the computer should report the current board status.
    3. The computer should then tell its move, followed by a report of the new board status.
    4. If the game is not yet over, the computer should then prompt for the user's second move, and repeat steps 2, 3 and 4.
    5. The computer should report the winner.
    6. Write your code to assure that the text that appears on the monitor looks like that displayed in the section below called Specifications for Output.
    7. You need not worry about the case where the user enters an illegal move. Assume the user will enter one of the three appropriate moves.
    8. Important: Do not use a loop to implement this program. You should program it using conditional statements.

    Specifications for Documentation and Format.
    Fully correct documentation for this homework assignment must include:

    • A file prologue at the top of your program file. The prologue must contain the program title, your name, class name, the date and the assignment number. The prologue must also contain a short description of the program.

    Specifications for Output.
    Directly below, you can see an example of the display that will appear on the monitor screen during the prompting for user input and the computer responses for four sample games. Note that after the user's second move the computer reports the final board status before announcing the winner. Underlined characters indicate user input.

      Sample Game 1:
      What is your move? X
      The board is X.
      I play O.
      The board is XO.
      What is your next move? X
      The board is XOX.
      I play OOO.
      The board is XOXOOO.
      I win!

      Sample Game 2:
      What is your move? XX
      The board is XX.
      I play O.
      The board is XXO.
      What is your next move? XXX
      The board is XXOXXX.
      You win!

      Sample Game 3:
      What is your move? XX
      The board is XX.
      I play O.
      The board is XXO.
      What is your next move? XX
      The board is XXOXX.
      I play O.
      The board is XXOXXO.
      I win!

      Sample Game 4:
      What is your move? XXX
      The board is XXX.
      I play OOO.
      The board is XXXOOO.
      I win!

    To Submit Your Finished Project:
    1. Hand in your decision tree in class on the project's due date.

    2. Hand in a hard copy of the file <username>_nim.py in class on the project's due date.

    3. Hand in a printout of 4 sample games like those shown above in class on the project's due date.

    4. Print your name at the top of the cover page and staple it to the top of your hard copy.

    5. In addition to the hard copy listed above, email your <username>_nim.py file to me at croyden@mathcs.holycross.edu. The subject line should read: MONT 112G hw5 <your_username>.

    Get started early and have fun!