chmengine.engines.cmhmey1

Cmhmey Sr.

Classes

CMHMEngine([board, depth])

A silly chess engine that picks moves using heatmaps.

class chmengine.engines.cmhmey1.CMHMEngine(board: Optional[Board] = None, depth: int = 1)

Bases: object

A silly chess engine that picks moves using heatmaps.

This class manages a chess board and evaluates moves by using recursion-based heatmaps.

property board: Board

Get the current chess board.

Returns:
chess.Board

The current board state.

Examples

>>> from chmengine import CMHMEngine
>>> engine = CMHMEngine()
>>> engine.board
Board('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1')
board_copy_pushed(move: Move, board: Optional[Board] = None) Board

Return a copy of the board with the given move applied.

Parameters:
movechess.Move

The move to push onto the board.

boardOptional[chess.Board], optional

The board to copy; if None, uses the engine’s current board.

Returns:
chess.Board

A new board instance with the move applied.

Examples

>>> from chmengine import CMHMEngine
>>> from chess import Move
>>> engine = CMHMEngine()
>>> new_board = engine.board_copy_pushed(Move.from_uci('e2e4'))
>>> new_board
Board('rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1')
current_moves_list(board: Optional[Board] = None) List[Move]

Retrieve the list of legal moves for the current board position.

Parameters:
boardOptional[chess.Board], default: None

The board for which to get legal moves. If None, the engine’s current board is used.

Returns:
List[chess.Move]

A list of legal moves available on the board.

Examples

>>> from chmengine import CMHMEngine
>>> engine = CMHMEngine()
>>> moves = engine.current_moves_list()
>>> moves[15]
Move.from_uci('e2e4')
current_player_heatmap_index(board: Optional[Board] = None) int

Get the heatmap index corresponding to the active (current) player.

Parameters:
boardOptional[chess.Board]
Returns:
int

The index for the current player. Typically, if the current turn is White (index 0), this returns 0, and vice versa.

Examples

>>> from chmengine import CMHMEngine
>>> engine = CMHMEngine()
>>> engine.current_player_heatmap_index()
0
property depth: int

Get the current recursion depth setting.

Returns:
int

The current recursion depth used for heatmap calculations.

Examples

>>> from chmengine import CMHMEngine
>>> engine = CMHMEngine()
>>> engine.depth
1
fen(board: Optional[Board] = None) str

Obtain the FEN string for a given board state. If no board is provided, the engine’s current board is used.

Parameters:
boardOptional[Board]

The board for which to retrieve the FEN string.

Returns:
str

The FEN string representing the board state.

Examples

>>> from chmengine import CMHMEngine2
>>> engine = CMHMEngine2()
>>> engine.fen()
'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
get_king_boxes(board: Optional[Board] = None) Tuple[List[int], List[int]]

Compute the bounding boxes for the kings on the board.

For both the current and opponent kings, this method calculates a “box” (a list of square indices) representing the king’s immediate surroundings.

Parameters:
boardOptional[chess.Board], default: None

The board to use; if None, the engine’s current board is used.

Returns:
Tuple[List[int], List[int]]

A tuple containing two lists: the first is the box for the current king, and the second is the box for the opponent king.

get_or_calc_move_maps(depth: Optional[int] = None) Dict[Move, GradientHeatmap]

Compute or retrieve precomputed heatmaps for all legal moves from the current board.

For each legal move from the current board, this method generates a corresponding heatmap by applying the move and evaluating the resulting position with a given recursion depth.

Parameters:
depthOptional[int], default: None

The recursion depth for the heatmap calculation. If None, the engine’s current depth is used.

Returns:
Dict[Move, GradientHeatmap]

A dictionary mapping each legal move to its corresponding heatmap.

Raises:
ValueError

If the current board has no legal moves.

Examples

>>> from chmengine import CMHMEngine
>>> engine = CMHMEngine()
>>> move_maps = engine.get_or_calc_move_maps()
>>> some_move = list(move_maps.keys())[0]
>>> type(move_maps[some_move])
<class 'heatmaps.ChessMoveHeatmap'>
get_or_calc_move_maps_list() List[Tuple[Move, GradientHeatmap]]

