%--------------------------------------------------------------% % Searching game trees (minimax and alpha-beta pruning) % % (C) 1998 Zdravko Markov (adaptation from Ivan Bratko's book) % % The game tree is represented by the following predicates: % % moves(Pos,ListOfSuccessorPositions) - possible moves % % utility(Pos,Utility) - utility function value of terminals % % min(Position) - MIN is to move at Positions % % max(Position) - MAX is to move at Positions % %--------------------------------------------------------------% %--------------------------------------------------------------% % Minimax search % % call: minimax(+Pos,-BestSuccessor,-Utility). % %--------------------------------------------------------------% minimax(Pos,Succ,Utility) :- moves(Pos,SuccList), !, % not a terminal position best_move(SuccList,Succ,Utility). minimax(Pos,_,Utility) :- % a terminal position utility(Pos,Utility). % an explicit utility best_move([P],P,V) :- minimax(P,_,V), !. best_move([P1|Ps],Succ,Val) :- minimax(P1,_,V1), best_move(Ps,P2,V2), better(P1,V1,P2,V2,Succ,Val). better(P1,V1,_,V2,P1,V1) :- min(P1), V1>V2, !. better(P1,V1,_,V2,P1,V1) :- max(P1), V1Beta, !. good_move(_,Alpha,_,P,V,P,V) :- max(P), VAlpha, !. newbounds(Alpha,Beta,P,V,Alpha,V) :- max(P), V