-
Notifications
You must be signed in to change notification settings - Fork 0
/
play_rummy.py
140 lines (92 loc) · 3.13 KB
/
play_rummy.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
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
"""Script for playing a game of rummy using the rummy module."""
import argparse
import sys
from pathlib import Path
from rummy import RummyGame
def get_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument("target_score", type=int)
parser.add_argument("game_file", type=Path)
args = parser.parse_args()
return args
def set_up_game(game_file: Path) -> RummyGame:
if not game_file.exists():
create = confirm(f"{game_file} does not exist, create new file")
if not create:
sys.exit(0)
game = RummyGame()
else:
game = RummyGame(game_file)
print(f"Loaded a previous game from {game_file}")
if game.empty:
while True:
player = input("Enter the name of a player to add to the game: ")
starting_score = get_int(
f"Enter the starting score for {player} (or nothing for 0)",
allow_empty=True,
)
game.add_player(player, starting_score)
if len(game.players) >= 2:
add_another = confirm("Add another player to the game")
if not add_another:
break
return game
def play_game(
game: RummyGame,
game_file: Path,
target_score: int,
) -> None:
print(f"\nStarting scoreboard:\n{game}")
print(f"Target score to win the game is {target_score}\n")
winner = game.find_winner(target_score)
if winner:
print(f"{winner} already has a winning score!")
sys.exit(0)
while True:
scores = {}
for player in game.players:
score = get_int(f"Enter the score for {player}")
scores[player] = score
print() # For spacing
record = confirm("Record the above scores")
if not record:
print("Aborted recording the above scores")
sys.exit(0)
for player, score in scores.items():
game.add_score(player, score)
game.save(game_file)
print(f"\nUpdated scoreboard:\n{game}\n")
print(f"(progress saved to {game_file})\n")
winner = game.find_winner(target_score)
if winner:
print(f"{winner} won the game!")
break
cont = confirm("Continue playing")
if not cont:
break
print() # For spacing, before next round
def get_int(message: str, allow_empty: bool = False) -> int:
resp = input(message + ": ")
if allow_empty:
if resp == "":
return 0
while True:
try:
integer = int(resp)
break
except ValueError:
resp = input("Oops! Input must be an integer. Please try again: ")
return integer
def confirm(message: str) -> bool:
resp = input(message + " (y/n)? ")
while True:
if resp in {"y", "n"}:
break
resp = input("Oops! Valid inputs are 'y' or 'n'. Please try again: ")
return resp == "y"
def main() -> None:
args = get_args()
game = set_up_game(args.game_file)
play_game(game, args.game_file, args.target_score)
if __name__ == "__main__":
main()