def evaluate(game: Game_IJK, player) -> (int, int): up, low = 0, 0 gamestate = game.state() if (gamestate == 'C'): up = float("inf") low = float("-inf") return float("inf") elif (gamestate == 'c'): return float("-inf") elif (gamestate == 'Tie'): return 0 adjscore = 0 board = game.getGame() for row in board: for e in row: if (e != ' '): if (e >= 'a'): low += (2**(ord(e) - ord('a') + 1) - 1) else: up += (2**(ord(e) - ord('A') + 1) - 1) for i in range(6): for j in range(6): if (j + 1 < 6 and abs(ord(board[i][j + 1]) - ord(board[i][j])) == 32): adjscore += (2**(ord(board[i][j + 1]) - ord('a') + 2) - 1) elif (i + 1 < 6 and abs(ord(board[i + 1][j]) - ord(board[i][j])) == 32): adjscore += (2**(ord(board[i + 1][j]) - ord('a') + 2) - 1) return up - low + (adjscore if game.getCurrentPlayer() == player else -adjscore)
def MIN_Value(game: Game_IJK, alpha: int, beta: int, depth: int, player: str) -> int: curbeta = beta if (depth == 0): upscore = evaluate(game, player) return upscore if player == '+' else -upscore gamestate = game.state() if (gamestate == 'C'): return float("inf") if player == '+' else float("-inf") elif (gamestate == 'c'): return float("-inf") if player == '+' else float("inf") elif (gamestate == 'Tie'): return 0 w = copy.deepcopy(game) w = w.makeMove('U') s = copy.deepcopy(game) s = w.makeMove('D') a = copy.deepcopy(game) a = w.makeMove('L') d = copy.deepcopy(game) d = w.makeMove('R') for g in [w, s, a, d]: curbeta = min(curbeta, MAX_Value(g, alpha, curbeta, depth - 1, player)) if (alpha >= curbeta): return curbeta return curbeta
def max_utility(game: Game_IJK, d, alpha, beta): d = d + 1 state = game.state() board = game.getGame() if state == 'K' or state == 'k' or state == 'tie' or d == 6: return heuristics(board) moves = ['U', 'D', 'L', 'R'] best_utility = float('-inf') for move in moves: clone = game.makeMove(move) utility = min_utility(clone, d, -float('inf'), float('inf')) if utility > best_utility: best_move = move best_utility = utility alpha = max(alpha, best_utility) if beta <= alpha: break return best_utility