Prolog "browser"

Admin User, erstellt 15. März 2024
         
/**
* Prolog code for the tic-tac-toe game.
* Min-max search via negation.
* Game board via Prolog term.
*
* Warranty & Liability
* To the extent permitted by applicable law and unless explicitly
* otherwise agreed upon, XLOG Technologies AG makes no warranties
* regarding the provided information. XLOG Technologies AG assumes
* no liability that any problems might be solved with the information
* provided by XLOG Technologies AG.
*
* Rights & License
* All industrial property rights regarding the information - copyright
* and patent rights in particular - are the sole property of XLOG
* Technologies AG. If the company was not the originator of some
* excerpts, XLOG Technologies AG has at least obtained the right to
* reproduce, change and translate the information.
*
* Reproduction is restricted to the whole unaltered document. Reproduction
* of the information is only allowed for non-commercial uses. Selling,
* giving away or letting of the execution of the library is prohibited.
* The library can be distributed as part of your applications and libraries
* for execution provided this comment remains unchanged.
*
* Restrictions
* Only to be distributed with programs that add significant and primary
* functionality to the library. Not to be distributed with additional
* software intended to replace any components of the library.
*
* Trademarks
* Jekejeke is a registered trademark of XLOG Technologies AG.
*/
:- ensure_loaded(library(misc/react)).
% main
main :-
add_click('a1'), add_click('a2'), add_click('a3'),
add_click('b1'), add_click('b2'), add_click('b3'),
add_click('c1'), add_click('c2'), add_click('c3').
% mark(+Event)
mark(E) :-
get_text('status', ''), !,
ir_object_current(E, 'target', C),
ir_object_current(C, 'ariaLabel', -),
ir_object_set(C, 'ariaLabel', x),
marked.
mark(_) :-
set_text('status', ''),
reset.
/*************************************************************/
/* Browser Play */
/*************************************************************/
% reset
reset :- init(X), set_board(X).
% marked
marked :- get_board(X), looser(X).
% looser(+Board)
looser(X) :- win(X, x), !, set_text('status', 'You win.').
looser(X) :- tie(X, o), !, set_text('status', 'Nobody won.').
looser(X) :- best(X, o, Y), !, set_board(Y), winner(Y).
looser(_) :- set_text('status', 'I give up.').
% winner(+Board)
winner(X) :- win(X, o), !, set_text('status', 'I win.').
winner(_).
% get_board(-Board)
get_board([[A,B,C],[D,E,F],[G,H,I]]) :-
get_cell('a1', A), get_cell('a2', B), get_cell('a3', C),
get_cell('b1', D), get_cell('b2', E), get_cell('b3', F),
get_cell('c1', G), get_cell('c2', H), get_cell('c3', I).
% set_board(+Board)
set_board([[A,B,C],[D,E,F],[G,H,I]]) :-
set_cell('a1', A), set_cell('a2', B), set_cell('a3', C),
set_cell('b1', D), set_cell('b2', E), set_cell('b3', F),
set_cell('c1', G), set_cell('c2', H), set_cell('c3', I).
/*************************************************************/
/* Browser Access */
/*************************************************************/
% get_cell(+Atom, -Atom)
get_cell(I, W) :-
dom_cell_goto(I, C),
ir_object_current(C, 'ariaLabel', W).
% set_cell(+Atom, +Atom)
set_cell(I, W) :-
dom_cell_goto(I, C),
ir_object_set(C, 'ariaLabel', W).
% add_click(+Atom)
add_click(I) :-
dom_cell_goto(I, C),
bind(C, 'click', E, mark(E)).
% get_text(+Atom, -Atom)
get_text(I, W) :-
dom_cell_goto(I, C),
ir_object_current(C, 'innerText', W).
% set_text(+Atom, +Atom)
set_text(I, W) :-
dom_cell_goto(I, C),
ir_object_set(C, 'innerText', W).
/*************************************************************/
/* Main Initialization */
/*************************************************************/
:- initialization(main).