Class: Yahtzee::Scorecard
- Inherits:
-
Object
- Object
- Yahtzee::Scorecard
- Defined in:
- lib/yahtzee.rb
Constant Summary collapse
- @@bonus_value =
35
- @@full_house_value =
25
- @@small_straight_value =
30
- @@large_straight_value =
40
- @@yahtzee_value =
50
- @@yahtzee_bonus_value =
100
Instance Method Summary collapse
- #add_section(sect) ⇒ Object
- #calculate_dice(dice, joker) ⇒ Object
- #calculate_totals ⇒ Object
- #get_cats ⇒ Object
-
#initialize ⇒ Scorecard
constructor
A new instance of Scorecard.
- #score(cat, dice, joker) ⇒ Object
Constructor Details
#initialize ⇒ Scorecard
Returns a new instance of Scorecard.
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 |
# File 'lib/yahtzee.rb', line 141 def initialize @upper_section = [ :aces, :twos, :threes, :fours, :fives, :sixes ] @lower_section = [ :three_of_a_kind, :four_of_a_kind, :full_house, :small_straight, :large_straight, :yahtzee, :chance, :yahtzee_bonus ] @categories = { :aces => -1, :twos => -1, :threes => -1, :fours => -1, :fives => -1, :sixes => -1, :upper_section_bonus => 0, :three_of_a_kind => -1, :four_of_a_kind => -1, :full_house => -1, :small_straight => -1, :large_straight => -1, :yahtzee => -1, :chance => -1, :yahtzee_bonus => 0, :upper_section_subtotal => 0, :upper_section_total => 0, :lower_section_total => 0, :grand_total => 0 } end |
Instance Method Details
#add_section(sect) ⇒ Object
275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/yahtzee.rb', line 275 def add_section sect sum = 0 @categories.each do |key, value| if (sect.include? key) && value > -1 sum += value end end return sum end |
#calculate_dice(dice, joker) ⇒ Object
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 |
# File 'lib/yahtzee.rb', line 172 def calculate_dice dice, joker cats = { :aces => 0, :twos => 0, :threes => 0, :fours => 0, :fives => 0, :sixes => 0, :three_of_a_kind => 0, :four_of_a_kind => 0, :full_house => 0, :small_straight => 0, :large_straight => 0, :yahtzee => 0, :chance => 0 } # upper section dice.each do |i| case i when 1 cats[:aces] += 1 when 2 cats[:twos] += 2 when 3 cats[:threes] += 3 when 4 cats[:fours] += 4 when 5 cats[:fives] += 5 when 6 cats[:sixes] += 6 end end # lower section freq = [0,0,0,0,0,0] sum = 0 dice.size.times do |i| sum += dice[i] freq[dice[i]-1] += 1 end sfreq = freq.sort.reverse max = sfreq[0] # number of most frequent dice smax = sfreq[1] # number of second most frequent small = false large = false ssum = 0 sdice = dice.sort.uniq sdice.size.times do |i| ssum += sdice[i] end large = true if sdice.length >= 5 and (ssum == 15 or ssum == 20) small = true if sdice.length >= 4 and (ssum == 10 or ssum == 14 or ssum == 18) or large cats[:three_of_a_kind] = sum if (max >= 3 || joker) cats[:four_of_a_kind] = sum if (max >= 4 || joker) cats[:full_house] = @@full_house_value if ((max == 3 and smax == 2) || joker) cats[:small_straight] = @@small_straight_value if (small || joker) cats[:large_straight] = @@large_straight_value if (large || joker) cats[:yahtzee] = @@yahtzee_value if (max >= 5 || joker) cats[:chance] = sum return cats end |
#calculate_totals ⇒ Object
287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/yahtzee.rb', line 287 def calculate_totals @categories[:upper_section_subtotal] = add_section @upper_section @categories[:upper_section_total] = @categories[:upper_section_subtotal] @categories[:upper_section_bonus] = @@bonus_value if @categories[:upper_section_subtotal] >= 63 @categories[:upper_section_total] += @categories[:upper_section_bonus] @categories[:lower_section_total] = add_section @lower_section @categories[:grand_total] = @categories[:upper_section_total] + @categories[:lower_section_total] end |
#get_cats ⇒ Object
299 300 301 |
# File 'lib/yahtzee.rb', line 299 def get_cats return @categories end |
#score(cat, dice, joker) ⇒ Object
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 270 271 272 273 |
# File 'lib/yahtzee.rb', line 240 def score cat, dice, joker pot = calculate_dice dice, joker case cat when (:aces or :twos or :threes or :fours or :fives or :sixes) if @categories[cat] == -1 @categories[cat] = pot[cat] calculate_totals return 1 end when :yahtzee return -1 if joker case @categories[:yahtzee] when -1 @categories[:yahtzee] = pot[:yahtzee] calculate_totals return 1 when @@yahtzee_value @categories[:yahtzee_bonus] += @@yahtzee_bonus_value end which = @upper_section[dice[0]-1] if @categories[which] == -1 return score which, dice, joker end return 0 else if @categories[cat] == -1 @categories[cat] = pot[cat] calculate_totals return 1 end end return -1 end |