-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathexercise_5.py
209 lines (170 loc) · 5.86 KB
/
exercise_5.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
#Skunk Game
from random import randrange
class Dice:
def __init__(self):
self.dice = [0] * 2
self.rollDice()
def rollDice(self):
for i in range(len(self.dice)):
self.dice[i] = randrange(1,7)
def values(self):
return self.dice[:]
class SkunkApp:
def __init__(self, interface):
self.dice = Dice()
self.interface = interface
self.players = []
self.makeplayers()
def run(self):
while not self.gameOver():
self.interface.newRound()
self.refreshPlayers()
print("Round: {}".format(self.interface.getRound() + 1))
print("\n")
self.playRound()
self.interface.gameSummary(self.players)
def playRound(self):
books = self.interface.getRound()
while not self.roundOver():
for player in self.players:
if player.active():
values = self.doRoll(player)
if values != [0, 0]:
x, y = values[0], values[1]
if x == 1 and y == 1:
player.resetRoundScores(books)
elif x ==1 or y == 1:
player.reset_rScore()
else:
player.inc_Score(x + y, books)
self.interface.currentscores(values, self.players)
for player in self.players:
player.tallyRound(books)
def refreshPlayers(self):
for player in self.players:
player.reactivate()
player.rScoreOnly()
def doRoll(self, player):
if self.interface.rollAgain(player.getName()):
self.dice.rollDice()
values = self.dice.values()
return values
else:
player.deactivate()
return [0, 0]
def makeplayers(self):
for name in self.interface.getPlayerNames():
x = Player(name)
self.players.append(x)
def roundOver(self):
over = True
for player in self.players:
if player.active():
over = False
return over
def gameOver(self):
if self.interface.getRound() >= 4:
return True
class Player:
def __init__(self, name):
self.name = name
self.rScore = 0
self.totScore = 0
self.roundScores = [0] * 5
self.play = True
def getName(self):
return self.name
def tallyRound(self, book):
self.roundScores[book] = self.get_rScore()
def resetRoundScores(self, books):
prevRoundScores = 0
for num in self.roundScores:
prevRoundScores += num
self.totScore -= prevRoundScores
for i in range(books):
self.roundScores[i] = 0
self.play = False
def inc_Score(self, score, books):
self.totScore = self.totScore + score
prevRoundScores = 0
for num in self.roundScores[0:books]:
prevRoundScores += num
self.rScore = self.totScore - prevRoundScores
def reset_rScore(self):
self.totScore -= self.rScore
self.rScore = 0
self.play = False
def get_rScore(self):
return self.rScore
def get_roundScores(self):
return self.roundScores
def get_totScore(self):
return self.totScore
def active(self):
return self.play
def reactivate(self):
self.play = True
def deactivate(self):
self.play = False
def rScoreOnly(self):
self.rScore = 0
class TextInterface:
def __init__(self):
self.round = -1
def getRound(self):
return self.round
def newRound(self):
self.round = self.round + 1
return self.round
print()
print("Entering Round {}".format(self.__whichRound()))
def setDice(self, values):
print("You rolled {}.".format(values))
print("\n")
def __whichRound(self):
letter = ["S", "K", "U", "N", "K"]
return letter[self.round]
def gameOver(self, name):
print("The Game is Over. {} wins!".format(name))
def currentscores(self, values, players):
for player in players:
name = player.getName()
rScore = player.get_rScore()
totscore = player.get_totScore()
print("Player: {0} Score: {1} Total: {2}".format(name, rScore, totscore))
self.setDice(values)
def finalScores(self, players):
print("Name: {0:>10}{1:>10}{2:>10}{3:>10}{4:>10}{5:>10}".format("S", "K", "U", "N", "K", "Total"))
print("-------------------------------------------------------------------")
for player in players:
scores = player.get_roundScores()
name = player.getName()
totscore = player.get_totScore()
print("{0:5}:{1:>10}{2:>10}{3:>10}{4:>10}{5:>10}{6:>10}".format(name, scores[0], scores[1], scores[2], scores[3], scores[4], totscore))
def rollAgain(self, name):
ans = None
while ans == None:
ans = input("Do you wish to roll again {}? >> ".format(name))
if ans[0] in "yY":
return True
elif ans[0] in "nN":
return False
else:
ans = None
print("Sorry, I don't understand.")
def getPlayerNames(self):
totplayers = []
x = 0
while x != '':
x = input("Enter a players name (<Enter> to Continue) >> ")
totplayers.append(x)
totplayers.remove('')
return totplayers
def gameSummary(self, players):
print("The game is over.")
self.finalScores(players)
def main():
inter = TextInterface()
app = SkunkApp(inter)
app.run()
main()