-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trivia.py
195 lines (152 loc) · 6.73 KB
/
Trivia.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
#!/usr/bin/env python3
from trivia_questions import TriviaQuestions
MAX_PLAYERS = 6
class Game:
def __init__(self, number_of_questions=50):
self.players = []
self.places = [0] * MAX_PLAYERS
self.purses = [0] * MAX_PLAYERS
self.in_penalty_box = [0] * MAX_PLAYERS
self.pop_questions = TriviaQuestions(number_of_questions, 'Pop').questions
self.science_questions = []
self.sports_questions = []
self.rock_questions = []
self.number_of_questions_in_category = number_of_questions
self.current_player = 0
self.is_getting_out_of_penalty_box = False
for i in range(number_of_questions):
self.pop_questions.append("Pop Question %s" % i)
self.science_questions.append("Science Question %s" % i)
self.sports_questions.append("Sports Question %s" % i)
self.rock_questions.append("Rock Question %s" % i)
def shuffle_questions(self):
self.populate_list("Pop", self.pop_questions)
self.rock_questions = []
self.shuffle_pop_questions()
self.shuffle_rock_questions()
def shuffle_pop_questions(self):
self.pop_questions = []
self.populate_list("Pop", self.pop_questions)
def shuffle_rock_questions(self):
self.rock_questions = []
for i in range(self.number_of_questions_in_category):
self.rock_questions.append("Rock Question %s" % i)
#
# def create_rock_question(self, index):
# return "Rock Question %s" % index
def add(self, player_name):
self.players.append(player_name)
self.places[self.how_many_players] = 0
self.purses[self.how_many_players] = 0
self.in_penalty_box[self.how_many_players] = False
print(player_name + " was added")
print("They are player number %s" % len(self.players))
return True
@property
def how_many_players(self):
return len(self.players)
def roll(self, roll):
print("%s is the current player" % self.players[self.current_player])
print("They have rolled a %s" % roll)
if self.in_penalty_box[self.current_player]:
self.player_in_penalty_box(roll)
else:
self.player_not_in_penalty_box(roll)
def player_not_in_penalty_box(self, roll):
self.places[self.current_player] = self.places[self.current_player] + roll
if self.places[self.current_player] > 11:
self.places[self.current_player] = self.places[self.current_player] - 12
print(self.players[self.current_player] + \
'\'s new location is ' + \
str(self.places[self.current_player]))
print("The category is %s" % self._current_category)
self._ask_question()
def player_in_penalty_box(self, roll):
if roll % 2 != 0:
self.is_getting_out_of_penalty_box = True
print("%s is getting out of the penalty box" % self.players[self.current_player])
self.player_not_in_penalty_box(roll)
else:
print("%s is not getting out of the penalty box" % self.players[self.current_player])
self.is_getting_out_of_penalty_box = False
def _ask_question(self):
self._ask_question_for_category("Science", self.science_questions)
self._ask_question_for_category("Pop", self.pop_questions)
self._ask_question_for_category("Sports", self.sports_questions)
self._ask_question_for_category("Rock", self.rock_questions)
def _ask_question_for_category(self, label, list):
if self._current_category == label:
if len(list) > 0:
print(list.pop(0))
else:
self.populate_list(label, list)
print(list.pop(0))
def populate_list(self, label, list):
for i in range(self.number_of_questions_in_category):
list.append("%s Question %s" % (label, i))
@property
def _current_category(self):
if self.places[self.current_player] == 0: return 'Pop'
if self.places[self.current_player] == 4: return 'Pop'
if self.places[self.current_player] == 8: return 'Pop'
if self.places[self.current_player] == 1: return 'Science'
if self.places[self.current_player] == 5: return 'Science'
if self.places[self.current_player] == 9: return 'Science'
if self.places[self.current_player] == 2: return 'Sports'
if self.places[self.current_player] == 6: return 'Sports'
if self.places[self.current_player] == 10: return 'Sports'
return 'Rock'
def was_correctly_answered(self):
if self.in_penalty_box[self.current_player]:
if self.is_getting_out_of_penalty_box:
print('Answer was correct!!!!')
self.purses[self.current_player] += 1
print(self.players[self.current_player] + \
' now has ' + \
str(self.purses[self.current_player]) + \
' Gold Coins.')
winner = self._did_player_win()
self.current_player += 1
if self.current_player == len(self.players): self.current_player = 0
return winner
else:
self.current_player += 1
if self.current_player == len(self.players): self.current_player = 0
return True
else:
print("Answer was correct!!!!")
self.purses[self.current_player] += 1
print(self.players[self.current_player] + \
' now has ' + \
str(self.purses[self.current_player]) + \
' Gold Coins.')
winner = self._did_player_win()
self.current_player += 1
if self.current_player == len(self.players): self.current_player = 0
return winner
def wrong_answer(self):
print('Question was incorrectly answered')
print(self.players[self.current_player] + " was sent to the penalty box")
self.in_penalty_box[self.current_player] = True
self.current_player += 1
if self.current_player == len(self.players): self.current_player = 0
return True
# Player needs 6 coins to win
def _did_player_win(self):
return not (self.purses[self.current_player] == 6)
from random import randrange
def play(number_of_questions):
not_a_winner = False
game = Game(number_of_questions)
game.add('Chet')
game.add('Pat')
game.add('Sue')
while True:
game.roll(randrange(1, 7))
if randrange(9) == 7:
not_a_winner = game.wrong_answer()
else:
not_a_winner = game.was_correctly_answered()
if not not_a_winner: break
if __name__ == '__main__':
play(5)