chmengine.engines.quartney

Cmhmey Jr’s Mom, Quartney

Classes

Quartney()

Mother class of Cmhmey Jr., managing Q‑table persistence and move selection.

class chmengine.engines.quartney.Quartney

Bases: object

Mother class of Cmhmey Jr., managing Q‑table persistence and move selection.

board: Optional[Board]
cache_dir: str = '.\\SQLite3Caches\\QTables'
property depth: int

Current search depth used for heatmap and Q‑table lookups.

Returns:
int

Depth ≥ 0.

Examples

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

Return the FEN string representing board.

Parameters:
boardchess.Board

Board to serialize.

Returns:
str

FEN string of board.

get_q_value(fen: Optional[str] = None, board: Optional[Board] = None, pieces_count: Optional[int] = None) Optional[float64]

Retrieve a cached Q‑value for a position, or None if uncached.

Parameters:
fenstr, optional

FEN of the position (deprecated; use board).

boardchess.Board, optional

Board object for filename lookup.

pieces_countint, optional

Override piece count for selecting DB file.

Returns:
float64 or None

Stored Q‑value, or None if no entry exists.

Examples

>>> from chmengine import CMHMEngine2
>>> engine = CMHMEngine2()
>>> q = engine.get_q_value()
abstract pick_move(pick_by: str = '', board: Optional[Board] = None, debug: bool = False) Pick

Select a move based on heatmap scores and update its Q‑value.

This method evaluates all legal moves on board (or on the engine’s current board if board is None), picks one of the top‑scoring moves at random, writes the new Q‑value to the database, and returns (move, score).

Parameters:
pick_bystr, default=””

Legacy parameter (ignored).

boardchess.Board, optional

Board to pick from; defaults to self.board.

debugbool, default=False

If True, print the full move‑score table.

Returns:
(chess.Move, numpy.float64)

The chosen move and its score.

Raises:
ValueError

If there are no legal moves.

Examples

>>> from chmengine.engines.cmhmey2 import CMHMEngine2
>>> engine = CMHMEngine2()
>>> move, score = engine.pick_move()
pieces_count(board: Optional[Board] = None) int

Return the number of pieces on the board in O(1) time.

This uses Board.occupied.bit_count() when given a board.

Parameters:
boardchess.Board, optional

Board whose pieces to count. Preferred parameter.

Returns:
int

Total number of pieces on the board.

Examples

>>> from chmengine.engines.cmhmey2 import CMHMEngine2
>>> engine = CMHMEngine2()
>>> engine.pieces_count()
32
qdb_path(board: Optional[Board] = None, pieces_count: Optional[Union[int, str]] = None) str

Get the full path to the Q‑table database file.

Parameters:
boardchess.Board, optional

Board object for file naming.

pieces_countint or str, optional

Explicit piece count override.

Returns:
str

Relative path: <cache_dir>/<qtable_filename(…)>

Examples

>>> import os
>>> from chmengine.engines.cmhmey2 import CMHMEngine2
>>> engine = CMHMEngine2()
>>> engine.qdb_path() == os.path.join(engine.cache_dir, engine.qtable_filename())
True
qtable_filename(board: Optional[Board] = None, pieces_count: Optional[Union[int, str]] = None) str

Build the Q‑table filename based on depth and piece count.

Parameters:
boardchess.Board, optional

Board object to derive FEN (and piece count) if fen is not given.

pieces_countint or str, optional

Explicit piece count to use. If provided, skips recomputing from board/FEN.

Returns:
str

File name of the form “qtable_depth_{depth}_piece_count_{pieces_count}.db”.

Examples

>>> import os
>>> from chmengine.engines.cmhmey2 import CMHMEngine2
>>> engine = CMHMEngine2()
>>> engine.qtable_filename() in os.listdir(path=engine.cache_dir)
True
set_q_value(value: float, fen: Optional[str] = None, board: Optional[Board] = None, pieces_count: Optional[int] = None) None

Insert or update the Q‑value for a given position in the DB.

Parameters:
valuefloat

Q‑value to store.

fenstr, optional

Position FEN (deprecated; prefer board).

boardchess.Board, optional

Board object for filename lookup.

pieces_countint, optional

Override piece count for selecting DB file.

Examples

>>> from chmengine import CMHMEngine2
>>> engine = CMHMEngine2()
>>> engine.set_q_value(0.0, '1k6/8/8/8/8/3K4/8/8 w - - 0 1')
abstract update_q_values(debug: bool = False) None

Back-propagate game outcome through the Q-table.

Pops all moves from the current board history and adjusts each stored Q-value in the database based on the final result (win/lose/draw).

Parameters:
debugbool, default=False

If True, print diagnostics for each back-step.

Notes

Updates the SQLite Q-table entries for every move in the game.

Examples

>>> from io import StringIO
>>> from chess import pgn
>>> from chmengine import CMHMEngine2
>>> pgn_buffer = StringIO(
...    '''
...    1. f3 e5 2. g4 Qh4# 0-1
...
...
...    '''
... )
>>> game = pgn.read_game(pgn_buffer)
>>> board = game.board()
>>> for move in game.mainline_moves():
...     board.push(move)
>>> engine = CMHMEngine2(board=board)
>>> engine.fen()
'rnb1kbnr/pppp1ppp/8/4p3/6Pq/5P2/PPPPP2P/RNBQKBNR w KQkq - 1 3'
>>> engine.update_q_values()
>>> engine.fen()
'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'