Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help with the code for this class: In the game of Nim, players begin with s

ID: 3539138 • 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;
   }

}

Explanation / Answer

Below is code that should work, didnt have time to check it. Maybe i spelled something wrong somwhere.




public class NimGame
{
   private int[] piles;
   private int currentPlayer;

   public int getCurrentPlayer()
   {
      return currentPlayer;
   }


   // Item 1: Create a constructor.


public NumGame(int startCoins){

piles = new int{startCoins,startCoins,StartCoins};


}


   // Item 2: Implement changePlayer().

public void changePlayer(){

if (currentPlayer == 0)

currentPlayer = 1

else

if((currentPlayer == 1)

currentPlayer = 2;

else

currentPlayer = 0;

}

   // Item 3: Implement getCoinCount(int pile).

public int getCoinCount(int pile){

if( pile > 2 || pile < 0)

return null

else

return piles[pile];

}


   // Item 4: Implement takeCoins(int pile, int coins).

public int takeCoins(){

if( coins == 0)

{

System.out.println("Can't chose zero coins");

return 0;

}

if(2 > piles[pile] && piles[pile] < 0)

{

System.out.println("Not a valid pile");

return 0;

}

if(coins > piles[pile])

{

System.out.println("Not enough coins in that plie");

return 0;

}


piles[pile] = piles[pile] - coins;

changePlayer();

return coins;

}

   // Item 5: Implement isGameOver().

public boolean isGameOver(){


if(piles[0] ==0 && piles[1] ==0 && piles[2] ==0)

return true;

else

return false;

}

   // 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;
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote