맥락: - 새 레포의 첫 범위를 RL 없는 Lost Cities classic 게임 구현으로 잡았다. - 기존 tier0-3 실험 축은 제거하고 classic 5-expedition 룰을 기본값으로 둔다. 변경: - games/classic 아래에 Cython 게임 엔진, env, bots, backend 경계, Rust core와 proto schema를 이식했다. - setuptools/Cython 빌드 설정과 package data, README, classic port notes를 추가했다. - 룰, 점수, 마스크, env, canonical state, bot, Rust parity 테스트를 새 경로로 가져왔다. 확인: - uv run pytest tests/games/classic - uv run lost-cities-classic
53 lines
1.7 KiB
Python
53 lines
1.7 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
from ..game import GameState, LostCitiesConfig
|
|
from ..interfaces import BackendName, Snapshot
|
|
from .common import snapshot_from_state, snapshot_summary
|
|
|
|
LOGGER = logging.getLogger("coolrl_lost_cities.games.classic.backends.python")
|
|
|
|
|
|
class PythonLostCitiesBackend:
|
|
name: BackendName = "python"
|
|
|
|
def __init__(self, config: LostCitiesConfig, seed: int | None):
|
|
self.config = config
|
|
self.seed = seed
|
|
self.state = GameState.new_game(config, seed=seed)
|
|
self.history: list[GameState] = []
|
|
LOGGER.debug("파이썬 백엔드 초기화: %s", snapshot_summary(self.snapshot()))
|
|
|
|
def snapshot(self) -> Snapshot:
|
|
return snapshot_from_state(self.state)
|
|
|
|
def apply(self, action_id: int) -> None:
|
|
before = self.snapshot()
|
|
self.history.append(self.state.clone())
|
|
self.state.apply_unified_action(action_id)
|
|
LOGGER.debug(
|
|
"파이썬 액션 적용: 액션=%s 이전={%s} 이후={%s} 되돌리기깊이=%s",
|
|
action_id,
|
|
snapshot_summary(before),
|
|
snapshot_summary(self.snapshot()),
|
|
len(self.history),
|
|
)
|
|
|
|
def can_undo(self) -> bool:
|
|
return bool(self.history)
|
|
|
|
def undo(self) -> bool:
|
|
if not self.history:
|
|
LOGGER.debug("파이썬 되돌리기 무시: 기록이 비어 있음")
|
|
return False
|
|
before = self.snapshot()
|
|
self.state = self.history.pop()
|
|
LOGGER.debug(
|
|
"파이썬 되돌리기: 이전={%s} 이후={%s} 되돌리기깊이=%s",
|
|
snapshot_summary(before),
|
|
snapshot_summary(self.snapshot()),
|
|
len(self.history),
|
|
)
|
|
return True
|