Skip to content

Commit

Permalink
implement a sliding window of questions, so earlier questions are dro…
Browse files Browse the repository at this point in the history
…pped as later ones are introduced. Issue rchatley#2
  • Loading branch information
rchatley committed Sep 8, 2011
1 parent 60973fd commit 0ebdf38
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/extreme_startup/question_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,9 @@ def initialize
end

def next_question(player)
available_question_types = @question_types[0..(@round * 2 - 1)]
window_end = (@round * 2 - 1)
window_start = [0, window_end - 4].max
available_question_types = @question_types[window_start..window_end]
available_question_types.sample.new(player)
end

Expand Down
19 changes: 19 additions & 0 deletions spec/extreme_startup/question_factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@ module ExtremeStartup
end

end

context "in the third round" do
before(:each) do
factory.advance_round
factory.advance_round
end

it "moves a sliding window forward, keeping 5 question types, so AdditionQuestions no longer appear" do
questions = 30.times.map { factory.next_question(player) }
questions.any? { |q| q.is_a?(AdditionQuestion) }.should be_false
questions.any? { |q| q.is_a?(MaximumQuestion) }.should be_true
questions.any? { |q| q.is_a?(MultiplicationQuestion) }.should be_true
questions.any? { |q| q.is_a?(SquareCubeQuestion) }.should be_true
questions.any? { |q| q.is_a?(MultiplicationQuestion) }.should be_true
questions.any? { |q| q.is_a?(SquareCubeQuestion) }.should be_true
questions.all? { |q| [MaximumQuestion, MultiplicationQuestion, SquareCubeQuestion, GeneralKnowledgeQuestion, PrimesQuestion].include? q.class }
end

end

end
end

0 comments on commit 0ebdf38

Please sign in to comment.