Page 1 of 2 12 LastLast
Results 1 to 20 of 40
Like Tree1Likes

Thread: Help please

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

    Default Help please

    Hello again all, So I have been working on making a visual version of my last text game character battle.

    I have everything working right except when I go to add battleArena(); in about line 218 it overrides charSetUp(); in line 217.

    I can have one or the other it seems. Either set up char and pick weapon or the battle arena.

    Also this is a rough draft so its not the most orderly yet.

    Java Code:
    package com.bluemain;
    
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import static javax.swing.JFrame.EXIT_ON_CLOSE;
    
    public class MonsterBattle extends JFrame implements ActionListener{
         /**
         * the variables.
         */
        
        double health = 100, monstHealth = 50, gc = 0;
        int str = 5, def = 5, monstStr = 5, monstDef = 5, killCount = 0;
        String charName, choice, wpnChoice;
        boolean charNameSet, wpnChoiceSet, monsterIsDead;
        String[] monstName = {"Zombie","Ware-wolf","Vampire","Gargoyle","Succubus",
        "Inncubus"};
        
        /**
         *All the gui components.
         */
        
    //main window components
        JPanel pane1 = new JPanel();
        JLabel banner = new JLabel("Monster Battle");
        
        JPanel pane2 = new JPanel();
        JLabel header1 = new JLabel("Try and defeat the monster!");
    
        JPanel pane3 = new JPanel();
        JTextArea text1 = new JTextArea("",75,35);
        
        JScrollPane pane3scroll = new JScrollPane(pane3);
        
        JPanel pane4 = new JPanel();
        JTextArea text2 = new JTextArea("",2,10);
        
        JPanel pane5 = new JPanel();
        JButton b1 = new JButton("Enter");
        JButton b2 = new JButton("Show Commands");
        JButton b5 = new JButton("Open Shop");
        
        JPanel pane6 = new JPanel();
        JLabel hlthDisplay = new JLabel("");
        JLabel manaDisplay = new JLabel("");
        JLabel smlhlthDisplay = new JLabel("");
        JLabel medhlthDisplay = new JLabel("");
        JLabel lrghlthDisplay = new JLabel("");
        
        //action window components
        JFrame cmd = new JFrame("Command list:");
        JPanel pane7 = new JPanel();
        JLabel header2 = new JLabel("Action Commands.");
     
        JPanel pane8 = new JPanel();
        JTextArea text3 = new JTextArea("",30,25);
        JScrollPane pane8scroll = new JScrollPane(pane8);
        JPanel pane9 = new JPanel();
        JButton b3 = new JButton("Close Commands.");
        
        //shop window components
        JFrame shop = new JFrame("Shop:");
        JPanel pane10 = new JPanel();
        JLabel header3 = new JLabel("Shop.");
       
        JPanel pane11 = new JPanel();
        JLabel smlhlth = new JLabel("Small Health Potions.");
        JLabel medhlth = new JLabel("Medium Health Potions.");
        JLabel lrghlth = new JLabel("Large Health Potions.");
        
        JPanel pane12 = new JPanel();
        JTextArea text4 = new JTextArea("",2,10);
        JButton b4 = new JButton("Close Shop.");
        
        JPanel pane13 = new JPanel();
        JLabel gcDisplay = new JLabel("Gold coins: ");
        
                 /**
                 * main window gui.
                 */
        
                public void MonsterBattleGui () {
                
            pane1.add(banner); 
            banner.setForeground(Color.RED);
            pane1.setBackground(Color.DARK_GRAY);
            pane2.add(header1);   
            header1.setForeground(Color.RED);
            pane2.setBackground(Color.DARK_GRAY);
            pane3.add(new JScrollPane(text1));
            pane3.setBackground(Color.DARK_GRAY);
            text1.setLineWrap(true);
            text1.setEditable(true);
            text1.setWrapStyleWord(true);
            pane4.add(text2);     
            pane4.setBackground(Color.DARK_GRAY);
            text2.setLineWrap(true);
            text2.setEditable(true);
            b1.addActionListener(this);
            b2.addActionListener(this);
            b5.addActionListener(this);
            pane5.add(b1);
            pane5.add(b2);        
            pane5.add(b5);
            pane5.setBackground(Color.DARK_GRAY);
            pane6.add(hlthDisplay);
            pane6.add(manaDisplay);
            pane6.add(smlhlthDisplay);
            pane6.add(medhlthDisplay);
            pane6.add(lrghlthDisplay);
            pane6.setBackground(Color.DARK_GRAY);
            
            
            JFrame main = new JFrame("Monster Battle");
            GridLayout grd1 = new GridLayout(6,1);
            main.setLayout(grd1);
            main.add(pane1);
            main.add(pane2);
            main.add(pane3scroll);
            main.add(pane4);
            main.add(pane5);
            main.add(pane6);
            
            main.setSize(500,500);
            main.setDefaultCloseOperation(EXIT_ON_CLOSE);
            main.setVisible(true);
            
            /**
             * command list gui.
             */
            
            pane7.add(header2);
            pane8.add(text3);
            text3.setLineWrap(true);
            pane9.add(b3);
            b3.addActionListener(this);
            
            GridLayout grd2 = new GridLayout(3,1);
            cmd.setLayout(grd2);
            cmd.add(pane7);
            cmd.add(pane8scroll);
            cmd.add(pane9);
            
            cmd.setSize(350,350);
            cmd.setDefaultCloseOperation(HIDE_ON_CLOSE);
            cmd.setLocation(500,0);
           
            /**
             * shop gui.
             */
            GridLayout grd3 = new GridLayout(1,2);
            
            pane10.add(header3);
            pane11.setLayout(grd2);
            pane11.add(smlhlth);
            pane11.add(medhlth);
            pane11.add(lrghlth);
            pane12.add(text4);
            pane12.add(b4);
            b4.addActionListener(this);
            pane13.add(gcDisplay);
            
            GridLayout grd4 = new GridLayout(4,1);
            shop.setLayout(grd4);
            shop.add(pane10);
            shop.add(pane11);
            shop.add(pane12);
            shop.add(pane13);
            
            shop.setSize(350,350);
            shop.setDefaultCloseOperation(HIDE_ON_CLOSE);
            shop.setVisible(false);
            shop.setLocation(850,0);
            }
    
         /**
         * some general utilities.
         */
        
        void setText1(String s1) {
            text1.setText(s1);
        }
        void setText2(String s1) {
            text2.setText(s1); 
        }
        void setText3(String s1) {
            text3.setText(s1);
        }
        void setText4(String s1) {
            text4.setText(s1);
        }
        String getText2() {
            String actionCmd = text2.getText().trim();
            return actionCmd;
        }
        String getText4() {
            String actionCmd = text4.getText().trim();
            return actionCmd;
        }
        double setHealthMax(double healthDiff) {
            health = health += healthDiff;
            return health;
        }
        double gcDiff(double gcDiff) {
            gc = gc += gcDiff;
            return gc;
        }
        
        /**
         * Set up  
         */
        
        public MonsterBattle () {
           MonsterBattleGui();        
           charSetUp();
           battleArena();
        }
        
        /**
         * action command set up
         * @param event 
         */
                public void actionPerformed (ActionEvent event) {
                String command = event.getActionCommand();
                if (command.equals("Enter Name")) {
                    charName = getText2();                
                    if (charName.length() > 0) {
                        weaponChoice();
                    } else {
                        setText2("Please enter a selection.");
                    }
                    
                }
                if (command.equals("Weapon choice")) {
                    wpnChoice = getText2();
                    if (wpnChoice.equalsIgnoreCase("sword")) {
                        sword();
                    }
                    if (wpnChoice.equalsIgnoreCase("mace")) {
                        mace();
                    }
                    if (wpnChoice.equalsIgnoreCase("dagger")) {
                        dagger();
                    }
                    if (wpnChoice.equalsIgnoreCase("stilleto")) {
                        stilleto();
                    }
                }
                
                if (command.equals("Enter")) {
                    choice = getText2();
                    if (choice.equalsIgnoreCase("fight")) {
                        attack();
                    }
                    if (choice.equalsIgnoreCase("defend")) {
                        defend();
                    }
                    if (choice.equalsIgnoreCase("done")) {
                        battleArena();
                    }
                }
                if (command.equals("Next Monster")) {
                    choice = getText2();
                    
                }
                if (event.getSource() == b2) {
                    cmd.setVisible(true);
                }
                if (event.getSource() == b3) {
                    cmd.setVisible(false);
                }
                if (event.getSource() == b4) {
                    shop.setVisible(false);
                }
                if (event.getSource() == b5) {
                    shop.setVisible(true);
                }
            }
                
                /**
                 * Main method.
                 * 
                 */
        
        public static void main(String[] args) {
            new MonsterBattle();
            
        }
        
        /**
         * here is monster battle arena
         */
        
        public void battleArena() {
            boolean a = testMonst(monstName[0],1,25);
            if(a) {
                inbetween();
                boolean b = testMonst(monstName[1],2,25);
                if (b) {
                    inbetween();
                    boolean c = testMonst(monstName[2],2,50);
                }
            }
        }
        
         /**
         * Here is where the character is set up.
         */
        
        
        void charSetUp() {
            text1.setText("Welcome Hero,\n"+
                        "You are about to embark on an epic battle.\n"+
                        "Fearsome monsters lay ahead of you.\n"+
                        "What is your name?");
            b1.setText("Enter Name");
                    
        }
           
         /**
         * 
         * here are the weapons.
         */
        
        public int sword () {
            str = str += 35;
            wpnChoiceSet = true;
            return str;
        }
        
        public int mace () {
            str = str += 30;
            wpnChoiceSet = true;
            return str;
        }
        
        public int stilleto () {
            str = str += 25;
            wpnChoiceSet = true;
            return str;
        }
        
        public int dagger () {
            str = str += 20;
            wpnChoiceSet = true;
            return str;
        }
             /**
         * 
         * here are your moves 
         */
        
        double attack() {        
            monstHealth = monstHealth -= str; 
            
            return monstHealth;
        }    
        double defend() {
            health = health += 16;
            monstHealth = monstHealth += 3;
            return health + monstHealth;
        }
       
        /**
         * Here is weapon choice.
         */
        
        boolean weaponChoice () {
            do {
            text1.setText("Welcome "+charName+" lets get to it.\n"+
                    "Please select a weapon to make it through\n"+
                    "this terrible battle ahead of you.\n"+
                    "Sword, Mace, Dagger, Stilleto.");
            b1.setText("Weapon choice");
            setText2("");
            if (wpnChoice.length() > 0) {
                return true;
            }
                } while (health > 0);
            return false;
        }
        
        /**
         * Here is the monster
         * 
         */
        
        boolean testMonst (String Mname, int MhealthDiff, int Mhealth) {
            monstHealth = 50 + Mhealth;
            monstHealth = monstHealth * MhealthDiff;
            b5.setEnabled(false);
            do {
                setText1("You have encountered a "+Mname
                +"\nHe looks hungry..."
                +"\nHis health is: "+monstHealth
                +"\nWhat will you do?"
                +"\nEnter command below.");
                b1.setText("Enter");
                if (monstHealth <= 0) {
                    return true;
                }
            } while (health > 0);
            return false;
        }
        
        /**
         * here is the inbetween battle prompt
         */
        
        void inbetween() {
            setText1("You have defeated the monster, good work.\nWhat is your next move?\n"
                    + "Will you goto the shop and refresh potions, or battle the next monster?");
            b5.setEnabled(true);
            setText2("");
        }
    }

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

    Default Re: Help please

    working right except when I go to add battleArena(); in about line 218 it overrides charSetUp(); in line 217.
    Please explain what is not "working right" means. What does the code do?
    What do you want the code to do differently.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Help please

    Well if i comment out the battlearena then it does charSetUp which leads into weaponChoice. Which should then lead into battlearena.

    However when i add battlearena after charSetUp it seems to skip the first 2 parts which worked initially and goes straight to battling the monster.

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

    Default Re: Help please

    if i comment out the battlearena then it does charSetUp which leads into weaponChoice
    The computer executes the statements that are in the program. If a statement is commented out then that statement is not executed.

    Can you explain what the problem is? What do you see that needs to be changed?

    it seems to skip the first 2 parts
    Please explain. How do you know it skips anything? To see what methods are executing, add print statements to all the methods that print out a message when they are executed. The print out will show what methods are executing and in what order.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Help please

    This isn't related to the question but usually, the less code you have, the easier it is to debug. When you do things
    like this:
    Java Code:
    /**     * 
         * here are the weapons.
         */
         
        public int sword () {
            str = str += 35;
            wpnChoiceSet = true;
            return str;
        }
         
        public int mace () {
            str = str += 30;
            wpnChoiceSet = true;
            return str;
        }
         
        public int stilleto () {
            str = str += 25;
            wpnChoiceSet = true;
            return str;
        }
         
        public int dagger () {
            str = str += 20;
            wpnChoiceSet = true;
            return str;
        }
    Think of eliminating repeated code. So you could do it like this.
    Java Code:
    public int sword () {
        return bumpStrength(35);
        }
         
        public int mace () {
        return bumpStrength(30);
        }
         
        public int stilleto () {
        return bumpStrength(25);
        }
         
        public int dagger () {
        return bumpStrength(20);
    }
    
    private int bumpStrength(int strength) {
            str += strength;
            wpnChoiceSet = true;
            return str;
    }
    Also, the following is incorrect.
    Java Code:
    str = str += 20;
    Just do it like this
    Java Code:
    str += 20;
    I assume the previous one compiled but I haven't tried it.

    You might even consider writing a weapon class.


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

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

    Default Re: Help please

    Thats what i mean. Char set up and weapon choice work and function until i add battlearena afterwords. Then it skips charsetup and weapo choice and goes straight to battle arena.

  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: Help please

    Then it skips charsetup and weapo choice and goes straight to battle arena.
    Did you add print statements to the methods to see it any of them were being skipped?

    Does this code show what you are talking about? Replace the constructor with this:
    Java Code:
        public MonsterBattle () {
           MonsterBattleGui();    
           try{Thread.sleep(3000);}catch(Exception x) {};    //<<<<<<<< for testing
           charSetUp();
           try{Thread.sleep(3000);}catch(Exception x) {};
           battleArena();
        }
    Last edited by Norm; 11-28-2016 at 05:26 PM.
    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: Help please

    Ok ill try that when i get home from work.

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

    Default Re: Help please

    So luckily it rained today and I got off work early so I could come home and code for free :)

    Anyways I tried what you said Norm and it worked. I guess they are all loading at once and the monster boolean is the last one read so it shows instead of the charSetUp or weaponChoice. At least thats my thoughts on whats happening.

    So I tweaked it a little so its more to what I need, but now I am wondering if once I have my name selected and weapon selected (and thats now when I hit the Thread.sleep(); ) can I make it possible so that when I have my weapon selected I can click the button and it cancels the Thread.sleep() that is currently being invoked?
    and proceed to battleArena or do I have to wait that out until its done?

    here is how I changed it to work for me.

    Java Code:
        public MonsterBattle () {
            MonsterBattleGui();
            charSetUp();
            try{Thread.sleep(15000);}catch(Exception x) {};
            battleArena();
        }
    Last edited by Blue2525989; 11-28-2016 at 07:31 PM.

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

    Default Re: Help please

    and it worked
    My sample code for testing was NOT a solution, It was to demonstrate that delaying the calls to the other methods would change the timing for what was shown in the GUI. The real solution involves displaying one state and then having an event sent to the program that will change the display. The event could be from a button press by a user or a Timer event send to a listener to change the state/display of the GUI.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Help please

    Well, when I ran you code, it provided a gui which prompted me for my name. Then when I hit enter, it started
    the battle. Unfortunately, it throws an NPE because wpnChoice is null.

    @OP are you sure you are running the posted code?

    And you might want to set the default exit setting on the JFrame. Otherwise you can't close it.

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

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

    Default Re: Help please

    Ahh i see. Well i think i will try the event being sent approach.

    And by now it is not the same anymore. But I dont recollect having that same issue when i posted that code. Those drive me crazy and i try to weed those out before posting on here. Ill post my updated code later on today, i had to run out to the store for a moment.

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

    Default Re: Help please

    Okay I am back and here is what I am working with as of now.

    I use netbeans and it shows no NPE being thrown with this version.

    This is how I would want it to operate at this point. This version uses the Thread.sleep();

    So I am going to work with changing charSetUp to a return value as well as weaponChoice so I can eliminate the Thread.sleep();

    I had previously had them as boolean values and tried returning them as true when the strings length() was > 0, that didnt work, and then tried when !string.isEmpty() , that did not work either. Lastly I tried putting a personal ActionListener act = (event) ->{}; but it kept saying bad return value inside the lambda statement.

    Java Code:
    package com.bluemain;
    
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import static javax.swing.JFrame.EXIT_ON_CLOSE;
    
    public class MonsterBattle extends JFrame implements ActionListener{
         /**
         * the variables.
         */
        
        double health = 100, mana = 0, monstHealth = 50, gc = 0;
        int str = 5, def = 5, monstStr = 5, monstDef = 5, killCount = 0;
        int smlhlth2 = 0, medhlth2 = 0, lrghlth2 = 0;
        String charName, choice, wpnChoice = "";
        boolean charNameSet, wpnChoiceSet, monsterIsDead;
        String[] monstName = {"Zombie","Ware-wolf","Vampire","Gargoyle","Succubus",
        "Inncubus"};
        
        /**
         *All the gui components.
         */
        
    //main window components
        JPanel pane1 = new JPanel();
        JLabel banner = new JLabel("Monster Battle");
        
        JPanel pane2 = new JPanel();
        JLabel header1 = new JLabel("Try and defeat the monster!");
    
        JPanel pane3 = new JPanel();
        JTextArea text1 = new JTextArea("",75,35);
        
        JScrollPane pane3scroll = new JScrollPane(pane3);
        
        JPanel pane4 = new JPanel();
        JTextArea text2 = new JTextArea("",2,10);
        
        JPanel pane5 = new JPanel();
        JButton b1 = new JButton("Enter");
        JButton b2 = new JButton("Show Commands");
        JButton b5 = new JButton("Open Shop");
        
        JPanel pane6 = new JPanel();
        JLabel hlthDisplay = new JLabel("Health: "+health);
        JLabel manaDisplay = new JLabel("Mana: ");
        JLabel smlhlthDisplay = new JLabel("sml Health: "+smlhlth2);
        JLabel medhlthDisplay = new JLabel("med Health: "+medhlth2);
        JLabel lrghlthDisplay = new JLabel("lrg Health: "+lrghlth2);
        
        //action window components
        JFrame cmd = new JFrame("Command list:");
        JPanel pane7 = new JPanel();
        JLabel header2 = new JLabel("Action Commands.");
     
        JPanel pane8 = new JPanel();
        JTextArea text3 = new JTextArea("",30,25);
        JScrollPane pane8scroll = new JScrollPane(pane8);
        JPanel pane9 = new JPanel();
        JButton b3 = new JButton("Close Commands.");
        
        //shop window components
        JFrame shop = new JFrame("Shop:");
        JPanel pane10 = new JPanel();
        JLabel header3 = new JLabel("Shop.");
       
        JPanel pane11 = new JPanel();
        JLabel smlhlth = new JLabel("Small Health Potions.");
        JLabel medhlth = new JLabel("Medium Health Potions.");
        JLabel lrghlth = new JLabel("Large Health Potions.");
        
        JPanel pane12 = new JPanel();
        JTextArea text4 = new JTextArea("",2,10);
        JButton b4 = new JButton("Close Shop.");
        
        JPanel pane13 = new JPanel();
        JLabel gcDisplay = new JLabel("Gold coins: ");
        
                 /**
                 * main window gui.
                 */
        
                public void MonsterBattleGui () {
                
            pane1.add(banner); 
            banner.setForeground(Color.RED);
            pane1.setBackground(Color.DARK_GRAY);
            pane2.add(header1);   
            header1.setForeground(Color.RED);
            pane2.setBackground(Color.DARK_GRAY);
            pane3.add(new JScrollPane(text1));
            pane3.setBackground(Color.DARK_GRAY);
            text1.setLineWrap(true);
            text1.setEditable(true);
            text1.setWrapStyleWord(true);
            pane4.add(text2);     
            pane4.setBackground(Color.DARK_GRAY);
            text2.setLineWrap(true);
            text2.setEditable(true);
            b1.addActionListener(this);
            b2.addActionListener(this);
            b5.addActionListener(this);
            pane5.add(b1);
            pane5.add(b2);        
            pane5.add(b5);
            pane5.setBackground(Color.DARK_GRAY);
            pane6.add(hlthDisplay);
            hlthDisplay.setForeground(Color.RED);
            pane6.add(manaDisplay);
            manaDisplay.setForeground(Color.RED);
            pane6.add(smlhlthDisplay);
            smlhlthDisplay.setForeground(Color.RED);
            pane6.add(medhlthDisplay);
            medhlthDisplay.setForeground(Color.RED);
            pane6.add(lrghlthDisplay);
            lrghlthDisplay.setForeground(Color.RED);
            pane6.setBackground(Color.DARK_GRAY);
            
            
            JFrame main = new JFrame("Monster Battle");
            GridLayout grd1 = new GridLayout(6,1);
            main.setLayout(grd1);
            main.add(pane1);
            main.add(pane2);
            main.add(pane3scroll);
            main.add(pane4);
            main.add(pane5);
            main.add(pane6);
            
            main.setSize(500,500);
            main.setDefaultCloseOperation(EXIT_ON_CLOSE);
            main.setVisible(true);
            
            /**
             * command list gui.
             */
            
            pane7.add(header2);
            pane8.add(text3);
            text3.setLineWrap(true);
            pane9.add(b3);
            b3.addActionListener(this);
            
            GridLayout grd2 = new GridLayout(3,1);
            cmd.setLayout(grd2);
            cmd.add(pane7);
            cmd.add(pane8scroll);
            cmd.add(pane9);
            
            cmd.setSize(350,350);
            cmd.setDefaultCloseOperation(HIDE_ON_CLOSE);
            cmd.setLocation(500,0);
           
            /**
             * shop gui.
             */
            GridLayout grd3 = new GridLayout(1,2);
            
            pane10.add(header3);
            pane11.setLayout(grd2);
            pane11.add(smlhlth);
            pane11.add(medhlth);
            pane11.add(lrghlth);
            pane12.add(text4);
            pane12.add(b4);
            b4.addActionListener(this);
            pane13.add(gcDisplay);
            
            GridLayout grd4 = new GridLayout(4,1);
            shop.setLayout(grd4);
            shop.add(pane10);
            shop.add(pane11);
            shop.add(pane12);
            shop.add(pane13);
            
            shop.setSize(350,350);
            shop.setDefaultCloseOperation(HIDE_ON_CLOSE);
            shop.setVisible(false);
            shop.setLocation(850,0);
            }
    
         /**
         * some general utilities.
         */
        
        void setText1(String s1) {
            text1.setText(s1);
        }
        void setText2(String s1) {
            text2.setText(s1); 
        }
        void setText3(String s1) {
            text3.setText(s1);
        }
        void setText4(String s1) {
            text4.setText(s1);
        }
        String getText2() {
            String actionCmd = text2.getText().trim();
            return actionCmd;
        }
        String getText4() {
            String actionCmd = text4.getText().trim();
            return actionCmd;
        }
        double setHealthMax(double healthDiff) {
            health = health += healthDiff;
            return health;
        }
        double gcDiff(double gcDiff) {
            gc = gc += gcDiff;
            return gc;
        }
        
        /**
         * Set up  
         */
        
        public MonsterBattle () {
            MonsterBattleGui();
            charSetUp();
            try{Thread.sleep(15000);}catch(Exception x) {};
            battleArena();
        }
        
        /**
         * action command set up
         * @param event 
         */
                public void actionPerformed (ActionEvent event) {
                String command = event.getActionCommand();
                if (command.equals("Enter Name")) {
                    charName = getText2();                
                    if (charName.length() > 0) {
                        weaponChoice();
                    } else {
                        setText2("Please enter a selection.");
                    }
                    
                }
                if (command.equals("Weapon choice")) {
                    wpnChoice = getText2();
                    if (wpnChoice.equalsIgnoreCase("sword")) {
                        sword();
                    }
                    if (wpnChoice.equalsIgnoreCase("mace")) {
                        mace();
                    }
                    if (wpnChoice.equalsIgnoreCase("dagger")) {
                        dagger();
                    }
                    if (wpnChoice.equalsIgnoreCase("stilleto")) {
                        stilleto();
                    }
                }
                
                if (command.equals("Enter")) {
                    choice = getText2();
                    if (choice.equalsIgnoreCase("fight")) {
                        attack();
                    }
                    if (choice.equalsIgnoreCase("defend")) {
                        defend();
                    }
                    if (choice.equalsIgnoreCase("done")) {
                        battleArena();
                    }
                }
                if (command.equals("Next Monster")) {
                    choice = getText2();
                    
                }
                if (event.getSource() == b2) {
                    cmd.setVisible(true);
                }
                if (event.getSource() == b3) {
                    cmd.setVisible(false);
                }
                if (event.getSource() == b4) {
                    shop.setVisible(false);
                }
                if (event.getSource() == b5) {
                    shop.setVisible(true);
                }
            }
                
                /**
                 * Main method.
                 * 
                 */
        
        public static void main(String[] args) {
            new MonsterBattle();
            
        }
        
        /**
         * here is monster battle arena
         */
        
        public void battleArena() {
            boolean a = testMonst(monstName[0],1,25);
            if(a) {
                inbetween();
            try{Thread.sleep(20000);}catch(Exception x) {};
                boolean b = testMonst(monstName[1],2,25);
                if (b) {
                    inbetween();
            try{Thread.sleep(20000);}catch(Exception x) {};
                    boolean c = testMonst(monstName[2],2,50);
                }
            }
        }
        
         /**
         * Here is where the character is set up.
         */
        
        
        void charSetUp() {
            text1.setText("Welcome Hero,\n"+
                        "You are about to embark on an epic battle.\n"+
                        "Fearsome monsters lay ahead of you.\n"+
                        "What is your name?");
            b1.setText("Enter Name");
                    
        }
        
        /**
         * Here is weapon choice.
         */
        
        boolean weaponChoice () {
            text1.setText("Welcome "+charName+" lets get to it.\n"+
                    "Please select a weapon to make it through\n"+
                    "this terrible battle ahead of you.\n"+
                    "Sword, Mace, Dagger, Stilleto.");
            b1.setText("Weapon choice");
            setText2("");
            return false;
        }
        
        /**
         * Here is the monster
         * 
         */
        
        boolean testMonst (String Mname, int MhealthDiff, int Mhealth) {
            monstHealth = 50 + Mhealth;
            monstHealth = monstHealth * MhealthDiff;
            b5.setEnabled(false);
            do {
                setText1("You have encountered a "+Mname
                +"\nHe looks hungry..."
                +"\nHis health is: "+monstHealth
                +"\nYour health is: "+health
                +"\nWhat will you do?"
                +"\nEnter command below.");
                b1.setText("Enter");
                //monster attack conditions
                if (monstHealth < 275 && monstHealth > 280) {
                    bite();
                }
                if (monstHealth < 260 && monstHealth > 250) {
                    bite();
                }
                if (monstHealth < 200 && monstHealth > 190) {
                    bite();
                }
                if (monstHealth < 175 && monstHealth > 170) {
                    bite();
                }
                if (monstHealth < 80 && monstHealth > 78) {
                    bite();
                }
                if (monstHealth < 62 && monstHealth > 60) {
                    bite();
                }
                if (monstHealth < 40 && monstHealth > 35) {
                    bite();
                }
                if (monstHealth <= 0) {
                    return true;
                }
                if (health <= 0) {
                    setText1("You have been defeated.");
                }
            } while (health > 0);
            return false;
        }
        
        /**
         * here is the inbetween battle prompt
         */
        
        void inbetween() {
            setText1("You have defeated the monster, good work.\nWhat is your next move?\n"
                    + "Will you goto the shop and refresh potions, or battle the next monster?");
            b5.setEnabled(true);
            setText2("");
        }
         
         /**
         * 
         * here are the weapons.
         */
        
        public int sword () {
            str = str += 35;
            wpnChoiceSet = true;
            return str;
        }
        
        public int mace () {
            str = str += 30;
            wpnChoiceSet = true;
            return str;
        }
        
        public int stilleto () {
            str = str += 25;
            wpnChoiceSet = true;
            return str;
        }
        
        public int dagger () {
            str = str += 20;
            wpnChoiceSet = true;
            return str;
        }
             /**
         * 
         * here are your moves 
         */
        
        double attack() {        
            monstHealth = monstHealth -= str;         
            return monstHealth;
        }    
        double defend() {
            health = health += 16;
            monstHealth = monstHealth += 3;
            return health + monstHealth;
        }   
            
        /**
         * 
         * here are the monster moves.
         */
        
        double bite () {
            health = health -= (6 * atkmultiplier());
            return health;
        }
        double claw () {
            health = health -= (8 * atkmultiplier());
            return health;
        }
        double slash () {
            health = health -= (10 * atkmultiplier());
            return health;
        }
        
        double mauled () {
            health = health -= (12 * atkmultiplier());
            return health;
        }
    
        double atkmultiplier () {
            double atkmultiplier = Math.random();
            double d = atkmultiplier * 5;
            int atkmult = (int) d;
            return atkmult;
        }
        
    }
    Last edited by Blue2525989; 11-28-2016 at 10:38 PM.

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

    Default Re: Help please

    The Thread.sleep was NOT a suggested way to write the program. It was only to allow the showing of values before the next method replaced them with something else.
    The program needs to be redesigned.
    It should build and present a GUI and return to the JVM to wait for a user input.
    When the user enters data and presses a Button, the code's listener gets control, processes the user's input, displays new GUI and returns to the JVM
    When the user enters data and presses a Button, the code's listener gets control, processes the user's input, displays new GUI and returns to the JVM
    When the user enters data and presses a Button, the code's listener gets control, processes the user's input, displays new GUI and returns to the JVM
    When the user enters data and presses a Button, the code's listener gets control, processes the user's input, displays new GUI and returns to the JVM
    etc etc etc

    It's a kind of loop between processing a user's input and waiting for the user to enter more data
    Last edited by Norm; 11-28-2016 at 11:15 PM. Reason: spelling
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Help please

    yes I know I agree. Jim Had posted that the previous one was getting a NPE, this one wont.

    This one is a model of what I want but needs to have that Thread.sleep removed. I got my IDE open right now working on it.

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

    Default Re: Help please

    needs to have that Thread.sleep removed.
    That was what my post#14 was all about.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Help please

    Hmm thats hard to get. I am going to think on that one.

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Help please

    It's at this point you really might want to think about breaking up your code.
    That mass up there is clearly getting too entangled for your to keep track of everything that's going on.

    There's at least three chunks involved here. The GUI, which handles displaying and reacting to the user. The code that handles setting up the battle, and the battle itself. Finally the model, which is the users selections for the battle.

    The BattleManager (for want of a better name) will listen on events in the GUI (have it implement an interface for that) and set up the model in response.
    Its job is to ensure everything has been set up before the user can actually do the fight.
    jim829 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Help please

    Yeah i think that might help.

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

    Default Re: Help please

    When you repackage your code you might want to examine this section of code too.

    Java Code:
                if (monstHealth < 275 && monstHealth > 280) {
                    bite();
                }
                if (monstHealth < 260 && monstHealth > 250) {
                    bite();
                }
                if (monstHealth < 200 && monstHealth > 190) {
                    bite();
                }
                if (monstHealth < 175 && monstHealth > 170) {
                    bite();
                }
                if (monstHealth < 80 && monstHealth > 78) {
                    bite();
                }
                if (monstHealth < 62 && monstHealth > 60) {
                    bite();
                }
                if (monstHealth < 40 && monstHealth > 35) {
                    bite();
                }
    1. Your first condition will never be met (monstHealth can never
    be less than 275 and greater than 280 at the same time). And
    further down, if something is less that 62 and greater than 60 it
    must be equal to 61 (although I can understand wanting consistency).
    2. Since all your conditions are mutually exclusive of each other (bite() does
    not modify monstHealth), use the if/else clause (once you meet a condition,
    no sense in checking others).
    3. After you get the code working, you might want to replace this with some
    data structure that can iterate thru the values using a single if clause to do the test.
    It won't necessarily reduce the amount of code but it might be cleaner
    and less prone to errors if you decide to change or add more conditions.

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

Page 1 of 2 12 LastLast

Posting Permissions

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