#!/usr/www/users/dimiter/cgi-bin/strawberry win_deep(1). % You can change this two numbers to make the AI more clever nolose_deep(1). ?- X is scan(get_environment_arg(0, getenv("QUERY_STRING"))), go(X). go(new):- say([[f,f,f,f],[f,f,f,f],[f,f,f,f],[f,f,f,f]], "It's your move.", yes). go(X):- Y is scan(get_environment_arg(1, getenv("QUERY_STRING"))), Current_Pos is scan(get_environment_arg(2, getenv("QUERY_STRING"))), member(Current_Pos, L, Y), member(L, f, X), !, replace(L2, L, x, X), replace(Current_Pos2, Current_Pos, L2, Y), think(Current_Pos2). think(Pos) :- victory(x, Pos), !, say(Pos, "Congratulations! You are the winner.", no). think(Pos) :- try_to_win(Pos), !. think(Pos) :- retractall(last_good_move(_)), try_nolose(Pos). think(_) :- last_good_move(Sit2), !, say(Sit2, "It's your move.", yes). think(Pos) :- move(o, Pos, Sit2), nolose(x, Sit2, 0), !, % Tries to make the lose farther say(Sit2, "Go on, you have big chances in this game.", yes). think(Pos) :- move(o, Pos, Sit2), say(Sit2, "Well, do you want to play a new game?", yes). try_nolose(Pos) :- move(o, Pos, Sit2), nolose_deep(Deep), nolose(x, Sit2, Deep), set(last_good_move(Sit2)), 0 is random(3), !, fail. % this makes the game more interesting try_to_win(Pos) :- move(o, Pos, Sit2), is_it_win(Sit2). is_it_win(Sit2) :- victory(o, Sit2), say(Sit2, "Sorry but this time you lost.", no). is_it_win(Sit2) :- win_deep(Deep), win(x, Sit2, Deep), say(Sit2, "Give up. You have no chance.", yes). member([Element, _, _, _], Element, 0). member([_, Element, _, _], Element, 1). member([_, _, Element, _], Element, 2). member([_, _, _, Element], Element, 3). replace([Element, A, B, C], [_, A, B, C], Element, 0). replace([A, Element, B, C], [A, _, B, C], Element, 1). replace([A, B, Element, C], [A, B, _, C], Element, 2). replace([A, B, C, Element], [A, B, C, _], Element, 3). member2(Pos, El, X, Y):- member(Pos, L, Y), member(L, El, X). victory( Who, [[Who, Who, Who, Who], [_, _, _, _], [_, _, _, _], [_, _, _, _]]). victory( Who, [[_, _, _, _], [Who, Who, Who, Who], [_, _, _, _], [_, _, _, _]]). victory( Who, [[_, _, _, _], [_, _, _, _], [Who, Who, Who, Who], [_, _, _, _]]). victory( Who, [[_, _, _, _], [_, _, _, _], [_, _, _, _], [Who, Who, Who, Who]]). victory( Who, [[Who, _, _, _], [Who, _, _, _], [Who, _, _, _], [Who, _, _, _]]). victory( Who, [[_, Who, _, _], [_, Who, _, _], [_, Who, _, _], [_, Who, _, _]]). victory( Who, [[_, _, Who, _], [_, _, Who, _], [_, _, Who, _], [_, _, Who, _]]). victory( Who, [[_, _, _, Who], [_, _, _, Who], [_, _, _, Who], [_, _, _, Who]]). victory( Who, [[Who, _, _, _], [_, Who, _, _], [_, _, Who, _], [_, _, _, Who]]). victory( Who, [[_, _, _, Who], [_, _, Who, _], [_, Who, _, _], [Who, _, _, _]]). victory( Who, [[_, _, Who, _], [_, Who, _, _], [Who, _, _, _], [_, _, _, _]]). victory( Who, [[_, Who, _, _], [_, _, Who, _], [_, _, _, Who], [_, _, _, _]]). victory( Who, [[_, _, _, _], [Who, _, _, _], [_, Who, _, _], [_, _, Who, _]]). victory( Who, [[_, _, _, _], [_, _, _, Who], [_, _, Who, _], [_, Who, _, _]]). win( o, Sit, Deep) :- move(o, Sit, Sit2), win(x, Sit2, Deep). win( x, Sit, _) :- victory(o, Sit). win( x, Sit, 0) :- !, fail. win( x, Sit, Deep) :- Deep1 is Deep - 1, not(nowin( x, Sit, Deep1)). nowin(x, Sit, _) :- not(move(Sit)). % the last move belongs to O nowin(x, Sit, Deep) :- move(x, Sit, Sit2), nowin(o, Sit2, Deep). nowin(o, Sit, _) :- victory(x, Sit). nowin(o, Sit, Deep) :- not(win(o, Sit, Deep)). lose( x, Sit, Deep) :- move(x, Sit, Sit2), lose(o, Sit2, Deep). lose( o, Sit, _) :- victory(x, Sit). lose( o, Sit, 0) :- !, fail. lose( o, Sit, Deep) :- Deep1 is Deep - 1, not(nolose( o, Sit, Deep1)). nolose(o, Sit, Deep) :- move(o, Sit, Sit2), nolose(x, Sit2, Deep). nolose(x, Sit, _) :- victory(o, Sit). nolose(x, Sit, _) :- not(move(Sit)). % the last move belongs to O nolose(x, Sit, Deep) :- not(lose(x, Sit, Deep)). move(P, Sit, Sit2) :- member(Sit, L, Y), member(L, f, X), replace(L2, L, P, X), replace(Sit2, Sit, L2, Y). move(Sit) :- member(Sit, L, _), member(L, f, _). say(Pos, Message, Continue):- set_content_type("text/html"), write("
| "),
member2(Pos, El, X, Y),
link(Continue, El, Pos, X, Y),
write(" |
"),
X=3,
write("
"), write("
Tick-Tack-Toe 4x4
"),
B1 is random(1),
banner(B1),
write("
").
link(yes, f, Pos, X, Y):- !,
write("").
link(_, _, _, _, _).
banner(0) :- write("