Created
June 29, 2012 19:33
-
-
Save brenfrow/3020142 to your computer and use it in GitHub Desktop.
dice game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # You need to write this method | |
| score = 0 | |
| map = {} | |
| #create map of { dice_num => times_rolled } | |
| dice.each do |num| | |
| if map.has_key? num | |
| map[num] += 1 | |
| else | |
| map[num] = 1 | |
| end | |
| end | |
| #find one if one is greater than 3 | |
| triple = map.detect {|k,v| v >= 3} | |
| unless triple.nil? | |
| #score the triple | |
| dice_value = triple[0] | |
| if dice_value == 1 | |
| score += 1000 | |
| else | |
| score += 100 * dice_value | |
| end | |
| #remove triple from array | |
| i = 3 | |
| dice.delete_if do |num| | |
| if( num == dice_value && i > 0) | |
| i -= 1 | |
| return true | |
| else | |
| return false | |
| end | |
| end | |
| end | |
| score += dice.select { |x| x == 5}.count * 50 | |
| score += dice.select { |x| x == 1}.count * 100 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment