Игра - Prolog
Решение задачи: «Игра»
textual
Листинг программы
% pn_step(State, Step, Player, Move, X, Y). :- dynamic(pn_step/6). pn_init(15, 17). pn_act(x, -2). pn_act(x, 5). pn_act(y, -3). pn_act(y, 2). pn_limit(x, 9, 20). pn_limit(y, 9, 20). pn_win(Z, Z). pn_loss(14, 13). pn_go(Method) :- retractall( pn_step(_, _, _, _, _, _) ), Step = 0, Player = n, pn_init(X, Y), Move = 0, State = start, asserta( pn_step(State, Step, Player, Move, X, Y) ), format('~w ~w ~w ~w ~w ~w~n', [State, Step, Player, Move, X, Y]), between(0, 100, N), N < 100, pn_play(Method), ( pn_step(win, _, _, _, _, _) ; pn_step(loss, _, _, _, _, _) ), !. pn_play(Method) :- once( pn_step(_, Step, Player, _, X, Y) ), ( Player = x -> Player1 = y ; Player1 = x ), pn_play(Method, Player1, X, Y, X1, Y1, Move), Step1 is Step + 1, ( pn_win(X1, Y1), State1 = win ; pn_loss(X1, Y1), State1 = loss ; State1 = step ), asserta( pn_step(State1, Step1, Player1, Move, X1, Y1) ), format('~w ~w ~w ~w ~w ~w~n', [State1, Step1, Player1, Move, X1, Y1]), !. pn_play(1, x, X, Y, X1, Y, Move) :- pn_move(1, x, X, X1, Move). pn_play(1, y, X, Y, X, Y1, Move) :- pn_move(1, y, Y, Y1, Move). pn_play(2, x, X, Y, X1, Y, Move) :- setof( Delta-X0-Move0, ( pn_move(1, x, X, X0, Move0), Delta is abs(abs(X0) - abs(Y)) ), Moves ), select(_-X1-Move, Moves, _), \+ pn_loss(X1, Y), \+ ( pn_move(1, y, Y, Y1, _), pn_win(X1, Y1) ). pn_play(2, y, X, Y, X, Y1, Move) :- setof( Delta-Y0-Move0, ( pn_move(1, y, Y, Y0, Move0), Delta is abs(abs(X) - abs(Y0)) ), Moves ), select(_-Y1-Move, Moves, _), \+ pn_loss(X, Y1), \+ ( pn_move(1, x, X, X1, _), pn_win(X1, Y1) ). pn_play(2, Player, X, Y, X1, Y1, Move) :- pn_play(1, Player, X, Y, X1, Y1, Move). pn_move(1, Player, Z, Z1, Move) :- pn_act(Player, Move), Z1 is Z + Move, pn_limit(Player, Low, High), Low < Z1, Z1 < High. /** <examples> ?- pn_go(1). ?- pn_go(2). */
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д