-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmove.rb
70 lines (54 loc) · 1.56 KB
/
move.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#fixme (can score yahtzees >1)
class Move
def initialize(move_type)
@move_type = move_type
end
def make(dice)
raise "Already made this move" if ([email protected]?)
@score = send(@move_type, dice)
end
attr_reader :score
private
def ones(dice)
dice.reject{|d| d != 1}.inject(0) {|sum, val| sum + val}
end
def twos(dice)
dice.reject{|d| d != 2}.inject(0) {|sum, val| sum + val}
end
def threes(dice)
dice.reject{|d| d != 3}.inject(0) {|sum, val| sum + val}
end
def fours(dice)
dice.reject{|d| d != 4}.inject(0) {|sum, val| sum + val}
end
def fives(dice)
dice.reject{|d| d != 5}.inject(0) {|sum, val| sum + val}
end
def sixes(dice)
dice.reject{|d| d != 6}.inject(0) {|sum, val| sum + val}
end
def full_house(dice)
dice.sort!
dice.uniq.length == 2 && (dice[0] == dice[1]) && (dice[-1] == dice[-2]) ? 25 : 0
end
def small_straight(dice)
sorted_dice = dice.uniq.sort
sorted_dice[0..3] == [1, 2, 3, 4] || sorted_dice[0..3] == [2, 3, 4, 5] || sorted_dice[1..4]== [3, 4, 5, 6] ? 30 : 0
end
def large_straight(dice)
dice.sort!
dice == [1, 2, 3, 4, 5] || dice == [2, 3, 4, 5, 6] ? 40 : 0
end
def three_of_a_kind(dice)
dice.reject{|d| d != dice.sort[2]}.length >= 3 ? dice.inject() {|sum, val| sum + val} : 0
end
def four_of_a_kind(dice)
dice.reject{|d| d != dice.sort[2]}.length >= 4 ? dice.inject() {|sum, val| sum + val} : 0
end
def yahtzee(dice)
dice == Array.new(5, dice[0]) ? 50 : 0
end
def chance(dice)
dice.inject() {|sum, val| sum + val}
end
end