Question: I'm having a little problem programming in Java.

I have a while loop that works until the game is over, when it runs I need to get a move from the player, I need the program to hold until the player click on the `JFrame` and makes his move, I need a method like `Scanner` from the keyboard, but for the mouse.

Any idea how to do it?

```
while(!gameOver())
        {
            if(!rw.board.possibleMoves(rw.board.turn).isEmpty())
            {
                // needs to wait until the player makes a move
                Move m =  rw.players[rw.board.turn].move;
                rw.board.makeMove(m);
                rw.updateNewGame();
            }
            // switch the turns after the move
            rw.players[rw.board.turn].move = null;
            rw.board.turn = (rw.board.turn == PLAYER1 ? PLAYER2 : PLAYER1);
        }
    }

```

Answer: 
GUI applications are **event driven**. This means you don't create such a while loop. You let the program go into its own event loop, and you make the moves and switch the turns as a response to the user's click event.

That is, you create a click event listener, and in its `actionPerformed()` method, you check:

```
if (!gameOver())
{
    if(!rw.board.possibleMoves(rw.board.turn).isEmpty())
    {
        // needs to wait until the player makes a move
        Move m =  rw.players[rw.board.turn].move;
        rw.board.makeMove(m);
        rw.updateNewGame();
    }
    // switch the turns after the move
    rw.players[rw.board.turn].move = null;
    rw.board.turn = (rw.board.turn == PLAYER1 ? PLAYER2 : PLAYER1);
}

```

Make sure that the methods called here change the state of the button or remove it when pressing it no longer makes sense (no possible moves or game over etc.).
Well this might be a good reason to implement it asynchronously.

Assuming you are one threaded, this will not be much of a problem: 
You'll add a while(m == null);, waiting for the Move to be an object.