Class: Jeopardy::Game
- Inherits:
-
Object
- Object
- Jeopardy::Game
- Defined in:
- lib/jeopardy.rb,
ext/jeopardy/jeopardy.c
Instance Attribute Summary collapse
-
#double_jeopardy_clues ⇒ Object
Returns the value of attribute double_jeopardy_clues.
-
#double_jeopardy_daily_doubles_left ⇒ Object
Returns the value of attribute double_jeopardy_daily_doubles_left.
-
#final_jeopardy_clue ⇒ Object
Returns the value of attribute final_jeopardy_clue.
-
#jeopardy_clues ⇒ Object
Returns the value of attribute jeopardy_clues.
-
#jeopardy_daily_doubles_left ⇒ Object
Returns the value of attribute jeopardy_daily_doubles_left.
-
#players ⇒ Object
Returns the value of attribute players.
-
#previous_daily_double_column ⇒ Object
Returns the value of attribute previous_daily_double_column.
Instance Method Summary collapse
-
#coryats ⇒ Object
Returns a hash containing the Jeopardy::Game#players as keys and the corresponding Coryat scores as values.
-
#daily_doubles(rounds = [1, 2]) ⇒ Object
Returns an array containing the Daily Doubles with the following parameter:.
-
#initialize(options = {}) ⇒ Game
constructor
Initializes a new Jeopardy::Game with an optional hash containing the following keys:.
-
#reset_scores(scores = [0, 0, 0]) ⇒ Object
Resets the games scores to the values found in the given array.
-
#scores(rounds = [1,2,3]) ⇒ Object
Returns a hash containing the Jeopardy::Game#players as keys and their corresponding scores as values, with the following optional parameter:.
-
#simulate(*args) ⇒ Object
Simulates a Jeopardy::Game with an options hash containing the following keys:.
Constructor Details
#initialize(options = {}) ⇒ Game
Initializes a new Jeopardy::Game with an optional hash containing the following keys:
- :jeopardy_clues
-
An array containing the Jeopardy::Clue instances remaining in the Jeopardy! round (default: a full round)
- :double_jeopardy_clues
-
An array containing the Jeopardy::Clue instances remaining in Double Jeopardy! (default: a full round)
- :players
-
An array containing three Jeopardy::Player instances (default: three average players with scores of zero)
- :jeopardy_daily_doubles_left
-
The number of Daily Doubles yet to be uncovered in the Jeopardy! round (default : 1)
- :double_jeopardy_daily_doubles_left
-
The number of Daily Doubles yet to be uncovered in the Double Jeopardy! round (default: 2)
- :previous_daily_double_column
-
The column that the first DJ Daily Double was found in, eliminating any remaining clues in that column from potentially being the remaining Daily Double (default: nil)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/jeopardy.rb', line 19 def initialize( = {}) [:jeopardy_clues] ||= 0.upto(29).map { |location| Jeopardy::Clue.new(row: location.to_i%5, column: location.to_i/5, value: location.to_i%5*200+200) } [:double_jeopardy_clues] ||= 0.upto(29).map { |location| Jeopardy::Clue.new(round: 2, row: location.to_i%5, column: location.to_i/5, value: location.to_i%5*400+400) } [:players] ||= [Jeopardy::Player.new, Jeopardy::Player.new, Jeopardy::Player.new] [:jeopardy_daily_doubles_left] ||= 1 [:double_jeopardy_daily_doubles_left] ||= 2 @jeopardy_clues = [:jeopardy_clues] @double_jeopardy_clues = [:double_jeopardy_clues] @final_jeopardy_clue = Jeopardy::Clue.new(round: 3, value: 0) @players = [:players] @previous_daily_double_column = [:previous_daily_double_column] @jeopardy_daily_doubles_left = [:jeopardy_daily_doubles_left] @double_jeopardy_daily_doubles_left = [:double_jeopardy_daily_doubles_left] end |
Instance Attribute Details
#double_jeopardy_clues ⇒ Object
Returns the value of attribute double_jeopardy_clues.
7 8 9 |
# File 'lib/jeopardy.rb', line 7 def double_jeopardy_clues @double_jeopardy_clues end |
#double_jeopardy_daily_doubles_left ⇒ Object
Returns the value of attribute double_jeopardy_daily_doubles_left.
7 8 9 |
# File 'lib/jeopardy.rb', line 7 def double_jeopardy_daily_doubles_left @double_jeopardy_daily_doubles_left end |
#final_jeopardy_clue ⇒ Object
Returns the value of attribute final_jeopardy_clue.
7 8 9 |
# File 'lib/jeopardy.rb', line 7 def final_jeopardy_clue @final_jeopardy_clue end |
#jeopardy_clues ⇒ Object
Returns the value of attribute jeopardy_clues.
7 8 9 |
# File 'lib/jeopardy.rb', line 7 def jeopardy_clues @jeopardy_clues end |
#jeopardy_daily_doubles_left ⇒ Object
Returns the value of attribute jeopardy_daily_doubles_left.
7 8 9 |
# File 'lib/jeopardy.rb', line 7 def jeopardy_daily_doubles_left @jeopardy_daily_doubles_left end |
#players ⇒ Object
Returns the value of attribute players.
7 8 9 |
# File 'lib/jeopardy.rb', line 7 def players @players end |
#previous_daily_double_column ⇒ Object
Returns the value of attribute previous_daily_double_column.
7 8 9 |
# File 'lib/jeopardy.rb', line 7 def previous_daily_double_column @previous_daily_double_column end |
Instance Method Details
#coryats ⇒ Object
Returns a hash containing the Jeopardy::Game#players as keys and the corresponding Coryat scores as values.
Note: Coryat scores cannot be calculated prior to a call to Jeopardy::Game#simulate
Usage
g = Jeopardy::Game.newg.simulateg.coryats-
#=>
{ g.players[0] => 14_400, g.players[1] => 15_800, g.players[2] => 14_000 }
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/jeopardy.rb', line 101 def coryats coryats = {} players.each {|player| coryats[player] = 0} (@jeopardy_clues + @double_jeopardy_clues).each do |clue| clue.answers.each do |player, answer| coryats[player] += clue.value if answer coryats[player] -= clue.value if !answer && clue.wagers.count == 0 end end return coryats end |
#daily_doubles(rounds = [1, 2]) ⇒ Object
Returns an array containing the Daily Doubles with the following parameter:
- :rounds
-
An array or integer declaring which round(s) to look for Daily Doubles in (default: [1,2])
Note: Daily Doubles are not determined until after a call to Jeopardy::Game#simulate.
Usage
g = Jeopardy::Game.newg.simulate(seed: 0)g.daily_doubles([1,2])-
#=>
[<Jeopardy::Clue (@round = 1)>, <Jeopardy::Clue (@round = 2)>, <Jeopardy::Clue (@round = 2)>] g.daily_doubles(1)-
#=>
[<Jeopardy::Clue (@round = 1)>]
84 85 86 87 88 |
# File 'lib/jeopardy.rb', line 84 def daily_doubles(rounds = [1, 2]) rounds = Set.new([rounds].flatten) all_clues = @jeopardy_clues + @double_jeopardy_clues all_clues.select {|clue| clue.wagers.count > 0 && rounds.include?(clue.round)} end |
#reset_scores(scores = [0, 0, 0]) ⇒ Object
Resets the games scores to the values found in the given array. Returns self so that a simulate call may be chained
- :scores
-
Three scores corresponding to the new score for each player (default: [0, 0, 0])
Usage
g = Jeopardy::Game.newg.simulateg.reset_scores([0, 0, 0])-
#=>
g
65 66 67 68 |
# File 'lib/jeopardy.rb', line 65 def reset_scores(scores = [0, 0, 0]) @players.each_with_index {|player, index| player.score = scores[index]} return self end |
#scores(rounds = [1,2,3]) ⇒ Object
Returns a hash containing the Jeopardy::Game#players as keys and their corresponding scores as values, with the following optional parameter:
rounds: An array or integer declaring which round(s) to calculate scores for (default: [1,2,3])
Note: Scores cannot be determined until after a call to Jeopardy::Game#simulate. If the simulation starts in the middle of a round, this will not coincide with the scores found in Jeopardy::Game#players, as those scores include the beginning value. These scores only show the results of the clues simulated.
Usage
g = Jeopardy::Game.newg.simulate(seed: 0)g.scores(1)-
#=>
{ g.players[0] => 5_200, g.players[2] => 4_600, g.players[3] => 7_400 } g.scores(2)-
#=>
{ g.players[0] => 9_200, g.players[2] => 6_000, g.players[3] => 7_600 } g.scores( [1,2])-
#=>
{ g.players[0] => 14_400, g.players[2] => 10_600, g.players[3] => 15_000 } g.scores(3)-
#=>
{ g.players[0] => 6801, g.players[2] => 0, g.players[3] => 13_801 } g.scores([1,2,3])-
#=>
{ g.players[0] => 21_201, g.players[1] => 10_600, g.players[2] => 28_801 }
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/jeopardy.rb', line 133 def scores(rounds=[1,2,3]) rounds = Set.new([rounds].flatten) all_clues = @jeopardy_clues + @double_jeopardy_clues + [@final_jeopardy_clue] clues_for_rounds = all_clues.select {|clue| rounds.include?(clue.round)} scores = {} players.each {|player| scores[player] = 0} clues_for_rounds.each do |clue| clue.answers.each do |player, answer| wager = clue.wagers[player] scores[player] += wager if !wager.nil? && answer scores[player] -= wager if !wager.nil? && !answer scores[player] += clue.value if wager.nil? && answer scores[player] -= clue.value if wager.nil? && !answer end end return scores end |
#simulate(*args) ⇒ Object
Simulates a Jeopardy::Game with an options hash containing the following keys:
- :trials
-
The number of times the Monte Carlo sim will be run (default: 1)
- :seed
-
The seed which is fed to the random number generators, or nil for a time based seed (default: nil)
Returns a hash with Jeopardy::Game#players as keys and their respective wins as values.
Usage
Jeopardy::Game.new.simulate(seed: 0)-
#=>
{ Jeopardy::Game.players[0] => 0, Jeopardy::Game.players[1] => 0, Jeopardy::Game.players[2] => 1 } Jeopardy::Game.new.simulate(seed: 0, trials: 1000)-
#=>
{ Jeopardy::Game.players[0] => 338, Jeopardy::Game.players[1] => 343, Jeopardy::Game.players[2] => 319 }
49 50 51 52 |
# File 'lib/jeopardy.rb', line 49 def simulate( = {}) #empty, just used a placeholder for documentation #actual implementation is in jeopardy.c end |