forked from caradojo/trivia
-
Notifications
You must be signed in to change notification settings - Fork 727
/
Copy pathtrivia.lua
169 lines (138 loc) · 5.73 KB
/
trivia.lua
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
local Game = {}
Game.__index = Game
function Game:new()
instance = {}
setmetatable(instance, Game)
instance.players = {}
instance.places = { 0,0,0,0,0,0}
instance.purses = { 0,0,0,0,0,0}
instance.in_penalty_box = {0,0,0,0,0,0}
instance.pop_questions = {}
instance.science_questions = {}
instance.sports_questions = {}
instance.rock_questions = {}
instance.current_player = 1
instance.is_getting_out_of_penalty_box = false
for i = 0,50,1 do
table.insert(instance.pop_questions, "Pop Question "..i)
table.insert(instance.science_questions, "Science Question "..i)
table.insert(instance.sports_questions, "Sports Question "..i)
table.insert(instance.rock_questions, instance:create_rock_question(i))
end
return instance
end
function Game:create_rock_question(index)
return "Rock Question "..index
end
function Game:is_playable()
return self:how_many_players() - 1 >= 2
end
function Game:add(name)
table.insert(self.players, 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(name.." was added")
print("They are player number "..tostring(#self.players))
return true
end
function Game:how_many_players()
return #self.players
end
function Game:roll(roll)
print(self.players[self.current_player].." is the current player")
print("They have rolled a "..tostring(roll))
if self.in_penalty_box[self.current_player] then
if roll % 2 ~= 0 then
self.is_getting_out_of_penalty_box = true
print(self.players[self.current_player].." is getting out of the penalty box")
self.places[self.current_player] = self.places[self.current_player] + roll
if self.places[self.current_player] > 11 then
self.places[self.current_player] = self.places[self.current_player] - 12
end
print(self.players[self.current_player].."'s new location is "..tostring(self.places[self.current_player]))
print("The category is "..self:current_category())
self:ask_question()
else
print(self.players[self.current_player].." is not getting out of the penalty box")
self.is_getting_out_of_penalty_box = false
end
else
self.places[self.current_player] = self.places[self.current_player] + roll
if self.places[self.current_player] > 11 then
self.places[self.current_player] = self.places[self.current_player] - 12
end
print(self.players[self.current_player].."'s new location is "..tostring(self.places[self.current_player]))
print("The category is "..self:current_category())
self:ask_question()
end
end
function Game:ask_question()
if self:current_category() == 'Pop' then print(table.remove(self.pop_questions,1)) end
if self:current_category() == 'Science' then print(table.remove(self.science_questions,1)) end
if self:current_category() == 'Sports' then print(table.remove(self.sports_questions,1)) end
if self:current_category() == 'Rock' then print(table.remove(self.rock_questions,1)) end
end
function Game:current_category()
if self.places[self.current_player] == 0 then return "Pop" end
if self.places[self.current_player] == 4 then return "Pop" end
if self.places[self.current_player] == 8 then return "Pop" end
if self.places[self.current_player] == 1 then return "Science" end
if self.places[self.current_player] == 5 then return "Science" end
if self.places[self.current_player] == 9 then return "Science" end
if self.places[self.current_player] == 2 then return "Sports" end
if self.places[self.current_player] == 6 then return "Sports" end
if self.places[self.current_player] == 10 then return "Sports" end
return "Rock"
end
function Game:was_correctly_answered()
if self.in_penalty_box[self.current_player] then
if self.is_getting_out_of_penalty_box then
print('Answer was correct!!!!')
self.purses[self.current_player] = self.purses[self.current_player] + 1
print (self.players[self.current_player]..' now has '..tostring(self.purses[self.current_player])..' Gold Coins.')
winner = self:did_player_win()
self.current_player = self.current_player + 1
if self.current_player == #(self.players) + 1 then self.current_player = 1 end
return winner
else
self.current_player = self.current_player + 1
if self.current_player == #(self.players) + 1 then self.current_player = 1 end
return true
end
else
print('Answer was corrent!!!!')
self.purses[self.current_player] = self.purses[self.current_player] + 1
print (self.players[self.current_player]..' now has '..tostring(self.purses[self.current_player])..' Gold Coins.')
winner = self:did_player_win()
self.current_player = self.current_player + 1
if self.current_player == #(self.players) + 1 then self.current_player = 1 end
return winner
end
end
function Game:wrong_answer()
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 = self.current_player + 1
if self.current_player == #(self.players) + 1 then self.current_player = 1 end
return true
end
function Game:did_player_win()
return not (self.purses[self.current_player] == 6)
end
math.randomseed(os.time())
not_a_winner = false
game = Game.new()
game:add('Chet')
game:add('Pat')
game:add('Sue')
while true do
game:roll(math.random(0,5) + 1)
if math.random(0,9) == 7 then
not_a_winner = game:wrong_answer()
else
not_a_winner = game:was_correctly_answered()
end
if not not_a_winner then break end
end