Results 1 to 6 of 6
  1. #1
    besweeet is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default Working with Methods

    Here's my code:
    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class lab6
    {
      public static void main(String [] args)
      {
        Scanner input = new Scanner(System.in);
        // Initial health amounts
        int playerHealth = 50;
        int computerHealth = 50;
        
        // Welcome junk
        System.out.println("Welcome! Please enter your name.");
        String playerName = input.nextLine();
       
        System.out.println("Please choose a fight move (name).");
        System.out.println("1. punch");
        System.out.println("2. throw");
        System.out.println("3. block");
        String fightInput = input.nextLine();
        boolean valid = checkInput(fightInput);
        
        while(valid == false)
        {
          System.out.println("Invalid input. Please try again.");
          System.out.println("Please choose a fight move (name).");
          System.out.println("1. punch");
          System.out.println("2. throw");
          System.out.println("3. block");
          fightInput = input.nextLine();
          valid = checkInput(fightInput);
        }
        if(valid == true)
        {
          System.out.println("You entered "+fightInput+". Would you like to continue fighting (y/n)?");
          String yORn = input.nextLine();
          if(yORn.equals("n"))
          {
            System.out.println("Player health: "+playerHealth+"");
            System.out.println("Computer health: "+computerHealth+"");
            if(playerHealth > computerHealth)
            {
              System.out.println("Player ("+playerName+") wins!");
              System.out.println("Thanks for playing.");
            }
            else
            {
              System.out.println("Computer wins!");
              System.out.println("Thanks for playing.");
            }
          }
          if(yORn.equals("y"))
          {
            System.out.println("Please choose a fight move (name).");
            System.out.println("1. punch");
            System.out.println("2. throw");
            System.out.println("3. block");
            fightInput = input.nextLine();
            valid = checkInput(fightInput);
          }
        }
      }
      public static boolean checkInput(String fightInput)
      {
        if(fightInput.equals("punch"))
        {
          return true;
        }
        else if(fightInput.equals("throw"))
        {
          return true;
        }
        if(fightInput.equals("block"))
        {
          return true;
        }
        else
        {
          return false;
        }
      }
      public static void calcComputerMove(int computerInput)
      {
        int computerMoveNumber = (int)(Math.random()*10);
        if(computerMoveNumber == 0 || computerMoveNumber == 1 || computerMoveNumber == 2 || computerMoveNumber == 3)
        {
          String computerMove = "punch";
        }
        else if(computerMoveNumber == 4 || computerMoveNumber == 5 || computerMoveNumber == 6)
        {
          String computerMove = "throw";
        }
        else if(computerMoveNumber == 7 || computerMoveNumber == 8 || computerMoveNumber == 9)
        {
          String computerMove = "block";
        }
      }
      public static int calcPlayerHealth(int playerHealth, String fightInput, String computerMove)
      {
        if(fightInput.equals("punch") && computerMove.equals("block"))
          playerHealth -= 2;
        else if(fightInput.equals("block") && computerMove.equals("throw"))
          playerHealth -= 4;
        else if(fightInput.equals("throw") && computerMove.equals("punch"))
          playerHealth -= 6;
        else
          playerHealth -= 0;
        return playerHealth;
      }
      public static int calcComputerHealth(int computerHealth, String fightInput, String computerMove)
      {
        if(computerMove.equals("punch") && fightInput.equals("block"))
          computerHealth -= 2;
        else if(computerMove.equals("block") && fightInput.equals("throw"))
          computerHealth -= 4;
        else if(computerMove.equals("throw") && fightInput.equals("punch"))
          computerHealth -= 6;
        else
          computerHealth -= 0;
        return computerHealth;
      }
    }
    I need help with the main method. If the fightInput is valid, then the user will be asked whether or not he/she wants to continue. If it's N, then the game will end: playerHealth and computerHealth will be displayed and whatnot. If it's Y, then it will start from the beginning, where it asks the user for the move. This process should be repeated forever until the user enters N. I need help with this part :(.

    I also need to get the health reduction/display working correctly. I'm not sure how to make a outside method work with main properly.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    So what happen, I can see that you've implement something related to that condition? Did you test it, and if so what happen? Any error message? Can you please answer to those questions.

  3. #3
    besweeet is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default

    The way it is now, there are no errors. After entering in the first move, it will ask Y/N.

    If N, the it will display the health for both players as well as the winner. Although the health that is displayed is the same as the initial values (they aren't being decreased like they should be).

    If Y, it will ask for another move. After that move is entered, the program will end itself.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    This is all what you've to do. Why?

    Java Code:
            while (valid == true) {
                System.out.println("You entered " + fightInput + ". Would you like to continue fighting (y/n)?");
                String yORn = input.nextLine();
                if (yORn.equals("n")) {
                    System.out.println("Player health: " + playerHealth + "");
                    System.out.println("Computer health: " + computerHealth + "");
                    if (playerHealth > computerHealth) {
                        System.out.println("Player (" + playerName + ") wins!");
                        System.out.println("Thanks for playing.");
                    } else {
                        System.out.println("Computer wins!");
                        System.out.println("Thanks for playing.");
                    }
    
                    valid = false;
                }
                else if (yORn.equals("y")) {
                    System.out.println("Please choose a fight move (name).");
                    System.out.println("1. punch");
                    System.out.println("2. throw");
                    System.out.println("3. block");
                    fightInput = input.nextLine();
                    valid = checkInput(fightInput);
                }
            }

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Please go through the changes and see what I've done.

    Regarding your code, actually it's really bad practice in coding. See how many things you've repeated. Just take a paper and a pencil and write down the logic or the pseudo code of your class. Then you can find what you've really messup.

  6. #6
    besweeet is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default

    Wow, thanks! Yes, I see all my mistakes. I'm still a noobie though :). I was just getting a little bit lost when writing the code in main.

    Could you help me with the health part? If we could get that finished, then today would be the happiest day ever.

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. working with JC
    By yuhobebbho in forum New To Java
    Replies: 0
    Last Post: 02-11-2010, 12:22 AM
  3. Replies: 4
    Last Post: 04-03-2009, 07:36 AM
  4. Replies: 8
    Last Post: 05-28-2008, 08:00 AM
  5. Static methods - not working
    By Echilon in forum New To Java
    Replies: 2
    Last Post: 12-21-2007, 02:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •