Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use Java please... The Game of Nim In this part of the assignment, we will continue developing a Java Object-Oriented program for the Game of

Use Java please...

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

The Game of Nim In this part of the assignment, we will continue developing a Java Object-Oriented program for the Game of Nim using the initial development from Assignment 1. We have already seen a simple, text-version of this game at the very first session. Following is a description of the game: There is a pile of some marbles and two players. The number of marbles in the pile can be between 10 and 100. The two players alternatively take marbles from one pile. In each move, a player must take at least one and at most half of the marbles. Then, the other player takes a turn. The player who takes the last marble loses the game. We consider two modes in this game. In the first mode, the computer plays against a human player. The computer can be Novice (Stupid), and in its turn just takes a random number of marbles, or it can be Smart, and in its turn uses a smart strategy to take the best number of marbles trying to win the game at the end. Note that in any case, computer will follow the game rules. In the second mode two human players can play against each other. Using the above description, we extracted some of the classes required for this game, and for each class, found the list of its properties and methods' signatures/headers. For each property of a class, its data type and possible values have been also specified. In this phase you must implement the body of the methods. To be in a common ground, the classes and their instance variables and methods will be shown here. Note that at this level, we have one class for Player. In the next assignment we will extend this class to have both human and computer players. Also, we have one class, Pile, for the pile of marbles. The third class, Game, is the class we create to run the game. Your job is that in each class, using the game description and comments provided, complete the implementation after each comment by one or more lines of code. Also, for each method write its standard .Java Dcoumentation, same as those that have been provided as the guides. public class Pile { private int initial NumberOfMarbles; private int currentNumberOfMarbles; // Initial number of marbles in a round // Current number of marbles in a round * @param numberOfMarbles Number of initial marbles public Pile(int numberOfMarbles) { // Write the code for the constructor. /** * @return The number of initial marbles public int getInitialNumberOfMarbles() { // Write the code for the method. public void setInitialNumberOfMarbles (int initialNumberOfMarbles) { // Write the code for the method. public int getCurrentNumberOfMarbles() { // Write the code for the method. private void setCurrentNumberOfMarbles (int numberOfMarbles) { // Write the code for the method. /* This method should first check the parameter, number. If it is valid, updates the number of current marbles, and otherwise returns false. public boolean takeMarbles (int number) { // Write the code for the method. /* This method will return a String to show the current pile like the one you can see on the sample run of the program. */ public String showPile() { // Write the code for the method. // This method will override the toString method. @Override public String toString() { // Write the code for the method. public abstract class Player { protected String name; private int numberOfPlays; private int score; // Name of the player // Number of plays the player has played so far // Current score of the player public Player() {} * @param name Name of the player * public Player(String name) { // Write the code for the method. /** * @return Name of the player public String getName() { // Write the code for the method. public void setName(String name) { // Write the code for the method. public int getNumberOfPlays() { // Write the code for the method. public int getScore() { // Write the code for the method. public void setNumberOfPlays (int number) { // Write the code for the method. public void setScore(int number) { // Write the code for the method. /* At this level, ignore the body of the following method. We the reason we need this method later on and how to develop it. public abstract int play(Pile pile); // This method will override the toString method. @Override public String toString() { // Write the code for the method. import java.util.Scanner; public class Game { * @param pl First player * @param p2 Second Player * @param pile Pile of marbles * @param turn Player who should play first public static void runOneGame (Player P1, Player p2, Pile pile, Player turn) { // Write a while loop header for a complete game between players p1 and p2 // Show the result of the game * @param pl First player * @param p2 Second Player * @param pile Pile of marbles * @param turn Player who should play first public static String gameStatus(Player P1, Player p2, Pile pile, Player turn) { // Show the status of all the games between players pl and p2, similar // to one you see on the sample run of the game. public static void main(String[] args) { // Print out a welcome message // Create two players // Create an object for reading inputs // Create the outer do while loop for the game // Show a prompt and ask user to select the mode of the game // Human-Human or Computer-Human // Write a selection statement for setting up the players based on the user inputs // Create the inner do loop for required setup tasks and then calling // runOneGame method. Setup tasks are creating a pile of marbles, // randomly select the player who should play first, and print out // the game status. // At the end of inner loop, ask the user to play again or not. // After exiting the inner loop, ask the user for a new play or exit the game (program) Java file names: One java file for each class To get a better idea, you can see a sample running of the game I showed you in class on the next two pages. Welcome to the game of Nim! H) Human-Human C) Computer-Human Name of the player: Alice This is a game between Novice Computer and Alice. Initial number of piles: 77. Current number of piles: 77. It is Novice Computer's turn to play. Novice Computer takes 27 marbles. There are 50 marbles left. Alice, how many marbles do you take? (min=1, max=25) 25 There are 25 marbles left. Novice Computer takes 4 marbles. There are 21 marbles left. Alice, how many marbles do you take? (min-1, max=10)8 There are 13 marbles left. Novice Computer takes 1 marbles. There are 12 marbles left. Alice, how many marbles do you take? (min=1, max=6)5 There are 7 marbles left. Novice Computer takes 3 marbles. There are 4 marbles left. Alice, how many marbles do you take? (min=1, max=2)1 There are 3 marbles left. Novice Computer takes 1 marbles. There are 2 marbles left. Alice, how many marbles do you take? (min=1, max=1)1 There are 1 marbles left. *** Alice is the winner! *** Player Name: Novice Computer, Number of plays: 1, Score: 0 Player Name: Alice, Number of Plays: 1, Score: 1 play again Quit This is a game between Novice Computer and Alice. Initial number of piles: 93. Current number of piles: 93. It is Novice Computer's turn to play. Novice Computer takes 45 marbles. There are 48 marbles left. Alice, how many marbles do you take? (min=1, max=24)24 There are 24 marbles left. Novice Computer takes 10 marbles. There are 14 marbles left. Alice, how many marbles do you take? (min=1, max=7)6 There are 8 marbles left. Novice Computer takes 1 marbles. There are 7 marbles left. Alice, how many marbles do you take? (min=1, max=3)3 There are 4 marbles left. Novice Computer takes 1 marbles. There are 3 marbles left. Alice, how many marbles do you take? (min=1, max=1) 1 There are 2 marbles left. Novice Computer takes 1 marbles. There are 1 marbles left. *** Novice Computer is the winner! *** Player Name: Novice Computer, Number of Plays: 2, Score: 1 Player Name: Alice, Number of Plays: 2, Score: 1 P) lay again Quit New play set Exit

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions