How to combine event listeners with “asking” for an event?

Your text based game has a loop that repeatedly asks questions to gather user input. Swing provides this loop for you by continually executing Runnable blocks of code that have been posted to the EventQueue. For example, when the user presses a button labeled E, code is posted to the queue that invokes your ActionEvent implementation to handle your game’s interpretation of the move east command.

For reference, a complete example of a very simple guessing game is examined here. In pseudocode, the corresponding text based game might look like this:

initialize
loop
    prompt "Guess what color!"
    get chosenColor
    if chosenColor = actualColor
        say "You win!"
        reset game
    else
        say "Keep trying."
end loop

A more elaborate game cited there includes the original text-based source.

Leave a Comment