Gunadarma

Studentsite

LOGIN


Kamis, 27 Oktober 2011

Project Game AI Tic Tac Toe 4x4 Strawberry Prolog

?-
set(pos([[f,f,f,f], [f,f,f,f], [f,f,f,f], [f,f,f,f]])),
set(lose_flag(false)),
brush(system_color(window)),
window( _, _, win_func(_), "Tick-Tack-Toe 4x4", 200, 100, 308, 328).


put_mark(Mark) :-
pos(Current_Pos),
member(Current_Pos, L, Y),
member(L, Mark, X),
X1 is 40 + 60*X, Y1 is 40 + 60*Y, X2 is X1 + 40, Y2 is Y1 +40,
write_mark(Mark, X1, Y1, X2, Y2),
fail.
put_mark(_).

write_mark(x, X1, Y1, X2, Y2) :-
line(X1, Y1, X2, Y2),
line(X2, Y1, X1, Y2).
write_mark(o, X1, Y1, X2, Y2) :-
ellipse(X1, Y1, X2, Y2).


win_func(paint) :-
pen(2, rgb(0, 0, 0)), % black
line(30, 30, 30, 270),
line(30, 270, 270, 270),
line(270, 270, 270, 30),
line(270, 30, 30, 30),
line(90, 30, 90, 270),
line(150, 30, 150, 270),
line(210, 30, 210, 270),
line(30, 90, 270, 90),
line(30, 150, 270, 150),
line(30, 210, 270, 210),
pen(4, rgb(255, 0, 0)), % red
put_mark(x),
pen(4, rgb(0, 0, 255)), % blue
put_mark(o),
fail.

win_func(mouse_click(X, Y)) :-
lose_flag(false),
X>30, Y>30,
X<270, Y<270,
X1 is (X - 30)//60,
Y1 is (Y - 30)//60,
pos(Current_Pos),
member(Current_Pos, L, Y1),
member(L, f, X1),
!,
replace(L2, L, x, X1),
replace(Current_Pos2, Current_Pos, L2, Y1),
set(pos(Current_Pos2)),
not(win_func(paint)),
think(Current_Pos2).

win_func(mouse_click(X, Y)) :-
beep.

think(Pos) :-
victory(x, Pos), !,
set(lose_flag(true)),
message("Unbelievable","You are the winner.",!).
think(Pos) :-
try_to_win(Pos), !.
think(Pos) :-
retractall(last_good_move(_)), try_nolose(Pos).
think(_) :-
last_good_move(Sit2), !,
set(pos(Sit2)),
not(win_func(paint)).
think(Pos) :-
message("Congratulations", "You have some chance in this game.", n),
move(o, Pos, Sit2),
nolose(x, Sit2, 0), !, % Tries to make the lose farther
set(pos(Sit2)),
not(win_func(paint)).
think(Pos) :-
move(o, Pos, Sit2),
set(pos(Sit2)),
not(win_func(paint)).

try_nolose(Pos) :-
move(o, Pos, Sit2),
nolose_deep(Deep),
nolose(x, Sit2, Deep),
set(last_good_move(Sit2)),
random(3)=:=0, !, 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),
set(pos(Sit2)),
not(win_func(paint)),
set(lose_flag(true)),
message("Sorry","You cannot win against the AI.",s).
is_it_win(Sit2) :-
win_deep(Deep),
win(x, Sit2, Deep),
set(pos(Sit2)),
not(win_func(paint)),
message("Advice","Give up. Don't lose my time.",i).

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).


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, _).

Tidak ada komentar:

Posting Komentar