/* finin@Umbc.edu, 2001 a simple minimax algorithm for playing two person games. To define the game, provide definitions for start(S) -- true if S is the initial game state. move(S1,S2) - true if there is a legal move from state S1 to S2. win(P,S) -- true if state S is a win for player P. draw(S) -- true of state S is a draw eval(S,V) -- true if the static evaluator for state S is V (with positive being good for player 1 and negative for player 2). printBoard(S) -- prints the curent board We assume that a state is of the form s(P,B) where P is the player whose turn is next (1 or 2) and B represents the "board position". */ :- use_module(mm). play :- play(2). % play(N) uses a lookahed depth of N for both players. play(N) :- play(N,N). % play(N1,N2) plays a game with a look ahead depth of N! for player 1 % and N2 for player2. play(N1,N2) :- start(S), showState(S), play(N1,N2,S). play(_,_,S) :- % the game is over if someone has won. win(P,S), format("Player ~p has won.\n",[P]). play(_,_,S) :- % the game is over if it's a draw draw(S), format("The game has ended in a draw.\n",[]). play(N1,N2,S) :- % chose the best move and continue playing from there. mm(N1,S,S2,_), showState(S2), play(N2,N1,S2). showState(s(Player,Board)) :- format("~p's move.\n",[Player]), printBoard(Board) -> true | defaultPrintBoard(Board). defaultPrintBoard(B) :- format(" ~p\n",[B]).