Results 1 to 6 of 6
  1. #1
    Hamran2011 is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default coverting console game to GUI

    Hi there, I am fairly new to Java (started learning it this August) and I have an assignment that I do not know how to approach. One of my first assignments was to create a simple console game and now I have been asked to convert this console game into game with that the user can play though a graphical user interface (GUI). I have not yet had any lessons on GUIs but have come across tutorials on building basic GUIs (e.g. adding 2 numbers). This, however, has not been of much help when it comes to thsi assignment.
    The console game's code can be found below and here is a brief summary of the 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. Once again, here is the code of the console game. Any tips as to how to go about converting thsi game to a GUI would be welcome!

    import java.util.Scanner;

    import Words.Vowels;

    public class Game {

    public Game() {
    // TODO Auto-generated constructor stub
    }

    /**
    * @param args
    */
    public static void main(String[] args) {

    Monster[] monsters = { new Goblin("Jules", new Sword()),
    new Troll("Vincent", new Hammer()),
    new Goblin("Marcellus", new Knife()),
    new Orc("Mia", new Club()),
    };

    while (true) {

    showAllMonsters(monsters);

    Scanner input = new Scanner(System.in);

    System.out.println("Select attacker: "); // prompt
    int input1 = input.nextInt(); // reads inputed attacker
    if (input1 <= 0 | input1 > monsters.length) {
    System.out.println("Please enter a number between 1 and "
    + monsters.length + "!");
    continue;
    }

    Monster attacker = monsters[input1 - 1];

    System.out.println("Select victim: "); // prompt
    int input2 = input.nextInt(); // reads inputed victim
    if (input2 <= 0 | input2 > monsters.length) {
    System.out.println("Please enter a number between 1 and "
    + monsters.length + "!");
    continue;
    }

    Monster victim = monsters[input2 - 1];

    if (input1 == input2) {
    System.out.println("A monster cannot attack itself!");
    continue;
    }

    if (attacker.healthPoints == 0) {
    System.out
    .println("A dead monster cannot attack another monster!");
    continue;
    }

    if (victim.healthPoints == 0) {
    System.out
    .println("A monster cannot attack a monster which is already dead!");
    continue;
    }

    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;
    }

    attacker.shouts();

    showFight(attacker, victim, lostHealthPoints);

    if (determineWinner(monsters, attacker) == attacker.name) {
    System.out.println(determineWinner(monsters, attacker)
    + " is the victor!");
    break;
    }
    }
    }

    public static void showAllMonsters(Monster[] monsters) {
    for (int i = 0; i < monsters.length; i++) {
    Monster monster = monsters[i];
    monster.print(i);
    }
    }

    public static void showFight(Monster attacker, Monster victim,
    int lostHealthPoints) {
    String aOrAn1;
    String aOrAn2;
    if (Vowels.startsWithVowel(attacker.type)){
    aOrAn1 = "an ";
    }else{
    aOrAn1 = "a ";
    }
    if (Vowels.startsWithVowel(victim.type)){
    aOrAn2 = "an ";
    }else{
    aOrAn2 = "a ";
    }
    System.out.println(attacker.name + " (" + aOrAn1 + attacker.type.toLowerCase()
    + ") " + attacker.w.action + " " + victim.name + " ("
    + aOrAn2 + victim.type.toLowerCase() + ") " + "with a " + attacker.w.type.toLowerCase()
    + "." + " " + victim.name + " loses " + lostHealthPoints
    + " HP.");

    }

    public static String determineWinner(Monster[] monsters, Monster attacker) {
    int sum = 0;
    String winner = null;
    for (int i = 0; i < monsters.length; i++) {
    sum = sum + monsters[i].healthPoints;
    }
    if (sum == attacker.healthPoints) {
    winner = attacker.name;
    }
    return winner;
    }

    }

  2. #2
    AmFreak is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Re: coverting console game to GUI

    I don't really know what u exactly want, but all u need seems to me something to choose a monster from (a dropdown list or radio buttons maybe) for each opponent and maybe a dropdown list to choose a weapon (if each monster can have each weapon). Then you need a button, that starts the fight (so doing the same as your programm after picking the opponents). The last thing you need is a textbox were you put out what you now put out on the console. When you start the fight by clicking on the command button the selected values of the dropdown lists are read in, so that you use the opponents the player has choosen.

  3. #3
    Hamran2011 is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default Re: coverting console game to GUI

    Hi there,
    I want a JFrame on which there is a list of Monsters. Then I want to be able to choose an attacker and victim by typing a number in a text field. For example, I type number 1 in the text field for selecting an attacker and 2 in the textfield for selecting a victim after which a messsage will appear with the text "Monster 1 (does something) to Monster 2". After this has been shown, the application shows the list of monsters again, but now with the values changed.
    I have not worked with dropdown lists and radio buttons in Java; in fact all I have made so far is a simple GUI that adds 2 numbers. So I don't know how to go about this assignment.
    Thanks again.

  4. #4
    AmFreak is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Re: coverting console game to GUI

    Here is a little Swing (aka Java Gui) Tutorial that should help you:
    The Java Swing tutorial
    under "Basic Swing Components" they show how you make a combo box or JList. Just work through the tutorial or use your simple gui as a starting point. I guess you have a comand button for adding the 2 numbers and a TextField or label that puts out the sum of the 2. Then just try to add a combo box as shown in the tutorial. If you have added it try to make it so that if you hit the command button the choosen value is displayed in the TextField or label you already have. Then when you know how to get the selected value from the Combo Box make another one and instead of putting the value in the textfield when your command button is pressed give it to your function that starts and calculates the fight. Just work step by step.
    If you really want to only use Text Fields, you can retrieve it's value with getText().
    Last edited by AmFreak; 10-31-2011 at 04:34 PM.

  5. #5
    jMaster's Avatar
    jMaster is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Default Re: coverting console game to GUI

    Try downloading a good java IDE(Intergrated Development Environment(I think)) preferably NetBeans -i use Netbeans- or Eclipse, go through a tutorial that will teach you the basics of using your IDE and then just draw up your GUI in a few minutes and make the minor adjustments to your game. I hope helps its certainly the quickest way I know.

  6. #6
    Hamran2011 is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default Re: coverting console game to GUI

    I have managed to create a simple GUI for the game. However, if the user selects the same monster as attacker and as victim, 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:
    Java Code:
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
           
           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;
            }       
        }                                        
      }

Similar Threads

  1. Help with Console
    By thiagohe in forum Advanced Java
    Replies: 7
    Last Post: 08-01-2011, 02:30 PM
  2. Replies: 1
    Last Post: 06-02-2011, 10:29 AM
  3. Replies: 2
    Last Post: 08-05-2010, 05:49 AM
  4. In-game console
    By Mr.Beans in forum New To Java
    Replies: 5
    Last Post: 08-17-2009, 11:22 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
  •