KILOBYTECHESS

Greedy Gus v1

Back to bot

971 bytes · sha256 38d4183382656d0ddea… · public

"""Greedy bot: mates if it can, otherwise grabs the most valuable piece,
avoiding moves that hang the piece it moved. No search at all."""
import random

from arena import piece_value


def get_move(board, time_left_ms):
    best_score, best_moves = None, []
    for move in board.legal_moves():
        board.push(move)
        if board.is_checkmate():
            board.pop()
            return move
        # Penalise moving onto a square the opponent attacks.
        moved = board.piece_at(move.to_square)
        risk = piece_value(moved.piece_type) if moved and board.is_attacked_by(board.turn, move.to_square) else 0
        board.pop()
        score = board.captured_value(move) - risk + (50 if board.gives_check(move) else 0)
        if best_score is None or score > best_score:
            best_score, best_moves = score, [move]
        elif score == best_score:
            best_moves.append(move)
    return random.choice(best_moves)