0

okay, so I'm working on a boardgame. The first 'while loop' checks if the game is over, the inner 'while loop' is supposed to wait for the user input (squareSelector and rooSelector are my mouselisteners, 'k' and 's' are the input that are supposed to be returned by the listeners) However the 'while loop' crashes my program and blocking my listeners from working. I read up about this and found out that swing is single threaded and so my listeners can't work while the whileloop is active. However I need to wait for the input in order to avoid nullpointers. Any ideas on how to solve this ? (as in a different approach, or how to make my loop wait for the input)

the gameloop

while(currentPlayer.haveIWon() == false){
        //wait for input
        while(!inputIsDone){
            System.out.println("waiting for move");
            System.out.println(squareSelector.isDone() + " " + rooSelector.isDone()) ;
            checkInput() ;
            if(squareSelector.isDone() && rooSelector.isDone()){
                inputIsDone = true ;
            }
        }
        //update board
        currentPlayer.performMove(k, s);
        rooSelector.setDone(false);
        squareSelector.setDone(false);
        inputIsDone = false ;
        //currentPlayer.setInput(false);
        //repaint
        Main.getState().getComponent().repaint();

    }
    //display winnner thing

the checkInput method

public void checkInput(){
    if(rooSelector.getSelected() != null){
        k = rooSelector.getSelected() ;
    }
    if(squareSelector.getSelected() != null){
        s = squareSelector.getSelected() ;
    }
}

If you need more code to understand what's going on let me know and I will add more.

Red fx
  • 1,071
  • 2
  • 12
  • 26
  • Thou shall not block the [event dispatch thread](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html). - The problem is that your loop prevents the EDT from handling the input events. The game loop (if necessary at all) should run in its own thread (e.g. via [SwingWorker](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html)). – Thomas Oct 18 '17 at 14:24
  • In cheking input wise loopings include a way to prevent the Thread of kill others by starvation: Thread.yield() or Thread.sleep(1) – Duloren Oct 18 '17 at 14:30
  • Generally a game that doesn't constantly update its UI (i.e. no animations) or world (i.e. not "real time") rarely needs a game loop. You state you need one to prevent NPEs but that rather indicates design flaws or coding errors than a real need for a loop. – Thomas Oct 18 '17 at 14:30

1 Answers1

0

Swing is single-threaded. Due to its design, everything related to GUI must be done on Swing thread, also called event dispatch thread. It basically does one thing: looks at event queue and if there is a something, it handles it, and if not, goes to sleep. Every mouse move, key press, repaint etc. is really an event in the event queue.

If your game only reacts on GUI events and has no lengthy operations, you don't have to use anything special. You can write your logic without while so that it will not block, only react to events. However, this possibility entirely depends on what you are trying to implement.

If you have any operations that block the thread for a long time, you'll want to use separate threads for that. This way, what other threads do won't impact your GUI, but you'll need to be very careful to send data from one thread to another or to call something on another thread.

jurez
  • 4,436
  • 2
  • 12
  • 20