Source code for chipiron.environments.chess_env.board.starting_position

"""
Module defining the starting position arguments for the chess board.
"""

from dataclasses import dataclass
from enum import Enum
from importlib.resources import as_file, files
from typing import Literal, Protocol

from chipiron.environments.chess_env.board.board_tools import convert_to_fen


[docs]class StartingPositionArgsType(str, Enum): """ Enum class representing the type of starting position arguments. """ fromFile = "from_file" fen = "fen"
[docs]@dataclass class StartingPositionArgs(Protocol): """ Dataclass representing the base class for starting position arguments. """ type: StartingPositionArgsType
[docs] def get_fen(self) -> str: ...
[docs]@dataclass class FenStartingPositionArgs: """ Dataclass representing the starting position arguments specified by FEN. """ type: Literal[StartingPositionArgsType.fen] fen: str
[docs] def get_fen(self) -> str: return self.fen
[docs]@dataclass class FileStartingPositionArgs(StartingPositionArgs): """ Dataclass representing the starting position arguments specified by a file. """ type: Literal[StartingPositionArgsType.fromFile] file_name: str
[docs] def get_fen(self) -> str: resource = files("chipiron").joinpath("data/starting_boards/" + self.file_name) with as_file(resource) as real_path: with open(real_path, "r", encoding="utf-8") as f: ascii_board: str = str(f.read()) fen: str = convert_to_fen(ascii_board) return fen
AllStartingPositionArgs = FenStartingPositionArgs | FileStartingPositionArgs