Prolog "sample"

Admin User, erstellt 14. März 2025
         
/**
* 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(lists)).
:- ensure_loaded(library(aggregate)).
:- ['tictac.p'].
% board_data(-List, -List)
board_data([T|X], [Z|Y]) :-
sys_generate_side(P),
sys_generate_board(L),
sys_generate_move(L, P, R),
sys_encode_side(P, T),
sys_encode_board(L, X),
sys_encode_board(R, Y),
(best(X, P, Y) -> Z = 1; Z = 0).
/*************************************************************/
/* Sample Generation */
/*************************************************************/
% sys_generate_side(-Atom)
sys_generate_side(o) :- random(V), V =< 0.5, !.
sys_generate_side(x).
% sys_generate_board(-List)
sys_generate_board([[A,B,C],[D,E,F],[G,H,I]]) :-
sys_generate_position(A), sys_generate_position(B), sys_generate_position(C),
sys_generate_position(D), sys_generate_position(E), sys_generate_position(F),
sys_generate_position(G), sys_generate_position(H), sys_generate_position(I).
% sys_generate_position(-Atom)
sys_generate_position(X) :-
random(V), I is floor(3*V),
nth0(I, [-,x,o], X).
% sys_generate_move(+List, +Atom, -List)
sys_generate_move(X, P, Y) :-
findall(H, move(X, P, H), L),
length(L, N), N > 0,
random(V), I is floor(N*V),
nth0(I, L, Y).
/*************************************************************/
/* Sample Preparation */
/*************************************************************/
% sys_encode_side(+Atom, -Integer)
sys_encode_side(o, 0).
sys_encode_side(x, 1).
% sys_encode_board(+List, -List)
sys_encode_board([[A,B,C],[D,E,F],[G,H,I]],
[A1,A2,B1,B2,C1,C2,
D1,D2,E1,E2,F1,F2,
G1,G2,H1,H2,I1,I2]) :-
sys_encode_position(A,A1,A2), sys_encode_position(B,B1,B2), sys_encode_position(C,C1,C2),
sys_encode_position(D,D1,D2), sys_encode_position(E,E1,E2), sys_encode_position(F,F1,F2),
sys_encode_position(G,G1,G2), sys_encode_position(H,H1,H2), sys_encode_position(I,I1,I2).
% sys_encode_position(+Atom, -Integer, -Integer)
sys_encode_position(-, 0, 0).
sys_encode_position(x, 1, 0).
sys_encode_position(o, 0, 1).