Results 1 to 17 of 17
  1. #1
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default working with return values

    hey so I have been working with return values instead of void methods. This is what I have come up with so far. This is a skeleton I plan to expand upon so there are variables that are not used yet but I plan to in the future. Its an adventure text game that lets you choose a character name and between three weapons and fight three different monsters. you can choose to attack or defend until each monster is defeated. defending gains you some health points back but also a smaller amount for the monster as well. Each monster has attacks it does when monstHealth is between certain points. Sometimes you skip through them depending on which weapon you choose and sometimes you don't. Be careful and make sure to defend otherwise the second monster can take your hitpoints out quickly.

    at some point do I break this into multiple files so there is not so much text or just keep making new classes and grouping similar items when things get larger?

    Java Code:
    package com.bluemain;
    
    import java.util.*;
    import static com.bluemain.quest.*;
    import static com.bluemain.utilAdv.*;
    
    public class Adventure {
        
        static int score = 0, str = 5, def = 5;
        static int monststr, monstdef;
        static double health = 100.0, exp = 0.0, monstHealth = 50;
        static Scanner scan = new Scanner(System.in);
        static String charName, wpnChoice;
        public static void main(String[] args) {
            
            prnt("Hero there is trouble afoot, what is your name?");
            char1build();
            wpnChoice();
            char1stats();
            prnt("");
            prnt("Theres a monster up ahead lets go investigate.");
            boolean a = monst1(str,def);
            if (a) {
                boolean b = monst2(str,def);
                if (b) {
                    boolean c = monst3(str,def);
                    if (c) {
                        prnt("You have slain all the monsters, Good work.");
                    }
                }
            } 
        }
    }
    class quest extends Adventure {
        
        /**
         * 
         * here is the character set up. 
         */
        
        public static boolean char1build () {
            do{            
            prnt("Please enter your characters name:");
                charName = input();
            }
            while (charName.isEmpty());
            if (!charName.isEmpty()) {
                return true;
            }       
            return false;
        }
        
        public static boolean wpnChoice () {
            do {            
            prnt("Please choose your weapon:");
            prnt("Sword, Mace, or Stilleto");
            wpnChoice = input();
            if (wpnChoice.equalsIgnoreCase("sword")) {
                sword();
            }
            if (wpnChoice.equalsIgnoreCase("mace")) {
                mace();
            }
            if (wpnChoice.equalsIgnoreCase("stilleto")) {
                stilleto();
            }
            } 
            while (wpnChoice.isEmpty());
            if (!wpnChoice.isEmpty()) {
                return true;
            } 
            return false;
        }
        
        public static double char1hlth() {
            health = health;
            return health;
        }
        public static int char1str() {
            str = str;
            return str;
        }
        public static int char1def() {
            def = def;
            return def;
        }
        public static double char1exp() {
            exp = exp;
            return exp;
        }
        public static double char1hlth(double healthDif) {
            health = health + healthDif;
            return health;
        }
        public static int char1str(int strDif) {
            str = str + strDif;
            return str;
        }
        public static int char1def(int defDif) {
            def = def + defDif;
            return def;
        }
        public static double char1exp(double expDif) {
            exp = exp + expDif;
            return exp;
        }
        public static void char1stats() {
            double health = char1hlth();
            int def1 = char1def();
            int str1 = char1str();
            prnt(charName+"'s health is: "+health+" str: "+str1+" def: "+def1);
        }
        
        /**
         * 
         * here are the items.
         */
        
        public static double smlHealth () {
            health = health += 10;
            return health;
        }
        
        public static double medHealth () {
            health = health += 25;
            return health;
        }
        
        public static double lrgHealth () {
            health = health += 50;
            return health;
        }
        /**
         * 
         * @param healthAffected minus difference
         * 
         */
        public static double psnPotion (double healthAffected) {
            return healthAffected;
        }
        
        public static double firePotion (double healthAffected) {
            return healthAffected;
        }
        
        /**
         * 
         * here are the weapons.
         */
        
        public static int sword () {
            str = str += 35;
            return str;
        }
        
        public static int mace () {
            str = str += 30;
            return str;
        }
        
        public static int stilleto () {
            str = str += 25;
            return str;
        }
        
        /**
         * Here are the monsters.
         * 
         */
        
        public static boolean monst1 (int str, int def) {
            monststr = 4;
            monstdef = 4;
            monstHealth = 50;
            do{
                prnt("");
                prnt("You have encountered a Zombie.");
                prnt("The zombies health is: "+monstHealth);
                prnt("Your health is: "+health);
                prnt("What will you choose to do?");
                prnt("fight or defend?");
                String pick = input();
                if (pick.equalsIgnoreCase("fight")) {
                    attack();
                }
                if (pick.equalsIgnoreCase("defend")) {
                    defend();
                }
                if (monstHealth <= 35 && monstHealth >= 30) {
                    prnt("Zombie bite");
                    health = health -= 15;
                }
                if (monstHealth <= 20 && monstHealth >= 16) {
                    prnt("Zombie bite");
                    health = health -= 15;
                }
                if (monstHealth <= 0) {
                    prnt("You defeated the Zombie.");
                    return true;
                }
                if (health <= 0) {
                    prnt("You were defeated by the Zombie.");
                    return true;
                }
            }while (monstHealth > 0);
            return false;
        }
        
        public static boolean monst2 (int str, int def) {
            monststr = 8;
            monstdef = 8;
            monstHealth = 100;
            do{
                prnt("");
                prnt("You have encountered a Warewolf.");
                prnt("The warewolfs health is: "+monstHealth);
                prnt("Your health is: "+health);
                prnt("What will you choose to do?");
                prnt("fight or defend?");
                String pick = input();
                if (pick.equalsIgnoreCase("fight")) {
                    attack();
                }
                if (pick.equalsIgnoreCase("defend")) {
                    defend();
                }
                if (monstHealth <= 65 && monstHealth >= 55) {
                    prnt("Warewolf assault.");
                    health = health -= 30;
                }
                if (monstHealth <= 45 && monstHealth >= 40) {
                    prnt("Warewolf Scratch.");
                    health = health -= 20;
                }
                if (monstHealth <= 20 && monstHealth >= 16) {
                    prnt("Warewolf bite");
                    health = health -= 15;
                }
                if (monstHealth <= 0) {
                    prnt("You defeated the Warewolf.");
                    return true;
                }
                if (health <= 0) {
                    prnt("You were defeated by the Warewolf.");
                    return true;
                }
            }while (monstHealth > 0);
            return false;
        }
        
        public static boolean monst3 (int str, int def) {
            monststr = 12;
            monstdef = 12;
            monstHealth = 150;
            do{
                prnt("");
                prnt("You have encountered a Vampire.");
                prnt("The vampires health is: "+monstHealth);
                prnt("Your health is: "+health);
                prnt("What will you choose to do?");
                prnt("fight or defend?");
                String pick = input();
                if (pick.equalsIgnoreCase("fight")) {
                    attack();
                }
                if (pick.equalsIgnoreCase("defend")) {
                    defend();
                }
                if (monstHealth <= 100 && monstHealth >= 90) {
                    prnt("Vampire bite.");
                    health = health -= 15;
                }
                if (monstHealth <= 80 && monstHealth >= 72) {
                    prnt("Vampire bite.");
                    health = health -= 15;
                }
                if (monstHealth <= 60 && monstHealth >= 50) {
                    prnt("Vampire bite.");
                    health = health -= 15;
                }
                if (monstHealth <= 35 && monstHealth >= 30) {
                    prnt("Vampire bite");
                    health = health -= 15;
                }
                if (monstHealth <= 20 && monstHealth >= 15) {
                    prnt("Vampire claw scratch.");
                    health = health -= 10;
                }
                if (monstHealth <= 0) {
                    prnt("You have defeated the Vampire.");
                    return true;
                }
                if (health <= 0) {
                    prnt("You have been defeated by the Vampire.");
                    return true;
                }
            }while (monstHealth > 0);
            return false;
        }
        
        /**
         * 
         * here are your moves 
         */
        public static double attack() {
            monstHealth = monstHealth -= (str / 4); 
            if (monstdef> str) {
                prnt("Monster is too strong!");
            }
            return monstHealth;
        }
        
        public static double defend() {
            health = health += 12;
            monstHealth = monstHealth += 4;
            return health + monstHealth;
        }
    }
    class utilAdv extends quest{
        /**
         * 
         * here are the utilities. 
         */
        public static void prnt(String s1) {
            System.out.println(s1);
        }
        public static String input() {
            String input = scan.nextLine().trim().toLowerCase();
            return input;
        }
    }

  2. #2
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    6

    Default Re: working with return values

    I think breaking this code up in multiple classes would make sense. Probably something along the lines of Player, Weapon, Enemy, MainAction.
    Right now you have a code that you could have done with a scripted language. Did you make all methods static, because this way you had access to them?
    The Player class would have a health property, maybe a name property. The player would choose a weapon, I guess.
    The Weapon class would have a property like impact or severeness. It would have instances like: mace, sword, ?
    The Enemy class would carry the monsterhealth property, the fight strength property. It would have the instances wereWolf, zombie, etc.
    The MainAction class would create all the instances, maybe with some choices for the player and then randomly pick an enemy and fight. Then check whether the player has some health left in it.
    Ok, these are just some wild thoughts of how an OOP program could look like.

    In the header you ask about working with return values: That's a different story: the method defines a return type and has to have a return method that does that. Typically the method computes that return value. But you have a couple of correct examples in your code.

  3. #3
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default Re: working with return values

    Yes i made them all static so i could access them all throughout while i build it up. Then when its closer to what i want i go back and remove all the static keywords i can.

    I also want to add some content inbetween battles. Rewards for winning. And other various things.

    I think today if i get a chance to code jll try and break them up more into classes.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: working with return values

    Learning how to use return values is not the problem. It is learning how "not" to use static.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default Re: working with return values

    Oh i thought you said i need to not use void methods so much because i wasnt fully utilizing the language.

    Yeah i do find the static keyword useful in the sense that i can make a variable and access it all through out. I do need to work on removing it. I was going to make more classes for each individual catagory. Ill try moving them around to the corresponding ones to minmize the static word usage.

    My last thread i posted i ended up removing all but 2 usages of the word like you suggested and it all still worked fine. Maybe its a bad habit i taught myself already.

  6. #6
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default Re: working with return values

    okay so first of all I have added a store and the option to buy healing potions after battle as well as 2 more weapons to choose from.

    more importantly ive removed the majority or static keywords from the program. I had to break it down into one main file and 3 supporting class files.
    still all my variables are static but the methods are not.

    i would prefer to break down the character class into multiple classes because right now it contains the character build the prompts and store as well as monsters. then there is a class for action moves and one for items.

    main
    Java Code:
    package com.bluemain;
    
    import java.util.*;
    
    public class Adventure extends character{
            
         /**
         * 
         * here are the utilities. 
         */
            
        public void prnt(String s1) {
            System.out.println(s1);
        }
        public String input() {
            String input = scan.nextLine().trim().toLowerCase();
            return input;
        }
        static int score = 0, str = 5, def = 5, smlhlth = 0, medhlth = 0, lrghlth = 0;
        static int monststr, monstdef;
        static double health = 100.0, exp = 0.0, monstHealth = 50, gc = 0.0;
        static Scanner scan = new Scanner(System.in);
        static String charName, wpnChoice;
        
        public Adventure() {
            
            prnt("Hero there is trouble afoot, what is your name?");
            char1build();
            wpnChoice();
            char1stats();
            prnt("");
            prnt("Theres a monster up ahead lets go investigate.");
            
            do {
            boolean a = monst1(str,def);
            if (a) {
                gotoShop();
                boolean b = monst2(str,def);
                if (b) {
                    gotoShop();
                    boolean c = monst3(str,def);
                    if (c) {
                        prnt("You have slain all the monsters, Good work.");
                    }
                }
            } 
        }   while (health > 0);
    }
        
        public static void main(String[] args) {
            
           new Adventure();     
        }
    }
    character
    Java Code:
    package com.bluemain;
    
    import static com.bluemain.Adventure.*;
    
    public class character extends actionMoves {
          
        /**
         * 
         * here is the character set up. 
         * @return true when char name entered
         */
    
        public boolean char1build () {
            do{            
            prnt("Please enter your characters name:");
                charName = input();
            }
            while (charName.isEmpty());
            if (!charName.isEmpty()) {
                return true;
            }       
            return false;
        }
        
        /**
         * 
         * here you are prompted to choose a weapon. 
         * @return when weapon choice is made
         */
        public boolean wpnChoice () {
            do {            
            prnt("Please choose your weapon:");
            prnt("Sword, Mace, Stilleto, Dagger, or Bowie Knife.");
            wpnChoice = input();
            if (wpnChoice.equalsIgnoreCase("sword")) {
                sword();
            }
            if (wpnChoice.equalsIgnoreCase("mace")) {
                mace();
            }
            if (wpnChoice.equalsIgnoreCase("stilleto")) {
                stilleto();
            }
            if (wpnChoice.equalsIgnoreCase("dagger")) {
                dagger();
            }
            if (wpnChoice.equalsIgnoreCase("bowie knife")) {
                bowieKnife();
            }
            } 
            while (wpnChoice.isEmpty());
            if (!wpnChoice.isEmpty()) {
                return true;
            } 
            return false;
        }
        
        /**
         * 
         * here are methods to affect various variables. 
         * @return 
         */
        public double char1hlth() {
            return health;
        }
        public int char1str() {
            return str;
        }
        public int char1def() {
            return def;
        }
        public double char1gc() {
            return gc;
        }
        public double char1hlth(double healthDif) {
            health = health + healthDif;
            return health;
        }
        public int char1str(int strDif) {
            str = str + strDif;
            return str;
        }
        public int char1def(int defDif) {
            def = def + defDif;
            return def;
        }
        public double char1gc(double gcDif) {
            gc = gc + gcDif;
            return gc;
        }
        public double char1exp(double expDif) {
            exp = exp + expDif;
            return exp;
        }
        
        /**
         * 
         * this return characters stats.
         */
        public void char1stats() {
            double health = char1hlth();
            int def1 = char1def();
            int str1 = char1str();
            double gc1 = char1gc();
            prnt(charName+"'s health is: "+health+" str: "+str1+" def: "+def1+" gold coins: "+gc1);
        }  
        
        /**
         * 
         * Here are the monsters.
         * 
         */
        
        public boolean monst1 (int str, int def) {
            monststr = 4;
            monstdef = 4;
            monstHealth = 50;
            do{
                prnt("");
                prnt("You have encountered a Zombie.");
                prnt("The zombies health is: "+monstHealth);
                prnt("Your health is: "+health);
                prnt("What will you choose to do?");
                prnt("fight or defend?");
                prnt("sml health: "+smlhlth+" med health: "+medhlth+" lrg health: "+lrghlth);
                String pick = input();
                if (pick.equalsIgnoreCase("fight")) {
                    attack();
                }
                if (pick.equalsIgnoreCase("defend")) {
                    defend();
                }
                if (pick.equalsIgnoreCase("sml health")) {
                    if (smlhlth > 0){
                    useSmlHealth();
                    smlhlth--;
                    } else {
                        prnt("You do not have any potions.");
                    }
                }
                if (pick.equalsIgnoreCase("med health")) {
                    if (medhlth > 0) {
                    useMedHealth();
                    medhlth--;
                    } else {
                        prnt("You do not have any potions.");
                    }
                }
                if (pick.equalsIgnoreCase("lrg health")) {
                    if (lrghlth > 0) {
                    useLrgHealth();
                    lrghlth--;
                    } else {
                        prnt("You do not have any potions.");
                    }
                }
                if (monstHealth <= 35 && monstHealth >= 30) {
                    prnt("Zombie bite");
                    health = health -= 15;
                }
                if (monstHealth <= 20 && monstHealth >= 16) {
                    prnt("Zombie bite");
                    health = health -= 15;
                }
                if (monstHealth <= 0) {
                    prnt("You defeated the Zombie.");
                    if (health >= 75) {
                        char1gc(15);
                    }
                    if (health >=50 && health <= 74) {
                        char1gc(10);
                    }
                    if (health < 50) {
                        char1gc(5);
                    }
                    return true;
                }
                if (health <= 0) {
                    prnt("You were defeated by the Zombie.");
                    return true;
                }
            }while (monstHealth > 0);
            return false;
        }
        
        public boolean monst2 (int str, int def) {
            monststr = 8;
            monstdef = 8;
            monstHealth = 100;
            do{
                prnt("");
                prnt("You have encountered a Warewolf.");
                prnt("The warewolfs health is: "+monstHealth);
                prnt("Your health is: "+health);
                prnt("What will you choose to do?");
                prnt("fight or defend?");
                prnt("sml health: "+smlhlth+" med health: "+medhlth+" lrg health: "+lrghlth);
                String pick = input();
                if (pick.equalsIgnoreCase("fight")) {
                    attack();
                }
                if (pick.equalsIgnoreCase("defend")) {
                    defend();
                }
                if (pick.equalsIgnoreCase("sml health")) {
                    if (smlhlth > 0){
                    useSmlHealth();
                    smlhlth--;
                    } else {
                        prnt("You do not have any potions.");
                    }
                }
                if (pick.equalsIgnoreCase("med health")) {
                    if (medhlth > 0) {
                    useMedHealth();
                    medhlth--;
                    } else {
                        prnt("You do not have any potions.");
                    }
                }
                if (pick.equalsIgnoreCase("lrg health")) {
                    if (lrghlth > 0) {
                    useLrgHealth();
                    lrghlth--;
                    } else {
                        prnt("You do not have any potions.");
                    }
                }
                if (monstHealth <= 65 && monstHealth >= 55) {
                    prnt("Warewolf assault.");
                    health = health -= 30;
                }
                if (monstHealth <= 45 && monstHealth >= 40) {
                    prnt("Warewolf Scratch.");
                    health = health -= 20;
                }
                if (monstHealth <= 20 && monstHealth >= 16) {
                    prnt("Warewolf bite");
                    health = health -= 15;
                }
                if (monstHealth <= 0) {
                    prnt("You defeated the Warewolf.");
                    if (health >= 75) {
                        char1gc(15);
                    }
                    if (health >=50 && health <= 74) {
                        char1gc(10);
                    }
                    if (health < 50) {
                        char1gc(5);
                    }
                    return true;
                }
                if (health <= 0) {
                    prnt("You were defeated by the Warewolf.");
                    return true;
                }
            }while (monstHealth > 0);
            return false;
        }
        
        public boolean monst3 (int str, int def) {
            monststr = 12;
            monstdef = 12;
            monstHealth = 150;
            do{
                prnt("");
                prnt("You have encountered a Vampire.");
                prnt("The vampires health is: "+monstHealth);
                prnt("Your health is: "+health);
                prnt("What will you choose to do?");
                prnt("fight or defend?");
                prnt("sml health: "+smlhlth+" med health: "+medhlth+" lrg health: "+lrghlth);
                String pick = input();
                if (pick.equalsIgnoreCase("fight")) {
                    attack();
                }
                if (pick.equalsIgnoreCase("defend")) {
                    defend();
                }
                if (pick.equalsIgnoreCase("sml health")) {
                    if (smlhlth > 0){
                    useSmlHealth();
                    smlhlth--;
                    } else {
                        prnt("You do not have any potions.");
                    }
                }
                if (pick.equalsIgnoreCase("med health")) {
                    if (medhlth > 0) {
                    useMedHealth();
                    medhlth--;
                    } else {
                        prnt("You do not have any potions.");
                    }
                }
                if (pick.equalsIgnoreCase("lrg health")) {
                    if (lrghlth > 0) {
                    useLrgHealth();
                    lrghlth--;
                    } else {
                        prnt("You do not have any potions.");
                    }
                }
                if (monstHealth <= 100 && monstHealth >= 90) {
                    prnt("Vampire bite.");
                    health = health -= 15;
                }
                if (monstHealth <= 80 && monstHealth >= 72) {
                    prnt("Vampire bite.");
                    health = health -= 15;
                }
                if (monstHealth <= 60 && monstHealth >= 50) {
                    prnt("Vampire bite.");
                    health = health -= 15;
                }
                if (monstHealth <= 35 && monstHealth >= 30) {
                    prnt("Vampire bite");
                    health = health -= 15;
                }
                if (monstHealth <= 20 && monstHealth >= 15) {
                    prnt("Vampire claw scratch.");
                    health = health -= 10;
                }
                if (monstHealth <= 0) {
                    prnt("You have defeated the Vampire.");
                    if (health >= 75) {
                        char1gc(15);
                    }
                    if (health >=50 && health <= 74) {
                        char1gc(10);
                    }
                    if (health < 50) {
                        char1gc(5);
                    }
                    return true;
                }
                if (health <= 0) {
                    prnt("You have been defeated by the Vampire.");
                    return true;
                }
            }while (monstHealth > 0);
            return false;
        }
        
        /**
         * 
         * here are the prompts for in between battle.
         * (soon to come)
         * including a shop to buy potions.
         */
           
        public static boolean intro () {
            
            return false;
        }
        
        /**
         * 
         * here is the shop to buy items.
         * @return
         */
        public boolean shop () {
                do {
                prnt("Welcome to the shop.");
                prnt("You have "+gc+" gold coins.");
                if (gc >= 5) {
                prnt("sml Health: +10, 5gc");
                }
                if (gc >= 10) {
                prnt("med Health: +25, 10gc");
                }
                if (gc >= 15) {
                prnt("lrg Health: +50, 15gc");
                }
                prnt("Exit.");
                prnt("Please enter a choice.");
                String a = input();
                if (a.equalsIgnoreCase("sml health")) {
                    smlhlth++;
                    gc = gc -= 5;
                }
                if (a.equalsIgnoreCase("med health")) {
                    medhlth++;
                    gc = gc -= 10;
                }
                if (a.equalsIgnoreCase("lrg health")) {
                    lrghlth++;
                    gc = gc -= 15;
                }
                if (a.equalsIgnoreCase("exit")) {
                    return true;
                }
                }while (gc>=0);
            return false;
        }
     /**
      * 
      *@return asks to goto the shop or not. 
      */    
        public boolean gotoShop () {
            int i = 1;
            
            do {
            prnt("Great job defeating that monster.");
            prnt("Do you need to visit the shop?");
            prnt("yes or no?");
            String a = input();
            if (a.equalsIgnoreCase("yes")) {
                shop();
                i = 0;
            }
            if (a.equalsIgnoreCase("no")) {
                prnt("Alright, good luck hero.");
                return true;
            }
            if (a.isEmpty()) {
                gotoShop();
            }
            return false;
        } while (i != 0);
        } 
    }
    actionMoves
    Java Code:
    package com.bluemain;
    
    import static com.bluemain.Adventure.*;
    
    public class actionMoves extends items {
        
         /**
         * 
         * here are the utilities. 
         */
        
        public void prnt(String s1) {
            System.out.println(s1);
        }
        public String input() {
            String input = scan.nextLine().trim().toLowerCase();
            return input;
        }
        
        /**
         * 
         * here are your moves 
         */
        
        public double attack() {
            
            monstHealth = monstHealth -= (str / 4); 
            if (monstdef> str) {
                prnt("Monster is too strong!");
            }
            return monstHealth;
        }
        
        public double defend() {
            health = health += 12;
            monstHealth = monstHealth += 4;
            return health + monstHealth;
        }
        
        public void useSmlHealth() {
            smlHealth();
        }
        public void useMedHealth() {
            medHealth();
        }
        public void useLrgHealth() {
            lrgHealth();
        }
    }
    items
    Java Code:
    package com.bluemain;
    
    import static com.bluemain.Adventure.*;
    
    public class items {
            
        /**
         * 
         * here are the weapons.
         */
        
        public int sword () {
            str = str += 35;
            return str;
        }
        
        public int mace () {
            str = str += 30;
            return str;
        }
        
        public int stilleto () {
            str = str += 25;
            return str;
        }
        
        public int dagger () {
            str = str += 20;
            return str;
        }
        
        public int bowieKnife () {
            str = str += 15;
            return str;
        }
            
        /**
         * 
         * here are the items.
         */
        
        public double smlHealth () {
            health = health += 10;
            return health;
        }
        
        public double medHealth () {
            health = health += 25;
            return health;
        }
        
        public double lrgHealth () {
            health = health += 50;
            return health;
        }
    }

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: working with return values

    The class hierarchy doesn't make sense:
    Adventure extends character
    How is the Adventure class a type of character?

    character extends actionMoves
    How is a character a type of actionMoves?

    actionMoves extends items
    etc
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default Re: working with return values

    Yeah i totally agree they are out of order. I was focused on making everything non static and non void methods per my usual submission to the forums.

    Is this a step in the right direction though as far as overall structure of how things should be set up? Despite the fact they dont make sense currently.

    I managed to make everything non static minus the variables. Would this possibly be corrected once things are in a more proper order by placing the variables in the top class and the rest will.inherit from it?

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: working with return values

    everything non static minus the variables
    The variables should all be non static.

    The reasons for most the the classes to extend other classes doesn't make sense. Some of the classes should be used within other classes.
    For example a Character goes on some Adventures and perhaps uses some ActionMoves and some Items.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default Re: working with return values

    Okay. So maybe put character inside adventure, and put items inside actionMoves and have that extend my Adventure?

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: working with return values

    The design depends on what the program is trying to do. For me a person can have multiple adventures and use items to help.
    I don't see where any of the classes here should extend another. A type of adventure like mountain climbing could extend adventure.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default Re: working with return values

    Hmm well maybe the name Adventure is misleading.

    It started as an idea as a text based adventure but instead i figured out how to set up mock battles and that was fun so i am mainly focused on that.

    I want to develop the character more by adding exp and level which increase str and def. And i want to figure out some ways to apply the values of characters str and def vs monststr monstdef and have that factor into damage.

    I also want to add a bunch of monsters to cycle through. I want to add multiple attacks that will happen when the monster gets to specific health but not a specific attack but maybe a chance of several different ones.

    I am not so much worried about the in betweens aka adventures. I should have changed the title i am sorry.

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: working with return values

    My suggestions are to remove static from the variables
    and don't use extends unless the sub class is a type of the super class: A Cat is an Animal; A Car is a Vehicle; An Apple is a Fruit.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default Re: working with return values

    alright Ive re done it into 2 classes, Character and CharacterBattle and removed all the static from everything. I've completely re-done the monster boolean so its much more interchangeable. I used extends because otherwise it wouldn't let me use the boolean monst(); in characterBattle.

    now my question is, I want to be able to choose from any of my lvl 1 attacks when monstHealth is in certain range choose, and from any lvl 2 attacks when monstHealth is in certain range so on and so on.
    I tried adding to Double[] lvl1atk = {mauled1(),claw1(),slash1(),bite1()}; and everytime i started the program it displayed all of thier prompts like they had been used and then proceeded to go to the normal routine.

    How would I add these to a Array or whatever so I can randomly pick one of them?

    Java Code:
    /**
         * 
         * here are the monster moves.
         */
        
        double bite1 () {
            health = health -= (monststr);
            prnt("");
            prnt("You have been bitten.");
            return health;
        }
        double bite2 () {
            health = health -= (monststr * 2);
            prnt("");
            prnt("You have been bitten.");
            return health;
        }
        double bite3 () {
            health = health -= (monststr * 3);
            prnt("");
            prnt("You have been bitten.");
            return health;
        }
        double bite4 () {
            health = health -= (monststr * 4);
            prnt("");
            prnt("You have been bitten.");
            return health;
        }
        double bite5 () {
            health = health -= (monststr * 5);
            prnt("");
            prnt("You have been bitten.");
            return health;
        }
        double claw1 () {
            health = health -= (monststr);
            prnt("");
            prnt("You have been clawed.");
            return health;
        }
        double claw2 () {
            health = health -= (monststr * 2);
            prnt("");
            prnt("You have been clawed.");
            return health;
        }
        double claw3 () {
            health = health -= (monststr * 3);
            prnt("");
            prnt("You have been clawed.");
            return health;
        }
        double claw4 () {
            health = health -= (monststr * 4);
            prnt("");
            prnt("You have been clawed.");
            return health;
        }
        double claw5 () {
            health = health -= (monststr * 5);
            prnt("");
            prnt("You have been clawed.");
            return health;
        }
        double slash1 () {
            health = health -= (monststr);
            prnt("");
            prnt("You have been slashed.");
            return health;
        }
        double slash2 () {
            health = health -= (monststr * 2);
            prnt("");
            prnt("You have been slashed.");
            return health;
        }
        double slash3 () {
            health = health -= (monststr * 3);
            prnt("");
            prnt("You have been slashed.");
            return health;
        }
        double slash4 () {
            health = health -= (monststr * 4);
            prnt("");
            prnt("You have been slashed.");
            return health;
        }
        double slash5 () {
            health = health -= (monststr * 5);
            prnt("");
            prnt("You have been slashed.");
            return health;
        }
        double mauled1 () {
            health = health -= (monststr);
            prnt("");
            prnt("You have been mauled.");
            return health;
        }
        double mauled2 () {
            health = health -= (monststr * 2);
            prnt("");
            prnt("You have been mauled.");
            return health;
        }
        double mauled3 () {
            health = health -= (monststr * 3);
            prnt("");
            prnt("You have been mauled.");
            return health;
        }
        double mauled4 () {
            health = health -= (monststr * 4);
            prnt("");
            prnt("You have been mauled.");
            return health;
        }
        double mauled5 () {
            health = health -= (monststr * 5);
            prnt("");
            prnt("You have been mauled.");
            return health;
        }

    Here is the entire code how I have re-done it this time.

    Java Code:
    package com.bluemain;
    
    import java.util.*;
    import java.util.stream.DoubleStream;
    
    public class character {
              
        int score = 0, str = 5, def = 5, smlhlth = 0, medhlth = 0, lrghlth = 0;
        int monststr, monstdef;
        double health = 100.0, exp = 0.0, monstHealth = 50, gc = 0.0;
        Scanner scan = new Scanner(System.in);
        String charName, wpnChoice;
        String[] monstName = {"Zombie","Were-wolf","Vampire"};
        
        /**
         * 
         * here are the utils. 
         */
        
        void prnt(String s1) {
            System.out.println(s1);
        }
        String input() {
            String input = scan.nextLine().trim().toLowerCase();
            return input;
        }
         /**
         * 
         * here are your moves 
         */
        
        double attack() {
            
            monstHealth = monstHealth -= (str / 3)-(monstdef / 2); 
            if (monstdef> str) {
                prnt("Monster is too strong!");
            }
            return monstHealth;
        }
        
        double defend() {
            health = health += 12;
            monstHealth = monstHealth += 4;
            return health + monstHealth;
        }
        
        void useSmlHealth() {
            smlHealth();
        }
        void useMedHealth() {
            medHealth();
        }
        void useLrgHealth() {
            lrgHealth();
        }
        
        /**
         * 
         * here are the monster moves.
         */
        
        double bite1 () {
            health = health -= (monststr);
            prnt("");
            prnt("You have been bitten.");
            return health;
        }
        double bite2 () {
            health = health -= (monststr * 2);
            prnt("");
            prnt("You have been bitten.");
            return health;
        }
        double bite3 () {
            health = health -= (monststr * 3);
            prnt("");
            prnt("You have been bitten.");
            return health;
        }
        double bite4 () {
            health = health -= (monststr * 4);
            prnt("");
            prnt("You have been bitten.");
            return health;
        }
        double bite5 () {
            health = health -= (monststr * 5);
            prnt("");
            prnt("You have been bitten.");
            return health;
        }
        double claw1 () {
            health = health -= (monststr);
            prnt("");
            prnt("You have been clawed.");
            return health;
        }
        double claw2 () {
            health = health -= (monststr * 2);
            prnt("");
            prnt("You have been clawed.");
            return health;
        }
        double claw3 () {
            health = health -= (monststr * 3);
            prnt("");
            prnt("You have been clawed.");
            return health;
        }
        double claw4 () {
            health = health -= (monststr * 4);
            prnt("");
            prnt("You have been clawed.");
            return health;
        }
        double claw5 () {
            health = health -= (monststr * 5);
            prnt("");
            prnt("You have been clawed.");
            return health;
        }
        double slash1 () {
            health = health -= (monststr);
            prnt("");
            prnt("You have been slashed.");
            return health;
        }
        double slash2 () {
            health = health -= (monststr * 2);
            prnt("");
            prnt("You have been slashed.");
            return health;
        }
        double slash3 () {
            health = health -= (monststr * 3);
            prnt("");
            prnt("You have been slashed.");
            return health;
        }
        double slash4 () {
            health = health -= (monststr * 4);
            prnt("");
            prnt("You have been slashed.");
            return health;
        }
        double slash5 () {
            health = health -= (monststr * 5);
            prnt("");
            prnt("You have been slashed.");
            return health;
        }
        double mauled1 () {
            health = health -= (monststr);
            prnt("");
            prnt("You have been mauled.");
            return health;
        }
        double mauled2 () {
            health = health -= (monststr * 2);
            prnt("");
            prnt("You have been mauled.");
            return health;
        }
        double mauled3 () {
            health = health -= (monststr * 3);
            prnt("");
            prnt("You have been mauled.");
            return health;
        }
        double mauled4 () {
            health = health -= (monststr * 4);
            prnt("");
            prnt("You have been mauled.");
            return health;
        }
        double mauled5 () {
            health = health -= (monststr * 5);
            prnt("");
            prnt("You have been mauled.");
            return health;
        }
        
         /**
         * 
         * here are the items.
         */
        
        double smlHealth () {
            health = health += 10;
            return health;
        }
        
        double medHealth () {
            health = health += 25;
            return health;
        }
        
        double lrgHealth () {
            health = health += 50;
            return health;
        }
        double char1gc(double gcDif) {
            gc = gc + gcDif;
            return gc;
        }
        /**
         * Here is the monster set up.
         * 
         */
        
        boolean monst (int Mstr,int Mdef,double Mhlth,String s1,int Mlvl) {
            monststr = (Mstr * Mlvl);
            monstdef = (Mdef * Mlvl);
            monstHealth = (Mhlth + (Mlvl * 10) );
            do {
                prnt("");
                prnt("You have encountered a "+s1+".");
                prnt("The "+s1+"'s health is: "+monstHealth);
                prnt("Your health is: "+health);
                prnt("What action do you choose?");
                prnt("Fight.");
                prnt("Defend.");
                prnt("sml Health: "+smlhlth+" med Health: "+medhlth+" lrg Health: "+lrghlth);
                 String pick = input();
                 //Actions client chooses
                if (pick.equalsIgnoreCase("fight")) {
                    attack();
                }
                if (pick.equalsIgnoreCase("defend")) {
                    if (health <= 100) {
                    defend();
                    } else if (health >= 100){
                        prnt("Your health is at max.");
                    }
                }
                if (pick.equalsIgnoreCase("sml health")) {
                    if (smlhlth > 0){
                    useSmlHealth();
                    smlhlth--;
                    } else {
                        prnt("You do not have any potions.");
                    }
                }
                if (pick.equalsIgnoreCase("med health")) {
                    if (medhlth > 0) {
                    useMedHealth();
                    medhlth--;
                    } else {
                        prnt("You do not have any potions.");
                    }
                }
                if (pick.equalsIgnoreCase("lrg health")) {
                    if (lrghlth > 0) {
                    useLrgHealth();
                    lrghlth--;
                    } else {
                        prnt("You do not have any potions.");
                    }
                }
                //Attack conditions.
                
                if (monstHealth <= 125 && monstHealth >= (115)) {
                    mauled2();
                }
                if (monstHealth <= 85 && monstHealth >= (80)) {
                    mauled1();
                }
                if (monstHealth <= 70 && monstHealth >= (64)) {
                    claw2();
                }
                if (monstHealth <= 52 && monstHealth >= (49)) {
                    bite2();
                }
                if (monstHealth <= 25 && monstHealth >= (15)) {
                    bite1();
                }            
                //end battle conditions.
                if (monstHealth <= 0) {
                    prnt("You defeated the "+s1);
                    if (health >= 75) {
                        char1gc(20);
                    }
                    if (health >=50 && health <= 74) {
                        char1gc(15);
                    }
                    if (health < 50) {
                        char1gc(10);
                    }
                    return true;
                }
                if (health <= 0) {
                    return true;
                }
            } while (monstHealth > 0);
            return false;
        }
    }
    characterbattle
    Java Code:
    package com.bluemain;
    
    public final class CharacterBattle extends character{
            
         /**
         * 
         * here are the utilities. 
         */
            
        public void prnt(String s1) {
            System.out.println(s1);
        }
        public String input() {
            String input = scan.nextLine().trim().toLowerCase();
            return input;
        }
        
        public CharacterBattle() {
            
            prnt("Hero there is trouble afoot, what is your name?");
            char1build();
            wpnChoice();
            char1stats();
            prnt("");
            prnt("Theres a monster up ahead lets go investigate.");
            do {
                boolean aa = monst(5,5,80,monstName[0],1);
                if (aa) {
                    gotoShop();
                    boolean b = monst(8,8,90,monstName[1],1);
                    if (b) {
                        gotoShop();
                        boolean c = monst(10,10,110,monstName[2],1);
                        gotoShop();
                    }
            }
        }   while (health > 0);
            if (health <= 0) {
                prnt("You have been defeated.");
            }
    }
        
        public static void main(String[] args) {        
           new CharacterBattle();     
        }
        
        /**
         * 
         * here is the character set up. 
         * @return true when char name entered
         */
    
        public boolean char1build () {
            do{            
            prnt("Please enter your characters name:");
                charName = input();
            }
            while (charName.isEmpty());
            if (!charName.isEmpty()) {
                return true;
            }       
            return false;
        }
        
        /**
         * 
         * here you are prompted to choose a weapon. 
         * @return when weapon choice is made
         */
        public boolean wpnChoice () {
            do {            
            prnt("Please choose your weapon:");
            prnt("Sword, Mace, Stilleto, Dagger, or Bowie Knife.");
            wpnChoice = input();
            if (wpnChoice.equalsIgnoreCase("sword")) {
                sword();
            }
            if (wpnChoice.equalsIgnoreCase("mace")) {
                mace();
            }
            if (wpnChoice.equalsIgnoreCase("stilleto")) {
                stilleto();
            }
            if (wpnChoice.equalsIgnoreCase("dagger")) {
                dagger();
            }
            if (wpnChoice.equalsIgnoreCase("bowie knife")) {
                bowieKnife();
            }
            } 
            while (wpnChoice.isEmpty());
            if (!wpnChoice.isEmpty()) {
                return true;
            } 
            return false;
        }
        
        /**
         * 
         * here are methods to affect various variables. 
         * @return 
         */
        public double char1hlth() {
            return health;
        }
        public int char1str() {
            return str;
        }
        public int char1def() {
            return def;
        }
        public double char1gc() {
            return gc;
        }
        public double char1hlth(double healthDif) {
            health = health + healthDif;
            return health;
        }
        public int char1str(int strDif) {
            str = str + strDif;
            return str;
        }
        public int char1def(int defDif) {
            def = def + defDif;
            return def;
        }
        public double char1exp(double expDif) {
            exp = exp + expDif;
            return exp;
        }
        
        /**
         * 
         * this return characters stats.
         */
        public void char1stats() {
            double health = char1hlth();
            int def1 = char1def();
            int str1 = char1str();
            double gc1 = char1gc();
            prnt(charName+"'s health is: "+health+" str: "+str1+" def: "+def1+" gold coins: "+gc1);
        }
        
    
         /**
         * 
         * here are the weapons.
         */
        
        public int sword () {
            str = str += 35;
            return str;
        }
        
        public int mace () {
            str = str += 30;
            return str;
        }
        
        public int stilleto () {
            str = str += 25;
            return str;
        }
        
        public int dagger () {
            str = str += 20;
            return str;
        }
        
        public int bowieKnife () {
            str = str += 15;
            return str;
        }
            
    
          public boolean shop () {
                do {
                prnt("Welcome to the shop.");
                prnt("You have "+gc+" gold coins.");
                if (gc >= 5) {
                prnt("sml Health: +10, 5gc");
                }
                if (gc >= 10) {
                prnt("med Health: +25, 10gc");
                }
                if (gc >= 15) {
                prnt("lrg Health: +50, 15gc");
                }
                prnt("Exit.");
                prnt("Please enter a choice.");
                String a = input();
                if (a.equalsIgnoreCase("sml health")) {
                    smlhlth++;
                    gc = gc -= 5;
                }
                if (a.equalsIgnoreCase("med health")) {
                    medhlth++;
                    gc = gc -= 10;
                }
                if (a.equalsIgnoreCase("lrg health")) {
                    lrghlth++;
                    gc = gc -= 15;
                }
                if (a.equalsIgnoreCase("exit")) {
                    return true;
                }
                }while (gc>=0);
            return false;
        }
        
        public static boolean intro () {
            
            return false;
        }
        
        public boolean gotoShop () {
            int i = 1;
            
            do {
                prnt("");
                prnt("Great job defeating that monster.");
                prnt("Do you need to visit the shop?");
                prnt("yes or no?");
            String a = input();
                if (a.equalsIgnoreCase("yes")) {
                shop();
                i = 0;
            }
                if (a.equalsIgnoreCase("no")) {
                prnt("Alright, good luck hero.");
                return true;
            }
                if (!a.equalsIgnoreCase("yes") || !a.equalsIgnoreCase("no")) {
                gotoShop();
            }
            return false;
        } while (i != 0);
        }   
    }
    Last edited by Blue2525989; 11-20-2016 at 02:24 AM.

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: working with return values

    Did you consider having a single method like this with
    a parameter?

    Java Code:
    double bite (int power) {        
    health = health -= (monststr * power);
            prnt("");
            prnt("You have been bitten.");
            return health;
        }
    instead of a separate method for each bite?

    Java Code:
    double bite2 () {  
         health = health -= (monststr * 2);
         prnt("");
         prnt("You have been bitten.");
         return health;
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  16. #16
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default Re: working with return values

    I mean I could do that and use a Random number 1 through whatever for power, But I was wanting to have a chance to do a different attack at the same monstHealth increment if you were to play it a second time. That is why I figured I would make predefined ones that I could group together and then randomly choose from.

  17. #17
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default Re: working with return values

    yeah I guess I will try that for now until I figure out how to switch them how I want. Maybe I can just use interchangeable names of the move as well as damage that way it can have some variety.

    here is what you suggested jim plus a random number method to spice it up.

    Java Code:
       double mauled () {
            health = health -= (monststr * atkmultiplier());
            prnt("");
            prnt("You have been mauled.");
            return health;
        }
    
        double atkmultiplier () {
            double atkmultiplier = Math.random();
            double d = atkmultiplier * 6;
            int atkmult = (int) d;
            return atkmult;
        }

Similar Threads

  1. Replies: 7
    Last Post: 04-02-2014, 03:15 PM
  2. How to return all values from a for loop?
    By ilovemysillybanana in forum New To Java
    Replies: 1
    Last Post: 04-12-2013, 05:21 PM
  3. Replies: 0
    Last Post: 11-03-2010, 05:35 PM
  4. Using functions that return values?
    By Megapixelz in forum New To Java
    Replies: 1
    Last Post: 04-30-2008, 05:07 AM
  5. how to return values from hashmap
    By oregon in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 05:56 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
  •