forked from kyclark/tiny_python_projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution2_typed_dict.py
More file actions
executable file
·113 lines (85 loc) · 2.78 KB
/
solution2_typed_dict.py
File metadata and controls
executable file
·113 lines (85 loc) · 2.78 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
#!/usr/bin/env python3
""" Interactive Tic-Tac-Toe using TypedDict """
from typing import List, Optional, TypedDict
class State(TypedDict):
board: str
player: str
quit: bool
draw: bool
error: Optional[str]
winner: Optional[str]
# --------------------------------------------------
def main() -> None:
"""Make a jazz noise here"""
state = State(board='.' * 9,
player='X',
quit=False,
draw=False,
error=None,
winner=None)
while True:
print("\033[H\033[J")
print(format_board(state['board']))
if state['error']:
print(state['error'])
elif state['winner']:
print(f"{state['winner']} has won!")
break
state = get_move(state)
if state['quit']:
print('You lose, loser!')
break
elif state['draw']:
print('No winner.')
break
# --------------------------------------------------
def get_move(state: State) -> State:
"""Get the player's move"""
player = state['player']
cell = input(f'Player {player}, what is your move? [q to quit]: ')
if cell == 'q':
state['quit'] = True
return state
if not (cell.isdigit() and int(cell) in range(1, 10)):
state['error'] = f'Invalid cell "{cell}", please use 1-9'
return state
cell_num = int(cell)
if state['board'][cell_num - 1] in 'XO':
state['error'] = f'Cell "{cell}" already taken'
return state
board = list(state['board'])
board[cell_num - 1] = player
return State(
board=''.join(board),
player='O' if player == 'X' else 'X',
winner=find_winner(board),
draw='.' not in board,
error=None,
quit=False,
)
# --------------------------------------------------
def format_board(board: str) -> str:
"""Format the board"""
cells = [str(i) if c == '.' else c for i, c in enumerate(board, 1)]
bar = '-------------'
cells_tmpl = '| {} | {} | {} |'
return '\n'.join([
bar,
cells_tmpl.format(*cells[:3]), bar,
cells_tmpl.format(*cells[3:6]), bar,
cells_tmpl.format(*cells[6:]), bar
])
# --------------------------------------------------
def find_winner(board: List[str]) -> Optional[str]:
"""Return the winner"""
winning = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7],
[2, 5, 8], [0, 4, 8], [2, 4, 6]]
for player in ['X', 'O']:
for i, j, k in winning:
combo = [board[i], board[j], board[k]]
if combo == [player, player, player]:
return player
return None
# --------------------------------------------------
if __name__ == '__main__':
main()