forked from bcorfman/raven-checkers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
onek_eval.py
63 lines (55 loc) · 2.52 KB
/
onek_eval.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
from globalconst import BLACK, WHITE, KING
from goalevaluator import GoalEvaluator
from onekingattack import GoalOneKingAttack
from onekingflee import GoalOneKingFlee
class OneKingAttackOneKingEvaluator(GoalEvaluator):
def __init__(self, bias):
GoalEvaluator.__init__(self, bias)
def calculate_desirability(self, board):
plr_color = board.to_move
enemy_color = board.enemy
# if we don't have one man on each side or the player
# doesn't have the opposition, then goal is undesirable.
if board.count(BLACK) != 1 or board.count(WHITE) != 1 or not board.has_opposition(plr_color):
return 0.0
player = board.get_pieces(plr_color)[0]
p_idx, p_val = player
p_row, p_col = board.row_col_for_index(p_idx)
enemy = board.get_pieces(enemy_color)[0]
e_idx, e_val = enemy
e_row, e_col = board.row_col_for_index(e_idx)
# must be two kings against each other and the distance
# between them at least three rows or cols away
if (p_val & KING) and (e_val & KING) and (abs(p_row - e_row) > 2 or abs(p_col - e_col) > 2):
return 1.0
return 0.0
def set_goal(self, board):
player = board.to_move
board.remove_all_subgoals()
goal_set = board.add_white_subgoal if player == WHITE else board.add_black_subgoal
goal_set(GoalOneKingAttack(board))
class OneKingFleeOneKingEvaluator(GoalEvaluator):
def __init__(self, bias):
GoalEvaluator.__init__(self, bias)
def calculate_desirability(self, board):
plr_color = board.to_move
enemy_color = board.enemy
# if we don't have one man on each side or the player
# has the opposition (meaning we should attack instead),
# then goal is not applicable.
if board.count(BLACK) != 1 or board.count(WHITE) != 1 or board.has_opposition(plr_color):
return 0.0
player = board.get_pieces(plr_color)[0]
p_idx, p_val = player
enemy = board.get_pieces(enemy_color)[0]
e_idx, e_val = enemy
# must be two kings against each other; otherwise it's
# not applicable.
if not ((p_val & KING) and (e_val & KING)):
return 0.0
return 1.0
def set_goal(self, board):
player = board.to_move
board.remove_all_subgoals()
goal_set = board.add_white_subgoal if player == WHITE else board.add_black_subgoal
goal_set(GoalOneKingFlee(board))