def actual_mobility(board, current_piece, other_piece): from Board_Representation import get_moves counter_legal_moves_current_piece = len(get_moves(board, current_piece)) counter_legal_moves_other_piece = len(get_moves(board, other_piece)) actual_mobility_heuristic_value = 0 if counter_legal_moves_current_piece + counter_legal_moves_other_piece != 0: actual_mobility_heuristic_value = 100 * ( counter_legal_moves_current_piece - counter_legal_moves_other_piece ) / (counter_legal_moves_current_piece + counter_legal_moves_other_piece) return actual_mobility_heuristic_value
def negamax_alg(board, current_piece, color, current_depth, target_depth): from Board_Representation import get_moves if current_depth == target_depth or is_terminal_state(board, current_piece) == True: return (None, None, color * heuristic_evaluation_function(board, computer_piece)) current_value = -math.inf current_row = -1 current_column = -1 valid_moves = get_moves(board, current_piece) if len(valid_moves) == 0: return (None, None, 0) else: for move in valid_moves: board_modified = np.copy(board) add_piece_to_board(board_modified, move[0], move[1], current_piece) new_row, new_column, new_value = negamax_alg(board_modified, 3 - current_piece, -color, current_depth + 1, target_depth) if -new_value > current_value: current_value = -new_value current_row = move[0] current_column = move[1] return (current_row, current_column, current_value)
def is_board_quiet(board, current_piece, prev_move_row, prev_move_column): from Board_Representation import get_moves, isOnBoard if len(get_moves(board, current_piece)) == 1: return False if isOnBoard(prev_move_row, prev_move_column) == True: if (prev_move_row, prev_move_column) in CORNERS_LIST == True or ( prev_move_row, prev_move_column) in ADJACENT_CORNERS_LIST == True: return False return True
def local_maximization(board, piece): from Board_Representation import get_moves current_value = -math.inf current_row = -1 current_column = -1 valid_moves = get_moves(board, piece) for move in valid_moves: board_modified = np.copy(board) add_piece_to_board(board_modified, move[0], move[1], piece) new_value = heuristic_evaluation_function(board_modified, piece) if new_value > current_value: current_value = new_value current_row = move[0] current_column = move[1] return (current_row, current_column)
def ordering_moves(board, current_piece, depth): from Board_Representation import get_moves possible_moves = get_moves(board, current_piece) scores_possible_moves = [] for move in possible_moves: board_modified = np.copy(board) add_piece_to_board(board_modified, move[0], move[1], current_piece) scores_possible_moves.append( heuristic_evaluation_function(board_modified, current_piece)) possible_moves_sorted = () if depth % 2 == 0: possible_moves_sorted, scores_possible_moves_sorted = zip( *sorted(zip(possible_moves, scores_possible_moves), reverse=True)) elif depth % 2 != 0: possible_moves_sorted, scores_possible_moves_sorted = zip( *sorted(zip(possible_moves, scores_possible_moves))) return [move for move in possible_moves_sorted]
def is_terminal_state(board, current_piece): from Board_Representation import get_moves if len(get_moves(board, current_piece)) == 0: return True else: return False