Selasa, 04 Desember 2012

Listing Game Checkers (6x)

% This program is created by Borislav Rizov
% from University of Sofia

?-
G_W=200,
  set(pos(w,[[m,1,0],[m,3,0],[m,5,0],
           [m,0,1],[m,2,1],[m,4,1]],
        [  [m,1,4],[m,3,4],[m,5,4],
            [m,0,5],[m,2,5],[m,4,5]])),
 
  G_Val is 50,
  G_Pawn_w is bitmap_image("../Examples/res/nama_gambar.bmp","../Examples/res/maska.bmp"),
  G_Pawn_ws is bitmap_image("../Examples/res/nama_gambar.bmp","../Examples/res/maska.bmp"),
  G_Pawn_b is bitmap_image("../Examples/res/nama_gambar.bmp","../Examples/res/maska.bmp"),
  G_King_w is bitmap_image("../Examples/res/nama_gambar.bmp","../Examples/res/maska.bmp"),
  G_King_ws is bitmap_image("../Examples/res/nama_gambar.bmp","../Examples/res/maska.bmp"),
  G_King_b is bitmap_image("../Examples/res/nama_gambar.bmp","../Examples/res/maska.bmp"),
 
  G_NoColor is brush(rgb(0,0,0)),
  G_Grey is brush(rgb(250,250,250)),
  pen(pen(0,rgb(30,100,100))),
  window(_,_,myFunc(_),"Checkers 6x6",100,50,378,500).
%In The Beginning There Was Silence And Darkness All Across The Earth...
%********************* Initialization ************************
newGame:-    set(pos(w,[[m,1,0],[m,3,0],[m,5,0],
           [m,0,1],[m,2,1],[m,4,1]],
        [  [m,1,4],[m,3,4],[m,5,4],
            [m,0,5],[m,2,5],[m,4,5]])),
    G_Clicked:= 0,update_window(_),(G_P=1->play).
myFunc(init):-
G_H = 60,G_N = 6,G_P = 0,
G_Clicked = 0,G_K = m,
G_X = 1,G_Depth = 3,
G_Y = 1,
%********************* Menu **********************************
menu(pop_up, _,_,myPopUpGame(_),"&Game"),
menu(pop_up, G_Menu,_,level(_),"&Level"),
menu( right, _, _, menu_help(_), "&Help"),
menu( right, _, _, menu_keluar(_), "&keluar").
level(init):-
    menu(normal,_,_,l2(_),"&Beginner"),
    menu(checked,_,_,l3(_),"&Advanced"),
    menu(normal,_,_,l4(_),"&Expert").
l2(press):-change_menu(2).
l3(press):-change_menu(3).
l4(press):-change_menu(4).
change_menu(M):-modify_menu(G_Menu,G_Depth- 1,normal,_),
                G_Depth:=M, modify_menu(G_Menu,M- 1,checked,_).

myPopUpGame(init):-
    menu(normal,_,_,newGame(_),"New game - play &first"),
    menu(normal,_,_,swapSides(_),"New game - play &second").
newGame(press):-G_P:= 0, newGame.
swapSides(press):-G_P:= 1, newGame.
menu_help(press):-shell_execute("Checkers.html").
menu_keluar(press) :- close_window(_).
    fail.
%********************* Paint *********************************
myFunc(paint):-drawTable,drawPos,fail.
drawTable:-
color_text(_, rgb(0, 0, 0)),
  font(10,30,"Arial Black"),
  text_out(10, 380, "Muhamad Asep Saputra"),
  text_out(10, 410, "54410563"),
  text_out(120, 410, "3 IA 03"),
  text_out(200, 410, "waktumu: "),
N is G_N- 1,
    for(I,0,N),
        for(J,0,N),
            X1 is I*G_H,Y1 is J*G_H,
            X2 is X1+G_H,Y2 is Y1+G_H,
            draw(I,J),
            rect(X1,Y1,X2,Y2),fail.
drawTable.

draw(I,J):- (I+J) mod 2=:=0 -> brush(G_Grey);brush(G_NoColor).

%========menampilin waktu disini=======
myFunc(paint):-
  G_waktu is set_timer(_,1,time_func).
time_func(end):-
G_W =:=0 ->
kill_timer(_,G_waktu),
message("permainan selesai"," kamu kalah dengan AI \n karena waktu abis",i),close_window(_)
else
G_W<>=G_W-1,
T is printq(G_W),
text_out(300,410,T).
fail.
%******************** Draws a man or king ********************
draw_Pul(m,C,X,Y):-
    X1 is X*G_H,Y1 is Y*G_H,
    pawn(C, Bitmap),
      draw_bitmap(X1,Y1,Bitmap,_,_).

draw_Pul(k,C,X,Y):-
    X1 is X*G_H,Y1 is Y*G_H,
    king(C, Bitmap),
      draw_bitmap(X1,Y1,Bitmap,_,_).
clear(X,Y):-brush(G_NoColor),X1 is X*G_H,Y1 is Y*G_H,X2 is X1+G_H,Y2 is Y1+G_H,rect(X1,Y1,X2,Y2).

pawn(w, G_Pawn_w).
pawn(mm, G_Pawn_ws).
pawn(b, G_Pawn_b).

king(w, G_King_w).
king(mm, G_King_ws).
king(b, G_King_b).

%********************** Mouse Click **************************
myFunc(mouse_click(X,Y)):-X<G_H*G_N,Y<G_H*G_N,
X1 is X // G_H,Y1 is Y // G_H,move(X1,Y1),!,fail.

move(X,Y):- G_Clicked = 0,pos(C,B,W),
    elem([K,X,Y],W),G_Clicked:=G_Clicked+1,draw_Pul(K,mm,X,Y),
    G_X:=X,G_Y:=Y,G_K:=K.
move(X,Y):- G_Clicked = 1,
    (pos(C,B,W),
    (elem([K,X,Y], W)->
      draw_Pul(G_K,w,G_X,G_Y),G_X:=X,G_Y:=Y,G_K:=K,draw_Pul(K,mm,X,Y);
    hodW(C,G_K,G_X,G_Y,X,Y,B,W));
    G_Clicked:=0, draw_Pul(G_K,w,G_X,G_Y)).
move(X,Y):- G_Clicked > 1,
    pos(C,B,W),
    take(G_K,G_X,G_Y,X,Y,B,W).

%********************** Logic Part **************************
%*** Can must check whether the player can take a pul(che) ***
can([H|L],B,W):-elemOfelem(_,X,Y,H),Y1 is Y+2,Y2 is Y- 2,X1 is X+2,X2 is X- 2,
(in(X1,Y1),t(X,Y,X1,Y1,B,W);in(X1,Y2),t(X,Y,X1,Y2,B,W);
 in(X2,Y1),t(X,Y,X2,Y1,B,W);in(X2,Y2),t(X,Y,X2,Y2,B,W)),
 G_Clicked:=0.
can([H|L],B,W):-can(L,B,W).
%********* checks whether a point [X,Y] is in table.*********
in(X,Y):-X>=0,X<G_N,Y>=0,Y<G_N.
%********************** HodW(hite) **************************
hodW(C,K,X1,Y1,X2,Y2,B,W):-
(take(K,X1,Y1,X2,Y2,B,W);can(W,B,W),draw_Pul(G_K,w,G_X,G_Y);step(K,X1,Y1,X2,Y2,B,W)).

t(X1,Y1,X2,Y2,B,W):-
    abs(X2-X1)=:=2,abs(Y2-Y1)=:=2,
    not(elem([_,X2,Y2],B)),not(elem([_,X2,Y2],W)),
    Y is (Y1+Y2)//2,X is (X1+X2)//2,elem([_,X,Y],B).

take(K,X1,Y1,X2,Y2,B,W):-t(X1,Y1,X2,Y2,B,W),
    Y is (Y1+Y2)//2,X is (X1+X2)//2,
    remove([_,X1,Y1],W,W1),
    (Y2=0->draw_Pul(k,w,X2,Y2),addB([k,X2,Y2],W1,W2);
         draw_Pul(K,w,X2,Y2),addB([K,X2,Y2],W1,W2)),
    remove([_,X,Y],B,B1),
    set(pos(w,B1,W2)),
    clear(X1,Y1),clear(X,Y),cantake(K,X2,Y2).
%********* Checkes whether the player can take again ********
cantake(K,X,Y):-pos(C,B,W),Y1 is Y+2,Y2 is Y- 2,X1 is X+2,X2 is X- 2,
    (in(X1,Y1),t(X,Y,X1,Y1,B,W),G_Clicked:=G_Clicked+1,draw_Pul(K,mm,X,Y),G_X := X,G_Y := Y,G_K:=K;
    in(X1,Y2),t(X,Y,X1,Y2,B,W),G_Clicked:=G_Clicked+1,draw_Pul(K,mm,X,Y),G_X := X,G_Y := Y,G_K:=K;
    in(X2,Y1),t(X,Y,X2,Y1,B,W),G_Clicked:=G_Clicked+1,draw_Pul(K,mm,X,Y),G_X := X,G_Y := Y,G_K:=K;
    in(X2,Y2),t(X,Y,X2,Y2,B,W),G_Clicked:=G_Clicked+1,draw_Pul(K,mm,X,Y),G_X := X,G_Y := Y,G_K:=K).
