-
Notifications
You must be signed in to change notification settings - Fork 71
/
Skeleton Program - Task Sheet 1 Answers.py
258 lines (234 loc) · 7 KB
/
Skeleton Program - Task Sheet 1 Answers.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# Skeleton Program code for the AQA COMP1 Summer 2014 examination
# this code should be used in conjunction with the Preliminary Material
# written by the AQA Programmer Team
# developed in the Python 3.2 programming environment
import random
from datetime import date
NO_OF_RECENT_SCORES = 3
class TCard():
def __init__(self):
self.Suit = 0
self.Rank = 0
class TRecentScore():
def __init__(self):
self.Name = ''
self.Score = 0
self.Date = None
Deck = [None]
RecentScores = [None]
Choice = ''
def GetRank(RankNo):
Rank = ''
if RankNo == 1:
Rank = 'Ace'
elif RankNo == 2:
Rank = 'Two'
elif RankNo == 3:
Rank = 'Three'
elif RankNo == 4:
Rank = 'Four'
elif RankNo == 5:
Rank = 'Five'
elif RankNo == 6:
Rank = 'Six'
elif RankNo == 7:
Rank = 'Seven'
elif RankNo == 8:
Rank = 'Eight'
elif RankNo == 9:
Rank = 'Nine'
elif RankNo == 10:
Rank = 'Ten'
elif RankNo == 11:
Rank = 'Jack'
elif RankNo == 12:
Rank = 'Queen'
else:
Rank = 'King'
return Rank
def GetSuit(SuitNo):
Suit = ''
if SuitNo == 1:
Suit = 'Clubs'
elif SuitNo == 2:
Suit = 'Diamonds'
elif SuitNo == 3:
Suit = 'Hearts'
else:
Suit = 'Spades'
return Suit
def DisplayMenu():
print()
print('MAIN MENU')
print()
print('1. Play game (with shuffle)')
print('2. Play game (without shuffle)')
print('3. Display recent scores')
print('4. Reset recent scores')
print()
print('Select an option from the menu (or enter q to quit): ', end='')
def GetMenuChoice():
Choice = input()
print()
return Choice.lower()[0]
def LoadDeck(Deck):
CurrentFile = open('deck.txt', 'r')
Count = 1
while True:
LineFromFile = CurrentFile.readline()
if not LineFromFile:
CurrentFile.close()
break
Deck[Count].Suit = int(LineFromFile)
LineFromFile = CurrentFile.readline()
Deck[Count].Rank = int(LineFromFile)
Count = Count + 1
def ShuffleDeck(Deck):
SwapSpace = TCard()
NoOfSwaps = 1000
for NoOfSwapsMadeSoFar in range(1, NoOfSwaps + 1):
Position1 = random.randint(1, 52)
Position2 = random.randint(1, 52)
SwapSpace.Rank = Deck[Position1].Rank
SwapSpace.Suit = Deck[Position1].Suit
Deck[Position1].Rank = Deck[Position2].Rank
Deck[Position1].Suit = Deck[Position2].Suit
Deck[Position2].Rank = SwapSpace.Rank
Deck[Position2].Suit = SwapSpace.Suit
def DisplayCard(ThisCard):
print()
print('Card is the', GetRank(ThisCard.Rank), 'of', GetSuit(ThisCard.Suit))
print()
def GetCard(ThisCard, Deck, NoOfCardsTurnedOver):
ThisCard.Rank = Deck[1].Rank
ThisCard.Suit = Deck[1].Suit
for Count in range(1, 52 - NoOfCardsTurnedOver):
Deck[Count].Rank = Deck[Count + 1].Rank
Deck[Count].Suit = Deck[Count + 1].Suit
Deck[52 - NoOfCardsTurnedOver].Suit = 0
Deck[52 - NoOfCardsTurnedOver].Rank = 0
def IsNextCardHigher(LastCard, NextCard):
Higher = False
if NextCard.Rank > LastCard.Rank:
Higher = True
return Higher
def GetPlayerName():
print()
valid = False
while not valid:
PlayerName = input('Please enter your name: ')
if len(PlayerName) > 0:
valid = True
else:
print("You must enter something for your name!")
print()
return PlayerName
def GetChoiceFromUser():
Choice = input('Do you think the next card will be higher than the last card (enter y or n)? ')
return Choice.lower()[0]
def DisplayEndOfGameMessage(Score):
print()
print('GAME OVER!')
print('Your score was', Score)
if Score == 51:
print('WOW! You completed a perfect game.')
print()
def DisplayCorrectGuessMessage(Score):
print()
print('Well done! You guessed correctly.')
print('Your score is now ', Score, '.', sep='')
print()
def ResetRecentScores(RecentScores):
for Count in range(1, NO_OF_RECENT_SCORES + 1):
RecentScores[Count].Name = ''
RecentScores[Count].Score = 0
RecentScores[Count].Date = None
def DisplayRecentScores(RecentScores):
print()
print('Recent Scores: ')
print()
print("{0:<12}{1:<10}{2:<5}".format("Date","Name","Score"))
print()
for Count in range(1, NO_OF_RECENT_SCORES + 1):
if RecentScores[Count].Date != None:
ScoreDate = RecentScores[Count].Date.strftime("%d/%m/%Y")
else:
ScoreDate = "N/A"
print("{0:<12}{1:<10}{2:<5}".format(ScoreDate,RecentScores[Count].Name,RecentScores[Count].Score))
print()
print('Press the Enter key to return to the main menu')
input()
print()
def UpdateRecentScores(RecentScores, Score):
valid = False
while not valid:
Choice = input("Do you want to add your score to the high score table? (y or n): ")
Choice = Choice.lower()[0]
if Choice in ["y","n"]:
valid = True
else:
print("Please enter a valid choice (y or n)")
if Choice == "y":
PlayerName = GetPlayerName()
FoundSpace = False
Count = 1
while (not FoundSpace) and (Count <= NO_OF_RECENT_SCORES):
if RecentScores[Count].Name == '':
FoundSpace = True
else:
Count = Count + 1
if not FoundSpace:
for Count in range(1, NO_OF_RECENT_SCORES):
RecentScores[Count].Name = RecentScores[Count + 1].Name
RecentScores[Count].Score = RecentScores[Count + 1].Score
Count = NO_OF_RECENT_SCORES
RecentScores[Count].Name = PlayerName
RecentScores[Count].Score = Score
RecentScores[Count].Date = date.today()
def PlayGame(Deck, RecentScores):
LastCard = TCard()
NextCard = TCard()
GameOver = False
GetCard(LastCard, Deck, 0)
DisplayCard(LastCard)
NoOfCardsTurnedOver = 1
while (NoOfCardsTurnedOver < 52) and (not GameOver):
GetCard(NextCard, Deck, NoOfCardsTurnedOver)
Choice = ''
while (Choice != 'y') and (Choice != 'n'):
Choice = GetChoiceFromUser()
DisplayCard(NextCard)
NoOfCardsTurnedOver = NoOfCardsTurnedOver + 1
Higher = IsNextCardHigher(LastCard, NextCard)
if (Higher and Choice == 'y') or (not Higher and Choice == 'n'):
DisplayCorrectGuessMessage(NoOfCardsTurnedOver - 1)
LastCard.Rank = NextCard.Rank
LastCard.Suit = NextCard.Suit
else:
GameOver = True
if GameOver:
DisplayEndOfGameMessage(NoOfCardsTurnedOver - 2)
UpdateRecentScores(RecentScores, NoOfCardsTurnedOver - 2)
else:
DisplayEndOfGameMessage(51)
UpdateRecentScores(RecentScores, 51)
if __name__ == '__main__':
for Count in range(1, 53):
Deck.append(TCard())
for Count in range(1, NO_OF_RECENT_SCORES + 1):
RecentScores.append(TRecentScore())
Choice = ''
while Choice != 'q':
DisplayMenu()
Choice = GetMenuChoice()
if Choice == '1':
LoadDeck(Deck)
ShuffleDeck(Deck)
PlayGame(Deck, RecentScores)
elif Choice == '2':
LoadDeck(Deck)
PlayGame(Deck, RecentScores)
elif Choice == '3':
DisplayRecentScores(RecentScores)
else:
ResetRecentScores(RecentScores)