Newer
Older
require 'set'
require 'prime'
module ExtremeStartup
class Question
class << self
def generate_uuid
@uuid_generator ||= UUID.new
@uuid_generator.generate.to_s[0..7]
end
end
def result
if @answer && self.answered_correctly?(answer)
"correct"
elsif @answer
"wrong"
else
@problem
end
end
def delay_before_next
case result
when "correct" then 5
when "wrong" then 10
else 20
end
end
def was_answered_correctly
result == "correct"
end
def was_answered_wrongly
result == "wrong"
end
def display_result
"\tquestion: #{self.to_s}\n\tanswer: #{answer}\n\tresult: #{result}"
end
def id
@id ||= Question.generate_uuid
end
def to_s
"#{id}: #{as_text}"
end
def answer=(answer)
@answer = answer.force_encoding("UTF-8")
end
def answer
@answer && @answer.downcase.strip
end
def answered_correctly?(answer)
correct_answer.to_s.downcase.strip == answer
end
def points
10
end
end
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
class GetQuestion < Question
def ask(player)
url = player.url + '?q=' + URI.escape(self.to_s)
puts "GET: " + url
begin
response = get(url)
if (response.success?) then
self.answer = response.to_s
else
@problem = "error_response"
end
rescue => exception
puts exception
@problem = "no_server_response"
end
end
def get(url)
HTTParty.get(url)
end
end
class PostQuestion < Question
def ask(player)
url = player.url + '?q=' + URI.escape(self.to_s)
puts "Post: " + url
begin
response = post(url, {data: self.get_data})
if (response.success?) then
self.answer = response.to_s
else
@problem = "error_response"
end
rescue => exception
puts exception
@problem = "no_server_response"
end
end
def post(url, data)
HTTParty.post(url, data)
end
end
class BinaryMathsQuestion < GetQuestion
def initialize(player, *numbers)
if numbers.any?
@n1, @n2 = *numbers
else
@n1, @n2 = rand(20), rand(20)
end
end
end
class TernaryMathsQuestion < GetQuestion
def initialize(player, *numbers)
if numbers.any?
@n1, @n2, @n3 = *numbers
else
@n1, @n2, @n3 = rand(20), rand(20), rand(20)
end
end
end
class SelectFromListOfNumbersQuestion < GetQuestion
def initialize(player, *numbers)
if numbers.any?
@numbers = *numbers
else
size = rand(2)
@numbers = random_numbers[0..size].concat(candidate_numbers.shuffle[0..size]).shuffle
end
end
def random_numbers
randoms = Set.new
loop do
randoms << rand(1000)
return randoms.to_a if randoms.size >= 5
end
end
def correct_answer
@numbers.select do |x|
should_be_selected(x)
end.join(', ')
end
end
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
259
260
261
262
263
264
265
266
267
268
269
class ListQuestion < GetQuestion
def random_numbers
randoms = Set.new
loop do
randoms << rand(1000)
return randoms.to_a if randoms.size >= 5
end
end
def candidate_numbers
(1..100).to_a
end
def generate_numbers(size)
random_numbers[0..size].concat(candidate_numbers.shuffle[0..size]).shuffle
end
def answer
@answer && @answer.downcase.strip.gsub(/[ \\t\\r\\n]*,[ \\t\\r\\n]*/, ',')
end
def answered_correctly?(answer)
correct_answer.to_s.downcase.gsub(/[ \\t\\r\\n]*,[ \\t\\r\\n]*/, ',') == answer
end
end
class ReverseListQuestion < ListQuestion
def initialize(player, *numbers)
if numbers.any?
@numbers = numbers
else
size = rand(5)
@numbers = generate_numbers(size)
end
end
def as_text
"reverse this list: " + @numbers.join(', ')
end
private
def correct_answer
@numbers.reverse.join(',')
end
end
class SumListQuestion < ListQuestion
def initialize(player, *numbers)
if numbers.any?
@numbers = numbers
else
size = rand(4) + 1
@numbers = generate_numbers(size)
end
end
def as_text
"give the sum of elements in: " + @numbers.join(', ')
end
private
def correct_answer
@numbers.reduce(:+)
end
end
class MultiplyElementsInListQuestion < ListQuestion
def initialize(player, *numbers)
if numbers.any?
@n = numbers[0]
@numbers = *numbers[1..-1]
else
@n = rand(1000)
size = rand(5)
@numbers = generate_numbers[0..size].concat(candidate_numbers.shuffle[0..size]).shuffle
end
end
def as_text
"multiply by #{@n} those elements: " + @numbers.join(', ')
end
private
def correct_answer
@numbers.map { |e| e * @n }.join(',')
end
end
class FibonacciListQuestion < ListQuestion
def initialize(player, *numbers)
if numbers.any?
@n = numbers[0] + 1
else
@n = rand(19) + 1
end
end
def as_text
count = "are the #{@n} first numbers"
if (@n == 1)
count = "is the first number"
end
"what #{count} of the Fibonacci sequence"
end
private
def fibonacci(n)
a, b = 0, 1
n.times { a, b = b, a + b }
a
end
def correct_answer
(1..@n).map { |e| fibonacci(e) }.join(',')
end
end
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
class MaximumQuestion < SelectFromListOfNumbersQuestion
def as_text
"which of the following numbers is the largest: " + @numbers.join(', ')
end
def points
40
end
private
def should_be_selected(x)
x == @numbers.max
end
def candidate_numbers
(1..100).to_a
end
end
class AdditionQuestion < BinaryMathsQuestion
def as_text
"what is #{@n1} plus #{@n2}"
end
private
def correct_answer
@n1 + @n2
end
end
class SubtractionQuestion < BinaryMathsQuestion
def as_text
"what is #{@n1} minus #{@n2}"
end
private
def correct_answer
@n1 - @n2
end
end
class MultiplicationQuestion < BinaryMathsQuestion
def as_text
"what is #{@n1} multiplied by #{@n2}"
end
private
def correct_answer
@n1 * @n2
end
end
class AdditionAdditionQuestion < TernaryMathsQuestion
def as_text
"what is #{@n1} plus #{@n2} plus #{@n3}"
end
def points
30
end
private
def correct_answer
@n1 + @n2 + @n3
end
end
class AdditionMultiplicationQuestion < TernaryMathsQuestion
def as_text
"what is #{@n1} plus #{@n2} multiplied by #{@n3}"
end
def points
60
end
private
def correct_answer
@n1 + @n2 * @n3
end
end
class MultiplicationAdditionQuestion < TernaryMathsQuestion
def as_text
"what is #{@n1} multiplied by #{@n2} plus #{@n3}"
end
def points
50
end
private
def correct_answer
@n1 * @n2 + @n3
end
end
class PowerQuestion < BinaryMathsQuestion
def as_text
"what is #{@n1} to the power of #{@n2}"
end
def points
20
end
private
def correct_answer
@n1 ** @n2
end
end
class SquareCubeQuestion < SelectFromListOfNumbersQuestion
def as_text
"which of the following numbers is both a square and a cube: " + @numbers.join(', ')
end
def points
60
end
private
def should_be_selected(x)
is_square(x) and is_cube(x)
end
def candidate_numbers
square_cubes = (1..100).map { |x| x ** 3 }.select{ |x| is_square(x) }
squares = (1..50).map { |x| x ** 2 }
square_cubes.concat(squares)
end
def is_square(x)
if (x ==0)
return true
end
(x % (Math.sqrt(x).round(4))) == 0
end
def is_cube(x)
if (x ==0)
return true
end
(x % (Math.cbrt(x).round(4))) == 0
end
end
class PrimesQuestion < SelectFromListOfNumbersQuestion
def as_text
"which of the following numbers are primes: " + @numbers.join(', ')
end
def points
60
end
private
def should_be_selected(x)
Prime.prime? x
end
def candidate_numbers
Prime.take(100)
end
end
class FibonacciQuestion < BinaryMathsQuestion
def ordinal(number)
abs_number = number.to_i.abs
if (11..13).include?(abs_number% 100)
"th"
else
case abs_number % 10
when 1; "st"
when 2; "nd"
when 3; "rd"
else "th"
end
end
end
def ordinalize(number)
"#{number}#{ordinal(number)}"
end
def as_text
n = @n1 + 4
return "what is the #{ordinalize(n)} number in the Fibonacci sequence"
end
def points
50
end
private
def correct_answer
n = @n1 + 4
a, b = 0, 1
n.times { a, b = b, a + b }
a
end
end
class GeneralKnowledgeQuestion < GetQuestion
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
class << self
def question_bank
[
["who is the Prime Minister of Great Britain", "Theresa May"],
["which city is the Eiffel tower in", "Paris"],
["what currency did Spain use before the Euro", "peseta"],
["what colour is a banana", "yellow"],
["who played James Bond in the film Dr No", "Sean Connery"]
]
end
end
def initialize(player)
question = GeneralKnowledgeQuestion.question_bank.sample
@question = question[0]
@correct_answer = question[1]
end
def as_text
@question
end
def correct_answer
@correct_answer
end
end
require 'yaml'
def as_text
possible_words = [@anagram["correct"]] + @anagram["incorrect"]
%Q{which of the following is an anagram of "#{@anagram["anagram"]}": #{possible_words.shuffle.join(", ")}}
end
def initialize(player, *words)
if words.any?
@anagram = {}
@anagram["anagram"], @anagram["correct"], *@anagram["incorrect"] = words
else
anagrams = YAML.load_file(File.join(File.dirname(__FILE__), "anagrams.yaml"))
@anagram = anagrams.sample
end
end
def correct_answer
@anagram["correct"]
end
end
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
def as_text
"what is the english scrabble score of #{@word}"
end
def initialize(player, word=nil)
if word
@word = word
else
@word = ["banana", "september", "cloud", "zoo", "ruby", "buzzword"].sample
end
end
def correct_answer
@word.chars.inject(0) do |score, letter|
score += scrabble_scores[letter.downcase]
end
end
private
def scrabble_scores
scores = {}
%w{e a i o n r t l s u}.each {|l| scores[l] = 1 }
%w{d g}.each {|l| scores[l] = 2 }
%w{b c m p}.each {|l| scores[l] = 3 }
%w{f h v w y}.each {|l| scores[l] = 4 }
%w{k}.each {|l| scores[l] = 5 }
%w{j x}.each {|l| scores[l] = 8 }
%w{q z}.each {|l| scores[l] = 10 }
scores
end
end
class QuestionFactory
attr_reader :round
def initialize
@round = 1
@question_types = [
AdditionQuestion,
MaximumQuestion,
MultiplicationQuestion,
SquareCubeQuestion,
GeneralKnowledgeQuestion,
PrimesQuestion,
SubtractionQuestion,
FibonacciQuestion,
PowerQuestion,
AdditionAdditionQuestion,
AdditionMultiplicationQuestion,
MultiplicationAdditionQuestion,
AnagramQuestion,
ScrabbleQuestion
]
end
def available_question_types
window_end = (@round * 2 - 1)
window_start = [0, window_end - 4].max
@question_types[window_start..window_end]
end
def next_question(player)
available_question_types.sample.new(player)
end
def advance_round
@round += 1
end
end
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
class SecondPhaseQuestionFactory < GetQuestion
attr_reader :round
def initialize
@round = 1
@question_types = [
ReverseListQuestion,
SumListQuestion,
MultiplyElementsInListQuestion,
FibonacciListQuestion
]
end
def available_question_types
window_end = (@round * 2 - 1)
window_start = [0, window_end - 4].max
@question_types[window_start..window_end]
end
def next_question(player)
available_question_types.sample.new(player)
end
def advance_round
@round += 1
end
end
class WarmupQuestion < GetQuestion
def initialize(player)
@player = player
end
def correct_answer
@player.name
end
def as_text
"what is your name"
end
end
class WarmupQuestionFactory
def available_question_types
[WarmupQuestion]
end