-
Notifications
You must be signed in to change notification settings - Fork 2
/
move_ordering.py
88 lines (68 loc) · 2.5 KB
/
move_ordering.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import random
from chess import BLACK, Board, Move
from psqt import evaluate_capture, evaluate_piece, get_phase
def organize_moves(board: Board):
"""
This function receives a board and it returns a list of all the
possible moves for the current player, sorted by importance.
Right now we are only sending the moves that are capturing pieces
at the starting positions in our array (so we can prune more and earlier).
Arguments:
- board: chess board state
Returns:
- legal_moves: list of all the possible moves for the current player.
"""
non_captures = []
captures = []
for move in board.legal_moves:
if board.is_capture(move):
captures.append(move)
else:
non_captures.append(move)
random.shuffle(captures)
random.shuffle(non_captures)
return captures + non_captures
def organize_moves_quiescence(board: Board):
"""
This function receives a board and it returns a list of all the
possible moves for the current player, sorted by importance.
Arguments:
- board: chess board state
Returns:
- moves: list of all the possible moves for the current player sorted based on importance.
"""
phase = get_phase(board)
# filter only important moves for quiescence search
captures = filter(
lambda move: board.is_zeroing(move) or board.gives_check(move),
board.legal_moves,
)
# sort moves by importance
moves = sorted(
captures,
key=lambda move: mvv_lva(board, move, phase),
reverse=(board.turn == BLACK),
)
return moves
def mvv_lva(board: Board, move: Move, phase: float) -> float:
"""
This function receives a board and a move and it returns the
move's value based on the phase of the game. It's based on the
idea that the most valuable victim being captured by the least
valuable attacker is the best move.
Arguments:
- board: chess board state
- move: chess move
- phase: current phase of the game
Returns:
- mvv_lva: value of the move
"""
move_value: float = 0
# evaluating position
from_value = evaluate_piece(board, move.from_square, phase)
to_value = evaluate_piece(board, move.to_square, phase)
move_value += to_value - from_value
# evaluating capture
if board.is_capture(move):
move_value += evaluate_capture(board, move, phase)
return -move_value if board.turn else move_value