cantake(K,X,Y):-G_Clicked := 0,play.
%************************ Makes a single step ***************
step(k,X1,Y1,X2,Y2,B,W):-
    abs(Y2-Y1)=:=1,abs(X2-X1)=:=1,
    not(elem([_,X2,Y2],B)),not(elem([_,X2,Y2],W)),
    remove([k,X1,Y1],W,W1),
    addB([k,X2,Y2],W1,W2),
    set(pos(w,B,W2)),
    draw_Pul(k,w,X2,Y2),clear(X1,Y1),G_Clicked:=0,play.

step(m,X1,Y1,X2,Y2,B,W):-
Y2=:=Y1- 1,abs(X2-X1)=:=1,
not(elem([_,X2,Y2],B)),not(elem([_,X2,Y2],W)),
    remove([m,X1,Y1],W,W1),   
    (Y2=0->addB([k,X2,Y2],W1,W2),draw_Pul(k,w,X2,Y2);
         addB([m,X2,Y2],W1,W2),draw_Pul(m,w,X2,Y2)),
    set(pos(w,B,W2)),
    clear(X1,Y1),G_Clicked:=0,play.
%********* This must search a move and play it. **************
play:-pos(C,B,W),(isFinal(B,W,X),end(w);
    set_text("sabar yaa, berdoa dulu sebelum main!",_),
    winB(G_Depth,B,W,X),(X> -32000->update_window(_);end(d)),
(pos(C,B1,W1),(isFinal(B1,W1,X),end(b);isDrawW(B1,W1,X),end(d));
set_text("Giliran mu Kawan",_))).
%********************* Kondisi anda **************************
s(w,Y):-Y is "berhasil, kamu menang lawan AI!! ".
s(b,Y):-Y is "coba lagii, kamu kalah dengan AI!! ".
s(d,Y):-Y is "hampir saja menang, now kamu masih seri!! ".
end(X):-s(X,Y),
(yes_no("permainan selesai",Y+"\n mau main lagi gak kau?",!) -> G_P:=1-G_P,
newGame; (!,G_Clicked:= -1,fail)).
end(X).
%*************************
winB(Depth,B,W,X):- isFinal(B,W,X),!.
winB(Depth,B,W,X):- Depth<1,eval(B,W,X),!.
winB(Depth,B,W,X):-can(B,W,B),P is 2,X := -32000,
    ( nextcanB(B1,W1,B,W),D is Depth- 1,winW(D,B1,W1,X1),
    (X1=X -> (Depth=G_Depth ->R:=random(P),P:=P+1,R<1,set(pos(w,B1,W1)))),
    (X1>X -> X:=X1,( Depth=G_Depth->set(pos(w,B1,W1)))),fail;true),!.
winB(Depth,B,W,X):-X := -32000,P is 2,
    ( nextstepB(B1,B,W),D is Depth- 1,winW(D,B1,W,X1),
    (X1=X -> (Depth=G_Depth ->R:=random(P),P:=P+1,R<1,set(pos(w,B1,W)))),
    (X1>X -> X:=X1,( Depth=G_Depth->set(pos(w,B1,W)))),fail;
    (Depth<G_Depth, X= -32000 -> X:=0)),!.


winW(Depth,B,W,X):- isFinal(B,W,X),!.
winW(Depth,B,W,X):- Depth < 1,!,eval(B,W,X).
winW(Depth,B,W,X):-can(W,B,W),X := 32000,
    (nextcanB(W1,B1,W,B),D is Depth- 1,winB(D,B1,W1,X1),
    (X1<X -> X:=X1),fail;true),!.
winW(Depth,B,W,X):-X := 32000,
    (nextstepW(W1,B,W),D is Depth- 1,winB(D,B,W1,X1),
    (X1<X -> X:=X1),fail; (X=32000->X:=0)),!.
%********************** Next(s) **************************
nextcanB(P,Q,B,W):-elem(Z,B),elemOfelem(K,X,Y,Z),
    elem(T,[2,-2]),elem(F,[2,-2]),X1 := X+T,Y1 := Y+F,
    in(X1,Y1),t(X,Y,X1,Y1,W,B),X2 is (X+X1)//2,Y2 is (Y+Y1)//2,
    remove([_,X,Y],B,B1),remove([_,X2,Y2],W,W1),
    (Y1=5->K1 is k;K1 is K),addB([K1,X1,Y1],B1,B2),
    cantakeB(P,Q,B2,W1,X1,Y1,K1).
cantakeB(P,Q,B,W,X,Y,K):-elem(T,[-2,2]),elem(F,[-2,2]),
    X1 is X+T,Y1 is Y+F,in(X1,Y1),t(X,Y,X1,Y1,W,B),
    X2 is (X+X1)//2,Y2 is (Y+Y1)//2,
    remove([_,X,Y],B,B1),remove([_,X2,Y2],W,W1),
    (Y1=5->K1 is k;K1 is K),addB([K1,X1,Y1],B1,B2),
    cantakeB(P,Q,B2,W1,X1,Y1,K1),!.
