-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
94 lines (73 loc) · 2.92 KB
/
cli.py
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
from bot import Knowledge, Mistake, Node
from hexapawn import actions, BLACK, display, initial_state, result, terminal, utility, WHITE
CONTROLS: dict[str, tuple[int, int]] = {
"7": (0, 0), "8": (0, 1), "9": (0, 2),
"4": (1, 0), "5": (1, 1), "6": (1, 2),
"1": (2, 0), "2": (2, 1), "3": (2, 2),
}
M, N = 3, 3
def white_chance() -> list[tuple[int, int]]:
"""Handles the player's move input for the white pieces."""
depart, dest = input("You: ").split()
return [(int(depart[0]), int(depart[1])), (int(dest[0]), int(dest[1]))]
def display_flaws(mistakes: list[Mistake]) -> None:
"""Displays mistakes identified during the game."""
print("The incorrect moves are:")
for mistake in mistakes:
display(mistake.configuration)
print(f"{mistake.fault_action[0]} -> {mistake.fault_action[1]}")
def retry() -> bool:
"""Prompts the user to retry the game."""
response = input("Try Again [Y/N]? ").strip().lower()
return response == "y"
def review_reflect(knowledge: Knowledge, history: Node | None) -> None:
"""Analyzes and logs mistakes for reflection."""
while history and knowledge.catch22(history.state):
mistake = Mistake(history.state, history.action)
knowledge.notify(mistake)
history = history.parent
print("Climbing up the game tree...")
if history:
knowledge.notify(Mistake(history.state, history.action))
def play() -> None:
"""Main function to handle the game loop."""
game_board = initial_state(M, N)
chance = [WHITE, BLACK]
history = None
knowledge = Knowledge()
while True:
display(game_board)
if terminal(game_board, chance[0]):
if utility(game_board, chance[0]) == WHITE:
print("Reviewing the mistakes...")
review_reflect(knowledge, history)
if retry():
game_board = initial_state(M, N)
chance = [WHITE, BLACK]
history = None
else:
break
continue
if chance[0] == WHITE:
depart, dest = white_chance()
else:
possible_moves = actions(game_board, chance[0]).items()
for pawn, moves in possible_moves:
for move in moves:
if not knowledge.is_mistake(game_board, [pawn, move]):
depart, dest = pawn, move
break
else:
continue
break
else:
# Handle no valid moves case (shouldn't normally occur)
depart, (dest, *_) = list(possible_moves)[0]
assert False # Shouldn't happen in a valid game
history = Node(game_board, history, [depart, dest])
game_board = result(game_board, depart, dest)
print("*" * 30)
chance.reverse()
display_flaws(knowledge.mistakes)
if __name__ == "__main__":
play()