need help

Discussion in 'Software' started by Zackary, Feb 24, 2010.

  1. Zackary

    Zackary Private E-2

    import java.util.Scanner;

    /**
    * <p>Original authors Namita Singla, Robert Cohen</p>
    * <p>Modified by Bob Wilson, 08/19/2005</p>
    *
    * <p>The simple NIM game.<br>
    * There are 21 sticks in the pile. On each move, each user take turns picking
    * up 1, 2, or 3 sticks until there are no more sticks left.
    * The one who picks up the last stick loses.</p>
    */

    public class SimpleNIM {
    private int sticksLeft = 21;
    private String player = "B";
    private int picksticks = 0;
    private int numSticks = 0;

    /**
    * Plays the game
    * @param args ignored
    */
    public static void main(String[] args) {

    // we can call a static method before/without instantiating an object
    welcome();

    // instantiate an object named pickUpSticks of class SimpleNIM
    SimpleNIM pickUpSticks = new SimpleNIM();

    // we can only call a non-static method via reference to an object name
    pickUpSticks.start();
    }

    /**
    * Displays the startup information banner.
    */
    private static void welcome() {
    System.out.println("WELCOME TO THE SIMPLE NIM GAME: 21 STICKS IN PILE");
    }

    /**
    * Starts and runs the game
    */
    public void start() {
    Scanner input = new Scanner(System.in);
    do {
    int picksticks = 0;
    boolean goodEntry = false;
    player = otherPlayer(player);
    do {
    System.out.print(
    "Player " + player +
    ": How many sticks do want to pick? (1, 2, or 3) ");
    picksticks = input.nextInt();
    if (validMove(picksticks)) {
    goodEntry = true;
    }
    else {
    System.out.println(
    sticksLeft - picksticks < 0
    ? "You can't pick "
    + picksticks
    + " sticks as only "
    + sticksLeft
    + " sticks left"
    : "That's an illegal move. "
    + "Choose 1, 2, or 3 sticks.");
    }
    }
    while (!goodEntry);

    updateSticksLeft(picksticks);
    System.out.println(
    "Player "
    + player
    + " takes "
    + picksticks
    + ( (picksticks == 1) ? " stick, " : " sticks, ")
    + "leaving "
    + sticksLeft
    + '\n');
    }
    while (gameOver() != true);
    System.out.println("Player " + otherPlayer(player) + " wins the game!");
    }

    /**
    * Update the number of sticks left in pile.
    * @param picksticks No. of sticks picked
    */
    private void updateSticksLeft(int picksticks) {
    sticksLeft = sticksLeft - picksticks;
    }

    /**
    * Game Over?
    * @return true if game is over.
    */
    private boolean gameOver() {
    return(sticksLeft <= 0);
    // YOUR CODE GOES HERE

    }

    /**
    * Returns the other player
    * @param p The current player ("B" or "A")
    * @return The other player ("A" or "B")
    */
    private String otherPlayer(String p) {
    if (p == "A")
    return "B";
    else
    return "A";
    // YOUR CODE GOES HERE

    }

    /**
    * Valid move?
    * @param numSticks The number of sticks picked
    * @return true iff there are enough sticks left and numSticks is between 1 and 3
    */

    private boolean validMove(int numSticks) {
    return ((numSticks >= 1 && numSticks <= 3)? true : false);
    // YOUR CODE GOES HERE
    }
    }

    i need help with the very last part. private boolean validMove(int numSticks).

    it's taking more sticks than there are left.

    i typed return (sticksLeft >0 );

    and other variations of it. but it doesn't work so help please.
     
  2. Wyatt_Earp

    Wyatt_Earp MajorGeek

    First off, for future reference, if you put your code in a code block, it will keep the formatting and be much easier to read. For example,

    Code:
    import java.util.Scanner;
    
    /**
    * <p>Original authors Namita Singla, Robert Cohen</p>
    * <p>Modified by Bob Wilson, 08/19/2005</p>
    *
    * <p>The simple NIM game.<br>
    * There are 21 sticks in the pile. On each move, each user take turns picking
    * up 1, 2, or 3 sticks until there are no more sticks left.
    * The one who picks up the last stick loses.</p>
    */
    
    public class SimpleNIM {
        private int sticksLeft = 21;
        private String player = "B";
        private int picksticks = 0;
        private int numSticks = 0;
        
        /**
        * Plays the game
        * @param args ignored
        */
        public static void main(String[] args) {
        
            // we can call a static method before/without instantiating an object
            welcome();
        
            // instantiate an object named pickUpSticks of class SimpleNIM
            SimpleNIM pickUpSticks = new SimpleNIM();
        
            // we can only call a non-static method via reference to an object name
            pickUpSticks.start();
        }
        
        /**
        * Displays the startup information banner.
        */
        private static void welcome() {
            System.out.println("WELCOME TO THE SIMPLE NIM GAME: 21 STICKS IN PILE");
        }
        
        /**
        * Starts and runs the game
        */
        public void start() {
            Scanner input = new Scanner(System.in);
            do {
                int picksticks = 0;
                boolean goodEntry = false;
                player = otherPlayer(player);
                do {
                    System.out.print(
                    "Player " + player +
                    ": How many sticks do want to pick? (1, 2, or 3) ");
                    picksticks = input.nextInt();
                    if (validMove(picksticks)) {
                        goodEntry = true;
                    }
                    else {
                        System.out.println(
                        sticksLeft - picksticks < 0
                        ? "You can't pick "
                        + picksticks
                        + " sticks as only "
                        + sticksLeft
                        + " sticks left"
                        : "That's an illegal move. "
                        + "Choose 1, 2, or 3 sticks.");
                    }
                }
                while (!goodEntry);
        
                updateSticksLeft(picksticks);
                System.out.println(
                "Player "
                + player
                + " takes "
                + picksticks
                + ( (picksticks == 1) ? " stick, " : " sticks, ")
                + "leaving "
                + sticksLeft
                + '\n');
            }
            while (gameOver() != true);
            System.out.println("Player " + otherPlayer(player) + " wins the game!");
        }
    
        /**
        * Update the number of sticks left in pile.
        * @param picksticks No. of sticks picked
        */
        private void updateSticksLeft(int picksticks) {
            sticksLeft = sticksLeft - picksticks;
        }
    
        /**
        * Game Over?
        * @return true if game is over.
        */
        private boolean gameOver() {
            return(sticksLeft <= 0);
            // YOUR CODE GOES HERE
        }
    
        /**
        * Returns the other player
        * @param p The current player ("B" or "A")
        * @return The other player ("A" or "B")
        */
        private String otherPlayer(String p) {
            if (p == "A")
                return "B";
            else
                return "A";
            // YOUR CODE GOES HERE
        }
    
        /**
        * Valid move?
        * @param numSticks The number of sticks picked
        * @return true iff there are enough sticks left and numSticks is between 1 and 3
        */
        
        private boolean validMove(int numSticks) {
            return ((numSticks >= 1 && numSticks <= 3)? true : false);
            // YOUR CODE GOES HERE
        }
    }
    
    Now, to fix the problem, it looks like the validMove() function only checks to see if the number of sticks selected is 1, 2, or 3. It also needs to validate that whatever number selected is less than or equal to the number of sticks left. You want all three checks in there "numSticks >= 1 and numSticks <= 3 and numSticks <= sticksLeft."
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds