-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.py
More file actions
198 lines (163 loc) · 4.98 KB
/
menu.py
File metadata and controls
198 lines (163 loc) · 4.98 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/python3
"""
This program serves as a hub for simple command line games.
# @TODO:
* [ ] - add ordered dict to printing dicts.ALL_DECKS
* [ ] - @TODO: update comments and documentation
* [ ] - edit eosdict definitions for length
* [ ] - format printing of definitions to fit to screen
* [ ] - add update_deck method to User class in case deckk changes
"""
# IMPORTS
import os
import sys
import pprint
import shelve
import dicts
import memory as m
import utils
def choose_deck():
"""Display options and return user deck choice."""
deck_prompt = "What deck would you like to play?"
choice = utils.choose_list(utils.DECKLIST, deck_prompt)
deck = dicts.ALL_DECKS[choice]
os.system('clear')
return deck
def play_memory(player, session_cards, deck):
game_cards, player = m.memory(player, deck)
session_cards.extend(game_cards)
play_again(player, session_cards, deck)
# PLAYER SETUP
def get_player():
"""
Direct player to new_player() or load_player().
Output:
user - the User instance of the player.
"""
os.system('clear')
print("Are you a returning player?\n[y/n]\n")
new = input('>')
print()
if new.lower() == 'n':
user = new_player()
elif new.lower() == 'y':
user = load_player()
else:
print("Please enter 'y' or 'n'")
return get_player()
return user
def new_player():
"""
Get player name and confirm not already in loadfile.
Output:
user - the new User object created for the player.
"""
print("Who is playing? \n")
player_name = input('>')
print()
with shelve.open('myfile') as loadfile:
try:
################
## FIX THIS ##
################
if loadfile[player_name] == None:
pass # return User(player_name)
else:
print("I already have a player with that name.")
print("Please try a different name:\n")
return new_player()
except KeyError:
return utils.User(player_name)
def load_player():
"""
Load a USer instance from loadfile.
Output:
player_name -- string input from player
"""
print("Who is playing? \n")
player_name = input('>')
print()
with shelve.open('myfile') as loadfile:
try:
user = loadfile[player_name]
except KeyError:
return no_name()
return user
def no_name():
"""
Exception handler when load fails because User not found.
Output:
User: user, if new selected. Else load_player()
"""
noname_prompt = "Sorry, I can't find that file. Would you like to:"
ans_list = ["Try a different name", "Start a new save"]
response = utils.choose_list(ans_list, noname_prompt)
if response == ans_list[0]:
return load_player()
elif response == ans_list[1]:
user = new_player()
return user
def save_player(user):
"""
Update savefile or create new user entry.
Input:
user - the User instance for the player from this session.
Output:
no return but writes to file.
"""
with shelve.open('myfile') as savefile:
savefile[user.name] = user
# POST GAME
# @TODO: pretty print this so user doesn't have to scroll back to top.
def return_stats(user, recent_words, deck):
"""
DOCSTRING: This function takes in a user and a list of session
words and prints that users stats with those cards
Input:
User: user
list of strings: recent_words
dictionary: deck - the dictionary being tested
Output:
No output, prints to screen
"""
os.system('clear')
print(f"{user.name},")
print(" In the last session you answered the following cards,")
print("here's your stats for them:\n")
for card in recent_words:
user.memory[deck['__dict_name__']][card].print_stats()
print()
print()
input(' Press enter to quit.')
os.system('clear')
def play_again(user, session_cards, deck):
"""
Ask user to play again, then restart or quit.
Input:
user -- the User instance for current player.
"""
print(f"{user.name},\n\tWould you like to play again?")
print("Enter 'y' or 'n'\n")
if input('>')[0].lower() != 'n':
print()
play_memory(user, session_cards, deck)
if __name__ == '__main__':
os.system('clear')
player = get_player()
games_list = ['Player Stats', 'Memory']
game_prompt = "What game would you like to play?"
game = utils.choose_list(games_list, game_prompt)
################
## Fix this ##
################
if game == 'Player Stats':
print("No stats yet")
input('Press enter to quit.')
sys.exit()
elif game == 'Memory':
session_cards = []
deck = choose_deck()
player.add_deck(deck)
play_memory(player, session_cards, deck)
return_stats(player, set(session_cards), deck)
save_player(player)