forked from kyclark/tiny_python_projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution1.py
More file actions
executable file
·101 lines (74 loc) · 2.68 KB
/
solution1.py
File metadata and controls
executable file
·101 lines (74 loc) · 2.68 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
#!/usr/bin/env python3
""" Interactive Tic-Tac-Toe using NamedTuple """
from typing import List, NamedTuple, Optional
class State(NamedTuple):
board: List[str] = list('.' * 9)
player: str = 'X'
quit: bool = False
draw: bool = False
error: Optional[str] = None
winner: Optional[str] = None
# --------------------------------------------------
def main() -> None:
"""Make a jazz noise here"""
state = State()
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("All right, we'll call it a draw.")
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':
return state._replace(quit=True)
if not (cell.isdigit() and int(cell) in range(1, 10)):
return state._replace(error=f'Invalid cell "{cell}", please use 1-9')
cell_num = int(cell)
if state.board[cell_num - 1] in 'XO':
return state._replace(error=f'Cell "{cell}" already taken')
board = state.board
board[cell_num - 1] = player
return state._replace(board=board,
player='O' if player == 'X' else 'X',
winner=find_winner(board),
draw='.' not in board,
error=None)
# --------------------------------------------------
def format_board(board: List[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()