Extra Credit: Simulating a Nim Game

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.

  1. 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.”

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.

//*******************************************************

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

  //———————————————-

  //Removes coins from the row.

//———————————————- public void takeCoins(int remove) { // add your code here }

//———————————————-

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

//*******************************************************

// TestNim.java // A simple driver to test the Nim class that simulates a Nim // game played by two persons with a row of coins. //******************************************************* import java.util.Scanner; public class TestNim { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println(“Who goes first, player 1 or 2?”); int player = scan.nextInt(); System.out.println(“how many coins do you want to play, choose a number in the range of 7 to 21”); int coins = scan.nextInt(); Nim nimGame = new Nim(coins, player); while(nimGame.getCoins()>0) { nimGame.gameStatus(); System.out.println(“how many coins would you like to take, 1, 2, or 3?”); int take = scan.nextInt(); nimGame.takeCoins(take); } System.out.println(“Game over!”); System.out.println(“Game won by: ” + nimGame.getPlayer()); } }



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *