Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: while loop in GUI game

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default while loop in GUI game

    I have managed to create a simple GUI for a game which was previously a console game. The console application starts by showing a list of available monsters and the properties of each monster. Each monsters has a unique number which the user can refer to and has a type, a name, health points (HP) and a weapon. There are different weapons with different behaviours: Sword (slashes and reduces HP with 10), Hammer (hits and reduces HP with 20), Knife (stabs and reduces HP with 15), Club (clubs and reduces HP with 30). After showing the list, the application asks the user to select an attacker by inputting the number of one of the monsters. It then asks the user to select a vistim by inputting the number of another monster. Then the monsters "fight", i.e. if a user has selected "Jules" as the attacker (by inputting "1") and Mia as the victim (by inputting "4"), the application shows the following: Jules (a goblin) slashes Mia (an orc) with a sword. Mia loses 10HP. After the result has been shown, the application shows the list of monsters again, but now with the values changed. The process is then repeated until every monster has 0 HP except one monster, which will then be the victor. The victor is shown and the application ends. However, if the user selects the same monster as attacker and as victim (i.e. if input 1 == input 2 in the code), I want the game to not allow a monsters to attack itself. In the console game I achieved this easily by simply breaking out of the loop by using the "continue" keyword. However, that doesn't work with the GUI. What code do I use for that? Here is my code for when the user clicks the "Play" button after selecting an attacker and a victim:
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
     
            //JList list = new JList(monsters);
            //list.setVisibleRowCount(4);
            //add(list);
     
            //showMonsters(monsters);
            while (true){
     
            String text1 = jTextField1.getText();
            int input1 = Integer.parseInt(text1);
            if (input1 <= 0 | input1 > monsters.length) {
                 JOptionPane.showMessageDialog(null,"Please enter a number between 1 and "
                + monsters.length + "!");
            }
     
            Monster attacker = monsters[input1-1];
     
            String text2 = jTextField2.getText();
            int input2 = Integer.parseInt(text2);
            if (input2 <= 0 | input2 > monsters.length){
                JOptionPane.showMessageDialog(null,"Please enter a number between 1 and "
                + monsters.length + "!", "Error Message", JOptionPane.ERROR_MESSAGE);
            }
     
            Monster victim = monsters[input2-1];
     
            if (input1 == input2) {
                JOptionPane.showMessageDialog(null,"A monster cannot attack itself!", "Error Message", JOptionPane.ERROR_MESSAGE);
            }
     
            if (attacker.healthPoints == 0) {
                JOptionPane.showMessageDialog(null,"A dead monster cannot attack another monster!", "Error Message", JOptionPane.ERROR_MESSAGE);
     
            }
     
    	if (victim.healthPoints == 0) {
                JOptionPane.showMessageDialog(null,"A monster cannot attack a monster which is already dead!", "Error Message", JOptionPane.ERROR_MESSAGE);
     
    	}
     
            int lostHealthPoints;
     
    	if (victim.healthPoints < attacker.w.effect) {
                lostHealthPoints = victim.healthPoints;
    	} else {
                lostHealthPoints = attacker.w.effect;
    	}
     
    	victim.healthPoints = victim.healthPoints - attacker.w.effect;
     
    	if (victim.healthPoints < 0) {
                victim.healthPoints = 0;
    	}
     
            showFight(attacker, victim, lostHealthPoints);
     
            if (determineWinner(monsters, attacker).equals(attacker.name)) {
                JOptionPane.showMessageDialog(null, determineWinner(monsters, attacker)
    				+ " is the victor!");
                break;
            }       
        }                                        
      }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: while loop in GUI game

    I honestly lost your question in that wall of text. What are you trying to do? Why won't continue work?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: while loop in GUI game

    Sorry about all that text. When I use "continue" (see below), a pop up dialog appears saying that a monster cannot attack itself. Once you click OK the message box (saying "A monster cannot attack itself!") keeps appearing and does not disappear once OK is clicked. How do I solve this? Once again, the use of "continue" (as shown below) doesn't work:
     if (input1 == input2) {
                JOptionPane.showMessageDialog(null,"A monster cannot attack itself!", "Error Message", JOptionPane.ERROR_MESSAGE); 
               continue;
            }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: while loop in GUI game

    If you do NOT change the value of input1 or input2 inside of the loop, then the if condition will always be true and you will continue to have the message displayed.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: while loop in GUI game

    Well, sounds like you don't want to continue that loop, you want to break out of it, right? Recommended reading: Branching Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: while loop in GUI game

    I tried using "break" but that stops the game. It's strange that continue works when playing the game via console but it doesn't work with JOptionPane.

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: while loop in GUI game

    Quote Originally Posted by Hamran2011 View Post
    I tried using "break" but that stops the game. It's strange that continue works when playing the game via console but it doesn't work with JOptionPane.
    It probably works because you ask the user for input again. How does it stop the game? I assume you have a GUI, right? Does that just disappear?

    I suggest throwing together an SSCCE that demonstrates the problem. Make sure it's as few lines as possible- the easier it is for us to help you, the faster you'll get a response.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  2. Stuck on a few Statements/Questions on timing of Main Game Loop.
    By StevenW in forum Java Theory & Questions
    Replies: 3
    Last Post: July 19th, 2011, 09:20 AM
  3. [SOLVED] Need Loop Help for Game
    By Shivam24 in forum Loops & Control Statements
    Replies: 9
    Last Post: July 18th, 2011, 07:41 PM
  4. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  5. A thread as game loop
    By maikeru in forum Threads
    Replies: 0
    Last Post: December 25th, 2009, 09:01 PM