Question: Programming Assignment #4 Interfaces and Polymorphism I. The Ancient and Honorable Game of Nim Nim is an ancient game with several variations. Heres one: Two
Programming Assignment #4
Interfaces and Polymorphism
I. The Ancient and Honorable Game of Nim
Nim is an ancient game with several variations. Heres one: Two players takes turns removing marbles from a pile. On each turn, the player must remove at least one but no more than half of the remaining marbles. The player who is forced to remove the last marble loses.
II. The Assignment
Write a Java program in which the computer plays against a human opponent.
Begin each game by asking the human whether she wants to go first or second, and whether the computer should play in smart or stupid mode.
In stupid mode, the computer removes a random number of marbles between 1 and n/2 each turn.
In smart mode, the computer removes exactly enough marbles to make the remaining pile a power of two minus one (1, 3, 7, 15, 31, or 63).
The computer cannot be beaten in smart mode if it has the first move, unless the initial pile size happens to be 15, 31, or 63. Naturally, a human who plays first and knows the winning strategy is also invincible.
Begin each game with a pile of between 10 and 100 marbles, inclusive, where the size is set randomly.
After each turn, print out the player name (humans actual name or computer), how many marbles the player took, and the number of marbles remaining.
When each game ends the winner should be announced and the human asked whether she wants to play again.
III. Specifications
1. To receive credit for this assignment, you must create a Java interface called Player, and 3 classes that implement it one class for each of the 3 types of players. The interface will declare a move() method and any others you may deem necessary.
2. Implement another class called Nim with a play() method that conducts the game (as shown in class). The play() method must call the move() method polymorphically.
3. Design the move() method of the HumanPlayer class so that the human is not be allowed to cheat by removing an illegal number of marbles. (You know how those humans are)
4. Implement another class called Pile to create and maintain a pile of marbles.
5. Your test class will create the Player objects, the Nim object, and call the play() method. After each game, ask whether the user wants to play again. The Nim class must depend only on the Player interface and Pile class, and not on any of the classes that implement the Player interface
Hint: Create the Player objects in your test class (or in a separate PlayerGenerator class) and pass them to the Nim constructor, or to the play() method The individual player classes and the Pile class must not depend on one another or on the Nim class
(Recall that class A depends on class B if A creates objects or declares object variables of B (including method parameters), calls static methods of B, or accesses static constants of B)
IV. Due Date: Thursday, March 2nd
Code must be throughly internally commented for full credit
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
