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