0

At the moment I'm currently re-writing a text-based program to have a GUI. One of the problems I am experiencing is that I want the program to wait until a certain condition is met. This condition can be met through the user clicking the "Walk" button until the player.walked attribute = 5. When using a text-based interface this is quite simple, use a while loop and inside have an input function.

while (player.getWalked() < 5) {
     //wait for user input via terminal through the scanner.
}

However when using a GUI and wanting to follow the approach of the Model-View Controller (i.e. keeping game mechanics and user interface stuff separate) it becomes rather difficult. After attempting to implement a GUI My program keeps freezing as the while loop is now empty. I will attempt to evidence this below but it is rather confusing. I apologise if this is unprofessional.

World Class:

public static void play(Player player) throws FileNotFoundException, IOException, ClassNotFoundException{ // play method is the centralised place for all in-game simulation and user interaction.
    welcome(player);
    while (player.getWalked() <5) {

    }

GUI Class:

Button walk_button = new Button("Walk");
      walk_button.setBounds(195, 395, 100,100);
      add(walk_button);
      walk_button.addActionListener((new ActionListener(){ 
        public void actionPerformed(ActionEvent evt) {
            try{
                label1.setVisible(false);
                label.setText(player.interaction("W"));
                label.setBounds(200,50,400,100);
                }
            catch (FileNotFoundException e) {System.out.println(e.getMessage());} catch (IOException e) {System.out.println(e.getMessage());} catch (ClassNotFoundException e) {System.out.println(e.getMessage());} 
            } 
        }));

Player class consisting of the interaction method:

public String interaction(String input) throws FileNotFoundException, IOException, ClassNotFoundException{ 
//String input = World.input("You have walked "+getWalked()+" miles so far.\nOnly "+(END_POINT - walked)+" miles until you reach the end of the town.\nPress 'w' to walk.\nPress 'c' to change weapon equipped.\nPress 's' to save and exit.");
if (input.equals("W")) {
   return walk(World.dice(4,1));
}

If anyone can find a solution to this I would be much appreciated. The end goal is for the program to keep running (allow the player to keep pressing the "Walk" button) until the while loop is broken.

Thank you very much and apologies if this is rather long, confusing and unprofessional.

Notorious
  • 41
  • 1
  • 7
  • 1
    1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 3) `Button walk_button = new Button(..);` Use a `JButton` – Andrew Thompson Apr 08 '20 at 20:38
  • 1
    Possible duplicate of [*How to combine event listeners with “asking” for an event?*](https://stackoverflow.com/q/24725420/230513) – trashgod Apr 09 '20 at 15:46

1 Answers1

1

It's not a great idea to have empty while loops, so I would suggest checking the player's position with an if-statement in the same method where it is set (right after the button trigger actionPerformed), and then continuing from there. I can't give you a specific implementation though because I don't know what you want to do.

EDIT:To clarify, I meant something like this:

public void actionPerformed(ActionEvent evt) {
  //set player's walked distance ...
  //...
  if (player.getWalked() > 5) {
    //put all your logic here, or extract to a method
  }
}

Side note: Instead of having multiple catch blocks where you do the same thing, just use FileNotFoundException | ClassNotFoundException | etc.

user
  • 7,435
  • 3
  • 14
  • 44
  • I do not want to have the empty while loop I want to put something into the while loop that keeps the program running, so basically doing nothing. At the moment an empty while loop keeps freezing the program. – Notorious Apr 08 '20 at 19:42
  • No, what I meant is, delete the while loop. Put all your logic in a method that will be called when the button is pressed and the player has walked enough. – user Apr 08 '20 at 19:43
  • 1
    Some related examples are examined [here](https://stackoverflow.com/a/24726834/230513). – trashgod Apr 09 '20 at 15:47