Listing Program Game "Grab Your Apples"




LISTING PROGRAM



?-
G_Comp_First=0,
G_Take_Last=0,
G_background is bitmap_image("b.bmp",_),
G_background1 is bitmap_image("menu1.bmp",_),
G_apel is bitmap_image("apple11.bmp",_),

pen(0,0),
 %======menu
window(G_menu,_,win_menu(_),"Grab Your Apples",100,100,770,525).

%============background menu
win_menu(paint):-
draw_bitmap(0,0,G_background1,_,_).
% tampilan button
win_menu(init):-
button(_,_,start(_),"Mulai",600,100,130,40),
button(_,_,help(_),"Cara main",600,150,130,40),
button(_,_,about(_),"Tentang",600,200,130,40).

start(press):-
pen(5,rgb(0,255,0)),
close_window(G_menu),
window_n(G_Main, _, win_func, "Manen Apel",100,100,770,525,_,_,rgb(255,255,255)).

help(press) :-
message("Cara main","Klik Apel yang ingin diambil. \n
Anda dapat mengambil seberapa banyak Anda ingin tetapi hanya
dari satu kolom.\n
Bila kebagian terakhir maka di anggap kalah.", i).

about(press):-
     message("Tentang"," Game Prolog : Apel
     \n Kelas : 3IA12 \n NPM : 54412151 \n Nama : La Ode Rahmat Putra Rustaman
      \n\n UNIVERSITAS GUNADARMA", i).

% Membuat menu tab
win_func(init) :-
menu( pop_up, _,_,menu_level(_), "&Tingkat"),
menu( normal, _, _, menu_options(_), "&Pengaturan"),
menu( normal, _,_,menu_exit(_), "&Exit").
% Membuat menu untuk level
menu_level(init):-
menu(normal,_,_,menu_easy(_),"&Mudah"),
menu(normal,_,_,menu_medium(_),"&Sedang"),
menu(normal,_,_,menu_hard(_),"&Susah").




menu_options(press) :-
  window_n( _, _, options_func, "Pengaturan", 200, 100, 300, 200,?,_,_).
options_func(init) :-
  check_box( A, _, check_func, "Komputer jalan dahulu", 20, 20, 200, 20),
  G_A:=A,
  set_check_box_value(G_A,G_Comp_First),
  check_box( B, _, check_func, "Mengambil yang terakhir", 20, 60, 200, 20),
  G_B:=B,
  set_check_box_value(G_B,G_Take_Last),
  button( _, _, ok_func(_), "OK", 20, 120, 80, 30),
  button( _, _, cancel_func(_), "Batal", 120, 120, 80, 30).

ok_func(press) :-
  G_Comp_First:=get_check_box_value(G_A),
  G_Take_Last:=get_check_box_value(G_B),
  (G_Take_Last=1->
    set_text("Manen Apel(Ambil yang terakhir)",G_Main)
  else
    set_text("Manen Apel(Jangan ambil yang terakhir)",G_Main)),
  close_window(parent(_)).
cancel_func(press) :-
  close_window(parent(_)).

check_func(press) :-
  set_check_box_value(_,1-get_check_box_value(_)).


menu_exit(press) :-
  close_window(_).

% Menu Easy
menu_easy(press) :-
(G_Comp_First=0->
set(pos([1,2,3,3,2,1]))
else
Pos=[1,2,3,3,2,1],
N is 2*random(1),
el(Pos,A, N),
A2 is A - 4,
replace(Pos2, Pos, A2, N),
set(pos(Pos2))),
update_window(_).
% Menu Medium
menu_medium(press) :-
(G_Comp_First=0->
set(pos([2,3,4,4,3,2]))
else
Pos=[2,3,4,4,3,2],
N is 2*random(3),
el(Pos,A, N),
A2 is A - 3,
replace(Pos2, Pos, A2, N),
set(pos(Pos2))),
update_window(_).





% Menu Hard
menu_hard(press) :-
(G_Comp_First=0->
set(pos([5,4,3,3,4,5]))
else
Pos=[5,4,3,3,4,5],
N is 2*random(5),
el(Pos,A, N),
A2 is A - 1,
replace(Pos2, Pos, A2, N),
set(pos(Pos2))),
update_window(_).
% Memanggil gambar sebagai background
win_func(paint):-
draw_bitmap(0,0,G_background,_,_),
pos(Pos),
el(Pos,El,N),
for(I,1,El),
% Jarak antara masing-masin item
X is 70+100*N,
Y is 300-60*I,
% Untuk bentuk item
draw_bitmap(X,Y,G_apel,_,_),
fail.
% Area mouse click
win_func(mouse_click(X,Y)):-
X1 is (X-50)//100,
Y1 is (255-Y)//40,
X1=<6,X>=40,
pos(Pos),
el(Pos,El,X1),
El>Y1,
replace(Pos2, Pos, Y1, X1),
rect(100*X1+40,0,100*X1+40,265- 40*Y1),
(Pos2=[0,0,0,0,0,0]->
set(pos(Pos2)),
end(0)
else
wait(0.5),
play(Pos3, Pos2),
(Pos3=[0,0,0,0,0,0]->
end(1)),
set(pos(Pos3))),
update_window(_).
win_func(mouse_click(X,Y)):-
beep.
% Inti program AI
play(Pos3, Pos2):-
G_Take_Last=0,
count_successes(not_trivial(Pos2))<2,
find_max(Pos2,Max,N),
New is (Max>1,count_successes(not_empty(Pos2)) mod
3=:=1 -> 1 else 0),
replace(Pos3, Pos2, New, N).



play(Pos3, Pos2):-
el(Pos2,A, N),
R:=0,
add_xor(R,Pos2,N),
R<A,
replace(Pos3, Pos2, R, N).
play(Pos3, Pos2):-
find_max(Pos2,Max,N),
Max2 is (Max>1, random(2)=:=0 ->
Max - 2
else
Max - 1),
replace(Pos3, Pos2, Max2, N).

not_empty(Pos):-
el(Pos,A, _),
A>0.
not_trivial(Pos):-
el(Pos,A, _),
A>1.
el([H|T],H,0).
el([H|T],El,N):-
el(T,El,N1),
N is N1+1.
replace([H|T],[_|T],H,0).
replace([H|T2],[H|T],El,N):-
replace(T2,T,El,N1),
N is N1+1.
find_max([H],H,0).
find_max([H|T],A, N) :-
find_max(T,A1,N1),
(A1<H->
A is H,
N is 0
else
A is A1,
N is N1+1).
add_xor(_,[],_):-!.
add_xor(R,[H|T],0):- !,
add_xor(R, T, -1).
add_xor(R,[H|T],N):-
N1 is N- 1,
R:=R xor H,
add_xor(R, T, N1).
end(Flag):-
Flag=G_Take_Last->
message("Maaf","Anda kalah dalam permainan",i),
beep("kalah.wav")
else
message("Selamat","Anda adalah pemenang",!),
beep("menang.wav").


Komentar

Postingan populer dari blog ini

Daftar Raja Muna

MENGENAL SUKU MUNA

STRATEGIC MANAGEMENT (Wheelen and Hunger)