cantakeB(B,W,B,W,X,Y,K).

nextcanW(P,Q,W,B):-elem(Z,B),elemOfelem(K,X,Y,Z),
    elem(T,[2,-2]),elem(F,[2,-2]),X1 := X+T,Y1 := Y+F,
    in(X1,Y1),t(X,Y,X1,Y1,W,B),X2 is (X+X1)//2,Y2 is (Y+Y1)//2,
    remove([_,X,Y],B,B1),remove([_,X2,Y2],W,W1),
    (Y1=0->K1 is k;K1 is K),addB([K1,X1,Y1],B1,B2),
    cantakeW(P,Q,B2,W1,X1,Y1,K1).
cantakeW(P,Q,W,B,X,Y,K):-elem(T,[-2,2]),elem(F,[-2,2]),
    X1 is X+T,Y1 is Y+F,in(X1,Y1),t(X,Y,X1,Y1,W,B),
    X2 is (X+X1)//2,Y2 is (Y+Y1)//2,
    remove([_,X,Y],B,B1),remove([_,X2,Y2],W,W1),
    (Y1=0->K1 is k;K1 is K),addB([K1,X1,Y1],B1,B2),
    cantakeW(P,Q,B2,W1,X1,Y1,K1),!.
cantakeW(B,W,W,B,X,Y,K).
%**********************
nextstepB(P,B,W):-elem(Z,B),elemOfelem(K,X,Y,Z),
    elem(T,[1,-1]),X1 is X+T,Y1 is Y+1,(
    in(X1,Y1),not(elem([_,X1,Y1],B)),not(elem([_,X1,Y1],W)),
    remove([_,X,Y],B,P1),
    (Y1=5->addB([k,X1,Y1],P1,P);addB([K,X1,Y1],P1,P));
    K=k,Y2 is Y- 1,
    in(X1,Y2),not(elem([_,X1,Y2],B)),not(elem([_,X1,Y2],W)),
    remove([_,X,Y],B,P1),addB([k,X1,Y2],P1,P)).

nextstepW(P,W,B):-elem(Z,B),elemOfelem(K,X,Y,Z),
    elem(T,[1,-1]),X1 is X+T,Y1 is Y- 1,(
    in(X1,Y1),not(elem([_,X1,Y1],B)),not(elem([_,X1,Y1],W)),
    remove([_,X,Y],B,P1),
    (Y1=0->addB([k,X1,Y1],P1,P);addB([K,X1,Y1],P1,P));
    K=k,Y2 is Y+ 1,
    in(X1,Y2),not(elem([_,X1,Y2],B)),not(elem([_,X1,Y2],W)),
    remove([_,X,Y],B,P1),addB([k,X1,Y2],P1,P)).
%********************** Draws Current Position ***************
drawPos:-pos(K,B,W),drawColor(b,B),drawColor(w,W),
         G_Clicked > 0, draw_Pul(G_K,mm,G_X,G_Y).

drawColor(C,[]).
drawColor(C,[H|B]):-drawColor(C,B),elemOfelem(K,X,Y,H),draw_Pul(K,C,X,Y).
%****************** elem(X,Y) X is element of Y***************
elem(H,[H|L]).
elem(A,[H|L]):-elem(A,L).
%********* Removes A element from list P: Result Q ***********
remove(A,[A|P],P).
remove(A,[H|P],[H|Q]):-remove(A,P,Q).
%*********** Adds element in the begining of the list*********
addB(A,P,[A|P]).
%******************* Takes the Ordered pair******************
elemOfelem(C,X,Y,[C,X,Y]).
%********************* Eval **********************************

evalB(0,[]):-!.
evalB(P,[H|L]):-elemOfelem(K,X,Y,H),(K=m->D is Y + G_Val;D is 5 + G_Val),
    evalB(Q,L),P is Q+D.
evalW(0,[]):-!.
evalW(P,[H|L]):-elemOfelem(K,X,Y,H),
    (K=m->D is 8- Y + G_Val;D is 8 + G_Val),
    evalW(Q,L),P is Q+D.
eval([],W,-2048).
eval(B,[],2048).
eval(B,W,X):-evalB(P,B),evalW(Q,W),X is P- Q.

isFinal([],W,-2048).
isFinal(B,[],2048).

stepW1(W,B):-elem(Z,B),elemOfelem(K,X,Y,Z),
    elem(T,[1,-1]),X1 is X+T,Y1 is Y- 1,(
    in(X1,Y1),not(elem([_,X1,Y1],B)),not(elem([_,X1,Y1],W));
    K=k,Y2 is Y+ 1,
    in(X1,Y2),not(elem([_,X1,Y2],B)),not(elem([_,X1,Y2],W))).

isDrawW(B,W,X):- not(stepW1(B,W)),not(can(W,B,W)).
%*************************************************************************