#!/usr/www/users/dimiter/cgi-bin/strawberry ?- X is scan(get_environment_arg(0, getenv("QUERY_STRING"))), go(X). go(new):- say([[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) :- try_to_win(Pos), !. think(Pos) :- try_nolose(Pos), !. think(Pos) :- say(Pos, "This game is equal.", no). try_nolose(Pos) :- move(o, Pos, Sit2), nolose(x, Sit2), say(Sit2, "It's your move.", yes). 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(x, Sit2), say(Sit2, "Give up. You have no chance.", yes). member([Element, _, _], Element, 0). member([_, Element, _], Element, 1). member([_, _, Element], Element, 2). replace([Element, A, B], [_, A, B], Element, 0). replace([A, Element, B], [A, _, B], Element, 1). replace([A, B, Element], [A, B, _], Element, 2). member2(Pos, El, X, Y):- member(Pos, L, Y), member(L, El, X). victory( Who, [[Who, Who, Who], [_, _, _], [_, _, _]]). victory( Who, [[_, _, _], [Who, Who, Who], [_, _, _]]). victory( Who, [[_, _, _], [_, _, _], [Who, Who, Who]]). victory( 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) :- move(o, Sit, Sit2), win(x, Sit2). win( x, Sit) :- victory(o, Sit). win( x, Sit) :- not(nowin( x, Sit)). nowin(x, Sit) :- move(x, Sit, Sit2), nowin(o, Sit2). nowin( o, Sit) :- victory(x, Sit). nowin(o, Sit) :- not(move(Sit)). % the last move belongs to X nowin(o, Sit) :- not(win(o, Sit)). lose( x, Sit) :- move(x, Sit, Sit2), lose(o, Sit2). lose( o, Sit) :- victory(x, Sit). lose( o, Sit) :- not(nolose( o, Sit)). nolose(o, Sit) :- not(move(Sit)). % the last move belongs to X nolose(o, Sit) :- move(o, Sit, Sit2), nolose(x, Sit2). nolose( x, Sit) :- victory(o, Sit). nolose(x, Sit) :- not(lose(x, Sit)). 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(" Tick-Tack-Toe "), B is random(1), banner(B), write("

"), for(Y, 0, 2), write(" "), for(X, 0, 2), write(" "), X=2, write(" "), Y=2, write("
"), member2(Pos, El, X, Y), link(Continue, El, Pos, X, Y), write("

"), write("

"), write(Message), write("



"), ( (Pos=[[f,f,f],[f,f,f],[f,f,f]]) -> write(""); write("") ), write(""), write(""), write(""), write("

Tick-Tack-Toe




"), B1 is random(1), banner(B1), write(" "). link(yes, f, Pos, X, Y):- !, write(""). link(_, _, _, _, _). banner(0) :- write("

").