Is anyone willing to help with my java programming homework?

Discussion in 'Software' started by mizzlea, Oct 10, 2007.

  1. mizzlea

    mizzlea Private E-2

    I am supposed to write a simple NIM code at the end of the code where it says YOUR CODE GOES HERE. I tried to use return but my code won't compile at all.

    A bit of info about the nim game if it's not familiaar to you : http://www.cut-the-knot.org/ctk/May2001.shtml

    Here are the instructions :
    Starting with this class, your job is to complete the following three methods:

    · boolean validMove(int numSticks) – returns true if it is valid to take the specified number of sticks.

    · boolean gameOver() – returns true if the game is over (i.e. there are no more sticks).

    · String otherPlayer(String p) – returns the identifier of the other player. If p is “A”, this method returns “B”. If p is “B”, this method returns “A”.

    The instance variable sticksLeft contains the number of sticks left in the pile.



    Here is the code: (everything should be fine until the last couple of lines(


    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";

    /**
    * 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() {
    // 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) {
    // 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) {
    // YOUR CODE GOES HERE

    }
    }
     
  2. Lev

    Lev MajorGeek

    I would encourage you to have a go at writing the code yourself first, and then when you have made a good attempt yourself, post back and ask others what they think.

    Simply asking people to do your homework for you when you haven't first made a serious attempt defeats the purpose of homework. Those who can do it here know they can already do it :)
     
  3. mizzlea

    mizzlea Private E-2

    You're right, but when you it's kind of hard to attempt it when you are totally clueless. I did find someone who was able to help me out. They didn't do it for me but they guided me through it and I did it. Thanks for replying.

     

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