How to structure tic tac toe game loop (or equivalent simple turn based game)?

by Sebastiano Verdolini   Last Updated October 09, 2019 11:13 AM

I'm developing the simple Tic Tac Toe game in Java and got stuck on the game loop architecture.

The game must support local, remote and automatic players. The graphical interface is based on JavaFX.

How can I best structure the game loop so that the game logic is as separate as possible from the graphic interface?

I have thought of two approaches to solve this problem

Approach 1 - wait/notify

The simplest approach I thought of is to develop classes for each type of player (remote, interactive and automatic) and implement the necessary code within the mark (Board) method. The update method iterates the players and for each one executes the mark method. In the case of the AI, no problem, but in the case of the interactive player I would find myself having to pause the current thread while waiting for input from the graphic interface thread. A solution that does not convince me.

public synchronized void mark(Board board) {
    nextMarkLocation = null;
    try {
        if(nextMarkLocation == null) {
    } catch (InterruptedException e) {
        // handling


public synchronized void setNextMarkLocation(Location location) {
    nextMarkLocation = location;

Approach 2 - expose methods

The second approach would be to implement the methods for setting the current player's action in the game class. In this case, at each execution of the update method the routine would be the following (pseudo code):

public void update() {
    var player = getCurrentPlayer();
    var location = player.getNextMarkLocation();
    var symbol = player.getSymbol();
    board.markCell(location, symbol);

The GUI will take care of updating itself according to the update of the match and setting the action for the current player. Here a check should be placed on the GUI owner. If the current player is not the same as the GUI owner, the game must ignore the action.

But the problem would be the reverse: And the automatic players?

Do you have any suggestions? Thanks

Related Questions

Updated February 13, 2019 15:13 PM

Updated June 04, 2019 21:13 PM

Updated August 14, 2016 08:05 AM

Updated October 11, 2017 20:13 PM

Updated May 01, 2015 21:05 PM