CS139 Algorithm Development

Software Requirements Specification

Programming Assignment 4

The Game of Nim

Due Friday Nov 6, by 5:00pm


Introduction

Purpose: From the Horstmann textbook.  "You will write a program in which the computer plays against a human opponent.
Generate a random integer between 10 and 100 to denote the initial size of the pile.
Generate a random integer between 0 and 1 to decide whether the computer or the human takes the first turn. Generate a random integer between 0 and 1 to decide
whether the computer plays smart or stupid. In stupid mode the computer simply takes a random legal value (between 1 and n/2) from the pile whenever it has a turn.
In smart mode the computer takes off enough marbles to make the size of the pile a power of two minus 1—that is, 3, 7, 15, 31, or 63. That is always a legal move,
except when the size of the pile is currently one less than a power of two. In that case, the computer makes a random legal move. "

Objectives - At the conclusion of this exercise the student will demonstrate that they can:

Deadlines

Prerequisites

You have covered the material in Chapter 4 & 5 of Gaddis.  

Program Behavior

The driver program, NimDriver, contains a main method that will create a new Nim object.  Nim contains the methods necessary to play the game.  Your program should do the following, IN THIS ORDER. NOTE: You must use your Random object for all random numbers used.

NOTE: In all cases, after reading in a value from the keyboard, output a new line character.

  1. Introduce the game to the user.  
  2. Create a Scanner to read from the keyboard.
  3. Ask the user for their name.
  4. Ask the user for a large integer to use to "start" the Random number generator.
    1. Do appropriate error checking as described in the error section.
  5. Setup the game.
  6. Start the game.
    1. Generate a random number between 10 and 100 to represent the number of marbles that are in the "pot".  
    2. Generate a random number of either 0 or 1 to determine whether the computer (0) or human (1) goes first.
    3. Announce the size of the marble pot and the decision about who goes first.
    4. Generate a random number of either 0 or 1 to determine if the computer plays smart or random.  (See Computer Player below)
    5. The first player "draws" marbles from the pile.  The number drawn must be between 1 and 1/2 the number of marbles in the pot.
      1. For the human player, prompt them to enter the marbles as shown in the Output section below.
      2. For the computer player, calculate the number of marbles drawn. (See Computer Player below)
    6. The next player "draws" marbles from the pile.  The number drawn must be between 1 and 1/2 the number of marbles in the pot.
      1. For the human player, prompt them to enter the marbles as shown in the Output section below.
      2. For the computer player, calculate the number of marbles drawn. (See Computer Player below)
    7. At the end of each "round" announce how many marbles each player took and the total number remaining.
    8. Repeat steps 5 and 6 until only one marble remains.  The player that must draw that final marble is announced as the loser and the other player the winner.
    9. Ask the user if they want to play another game.
      1. If the player enters 'Y', 'y' or any case variant of "yes", repeat each step in Item 6.
      2. If not, announce the end of the game.

Computer Player

Random play - If the computer player is playing in random mode, you should generate a Random number in the range of 1 - 1/2 the current size of the pile of marbles.  
Smart play - In smart mode the computer takes off enough marbles to make the size of the pile a power of two minus 1—that is, 3, 7, 15, 31, or 63. That is always a legal move, except when the size of the pile is currently one less than a power of two. In that case, the computer makes a random legal move.

Output Statements

Program Behavior
Reference
Output printf statement parameter 1 parameter 2 parameter 3
1 "Welcome to the game of Nim\n\n"
3 "What is your name: " 
3, 4 "\n" - After reading in the value.
4 "Enter a large integer to 'start' the process: "
4 - Error "This is not an integer.  Try again.\n"
Toolkit - change
All error messages should echo
the bad value as this
"Bad value %s\n" The bad value read from the keyboard.  This would be directly followed by the error message.
6c "The marbles in the pile are %d.\n %s starts.\n" The number of marbles generated Either the name of the human player or "Computer"
6ei "%s, how many marbles do you want? (1-%d) " The name of the human player The maximum number of marbles the person can choose
6ei - error - non integer "This is not an integer. Try again.\n"
6ei - Error - not in range "%d is not in the range of 1 - %d. Try again.\n" The number the human entered The maximum number they can enter.
6f "%s: took %d\t%s: took %d\t%d remain\n" The name of the first player, number of marbles The name of the second player, number of marbles The number of marbles remaining
6h "1 marble left. %s loses.\n" The name of the player that will have to take the last marble
6i "That was fun.  Play again? "
6i ii "Bye\n"
After all input "\n"


Toolkit changes

You will need to make changes to your getInteger and getDouble methods.  Specifically they must:
  1. Instead of using a default value, you will loop until the user provides a legal integer.  You should remove the default parameter and related code.
  2. Before you print the error message print the literal "Bad value: " followed by the bad value read in followed by the newline.  See the Toolkit line in the output section above.

Additional Program Requirements

  1. Your program must conform to standard Java programming standards and the additional standards for this class. See the Style Guide for your class.
  2. There is no test required to be submitted for this program, but you should test.  

Honor Code

This work must conform to the JMU Honor Code and the specific requirements of this class. NO help may be provided by any student to another student. Authorized help is limited to your textbook, the TAs for any CS139 or CS239 section, and the professor for your section. See collaboration policy.

Grading

        For submissions after:

HINTS

  1. You may want to divide this problem into some subproblems and use methods to take care of some of the code. For example, the "smart" computer move may be best done in its own method.  
  2. You must use a single Random number generator for this problem.  Using multiple generators (calling the constructor multiple times) will result in a different pattern of values and will cause submit to fail.
  3. You must use a single Scanner object for this problem.  Using multiple Scanner objects (calling the Scanner constructor with System.in multiple times) will result in a failure to read all of the input in submit.
  4. Stub out your code and build each method individually.
  5. Start with the provided code.  We have built the shell of the Nim game and the NimDriver that you will use.  You will submit only Nim and your Toolkit to the submit system.
  6. To control the tests, build a small program that will generate the random numbers needed in this program.  Use different seeds to generate different patterns that will allow you to test the computer going first, the human going first and the computer playing in each of smart and random modes.
  7. Begin early.  Students run into trouble by waiting too long to start the program. 
  8. Understand the problem at hand.  Make sure that you follow the requirements precisely.  Don't add additional "flourishes".  You will be downgraded.
  9. Play the game with a friend and simulate the computer moves to understand the smart move.
  10. In the body of your main method, outline your steps with comment lines for each part of the project.
  11. If you wish to use additional methods to break up your code, feel free to do so.