Member Avatar for KatWoman27
KatWoman27

I got an assignment, and this are the instructions:
"Design and implement a Nim class and a NimGame application to allow two human players to play the game of Nim. The game starts with three piles of stones. The first pile has 3 stones, the second pile has 5 stones, and the last pile has 7 stones. The players take turns removing stones. On a single turn a player may only take stones from a single pile. A player may not take more than half the pile, except when it’s the last stone in the pile. The loser of the game is the player to take the very last stone".

This is what I got so far, I can't seem to "end" the game or declare a loser if the last stone is taken.

/*
*
*/
import java.util.Scanner;
/*
 *
 */
public class NimGame {

    public static void main(String[] args) {
        //variables
        int player = 1;
        int pile = 0;
        int stones = 0;
        int counter = 0;

             //Scanner construct
        Scanner input = new Scanner(System.in);

        Nim theBoard = new Nim(); // object construct

        while(counter == 0)// begin while loop
{
            while ( theBoard.playerMove(pile, stones)== false)
            {
                theBoard.displayBoard();
                   // prints out a command to user
                System.out.print("Player " + player + " Enter your pile (1-3)\n");
                pile = input.nextInt();

                while (pile < 1 || pile > 3 )
                {
                    // prints out error message and asks to re-enter input
                    System.out.println("Invalid Pile, Please enter your pile (1-3)\n");
                    pile = input.nextInt();
                }
                System.out.print("Player " + player + " Enter your stones\n");
                stones = input.nextInt();

                while (stones <= 0)
                {
                    // prints out error message and ask to re-enter input
                    System.out.println("Invalid stones, Please enter your stones\n");
                    stones = input.nextInt();
                }
            }
            if (player == 1)
                player = 2;
            else
                player = 1;

            theBoard.displayBoard();

            if (theBoard.determineWinner() != -1)
                counter = 1;
        }

        System.out.println("Game Over!");

    }// end main method
}// end NimGame class

This is the Nim class that goes with this code:

class Nim {

//class variables
    private int [] piles;

    //constructor
    public Nim()
    {
        piles = new int [3];
        piles[0] = 3;
        piles[1] = 5;
        piles[2] = 7;
    }

    //method playerMove
    public boolean playerMove(int pile, int stones)
    {
        if (( pile < 1 ) || ( pile > piles.length ))
        {
            return false;
        }

        if ( piles [ pile - 1 ] >= stones)
        {
            piles[ pile - 1 ] -= stones;
            return true;
        }

        else
        {
            return false;
        }
    }

    // displayBoard method
    public void displayBoard()
    {
        for(int row = 0; row < 3; row++)
        {
                if(piles[row] == 0)
                    System.out.print("    _   ");
                else
                    System.out.print("    " + piles[row] + "   ");
         }

            System.out.println("\n  Pile 1   Pile 2   Pile 3 ");
    }

    // method determineWinner
    public int determineWinner()
    {
        System.out.println(" Winner!");
        return -1;
        
    } // end determine winner int
}//end Nim class

PLEASE HELP ME!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.