-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproj2_simulation.py
More file actions
150 lines (125 loc) · 4.81 KB
/
Copy pathproj2_simulation.py
File metadata and controls
150 lines (125 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"""Chess Opening Explorer - Simulation
Simulate an interactive session of the chess opening explorer
by feeding in a list of commands to be executed in sequence.
"""
from __future__ import annotations
from typing import Optional
from dataclasses import dataclass
from traverser import Traverser
from move_tree import MoveTree
from chess_data import ChessData
from game_reader import read_pgn
from openings_reader import get_openings
ALL_GAMES = [
"data/games/lichess_tournament_2025.03.26_G0j0ZKLB_2000-superblitz (1).pgn",
"data/games/lichess_tournament_2025.03.28_bHUDi9Ci_daily-rapid.pgn",
"data/games/lichess_tournament_2025.03.24_aByKLmHE_daily-superblitz.pgn",
"data/games/lichess_tournament_2025.03.25_45UeSiXu_daily-blitz.pgn",
"data/games/lichess_tournament_2025.03.25_OvDcYlTU_daily-rapid.pgn",
"data/games/lichess_tournament_2025.03.25_tqzGNmOv_daily-bullet.pgn"
]
BLITZ_ONLY = [
"data/games/lichess_tournament_2025.03.25_45UeSiXu_daily-blitz.pgn",
"data/games/lichess_tournament_2025.03.24_aByKLmHE_daily-superblitz.pgn"
]
BULLET_ONLY = [
"data/games/lichess_tournament_2025.03.25_tqzGNmOv_daily-bullet.pgn"
]
RAPID_ONLY = [
"data/games/lichess_tournament_2025.03.28_bHUDi9Ci_daily-rapid.pgn",
"data/games/lichess_tournament_2025.03.25_OvDcYlTU_daily-rapid.pgn"
]
@dataclass
class SimulationConfig:
"""Configuration for setting up and running a chess explorer simulation."""
dataset_choice: int
opening_path: str
max_moves_val: int
default_tc: Optional[int]
command_list: list[str]
class ChessExplorerSimulation:
"""A simulation of the chess opening explorer using a Traverser."""
_traverser: Traverser
_command_log: list[str]
def __init__(self, sim_config: SimulationConfig) -> None:
"""Initialize the chess simulation with selected data and a command list."""
game_file_paths = self._select_dataset(sim_config.dataset_choice)
games_database = read_pgn(game_file_paths)
openings_database = get_openings(sim_config.opening_path, sim_config.max_moves_val)
root = MoveTree("", data=ChessData([], games_database))
for move_sequence in openings_database:
root.insert_sequence(list(move_sequence), games_database, openings_database)
self._traverser = Traverser(root, sim_config.default_tc)
self._command_log = sim_config.command_list
def run(self) -> None:
"""Run the simulation by executing each command in order."""
print("\n=== Starting Simulation ===\n")
for cmd in self._command_log:
print(f"> {cmd}")
base, param = self._parse_command(cmd)
if self._validate_command(base):
self._traverser.handle_input(base, param)
else:
print(f"Invalid command: {cmd}")
@staticmethod
def _parse_command(command: str) -> tuple[str, Optional[str]]:
split = command.strip().split(maxsplit=1)
return (split[0], split[1] if len(split) > 1 else None)
@staticmethod
def _validate_command(cmd: str) -> bool:
return cmd in {'ls', 'cd', 'tree', 'info', 'settc', 'help', 'find', 'stats', 'timecontrols'}
@staticmethod
def _select_dataset(choice: int) -> list[str]:
if choice == 1:
return ALL_GAMES
elif choice == 2:
return BLITZ_ONLY
elif choice == 3:
return BULLET_ONLY
elif choice == 4:
return RAPID_ONLY
else:
print("Invalid choice. Defaulting to all games.")
return ALL_GAMES
if __name__ == '__main__':
pass
# simulation_config = SimulationConfig(
# dataset_choice=2,
# opening_path="data/openings",
# max_moves_val=2,
# default_tc=180,
# command_list=[
# "ls desc",
# "ls asc",
# "cd e4",
# "ls",
# "cd e5",
# "settc 180",
# "stats",
# "tree",
# "cd ../..",
# "cd e4/e6/Nc3",
# "stats",
# "help",
# "timecontrols",
# ]
# )
# sim = ChessExplorerSimulation(simulation_config)
# sim.run()
# import doctest
# doctest.testmod(verbose=True)
# import python_ta
# python_ta.check_all(config={
# 'max-line-length': 120,
# 'disable': ['E1136', 'W0221'],
# 'extra-imports': ['move_tree',
# 'openings_reader',
# 'Optional',
# 'chess_data',
# 'traverser',
# 'Traverser',
# 'read_pgn',
# 'game_reader'],
# 'allowed-io': ['ChessExplorerSimulation.run', 'ChessExplorerSimulation._select_dataset'],
# 'max-nested-blocks': 4
# })