Retrieve a list of move-to-heatmap mappings.

This method converts the dictionary returned by get_or_calc_move_maps() into a list of tuples for easier iteration, where each tuple contains a move and its corresponding heatmap.

Returns:
List[Tuple[Move, GradientHeatmap]]

A list of (move, heatmap) tuples.

Examples

>>> from chmengine import CMHMEngine
>>> engine = CMHMEngine()
>>> first_move = engine.get_or_calc_move_maps_list()[0][0]
>>> first_move
Move.from_uci('g1h3')
static heatmap_data_is_zeros(heatmap: GradientHeatmap) bool

Check if the heatmap data is entirely zero.

Parameters:
heatmapheatmaps.GradientHeatmap

The heatmap object to check.

Returns:
bool

True if all values in the heatmap data are zero; otherwise, False.

Examples

>>> from chmengine import CMHMEngine
>>> from heatmaps import GradientHeatmap
>>> hmap = GradientHeatmap()
>>> CMHMEngine.heatmap_data_is_zeros(hmap)
True
>>> hmap[32][1] = 1.0
>>> CMHMEngine.heatmap_data_is_zeros(hmap)
False
other_player_heatmap_index(board: Optional[Board] = None) int

Get the heatmap index corresponding to the inactive (other) player.

Returns:
int

The index for the other player. Typically, if the current turn is White (index 0), this returns 1, and vice versa.

Examples

>>> from chmengine import CMHMEngine
>>> engine = CMHMEngine()
>>> engine.other_player_heatmap_index()
1
pick_move(pick_by: str = 'all-delta', board: Optional[Board] = None) Pick

Select a move based on various heatmap-derived criteria.

The method evaluates candidate moves using multiple heuristics (such as delta, maximum, minimum, king attack/defense, etc.) and returns one move chosen at random from the set of the best candidate moves according to the specified criteria.

The evaluation score is provided from the perspective of the player making the move:

a positive score indicates a move that is considered beneficial for the mover, while a negative score indicates a move that is considered detrimental.

This scoring convention is different from many traditional chess engines, where scores are often expressed as positive for White advatage and negative for Black.

Parameters:
pick_bystr, default: “all-delta”

A string indicating the selection heuristic. Supported options include “all-delta”, “all-max”, “all-min”, “king-atk”, “king-def”, and “king-delta”.

boardchess.Board, default: None

Used by child classes; pick_move method

Returns:
Pick

A tuple-like containing the chosen move and its associated evaluation score, where the score is from the perspective of the player making the move (i.e., positive values indicate favorable outcomes for that player, and negative values indicate unfavorable outcomes).

Examples

>>> from chmengine import CMHMEngine
>>> engine = CMHMEngine()
>>> pick = picked_move, eval_score = engine.pick_move()
>>> pick
Pick(move=e2e4, score=10.0)
>>> picked_move
Move.from_uci('e2e4')
>>> # A positive score indicates a move leading to a good position for the mover,
>>> # whereas a negative score indicates a leading to a bad position.
>>> eval_score
10.0
static update_target_moves_by_delta(target_moves_by_delta: List[Union[Tuple[None, None], Pick]], current_player_sum: float64, other_player_sum: float64, move: Move) List[Pick]

Update the candidate moves based on the delta between current and other player’s sums.

The delta is computed as the difference between the current player’s sum and the other player’s sum. If the calculated delta is greater than the current best, the candidate list is replaced; if equal, the move is appended.

Parameters:
target_moves_by_deltaList[Union[Tuple[None, None], Pick]]

The current list of candidate moves and their delta scores.

current_player_sumnumpy.float64

The sum of move intensities for the current player.

other_player_sumnumpy.float64

The sum of move intensities for the other player.

movechess.Move

The move being evaluated.

Returns:
List[Pick]

The updated list of candidate moves with their delta scores.

static update_target_moves_by_king_delta(target_moves_by_king_delta: List[Union[Tuple[None, None], Pick]], move: Move, current_king_min: float64, other_king_sum: float64) List[Pick]

