Board Play

The game play mediates between the game search and the board. The game play also uses a status line to show the outcome of the game. The game play is initiated by the HTML page, which has GUI interaction logic. If the end-user hits an enabled button, the GUI interaction logic labels the button with "x" and triggers the game play as follows:

    let help = e.target;
help.setAttribute('aria-label', 'x');
help.setAttribute('disabled', 'disabled');
try {
perform("marked");
} catch (error) {
show(error);
}

The game play first checks the board. If the game is not yet complete, it calls best/3, which succeeds non-deterministically with new boards. A more elaborate solution would then randomly pick a solution. For simplicity we only pick the first solution, which is done by placing a Prolog cut !/0. The game play then checks the board again.

looser(X) :- win(X, x), !, write('you win.'), nl, set_complete.
looser(X) :- tie(X, o), !, write('nobody won.'), nl, set_complete.
looser(X) :- best(X, o, Y), !, set_board(Y), winner(Y).
looser(_) :- write('I give up.'), nl, set_complete.

The HTML page also defines some output call back. We simply carried over the approach from the sandbox example and write into a DOM element. In the above, the status line is then popu-lated by invoking the standard Prolog write/1 and nl/0. More advanced solutions might combine it with audio feedback or other visual cues.

Kommentare