Welcome to Dream.In.Code
Become a Java Expert!

Join 149,598 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,925 people online right now. Registration is fast and FREE... Join Now!




Simple nim code in java, i'm getting a missing return statement er

 
Reply to this topicStart new topic

Simple nim code in java, i'm getting a missing return statement er

mizzlea
10 Oct, 2007 - 09:05 PM
Post #1

New D.I.C Head
*

Joined: 10 Oct, 2007
Posts: 1


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

  /**
   * 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
      if(sticksLeft == 0) return true;
      else return false;
  }



  /**
   * 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
    if (p== "A") return "B";
      else return "A";

  }

  /**
   * 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
      if (numSticks > 1)
      {
      if (numSticks < 3)
      {
      return true;
      }
    else return false;
      }

}
}

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Simple Nim Code In Java, I'm Getting A Missing Return Statement Er
10 Oct, 2007 - 09:50 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Yeah the problem lies in your validMove function. Notice that you don't have a return statement for the situation where numSticks is less than or equal to 1. Your inner if statement never gets executed and then the function attempts to end without returning anything. So all you need to do is put in a return statement there.

CODE

private boolean validMove(int numSticks) {
    // YOUR CODE GOES HERE
    if (numSticks > 1)
    {
        if (numSticks < 3)
        {
            return true;
        }
        else return false;
    }
        
    // numSticks was less than or equal to 1 so return false
    return false;
}


As you can see from the code above, we put in the return statement of false at the end just in case the numSticks was less than or equal to one. Remember that you must cover "all paths of execution" with your return statements.

This will fix your problem. Enjoy! smile.gif
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 11:42PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month