Skip to main content

How to wait for a user click in a while loop?

I finished making a board game (Kamisado) in java with swing (JFrame). Now I am trying the same with client side javascrip/typescript (browser). The idea is something like this:

while(!isGameOver(board)) {
    Player turn = getTurn(board);
    Move move = turn.getMove(board);
    tryMove(move, board);
}

I have a abstract class Player with abstract method getMove(Board board).

So i can change the kind of player in the main function.

Example:

Player white = new PlayerAI();
Player black = new PlayerHuman();

PlayerHuman creates a View, in which the human user can click for moves, like this:

class PlayerHuman extends Player {

    View view;

    PlayerHuman() {
        view = new View();
    }

    getMove(Board board) {
        return view.getMove(board);
    }
}
class View {

    Move move;

    View {
        // add even listener which calls click
    }
    
    click(Move move) {
        this.move = move;
        synchronized (monitor)
            monitor.notify();
    }
    
    getMove(Board board) {
        updateView(board);
        synchronized (monitor)
            monitor.wait();
        return move;
    }       
    
}

I have gone through some of the solutions on the websites and others as well but can't find the solution to the problem.

Edit 1: Here is what i already have in visual studio code, typescript:

https://github.com/osbornx22/Kamisado

Via Active questions tagged javascript - Stack Overflow https://ift.tt/g0dNhc9

Comments