Created
August 13, 2022 19:46
-
-
Save ronzhin-dmitry/1468279e299f9fb83537d2b5e4ad7aab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def evaluate(self, last_moves): #Very simple static evaluation, only last move is taken into consideration. Counts number of line segments that this move appears in and returns score for the move | |
evaluation = 0 | |
init_move_switch = self.board[last_moves[0]] | |
move_switch = init_move_switch | |
for position in last_moves: | |
self.board[position] = -1 | |
for position in last_moves: | |
self.board[position] = move_switch | |
move_switch = (move_switch + 1) % 2 | |
turn = self.board[position] | |
multiplier = 1 | |
if turn > 0: | |
multiplier = -0.5 | |
for direction in self.positive_directions: | |
line_count = 1 | |
can_move = True | |
cur_position = list(position) | |
is_blocked1 = True | |
while can_move: | |
cur_position[0] += direction[0] | |
cur_position[1] += direction[1] | |
if not (cur_position[0] >= 0 and cur_position[0] <= 14 and cur_position[1] >= 0 and cur_position[1] <= 14): | |
can_move = False | |
else: | |
if self.board[tuple(cur_position)] == turn: | |
line_count += 1 | |
if line_count >= 5: | |
for position in last_moves: | |
self.board[position] = init_move_switch | |
init_move_switch = (init_move_switch + 1) % 2 | |
return multiplier * float('inf') | |
else: | |
if self.board[tuple(cur_position)] == -1: | |
is_blocked1 = False | |
can_move = False | |
cur_position = list(position) | |
can_move = True | |
is_blocked2 = True | |
while can_move: | |
cur_position[0] -= direction[0] | |
cur_position[1] -= direction[1] | |
if not (cur_position[0] >= 0 and cur_position[0] <= 14 and cur_position[1] >= 0 and cur_position[1] <= 14): | |
can_move = False | |
else: | |
if self.board[tuple(cur_position)] == turn: | |
line_count += 1 | |
if line_count >= 5: | |
for position in last_moves: | |
self.board[position] = init_move_switch | |
init_move_switch = (init_move_switch + 1) % 2 | |
return multiplier * float('inf') | |
else: | |
if self.board[tuple(cur_position)] == -1: | |
is_blocked2 = False | |
can_move = False | |
cur_position = list(position) | |
if line_count >= 5: | |
for position in last_moves: | |
self.board[position] = init_move_switch | |
init_move_switch = (init_move_switch + 1) % 2 | |
return multiplier * float('inf') | |
mul_blocked = 1 | |
if line_count == 4: | |
mul_blocked = 4 | |
elif line_count == 3: | |
mul_blocked = 2 | |
if not is_blocked1: | |
mul_blocked *= line_count | |
if not is_blocked2: | |
mul_blocked *= line_count | |
if line_count > 1: | |
evaluation += multiplier * line_count * mul_blocked | |
if evaluation == 0: | |
evaluation = multiplier * 1 | |
return evaluation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment