873 8 [8,.] [.,8]. 8 916 9 [9,.] 8 [.,8] 24 [2,.] 1 [4,.]4 4

Post on 29-Dec-2015

238 Views

Category:

Documents

4 Downloads

Preview:

Click to see full reader

TRANSCRIPT

8 7 3

8[8,.]

[.,8]

8

9 1 6

9

[.,8]

[9,.]

8

[.,8]

2 4

[2,.]

1

[4,.]4

4

1 3 5

5[1,.]

[.,5]4

[3,.]

5

3 9 2

9

[.,5]

[3,.]

4

[9,.]

5

[.,5]

6 5

[6,.]

2

6

54

54

3

5

done

• Continue with Prolog code for minimax

• Jump to alpha-beta pruning

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).

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). ).

8 7 3

8[8,.]

[.,8]

8

9

[.,8]

[9,8][.,8]

X

8

[.,8]

2 4

[2,8]

1

[4,8]4

4

[.,8]

1 3 5

5[4,.]

[4,5]4

[4,.]

[4,.]

5

3 9

X

[4,5]

[4,5]

4

[9,5]

[4,.]

5

[4,5]

6

[6,5]X

54

[4,5]

[4,.]

54

[5,.]

[5,.]

[5,.]

2

[5,.]X

54

5[5,.]

[5,.]

2 3

< 5[5,.]

[5, <5]X

X

done

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).

/* 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) ).

Othello

Othello

> 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/<username>/<path>/myothello.pl’.…(Othello window pops up.)

OthelloWhite player program Shell program Black player program

initialize(white,SW)initialize(black,SB)

SW SB

best_move(SB, Move)

Move=6-5

Ask move

Initialize

move(6-5, SB, NSB)Execute move(shell executes movein Othello window)

opponent_move(6-5, SW, NSW)

OthelloWhite player program Shell program Black player program

game_over(SB, …)

Game over?no

best_move(SW, Move)

SW SB

Move = c-r

Ask move

Execute move

move(c-r, SW, NSW) opponent_move(c-r, SB, NSB)

top related