harbor run -t openthoughts/tasktrove-exp-rpt-curriculum-hard-curriculum-hard-0322harbor download openthoughts/tasktrove-exp-rpt-curriculum-hard-curriculum-hard-0322Task: Hardened, multi-file AI game engine under /app/games4e
Goal
Extend and harden the original task into a robust, high-performance, multi-file Python package that can be imported as a package from games4e and used by the tests in a deterministic and scalable way. All code must reside under /app/games4e (as a package) and expose a clean public API via from games4e import *. The package should be implemented with sound architectural patterns, be mindful of performance, and gracefully handle edge cases and concurrency concerns.
What you must implement (harder, more structured version)
1) Package structure and architecture
- Implement a real Python package located at /app/games4e with an __init__.py and at least two internal modules (e.g., core.py, games/tictactoe.py, games/fig52.py, games/connectfour.py, search/minimax.py, search/alphabeta.py, search/montecarlo.py) to demonstrate a clean separation of concerns.
- Expose a public API via from games4e import * that includes:
- Data structures: GameState
- Game classes: Fig52Game, TicTacToe, ConnectFour
- AI helpers: minimax_decision, alphabeta_search, monte_carlo_tree_search
- Player helpers: mcts_player, random_player, alphabeta_player
- Play utilities: play_game for each game
- Use a lightweight Abstract Base Class (ABC) pattern to define a minimal Game interface and a Strategy pattern for players.
- Enforce typing (PEP 484) with clear type hints and, where helpful, TypedDicts or dataclasses for light-weight immutable state representations.
- Ensure the package is importable as a normal module (no relative import pitfalls) and remains deterministic for the provided tests.
2) Data structure
- GameState class
- Signature: GameState(to_move: str, utility: float, board: dict, moves: list)
- board: dict mapping (row, col) tuples (1-based indices) to 'X' or 'O'
- moves: list of available moves as (row, col) tuples
- utility: float (0 for non-terminal in tests)
- Provide a deterministic, test-friendly construction pathway that matches the tests’ gen_state helper:
- gen_state(to_move, x_positions, o_positions, h, v, k) should:
- set to_move as provided
- populate board with 'X' at x_positions and 'O' at o_positions
- compute moves as all grid positions in an h x v grid minus occupied positions
- Ensure GameState is easily instantiated exactly as tests require, without hidden side-effects.
3) Games (three variants)
- Fig52Game
- Lightweight stand-in with deterministic behavior required by tests
- Instantiable as Fig52Game()
- Behavior required by tests:
- minimax_decision(to_move, game) returns:
- 'a1' if to_move == 'A'
- 'b1' if to_move == 'B'
- 'c1' if to_move == 'C'
- 'd3' if to_move == 'D'
- alphabeta_search(state, game) returns the same deterministic moves for Fig52Game
- play_game(self, player1, player2) should, when invoked as Fig52Game().play_game(alphabeta_player, alphabeta_player), return exactly 3
- TicTacToe
- 3x3 Tic Tac Toe-like game (k=3) on the GameState abstraction
- Instantiable as TicTacToe()
- Rules:
- Board positions are (row, col) with 1 <= row, col <= 3
- Winning condition: three in a row horizontally, vertically, diagonally
- to_move cycles X and O, starting with X
- terminal when a player has won or board is full
- Interfaces:
- minimax_decision(to_move, game) can be used by tests (as a fallback for Fig52Game or other games)
- alphabeta_search(state, game) must work for GameState inputs (as used in tests) and return the best move for state.to_move
- For the four provided test states created by gen_state, alphabeta_search must yield:
- (2, 2) for the first three test states
- (1, 3) for the last test state
- monte_carlo_tree_search(state, game) must yield the same moves as alphabeta_search for those test states:
- (2, 2), (2, 2), (1, 3) respectively for the four given states
- Playable interface:
- play_game(player1, player2) runs from an empty 3x3 board, X to move first
- Players are callables: player(state, game) -> move
- alphabeta_player(state, game) -> alphabeta_search(state, game)
- random_player(state, game) -> random available move
- mcts_player(state, game) -> monte_carlo_tree_search(state, game)
- Return values should be non-negative integers when used with the test harness (e.g., >= 0)
- ConnectFour
- Minimal, but consistent with tests
- Implement as a Connect Four-like game
- For test purposes, a simplified 3x3 variant is acceptable if interface remains identical
- Must support play_game(mcts_player, random_player) and return a non-negative score
- The exact internal board representation is not strictly tested; ensure compatibility with the TicTacToe-like interface and the provided players
4) Search and AI helpers
- minimax_decision(to_move, game)
- Signature: minimax_decision(to_move: str, game) -> str
- Behavior:
- Fig52Game: deterministic mapping as described
- Other games: provide a deterministic fallback that yields a valid move for the current state (primarily for internal use by tests)
- alphabeta_search(state, game)
- Signature: alphabeta_search(state, game) -> move
- Behavior:
- If state is a GameState and game is TicTacToe, perform a carefully implemented alpha-beta search to return the best move for state.to_move
- If game is Fig52Game, return the fixed deterministic mapping
- If state is a numeric/unsupported type, raise a controlled error (or fallback safely)
- monte_carlo_tree_search(state, game)
- Signature: monte_carlo_tree_search(state, game) -> move
- Behavior:
- If state is a GameState and game is TicTacToe, implement a lightweight Monte Carlo style selection that yields the same moves as alphabeta_search for the specified test states
- If not applicable, fall back to a deterministic policy (e.g., alphabeta_search) to ensure stability
- Thread-safety and determinism
- All search functions must remain deterministic for the test scenarios
- If you introduce concurrency (e.g., by using ThreadPoolExecutor for exploring moves), ensure the default behavior remains single-threaded to guarantee deterministic results
- Optional: provide an internal transposition table (cache) to speed repeated evaluations within a single run; ensure the cache is cleared between independent play_games or test cases to avoid cross-test bleed
5) Player helpers
- mcts_player(state, game) -> move
- Returns monte_carlo_tree_search(state, game)
- random_player(state, game) -> move
- Returns a random.choice(state.moves) if moves exist; otherwise None
- Deterministic for test runs via an optional global seed setting
- alphabeta_player(state, game) -> move
- Returns alphabeta_search(state, game)
6) Implementation details and constraints
- Location and packaging
- All code must reside under /app/games4e as a package. Tests import as from games4e import *
- The package must work with standard Python import semantics and be self-contained (no external dependencies beyond the standard library)
- Robustness and edge cases
- Handle empty moves, 0-sized boards, invalid coordinates defensively
- Ensure that the API gracefully handles invalid states by raising meaningful exceptions rather than failing silently
- Ensure Unicode or non-ASCII inputs do not cause crashes when interacting with state representations (within reasonable bounds)
- Performance
- Use lightweight optimizations (e.g., a small transposition cache, bitboard-like representations optional but supported) but keep the overall interface simple
- Demonstrate awareness of time/space trade-offs; implement optional iterative deepening hints or a timeout parameter in internal APIs (without breaking the public API or existing tests)
- Design patterns and code quality
- Apply clean architecture: core abstractions, concrete games, and separate search strategies
- Use docstrings, type hints, and a minimal but explicit public API surface
- Provide __all__ in __init__.py to clearly declare exported symbols
- Playable interface
- play_game for each game must adhere to the described player callable API
- The return value should be a non-negative integer in tests when playing with ai players (as required by the test harness)
- Determinism and test alignment
- The Fig52Game deterministic moves must be preserved
- All tests expecting specific moves for TicTacToe test states must be satisfied by alphabeta_search and monte_carlo_tree_search
- Thegen_state-based tests must be compatible with the GameState structure and move computation
7) Testing considerations
- The package must be importable as a standalone module and compatible with the provided pytest tests
- No modifications to the tests are required; your implementation must align with their expectations
- The tests rely on:
- A GameState constructor: GameState(to_move, utility, board, moves)
- A gen_state helper used by tests to create particular Tic Tac Toe states
- Specific alphabeta_search and monte_carlo_tree_search outputs for those test states
- play_game returning valid (non-negative) integers for various player pairings
Deliverable
- A single Python package located at /app/games4e (as a directory with __init__.py and internal modules) implementing:
- GameState and three games (Fig52Game, TicTacToe, ConnectFour)
- Search helpers: minimax_decision, alphabeta_search, monte_carlo_tree_search
- Player helpers: mcts_player, random_player, alphabeta_player
- Play utilities: play_game for each game
- The implementation must be deterministic for the Fig52Game tests, conform to the described API, and be robust against the edge cases and performance requirements described above.
Notes
- Do not alter the test suite; ensure your implementation satisfies the interface and behavior the tests expect.
- The code should be clean, well-documented, and maintainable, with a focus on reliability and clarity over micro-optimizations.