Update candidate moves based on the king’s delta value.

Calculates the delta between the opponent’s king intensity and the current king’s minimum intensity, updating the candidate list if this delta is greater than the current best.

Parameters:
target_moves_by_king_deltaList[Union[Tuple[None, None], Pick]]

The current candidate moves for the king delta criterion.

movechess.Move

The move being evaluated.

current_king_minnumpy.float64

The minimum intensity value for the current king.

other_king_sumnumpy.float64

The total intensity value for the opponent’s king.

Returns:
List[Pick]

The updated list of candidate moves based on king delta.

static update_target_moves_by_max_current(target_moves_by_max_current: List[Union[Tuple[None, None], Pick]], transposed_map: ndarray, move: Move, color_index: int) Tuple[float64, List[Pick]]

Update the candidate moves for maximizing the current player’s intensity.

Computes the current player’s total intensity from the transposed heatmap and updates the candidate list if the new sum is greater than the current best.

Parameters:
target_moves_by_max_currentList[Union[Tuple[None, None], Pick]]

The current candidate moves for maximizing the current player’s intensity.

transposed_mapnumpy.ndarray

The transposed heatmap data array.

movechess.Move

The move being evaluated.

color_indexint

The index corresponding to the current player.

Returns:
Tuple[float64, List[Pick]]

A tuple containing the current player’s sum and the updated candidate list.

static update_target_moves_by_max_other_king(target_moves_by_max_other_king: List[Union[Tuple[None, None], Pick]], heatmap: GradientHeatmap, move: Move, color_index: int, other_king_box: List[int]) Tuple[float64, List[Pick]]

Update candidate moves for maximizing the opponent king’s intensity.

Calculates the opponent king’s total intensity from the heatmap over the specified area and updates the candidate list if a higher intensity sum is found.

Parameters:
target_moves_by_max_other_kingList[Union[Tuple[None, None], Pick]]

The candidate moves list for maximizing opponent king’s intensity.

heatmapheatmaps.GradientHeatmap

The heatmap object containing move intensities.

movechess.Move

The move being evaluated.

color_indexint

The index corresponding to the opponent.

other_king_boxList[int]

A list of board squares representing the area around the opponent’s king.

Returns:
Tuple[float64, List[Pick]]

A tuple containing the opponent king’s sum and the updated candidate list.

static update_target_moves_by_min_current_king(target_moves_by_min_current_king: List[Union[Tuple[None, None], Pick]], heatmap: GradientHeatmap, move: Move, other_index: int, current_king_box: List[int]) Tuple[float64, List[Pick]]

Update candidate moves for minimizing the current king’s intensity.

Extracts the intensity values for the current king from the heatmap and updates the candidate list if a lower intensity sum is found.

Parameters:
target_moves_by_min_current_kingList[Union[Tuple[None, None], Pick]]

The candidate moves list for minimizing current king’s intensity.

heatmapheatmaps.GradientHeatmap

The heatmap object containing move intensities.

movechess.Move

The move being evaluated.

other_indexint

The index corresponding to the opponent.

current_king_boxList[int]

A list of board squares representing the area around the current king.

Returns:
Tuple[float64, List[Pick]]

A tuple containing the current king’s sum and the updated candidate list.

static update_target_moves_by_min_other(target_moves_by_min_other: List[Union[Tuple[None, None], Pick]], transposed_map: ndarray, move: Move, other_index: int) Tuple[float64, List[Pick]]

Update the candidate moves for minimizing the opponent’s sum.

Calculates the opponent’s total move intensity from the transposed heatmap, and updates the candidate list if the new sum is lower than the current best.

Parameters:
target_moves_by_min_otherList[Union[Tuple[None, None], Pick]]

The current candidate moves for minimizing the opponent’s intensity.

transposed_mapnumpy.ndarray

The transposed heatmap data array.

movechess.Move

The move being evaluated.

other_indexint

The index corresponding to the opponent.

Returns:
Tuple[float64, List[Pick]]

A tuple containing the opponent’s sum and the updated candidate list.