#!/usr/bin/pl -g "main." -s
% 5x5 tic-tac-toe by Remigiusz 'lRem' Modrzejewski <http://lrem.net>
% Distributed under the GPL, for more details see:
% http://lrem.net/pages/view/prolog_tic_tac_toe
boardsize(5).
opponent(o,x).
opponent(x,o).
unfold(M, X, Y) :-
boardsize(S),
X is M mod S,
Y is (M-X)/S.
% Initialize the board% {{{
/*board(['.', '.', '.', '.', '.', '.',
'.', '.', '.', '.', '.', '.',
'.', '.', '.', '.', '.', '.',
'.', '.', '.', '.', '.', '.',
'.', '.', '.', '.', '.', '.',
'.', '.', '.', '.', '.', '.' ]).*/
board(B) :-
boardsize(S),
F is S*S,
board(B, F).
board([], 0) :- !.
board(['.'|T], N) :-
N1 is N-1,
board(T, N1).
% }}}
% Show the board% {{{
show(B) :- write(' 0123456'), nl, write(0), showloop(B, 0), write('0123456'), nl.
showloop([], _).
showloop([H|T], I) :-
boardsize(S),
I mod S =:= S-1,
L is (I-S+1)/S,
NL is (I+1)/S,
write(H), write(L), nl, write(NL),
I1 is I + 1,
showloop(T, I1), !.
showloop([H|T], I) :-
write(H),
I1 is I + 1,
showloop(T, I1).
% }}}
% Gets an index of array% {{{
get([H|_], 0, H) :- !.
get([_|T], N, R) :-
N1 is N-1,
get(T, N1, R).
% }}}
% Gets a position on board% {{{
get(B, X, Y, R) :-
boardsize(S),
-1 < X, X < S, % Check whether inside the board
-1 < Y, Y < S,
N is X + S*Y,
get(B, N, R).
% }}}
% Sets an index of array% {{{
set([_|T], 0, X, [X|T]) :- !.
set([H|T], N, X, [H|R]) :-
N1 is N-1,
set(T, N1, X, R).
% }}}
% Sets a position on board% {{{
set(B, X, Y, M, NB) :-
boardsize(S),
N is X + S*Y,
set(B, N, M, NB).
% }}}
% Counts how much points player P gets for the mark% {{{
points(B, X, Y, P, R) :-
get(B, X, Y, M), M == '.', % Asserting it's legal to put it there
points(B, X, Y, P, 1, 0, R1),
points(B, X, Y, P, 0, 1, R2),
points(B, X, Y, P, 1, 1, R3),
points(B, X, Y, P, 1, -1, R4),
R is R1 + R2 + R3 + R4.
points(B, X, Y, P, DX, DY, R) :-
points(B, X, Y, P, DX, DY, 1, R1),
MDX is -DX, MDY is -DY,
points(B, X, Y, P, MDX, MDY, 1, R2),
RS is R1 + R2 + 1,
points(RS, R).
points(_, _, _, _, _, _, 3, 0) :- !. % Search for half-lines no longer than 2
points(B, X, Y, P, DX, DY, D, R) :-
X1 is X + DX*D,
Y1 is Y + DY*D,
get(B, X1, Y1, M),
M == P,
D1 is D+1,
points(B, X, Y, P, DX, DY, D1, R1),
R is R1 + 1, !.
points(_, _, _, _, _, _, _, 0).
points(1, 0) :- !.
points(N, R) :- R is N-2.
% }}}
%Counts open lines in the neighbourhood{{{
nonblocking('.', _).
nonblocking(A, A).
open(B, X, Y, P, R) :-
get(B, X, Y, M), M == '.', % Asserting starting point is open
open(B, X, Y, P, 1, 0, R1),
open(B, X, Y, P, 0, 1, R2),
open(B, X, Y, P, 1, 1, R3),
open(B, X, Y, P, 1, -1, R4),
R is R1 + R2 + R3 + R4.
open(B, X, Y, P, DX, DY, R) :-
open(B, X, Y, P, DX, DY, 1, R1),
MDX is -DX, MDY is -DY,
open(B, X, Y, P, MDX, MDY, 1, R2),
RS is R1 + R2 + 1,
open(RS, R).
open(_, _, _, _, _, _, 3, 0) :- !. % Search for half-lines no longer than 2
open(B, X, Y, P, DX, DY, D, R) :-
X1 is X + DX*D,
Y1 is Y + DY*D,
get(B, X1, Y1, M),
nonblocking(M, P),
D1 is D+1,
open(B, X, Y, P, DX, DY, D1, R1),
R is R1 + 1, !.
open(_, _, _, _, _, _, _, 0).
open(1, 0) :- !.
open(N, R) :- R is N-2.
%}}}
%The value function {{{
value(B, P, M, R) :-
unfold(M, X, Y),
points(B, X, Y, P, RPM),
open(B, X, Y, P, ROM),
opponent(P, O),
points(B, X, Y, O, RPO),
open(B, X, Y, O, ROO),
R is 9*RPM + 9*RPO + 3*ROM + 1*ROO.
nice(B, P, M) :-
value(B, P, M, R),
R > 17.
%}}}
%Moves generator{{{
free(B, M) :-
get(B, M, R),
R == '.'.
genmoves(B, P, ML) :-
genmoves(B, P, 0, ML, MC),
%write([MC, 'nice moves']), nl,
MC > 0, !.
genmoves(B, P, ML) :-
%write('finding all free'), nl,
findfree(B, 0, ML).
%genmoves(_, _, 36, [], 0) :- !.
genmoves(_, _, N, [], 0) :-
boardsize(S),
N =:= S*S, !.
genmoves(B, P, N, [N|T], MC) :-
nice(B, P, N),
N1 is N+1,
genmoves(B, P, N1, T, MC1),
MC is MC1+1, !.
genmoves(B, P, N, T, MC) :-
N1 is N+1,
genmoves(B, P, N1, T, MC).
findfree([], _, []).
findfree([H|T], N, [N|RT]) :-
H == '.',
N1 is N+1,
findfree(T, N1, RT), !.
findfree([H|T], N, RT) :-
N1 is N+1,
findfree(T, N1, RT).
%}}}
%Openings base{{{
minimax(B, _, _, 12, 0) :-
boardsize(5),
free(B, 12), !.
/*minimax(B, _, _, 7, 0) :-
boardsize(5),
free(B, 7), !.*/
%}}}
%The minimax search {{{
%Board, player, depth, next best move, value
minimax(B, P, 0, _, 0) :- !.
minimax(B, P, D, M, V) :-
genmoves(B, P, ML),
choose(B, P, D, ML, M, V).
choose(_, _, _, [], _, -999).
choose(B, P, D, [H|T], M, V) :-
value(B, P, H, V1),
set(B, H, P, NB),
opponent(P, O),
D1 is D-1,
minimax(NB, O, D1, _, V2),
VH is V1 - V2,
choose(B, P, D, T, MT, VT),
max(VH, H, VT, MT, V, M).
max(VH, H, VT, MT, VH, H) :-
VH > VT, !.
max(_, _, VT, MT, VT, MT).
%}}}
turn(B, x, RX, RO) :-
show(B),
write('x coord:'), read(X),
write('y coord:'), read(Y),
points(B, X, Y, x, RX1),
set(B, X, Y, x, NB),
turn(NB, o, RX2, RO),
RX is RX1 + RX2, !.
turn(B, o, RX, RO) :-
show(B),
%write('x coord:'), read(X),
%write('y coord:'), read(Y),
minimax(B, o, 4, M, _),
unfold(M, X, Y),
points(B, X, Y, o, RO1),
set(B, X, Y, o, NB),
turn(NB, x, RX, RO2),
RO is RO1 + RO2, !.
turn(_, _, 0, 0).
main :-
board(B),
turn(B, x, RX, RO),
write([x, RX, points, o, RO, points]), nl,
/*set(B, 2, 0, o, NB1),
set(NB1, 2, 1, o, NB2),
points(NB2, 2, 2, o, R),
write(R), nl,
show(NB2),*/
halt.
main :-
write(nowai),
halt.