-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhive.lua
209 lines (182 loc) · 5.75 KB
/
hive.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
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
require("answer")
local Ranges = {}
Ranges[Categories.ADDITION] = {}
Ranges[Categories.ADDITION][Difficulties.EASY] = {
Min = 4,
Max = 12,
}
Ranges[Categories.ADDITION][Difficulties.HARD] = {
Min = 10,
Max = 25,
}
Ranges[Categories.SUBTRACTION] = {}
Ranges[Categories.SUBTRACTION][Difficulties.EASY] = {
Min = 2,
Max = 8,
}
Ranges[Categories.SUBTRACTION][Difficulties.HARD] = {
Min = 4,
Max = 15,
}
Ranges[Categories.MULTIPLICATION] = {}
Ranges[Categories.MULTIPLICATION][Difficulties.EASY] = {
Min = 4,
Max = 15,
}
Ranges[Categories.MULTIPLICATION][Difficulties.HARD] = {
Min = 8,
Max = 30,
}
Ranges[Categories.DIVISION] = {}
Ranges[Categories.DIVISION][Difficulties.EASY] = {
Min = 2,
Max = 8,
}
Ranges[Categories.DIVISION][Difficulties.HARD] = {
Min = 6,
Max = 15,
}
Ranges[Categories.MULTIPLES] = {}
Ranges[Categories.MULTIPLES][Difficulties.EASY] = {
Min = 2,
Max = 8,
}
Ranges[Categories.MULTIPLES][Difficulties.HARD] = {
Min = 6,
Max = 15,
}
local MIN_ANSWERS = 2
local MAKE_PREFIX = "Make "
local MULTIPLES_PREFIX = "Multiple of "
local flat_map = function (a, fn)
local mapped = {}
local int i = 1
for _,v in ipairs(a) do
local map_results = fn(v)
for _,mv in ipairs(map_results) do
mapped[i] = mv
i = i + 1
end
end
return mapped
end
local multiples_to_texts = function(p)
return {p}
end
local subtraction_to_texts = function (p)
local txts = {}
txts[1] = p[1].."-"..p[2]
return txts
end
local division_to_texts = function (p)
local txts = {}
txts[1] = p[1].."÷"..p[2]
return txts
end
local addition_to_texts = function(p)
local txts = {}
txts[1] = p[1].."+"..p[2]
txts[2] = p[2].."+"..p[1]
return txts
end
local multiplication_to_texts = function(p)
local txts = {}
txts[1] = p[1].."×"..p[2]
txts[2] = p[2].."×"..p[1]
return txts
end
local prepare_hive_helper = function(hive, min, max, question_prefix, gen_answers, gen_traps, to_texts)
local enough_answers = false
local as
while (not enough_answers) do
hive.value = math.random(min, max)
hive.question = question_prefix .. hive.value
as = gen_answers(hive.value)
if (#as >= MIN_ANSWERS) then
enough_answers = true
end
end
local ts = gen_traps(hive.value)
hive.answers = flat_map(as, to_texts)
hive.traps = flat_map(ts, to_texts)
function hive:get_answer()
return self.answers[math.random(table.getn(self.answers))]
end
function hive:get_trap()
return self.traps[math.random(table.getn(self.traps))]
end
end
local build_addition_hive = function(difficulty)
local hive = {}
prepare_hive_helper(hive, Ranges[Categories.ADDITION][difficulty].Min, Ranges[Categories.ADDITION][difficulty].Max, MAKE_PREFIX, gen_addition_answers, gen_addition_traps, addition_to_texts)
return hive
end
local build_subtraction_hive = function(difficulty)
local hive = {}
prepare_hive_helper(hive, Ranges[Categories.SUBTRACTION][difficulty].Min, Ranges[Categories.SUBTRACTION][difficulty].Max, MAKE_PREFIX, gen_subtraction_answers, gen_subtraction_traps, subtraction_to_texts)
return hive
end
local build_multiplication_hive = function(difficulty)
local hive = {}
prepare_hive_helper(hive, Ranges[Categories.MULTIPLICATION][difficulty].Min, Ranges[Categories.MULTIPLICATION][difficulty].Max, MAKE_PREFIX, gen_multiplication_answers, gen_multiplication_traps, multiplication_to_texts)
return hive
end
local build_division_hive = function(difficulty)
local hive = {}
prepare_hive_helper(hive, Ranges[Categories.DIVISION][difficulty].Min, Ranges[Categories.DIVISION][difficulty].Max, MAKE_PREFIX, gen_division_answers, gen_division_traps, division_to_texts)
return hive
end
local build_multiples_hive = function(difficulty)
local hive = {}
prepare_hive_helper(hive, Ranges[Categories.MULTIPLES][difficulty].Min, Ranges[Categories.MULTIPLES][difficulty].Max, MULTIPLES_PREFIX, gen_multiples_answers, gen_multiples_traps, multiples_to_texts)
return hive
end
local hive_builders = {}
hive_builders[Categories.ADDITION] = build_addition_hive
hive_builders[Categories.SUBTRACTION] = build_subtraction_hive
hive_builders[Categories.MULTIPLICATION] = build_multiplication_hive
hive_builders[Categories.DIVISION] = build_division_hive
hive_builders[Categories.MULTIPLES] = build_multiples_hive
function new_hive(question_type, difficulty)
question_type = question_type or Categories.ADDITION
local hive = hive_builders[question_type](difficulty)
function hive:new_fly(box_size, col, row, prob_correct)
prob_correct = prob_correct or .5
local c = math.random() < prob_correct
local fly = {
score = 5,
scale = .003413 * box_size,
row = row,
real = true,
col = col,
box_size = box_size,
correct = c,
font = love.graphics.newFont(main_font_path,0.107 * box_size),
text_off = vector(.146 * box_size, .235 * box_size),
img = love.graphics.newImage "assets/image/fly.png",
move = vector(0,0)
}
fly_offx = (game.grid_box_size - fly.img:getWidth() * fly.scale) / 2 + game.offx
fly_offy = 0
fly.off = vector(fly_offx,fly_offy)
if c then
fly.text = self:get_answer()
else
fly.text = self:get_trap()
end
function fly:draw()
local m = self.move
love.graphics.setColor(255, 255, 255)
love.graphics.setFont(self.font)
rowcol_scaled = vector(self.row, self.col) * game.grid_box_size + self.off
love.graphics.draw(self.img, rowcol_scaled.x+m.x, rowcol_scaled.y+m.y, 0, self.scale, self.scale)
love.graphics.setColor(0, 0, 0)
floor_print(self.text, rowcol_scaled.x + self.text_off.x+m.x, rowcol_scaled.y + self.text_off.y + m.y,0.356*self.box_size,"center")
end
return fly
end
function hive:empty_fly()
return { draw = function() end, real = false}
end
return hive
end