Download presentation
Presentation is loading. Please wait.
Published byVivien Merritt Modified over 9 years ago
1
873 8 [8,.] [.,8]
2
8 916 9 [9,.]
3
8 [.,8] 24 [2,.] 1 [4,.]4 4
4
135 5 [1,.] [.,5] 4 [3,.]
5
5 392 9 [.,5] [3,.] 4 [9,.]
6
5 [.,5] 65 [6,.] 2 6 5 4
7
5 4 … 3 5
8
done Continue with Prolog code for minimax Jump to alpha-beta pruning
9
Certainly the worst value/move /* Uses: move(+Pos, -Move) :- Move is a legal move in position Pos. move(+Move, +Pos, -Pos1) :- Making Move in position Pos results in position Pos1. value(+Pos, -V) :- V is the static value of position Pos for player 1. Should be between -999 and +999, where +999 is best for player 1. */ minimax(Pos, Move, Depth) :- minimax(Depth, Pos, 1, _, Move). /* minimax(+Depth, +Position, +Player, -BestValue, -BestMove) :- Chooses the BestMove from the from the current Position using the minimax algorithm searching Depth ply ahead. Player indicates if this move is by player (1) or opponent (-1). */ minimax(0, Position, Player, Value, _) :- value(Position, V), Value is V*Player. % Value is from the current player’s perspective. minimax(D, Position, Player, Value, Move) :- D > 0, D1 is D - 1, findall(M, move(Position, M), Moves), % There must be at least one move! minimax(Moves, Position, D1, Player, -1000, nil, Value, Move).
10
minimax(0, Position, Player, Value, _) :- value(Position, V), Value is V*Player. % Value is from the current player’s perspective. minimax(D, Position, Player, Value, Move) :- D > 0, D1 is D - 1, findall(M, move(Position, M), Moves), % There must be at least one move! minimax(Moves, Position, D1, Player, -1000, nil, Value, Move). /* minimax(+Moves,+Position,+Depth,+Player,+Value0,+Move0,-BestValue,-BestMove) Chooses the Best move from the list of Moves from the current Position using the minimax algorithm searching Depth ply ahead. Player indicates if we are currently minimizing (-1) or maximizing (1). Move0 records the best move found so far and Value0 its value. */ minimax([], _, _, _, Value, Best, Value, Best). minimax([Move|Moves],Position,D,Player, Value0,Move0,BestValue,BestMove):- move(Move, Position, Position1), Opponent is -Player, minimax(D, Position1, Opponent, OppValue, _OppMove), Value is -OppValue, ( Value > Value0 -> minimax(Moves,Position,D,Player, Value,Move,BestValue,BestMove). ; minimax(Moves,Position,D,Player, Value0,Move0,BestValue,BestMove). ).
11
873 8 [8,.] [.,8]
12
8 9 [9,8] [.,8] X
13
8 24 [2,8] 1 [4,8]4 4 [.,8]
14
135 5 [4,.] [4,5] 4 [4,.]
15
5 39 X [4,5] 4 [9,5] [4,.]
16
5 [4,5] 6 [6,5] X 5 4 [4,5] [4,.]
17
5 4 [5,.] 2 X
18
5 4 5 23 < 5 [5,.] [5, <5] X X
19
done
20
1000 serves as infinity /* Uses: move(+Pos, -Move) :- Move is a legal move in position Pos. move(+Move, +Pos, -Pos1) :- Making Move in position Pos results in position Pos1. value(+Pos, -V) :- V is the static value of position Pos for player 1. Should be between -999 and +999, where +999 is best for player 1. */ alph_bet(Pos, Move, Depth) :- alph_bet(Depth, Pos, 1, -1000, 1000, _, Move). /* alph_bet(+Depth, +Position, +Player, +Alpha, +Beta, -BestValue, -BestMove) :- Chooses the BestMove from the from the current Position using the alpha beta algorithm searching Depth ply ahead. Player indicates if the next move is by player (1) or opponent (-1). */ alph_bet(0, Position, Player, _, _, Value, _) :- value(Position, V), Value is V*Player. alph_bet(D, Position, Player, Alpha, Beta, Value, Move) :- D > 0, D1 is D - 1, findall(M, move(Position, M), Moves), alph_bet(Moves, Position, D1, Player, Alpha, Beta, nil, Value, Move).
21
/* alph_bet(+Moves,+Position,+Depth,+Player,+Alpha,+Beta,+Move0, -BestValue,-BestMove) Chooses the Best move from the list of Moves from the current Position using the alpha beta algorithm searching Depth ply ahead. Player indicates if the next move is by player (1) or opponent (-1). Move0 records the best move found so far and Alpha its value. If a value >= Beta is found, then this position is too good to be true: the opponent will not move us into this position. */ alph_bet([], _, _, _, Value, _, Best, Value, Best). alph_bet([Move|Moves], Position, D, Player, Alpha, Beta, Move0, BestValue, BestMove):- move(Move, Position, Position1), Opponent is -Player, OppAlpha is -Beta, OppBeta is -Alpha, alph_bet(D, Position1, Opponent, OppAlpha, OppBeta, OppValue, _OppMove), Value is -OppValue, ( Value >= Beta -> BestValue = Value, BestMove = Move % abort: too good to be true ; Value > Alpha -> alph_bet(Moves,Position,D,Player,Value,Beta,Move,BestValue,BestMove) ; alph_bet(Moves,Position,D,Player,Alpha,Beta,Move0,BestValue,BestMove) ).
22
Othello
23
> prolog –l /it/kurs/logpro/othello/play_game.pl … | ?- start_game. Select white player. (1) human (2) program |: 2. White player full program name : |: std. … Black players full program name : |: ’/home/ / /myothello.pl’. … (Othello window pops up.)
24
Othello White player programShell programBlack player program initialize(white,SW) initialize(black,SB) SWSB best_move(SB, Move) Move=6-5 Ask move Initialize move(6-5, SB, NSB) Execute move (shell executes move in Othello window) opponent_move(6-5, SW, NSW)
25
Othello White player programShell programBlack player program game_over(SB, …) Game over? no best_move(SW, Move) SWSB Move = c-r Ask move Execute move move(c-r, SW, NSW) opponent_move(c-r, SB, NSB)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.