Need help with the code for this class: In the game of Nim, players begin with s
ID: 3539145 • Letter: N
Question
Need help with the code for this class:
In the game of Nim, players begin with small objects (typically coins) in one or more piles. Players alternate
turns, removing one or more coins from any one pile. The object of the game is to force one's opponent to
remove the last coin.
In this assignment, we simplify by assuming the game will always have 3 piles, each starting with the
same number of items.
The le NimGame.java contains the skeleton of a class NimGame.
1. Create a constructor that takes one argument, initializes the array (to length 3), and sets the value of
each element to that argument. (Your program need not check to see if the number entered is positive.)
2. Create an int method called getCoinCount(int pile) that will accept a pile (from 0 to 2) and return
the number of objects in that pile.
3. Create a private void method called changePlayer() which will set the current player to 2 if it is
already 1, and 1 otherwise.
4. Create an int method called takeCoins(int pile, int coins) that will remove the specied number
of coins from the specied pile and return the number of coins removed.
Things to note:
A player cannot remove 0 items. If the number of coins the player wishes to remove is 0 (or less),
print an error message and return 0.
A player cannot remove items from a pile that doesn't exist. If the pile requested is less than 0
or greater than 2, print an error message and return 0.
A pile cannot have negative coins. If the number of coins retrieved is greater than the number
still in the pile, print an error message and return 0.
If any coins are actually taken, the current player changes (you need to call changePlayer()).
5. Create a boolean method called isGameOver() that returns true if and only if all of the piles are
empty.
public class NimGame
{
private int[] piles;
private int currentPlayer;
public int getCurrentPlayer()
{
return currentPlayer;
}
// Item 1: Create a constructor.
// Item 2: Implement changePlayer().
// Item 3: Implement getCoinCount(int pile).
// Item 4: Implement takeCoins(int pile, int coins).
// Item 5: Implement isGameOver().
// Print out the piles.
public String toString()
{
String ret = "";
ret +=
"------------------------------------------------------------ ";
ret += "PILE 1 PILE 2 PILE 3 ";
ret += "------ ------ ------ ";
ret += " " + piles[0] + " " + piles[1] + " " + piles[2] + " ";
ret +=3D =
"------------------------------------------------------------ ";
return ret;
}
}
***********************************************************************************
Below is the driver program for this class when it works properly:
****************************************************************************************
public class NimRunner
{
// Print an opening banner for the game.
public static void printBanner()
{
System.out.println("------------------------------------------------------------");
System.out.println("| NIM |");
System.out.println("------------------------------------------------------------");
System.out.println("");
}
// Get the number of coins in each pile from the user.
//
// Accepts a Scanner object from which to read the number.
public static int askCoinTotal(Scanner scan)
{
int count;
do {
System.out.println("How many coins in each pile? ");
count = scan.nextInt();
if (count <= 0) {
System.out.println("Sorry, you can't have a game with no =coins.");
}
} while (count <= 0);
return count;
}
// Print whose turn it is.
//
// Accepts the player number.
public static void announceTurn(int player)
{
System.out.println("It is now player " + player + "'s turn!");
}
// Get the pile number from which the coins should be taken.
public static int askPile(Scanner scan)
{
System.out.println("Which pile? (1, 2, or 3) ");
return scan.nextInt() - 1;
}
// Get the number of coins to remove from the pile.
public static int askCoins(Scanner scan, int max)
{
System.out.println("How many coins? (up to " + max + ") ");
return scan.nextInt();
}
// Execution begins here.
public static void main(String[] args)
{
// Create a scanner reading from the keyboard.
Scanner scan = new Scanner(System.in);
// Print the opening banner.
printBanner();
// Get the number of coins in each pile, and create the game.
int coinTotal = askCoinTotal(scan);
NimGame game = new NimGame(coinTotal);
// Keep looping while the game is still going:
while (! game.isGameOver())
{
// Print the current status of the game.
System.out.println(game);
// Print whose turn it is.
announceTurn(game.getCurrentPlayer());
// Get the current user's play.
int pile = askPile(scan);
int coins = askCoins(scan, game.getCoinCount(pile));
// Attempt to make the play.
int coinsTaken = game.takeCoins(pile, coins);
// If the move is invalid, say so.
// We don't have to worry about whose turn it is here; that's handled
// in the game itself.
if (coinsTaken == 0) {
System.out.println("Invalid move.");
// Otherwise, if the move is incomplete, report it.
} else if (coinsTaken < coins) {
System.out.println("Excess coins requested.");
}
}
// Once the game is over, print the winner (and the winner is the
// current player when the game is over).
System.out.println("The winner is: player " + game.getCurrentPlayer());
}
}
Explanation / Answer
i wil do it...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.