Программирование игр, страница 2

write("O"),

shiftwindow(move_w),

writef(" (%2,%2) ",Y,X).

play(user) :five_in_line(o),!,

shiftwindow(status_w),

write("Ты не выдержал испытания, дерзкий плут!"),nl.

play(comp) :five_in_line(x),!,

shiftwindow(status_w),

write("Проиграть Вам, милорд, - честь для меня!"),nl.

play(Player) :make_move(Player,Move),

player_type(Player,Type),

assertz(x_o(Type,Move)),

show_move(Player,Move),

opponent(Player,Player1),

play(Player1).

alpha_beta(Move,Weight) :cell(Move1),

retract(cell(Move1),Cells),

weight(Move1,Weight1),

alpha_beta(Move2,Weight2),

best_move(Move1,Weight1,Move2,Weight2,Move,Weight).

alpha_beta(a(Y,X),0) :Y=round(height/2),

X=round(width/2).

comp_make_moves :retractall(cell(_),Cells),

add_all_near_cell(x,[]),

add_all_near_cell(o,[]).

best_move(Move,Weight1,_,Weight2,Move,Weight1) :Weight1>Weight2,!.

best_move(_,_,Move,Weight,Move,Weight).

five_in_line(Type) :x_o(Type,Move),

search_in_direction(Type,Move,5,+1,0).

five_in_line(Type) :x_o(Type,Move),

search_in_direction(Type,Move,5,0,+1).

five_in_line(Type) :x_o(Type,Move),

search_in_direction(Type,Move,5,-1,+1).

five_in_line(Type) :x_o(Type,Move),

search_in_direction(Type,Move,5,+1,-1).

search_in_direction(Type,a(X,Y),N,Direction1,Direction2) :X1=X+Direction1,

Y1=Y+Direction2,

x_o(Type,a(X1,Y1)),

search_in_direction(Type,a(X1,Y1),N1,Direction1,Direction2),

N=N1+1.

search_in_direction(_,_,1,_,_).

opponent(user,comp).

opponent(comp,user).

add_cell(a(X,Y)) :X>=0,

Y>=0,

TX=width+1,

TY=height+1,

X<=TX,

Y<=TY,

not(cell(a(X,Y))),

not(x_o(_,a(X,Y))),

assertz(cell(a(X,Y))).

add_cell(_).

add_all_near_cell(Type,Used) :x_o(Type,a(X,Y)),

not(belong(a(X,Y),Used)),

Xm1=X-1,

Xp1=X+1,

Ym1=Y-1,

Yp1=Y+1,

add_cell(a(Xm1,Y)),

add_cell(a(Xp1,Y)),

add_cell(a(X,Ym1)),

add_cell(a(X,Yp1)),

add_cell(a(Xm1,Ym1)),

add_cell(a(Xm1,Yp1)),

add_cell(a(Xp1,Ym1)),

add_cell(a(Xp1,Yp1)),

add_all_near_cell(Type,[a(X,Y)|Used]).

add_all_near_cell(_,_).

move_cursor(false,escape,X,Y) :- !,

shiftwindow(status_w),

clearwindow,

write("Вы желаете покинуть поле сражения? (y/n) "),

get_char(L1,C1),

write(C1),nl,

want_exit(L1,C1),

write("Я рад, что вы передумали."),nl,

shiftwindow(field_w),

get_char(L2,C2),

move_cursor(L2,C2,X,Y).

move_cursor(false,enter,X,Y) :- !,

cursor(Y,X).

move_cursor(true,up,X,Y) :cursor(Old_Y,Old_X),

Temp=Old_Y-1,

max(Temp,0,New_Y),

cursor(New_Y,Old_X),

get_char(L,C),

move_cursor(L,C,X,Y).

move_cursor(true,down,X,Y) :cursor(Old_Y,Old_X),

Temp=Old_Y+1,

min(Temp,height,New_Y),

cursor(New_Y,Old_X),

get_char(L,C),

move_cursor(L,C,X,Y).

move_cursor(true,left,X,Y) :cursor(Old_Y,Old_X),

Temp=Old_X-1,

max(Temp,0,New_X),

cursor(Old_Y,New_X),

get_char(L,C),

move_cursor(L,C,X,Y).

move_cursor(true,right,X,Y) :cursor(Old_Y,Old_X),

Temp=Old_X+1,

min(Temp,width,New_X),

cursor(Old_Y,New_X),

get_char(L,C),

move_cursor(L,C,X,Y).

move_cursor(true,home,X,Y) :cursor(Old_Y,_),

New_X=0,

cursor(Old_Y,New_X),

get_char(L,C),

move_cursor(L,C,X,Y).

move_cursor(true,end,X,Y) :cursor(Old_Y,_),

New_X=width,

cursor(Old_Y,New_X),

get_char(L,C),

move_cursor(L,C,X,Y).

move_cursor(true,pgup,X,Y) :cursor(_,Old_X),

New_Y=0,

cursor(New_Y,Old_X),

get_char(L,C),

move_cursor(L,C,X,Y).

move_cursor(true,pgdn,X,Y) :cursor(_,Old_X),

New_Y=height,

cursor(New_Y,Old_X),

get_char(L,C),

move_cursor(L,C,X,Y).

move_cursor(_,_,X,Y) :get_char(L,C),

move_cursor(L,C,X,Y).

weight(Move,X) :wf(X,Type,DX1,DY1,DX2,DY2,N),

search_in_direction(Type,Move,N1,DX1,DY1),

search_in_direction(Type,Move,N2,DX2,DY2),

N1+N2=N.

weight(_,4).

wf(10,x,DX1,DY1,DX2,DY2,6) :wd(DX1,DY1,DX2,DY2).

wf(9,o,DX1,DY1,DX2,DY2,6) :wd(DX1,DY1,DX2,DY2).

wf(8,x,DX1,DY1,DX2,DY2,5) :wd(DX1,DY1,DX2,DY2).

wf(7,o,DX1,DY1,DX2,DY2,5) :wd(DX1,DY1,DX2,DY2).

wf(6,x,DX1,DY1,DX2,DY2,4) :wd(DX1,DY1,DX2,DY2).

wf(5,o,DX1,DY1,DX2,DY2,4) :wd(DX1,DY1,DX2,DY2).

wd(-1,0,+1,0).

wd(-1,+1,+1,-1).

wd(0,+1,0,-1).

wd(+1,+1,-1,-1).

get_char(L,C) :readchar(C1),

check_char(C1,L,C).

check_char('\0',true,C) :- !,

readchar(C).

check_char(C,false,C).

belong(X,[X|_]) :- !.

belong(X,[_|L]) :belong(X,L).

max(X,Y,X) :X>=Y,!.

max(_,Y,Y).

min(X,Y,X) :X<=Y,!.

min(_,Y,Y ).

want_exit(false,C) :yes(C),

exit.

want_exit(_,_).

yes('Y').

yes('y').

player_type(user,x).

player_type(comp,o).

GOAL

init_player(Player),

play(Player).