Extra Credit: Simulating a Nim Game File Nim.java contains a partial definition
ID: 3547161 • Letter: E
Question
Extra Credit: Simulating a Nim Game
File Nim.java contains a partial definition for a class representing a Nim game. Save it to your directory and study it to see what methods it contains. Then complete the Nim class as described below.
b. Fill in the code for method takeCoins which decrease nCoins (the number of coins left) by remove(the number of coins to be removed) if it is less than or equal to the nCoin (the number of coins left). Otherwise prints "You can only remove "+ nCoins + " or less from the row." Since on each turn a player must remove one, two, or three from the row, prints "You can only remove one, tow, or three from the row." if remove is not in the range (1, 2, or 3). You will also need to change player after decreasing nCoins.
2. File TestNim.java contains a driver program that uses the Nim class above. Save it to your directory and study it to see what it does. Then compile and run it to test the Nim class.
//*******************************************************
//*******************************************************
//----------------------------------------------
//Constructor -- initializes nCoins and player
//----------------------------------------------
//----------------------------------------------
//Removes coins from the row.
//----------------------------------------------
// Display the game status.
//----------------------------------------------
// Returns nCoins.
//----------------------------------------------
//----------------------------------------------
// Returns player.
//----------------------------------------------
//*******************************************************
A Nim game is played by two persons with a row of coins. The players alternate turn. On each turn a player must remove one, two, or three from the row. The play who removes the last coin from the row loses. The game can begin with any number of coins, but starting with 21 coins is quite common. File Nim.java contains a partial definition for a class representing a Nim game. Save it to your directory and study it to see what methods it contains. Then complete the Nim class as described below. Fill in the code for constructor, which should initialize the instance variable nCoins as specified by the parameter coins if coins is in the range of 7 to 21 inclusive. Otherwise prints a message "Invalid input, 21 coins is assigned to your game." The constructor should also initialize the instance variable player as specified by the parameter starter if starter is 1 or 2. Otherwise prints a message "Invalid input, player 1 is assigned to be the first." Fill in the code for method takeCoins which decrease nCoins (the number of coins left) by remove(the number of coins to be removed) if it is less than or equal to the nCoin (the number of coins left). Otherwise prints "You can only remove "+ nCoins + " or less from the row." Since on each turn a player must remove one, two, or three from the row, prints "You can only remove one, tow, or three from the row." if remove is not in the range (1, 2, or 3). You will also need to change player after decreasing nCoins. File TestNim.java contains a driver program that uses the Nim class above. Save it to your directory and study it to see what it does. Then compile and run it to test the Nim class.Explanation / Answer
//*******************************************************
// Nim.java
// A Nim game that is played by two persons with a row of coins.
//*******************************************************
public class Nim
{
//instance data
private int nCoins = 21;
private int player = 1;
//----------------------------------------------
//Constructor -- initializes nCoins and player
//----------------------------------------------
public Nim(int coins, int starter)
{
// add your code here
nCoins = coins;
player = starter;
if (nCoins < 7 || nCoins > 21)
{
System.out.println("Invalid input," +"21 coins is assigned to your game.");
nCoins = 21;
}
if (player < 1 || player > 2)
{
System.out.println("Invalid input," +"player 1 is assigned to your game.");
player = 1;
}
}
//----------------------------------------------
//Removes coins from the row.
//----------------------------------------------
public void takeCoins(int remove)
{
// add your code here
if (remove >= 1 && remove <= 3)
{
if (nCoins >= remove)
{
nCoins -= remove;
player = player == 1 ? 2 : 1;
}
else
{
System.out.println("You can only remove " + nCoins + " or less from the row." );
}
}
else
{
System.out.println("You can only remove " + "one, two, or three from the row.");
}
}
//----------------------------------------------
// Display the game status.
//----------------------------------------------
public void gameStatus()
{
System.out.println("Number of coins left: " + nCoins);
System.out.println();
System.out.print("Player" + player + ", ");
}
//----------------------------------------------
// Returns nCoins.
//----------------------------------------------
public int getCoins()
{
return nCoins ;
}
//----------------------------------------------
// Returns player.
//----------------------------------------------
public int getPlayer()
{
return player;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.