functional-yahtzee 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ require 'test_helper'
2
+ require 'scoring'
3
+
4
+ describe Yahtzee::Scoring do
5
+ subject { Yahtzee::Scoring }
6
+
7
+ describe "score(dice, placement)" do
8
+ describe "UpperCard Scoring" do
9
+ let(:score_card) { Yahtzee::ScoreCard.new }
10
+ let(:save_card) { Yahtzee::ScoreCard.persist(score_card) }
11
+ it "must keep state" do
12
+ card = subject.score([1,1,3,4,5], :aces, &save_card)
13
+ card = subject.score([1,2,2,4,5], :twos, &save_card)
14
+ card.aces.must_equal 2
15
+ card.twos.must_equal 4
16
+ end
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,19 @@
1
+ if RUBY_ENGINE == "ruby"
2
+ begin
3
+ require 'simplecov'
4
+ require 'coveralls'
5
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
6
+ Coveralls::SimpleCov::Formatter,
7
+ SimpleCov::Formatter::HTMLFormatter
8
+ ]
9
+ SimpleCov.start do
10
+ add_filter "/test/"
11
+ end
12
+ rescue LoadError
13
+ warn "unable to load Coveralls!"
14
+ end
15
+ end
16
+ require 'minitest/autorun'
17
+
18
+ require_relative '../lib/yahtzee'
19
+
@@ -0,0 +1,40 @@
1
+ require 'test_helper'
2
+
3
+ require 'dice'
4
+
5
+ describe Yahtzee::Dice do
6
+ subject { Yahtzee::Dice }
7
+
8
+ describe ".reroll(keepers)" do
9
+ subject { Yahtzee::Dice.reroll([1,2,3]) }
10
+
11
+ it "must return the keepers + the new roll" do
12
+ subject.slice(0..2).must_equal([1,2,3])
13
+ end
14
+
15
+ it "must always return 5 digits" do
16
+ [[1,2,3],[1,2],[1,2,3,4], [1,2,3,4,5,6]].each do |keepers|
17
+ Yahtzee::Dice.reroll(keepers).count.must_equal 5
18
+ end
19
+ end
20
+ end
21
+
22
+ describe ".roll(num)" do
23
+ 1.upto(5).each do |roll|
24
+ it "must retun #{roll} dice" do
25
+ subject.roll(roll).size.must_equal roll
26
+ end
27
+ end
28
+
29
+ it "wont allow rolls with more then 5 die" do
30
+ subject.roll(6).size.must_equal(5)
31
+ end
32
+
33
+ it "must only roll 1-6" do
34
+ subject.roll(6).each do |roll|
35
+ (1..6).include?(roll).must_equal true
36
+ end
37
+ end
38
+ end
39
+
40
+ end
@@ -0,0 +1,27 @@
1
+ require 'test_helper'
2
+ require 'game'
3
+
4
+ describe Yahtzee::Game do
5
+ subject { Yahtzee::Game }
6
+
7
+ describe "first_roll" do
8
+ it "must roll the dice and return the roll" do
9
+ subject.first_roll.must_be_kind_of Array
10
+ end
11
+ end
12
+
13
+ describe "second_roll(keepers)" do
14
+ let(:keepers) { [1,2,3] }
15
+ it "must keep the die they want and reroll the rest" do
16
+ (subject.second_roll(keepers) & [1,2,3]).must_equal keepers
17
+ end
18
+ end
19
+
20
+ describe "third_roll(keepers)" do
21
+ let(:keepers) { [1,2,3,4] }
22
+ it "must keep the die they want and reroll the rest" do
23
+ (subject.third_roll(keepers) & [1,2,3,4]).must_equal keepers
24
+ end
25
+ end
26
+ end
27
+
@@ -0,0 +1,35 @@
1
+ require 'test_helper'
2
+ require 'probability'
3
+
4
+ describe Yahtzee::Probability do
5
+ subject { Yahtzee::Probability }
6
+
7
+ describe "large_straight(dice, rolls_left)" do
8
+ it "must return 0.3333333333333333 with 2 rolls left" do
9
+ subject.large_straight([1,2,3,4,6], 2).must_equal(0.3333333333333333)
10
+ end
11
+
12
+ it "must return 0.16666666666666666 with 1 rolls left" do
13
+ subject.large_straight([1,2,3,4,6], 1).must_equal(0.16666666666666666)
14
+ end
15
+
16
+ it "must return 0.03333333333333333 with 1 rolls left" do
17
+ subject.large_straight([1,2,2,6,6], 1).must_equal(0.03333333333333333)
18
+ end
19
+ end
20
+
21
+ describe "small_straight(dice, rolls_left)" do
22
+ it "must return 0.16666666666666666 with 2 rolls left" do
23
+ subject.small_straight([1,2,3,5,6], 2).must_equal(0.16666666666666666)
24
+ end
25
+
26
+ it "must return 0.08333333333333333 with 1 rolls left" do
27
+ subject.small_straight([1,2,3,5,6], 1).must_equal(0.08333333333333333)
28
+ end
29
+
30
+ it "must return 0.03333333333333333 with 1 rolls left" do
31
+ subject.small_straight([1,2,5,5,6], 1).must_equal(0.03333333333333333)
32
+ end
33
+ end
34
+ end
35
+
@@ -0,0 +1,77 @@
1
+ require 'test_helper'
2
+ require 'score_card'
3
+
4
+ describe Yahtzee::ScoreCard do
5
+ subject { Yahtzee::ScoreCard }
6
+
7
+ describe ".upper_keys" do
8
+ subject { Yahtzee::ScoreCard.upper_keys }
9
+ it "must contain all the keys on the upper card" do
10
+ subject.must_equal [:aces, :twos, :threes, :fours,
11
+ :fives, :sixes]
12
+ end
13
+ end
14
+
15
+ describe ".lower_keys" do
16
+ subject { Yahtzee::ScoreCard.lower_keys }
17
+ it "must contain all the keys on the upper card" do
18
+ subject.must_equal [:yahtzee, :bonus_yahtzee_1, :bonus_yahtzee_2,
19
+ :bonus_yahtzee_3, :three_of_a_kind,
20
+ :four_of_a_kind, :full_house, :small_straight,
21
+ :large_straight, :chance]
22
+ end
23
+ end
24
+
25
+ describe ".placement_keys" do
26
+ subject { Yahtzee::ScoreCard.placement_keys }
27
+ it "must include all the placement keys" do
28
+ subject.must_equal [:aces, :twos, :threes, :fours, :fives,
29
+ :sixes, :small_straight, :large_straight,
30
+ :full_house, :three_of_a_kind, :four_of_a_kind,
31
+ :yahtzee, :chance]
32
+ end
33
+ end
34
+
35
+ describe ".meta_keys" do
36
+ subject { Yahtzee::ScoreCard.placement_keys }
37
+ it "must include all the placement keys" do
38
+ subject.must_equal [:aces, :twos, :threes, :fours, :fives,
39
+ :sixes, :small_straight, :large_straight,
40
+ :full_house, :three_of_a_kind, :four_of_a_kind,
41
+ :yahtzee, :chance]
42
+ end
43
+ end
44
+
45
+ describe ".persist" do
46
+ subject { Yahtzee::ScoreCard.persist(score_card) }
47
+ let(:score_card) { Yahtzee::ScoreCard.new( yahtzee: 50 ) }
48
+
49
+ it "will return a new instance of ScoreCard with the old+new vals" do
50
+ old_id = score_card.object_id
51
+ score_card = subject.call(:small_straight, 30)
52
+ score_card.object_id.wont_equal old_id
53
+ end
54
+ end
55
+
56
+ describe "initialize" do
57
+ it "will update attrs when passed in" do
58
+ subject.new( {:aces => 4} ).aces.must_equal 4
59
+ end
60
+ end
61
+
62
+ describe "to_hash" do
63
+ it "must return a hash of the score card elements" do
64
+ subject.new({aces: 4}).to_hash.must_equal({
65
+ aces: 4, twos: nil, threes: nil,
66
+ fours: nil, fives: nil, sixes: nil,
67
+ upper_subtotal: nil,
68
+ upper_total: nil, small_straight: nil,
69
+ large_straight: nil, full_house: nil,
70
+ three_of_a_kind: nil, four_of_a_kind: nil,
71
+ yahtzee: nil, chance: nil, bonus_yahtzee_1: nil,
72
+ bonus_yahtzee_2: nil, bonus_yahtzee_3: nil,
73
+ lower_subtotal: nil, game_total: nil
74
+ })
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,71 @@
1
+ require 'test_helper'
2
+ require 'scoring/lower_card'
3
+
4
+ describe Yahtzee::Scoring::LowerCard do
5
+ subject { Yahtzee::Scoring::LowerCard }
6
+
7
+ describe "score_yahtzee(dice)" do
8
+ it "must score a yahtzee" do
9
+ subject.score_yahtzee([4,4,4,4,4]).must_equal 50
10
+ subject.score_yahtzee([4,4,4,4,6]).must_equal 0
11
+ end
12
+ end
13
+
14
+ describe "score_bonus_yahtzee(dice)" do
15
+ it "must score a bonus yahtzee" do
16
+ subject.score_bonus_yahtzee([4,4,4,4,4]).must_equal 100
17
+ subject.score_bonus_yahtzee([4,4,4,4,6]).must_equal 0
18
+ end
19
+
20
+ #1.upto(3) do |i|
21
+ # it "must respond to score_bonus_yahtzee_#{i}" do
22
+ # subject.send("score_bonus_yahtzee_#{i}",[4,4,4,4,4]).must_equal 100
23
+ # end
24
+ #end
25
+ end
26
+
27
+ describe "score_small_straight(dice)" do
28
+ it "must score a small straight" do
29
+ subject.score_small_straight([1,2,3,4,6]).must_equal 30
30
+ subject.score_small_straight([1,2,3,5,6]).must_equal 0
31
+ end
32
+ end
33
+
34
+ describe "score_large_straight(dice)" do
35
+ it "must score a large straight" do
36
+ subject.score_large_straight([1,2,3,4,5]).must_equal 40
37
+ subject.score_large_straight([1,2,3,4,4]).must_equal 0
38
+ end
39
+ end
40
+
41
+ describe "score_full_house(dice)" do
42
+ it "must return a score of 25 for a full house" do
43
+ subject.score_full_house([2,2,2,4,4]).must_equal 25
44
+ end
45
+
46
+ it "must return a score of 0 for no full-house" do
47
+ subject.score_full_house([1,2,4,4,4]).must_equal 0
48
+ end
49
+ end
50
+
51
+ describe "score_three_of_a_kind(dice)" do
52
+ it "must total matches 3 or over" do
53
+ subject.score_three_of_a_kind([1,2,4,4,4]).must_equal 12
54
+ subject.score_three_of_a_kind([1,2,2,4,4]).must_equal 0
55
+ end
56
+ end
57
+
58
+ describe "score_four_of_a_kind(dice)" do
59
+ it "must total matches 4 or over" do
60
+ subject.score_four_of_a_kind([1,4,4,4,4]).must_equal 16
61
+ subject.score_four_of_a_kind([1,2,2,4,4]).must_equal 0
62
+ end
63
+ end
64
+
65
+ describe "score_min_of_a_kind(dice, min)" do
66
+ it "must total matches of min or over" do
67
+ subject.score_min_of_a_kind([1,4,4,4,4], 4).must_equal 16
68
+ subject.score_min_of_a_kind([1,2,4,4,4], 3).must_equal 12
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,53 @@
1
+ require 'test_helper'
2
+ require 'score_card'
3
+ require 'scoring/upper_card'
4
+
5
+ describe Yahtzee::Scoring::UpperCard do
6
+ subject { Yahtzee::Scoring::UpperCard }
7
+
8
+
9
+ describe "score_sums(dice, scoring_die)" do
10
+ it "must add up only the scoring_die" do
11
+ subject.score_sums([1,1,0,0,0], 1).must_equal 2
12
+ subject.score_sums([1,1,4,4,1], 1).must_equal 3
13
+ end
14
+ end
15
+
16
+ describe "scoring matching digits" do
17
+ describe "score_aces(dice)" do
18
+ it "must score only aces" do
19
+ subject.score_aces([1,2,3,4,5]).must_equal(1)
20
+ end
21
+ end
22
+
23
+ describe "score_twos(dice)" do
24
+ it "must score only twos" do
25
+ subject.score_twos([1,2,3,4,5]).must_equal(2)
26
+ end
27
+ end
28
+
29
+ describe "score_threes(dice)" do
30
+ it "must score only threes" do
31
+ subject.score_threes([1,2,3,4,5]).must_equal(3)
32
+ end
33
+ end
34
+
35
+ describe "score_fours(dice)" do
36
+ it "must score only fours" do
37
+ subject.score_fours([1,2,3,4,5]).must_equal(4)
38
+ end
39
+ end
40
+
41
+ describe "score_fives(dice)" do
42
+ it "must score only fives" do
43
+ subject.score_fives([1,2,3,4,5]).must_equal(5)
44
+ end
45
+ end
46
+
47
+ describe "score_sixes(dice)" do
48
+ it "must score only sixes" do
49
+ subject.score_sixes([1,2,3,4,6]).must_equal(6)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,169 @@
1
+ require 'test_helper'
2
+ require 'scoring'
3
+
4
+ describe Yahtzee::Scoring do
5
+ subject { Yahtzee::Scoring }
6
+
7
+ describe "score(dice, placement)" do
8
+ let(:score_card) { Yahtzee::ScoreCard.new }
9
+ let(:writer) { Yahtzee::ScoreCard.persist(score_card) }
10
+
11
+ describe "score_game_total(scores, &writer)" do
12
+ let(:scores) {{
13
+ upper_total: 119, lower_subtotal: 166
14
+ }}
15
+
16
+ it "must return a score card with the game total of 285 (119+166)" do
17
+ subject.score_game_total(scores, &writer).
18
+ game_total.must_equal 285
19
+ end
20
+ end
21
+
22
+ describe "UpperCard Scoring" do
23
+ describe "Aces" do
24
+ it "must return a score card with aces: 2" do
25
+ subject.score([1,1,3,4,5], :aces, &writer).
26
+ aces.must_equal 2
27
+ end
28
+ end
29
+
30
+ describe "Twos" do
31
+ it "must return a score card with twos: 4" do
32
+ subject.score([1,2,2,4,5], :twos, &writer).
33
+ twos.must_equal 4
34
+ end
35
+ end
36
+
37
+ describe "Threes" do
38
+ it "must return a score card with threes: 6" do
39
+ subject.score([1,3,3,4,5], :threes, &writer).
40
+ threes.must_equal 6
41
+ end
42
+ end
43
+
44
+ describe "Fours" do
45
+ it "must return a score card with fours: 8" do
46
+ subject.score([1,3,4,4,5], :fours, &writer).
47
+ fours.must_equal 8
48
+ end
49
+ end
50
+
51
+ describe "Fives" do
52
+ it "must return a score card with fives: 10" do
53
+ subject.score([1,3,4,5,5], :fives, &writer).
54
+ fives.must_equal 10
55
+ end
56
+ end
57
+
58
+ describe "Sixes" do
59
+ it "must return a score card with sixes: 12" do
60
+ subject.score([1,3,6,5,6], :sixes, &writer).
61
+ sixes.must_equal 12
62
+ end
63
+ end
64
+
65
+ describe "Upper Subtotal" do
66
+ let(:scores) {{
67
+ aces: 2, twos: 4, threes: 6,
68
+ fours: 8, fives: 10, sixes: 12
69
+ }}
70
+
71
+ it "must return a score card with upper_subtotal: 42" do
72
+ subject.score_subtotal(scores, :upper_subtotal, &writer).
73
+ upper_subtotal.must_equal 42
74
+ end
75
+ end
76
+
77
+ describe "score_upper_total(scores, &writer)" do
78
+ let(:scores) {{
79
+ aces: 4, twos: 8, threes: 12,
80
+ fours: 16, fives: 20, sixes: 24
81
+ }}
82
+
83
+ it "must a score card with an upper_total of 119 (84+35)" do
84
+ subject.score_upper_total(scores, &writer).
85
+ upper_total.must_equal 119
86
+ end
87
+ end
88
+ describe "when the sum is not over 63" do
89
+ let(:scores) {{
90
+ aces: 2, twos: 4, threes: 6,
91
+ fours: 8, fives: 10, sixes: 12
92
+ }}
93
+ it "must return a score card with an upper_total of 42" do
94
+ subject.score_upper_total(scores, &writer).
95
+ upper_total.must_equal 42
96
+ end
97
+ end
98
+ end
99
+
100
+ describe "LowerCard Scoring" do
101
+ describe "chance" do
102
+ it "must sum the dice" do
103
+ subject.score([1,2,3,4,5], :chance, &writer).
104
+ chance.must_equal 15
105
+ end
106
+ end
107
+
108
+ describe "full_house" do
109
+ it "must return a score card with full_house: 25" do
110
+ subject.score([1,1,1,2,2], :full_house, &writer).
111
+ full_house.must_equal 25
112
+ end
113
+ end
114
+
115
+ describe "small_straight" do
116
+ it "must return a score card with small_straight: 30" do
117
+ subject.score([1,2,3,4,6], :small_straight, &writer).
118
+ small_straight.must_equal 30
119
+ end
120
+ end
121
+
122
+ describe "large_straight" do
123
+ it "must return a score card with large_straight: 40" do
124
+ subject.score([1,2,3,4,5], :large_straight, &writer).
125
+ large_straight.must_equal 40
126
+ end
127
+ end
128
+
129
+ describe "three_of_a_kind" do
130
+ it "must return a score card with three_of_a_kind: 9" do
131
+ subject.score([1,2,3,3,3], :three_of_a_kind, &writer).
132
+ three_of_a_kind.must_equal 9
133
+ end
134
+ end
135
+
136
+ describe "four_of_a_kind" do
137
+ it "must return a score card with four_of_a_kind: 16" do
138
+ subject.score([1,4,4,4,4], :four_of_a_kind, &writer).
139
+ four_of_a_kind.must_equal 16
140
+ end
141
+ end
142
+
143
+ describe "yahtzee" do
144
+ it "must return a score card with yahtzee: 50" do
145
+ subject.score([1,1,1,1,1], :yahtzee, &writer).yahtzee.must_equal 50
146
+ end
147
+ end
148
+
149
+ describe "bonus_yahtzee_1" do
150
+ it "must return a score card with bonus_yahtzee_1: 100" do
151
+ subject.score([1,1,1,1,1], :bonus_yahtzee_1, &writer).
152
+ bonus_yahtzee_1.must_equal 100
153
+ end
154
+ end
155
+
156
+ describe "Lower Subtotal" do
157
+ let(:scores) {{
158
+ full_house: 25, small_straight: 30,
159
+ large_straight: 40, three_of_a_kind: 9,
160
+ four_of_a_kind: 12, yahtzee: 50
161
+ }}
162
+ it "must return a score card with lower_subtotal: 166" do
163
+ subject.score_subtotal(scores, :lower_subtotal, &writer).
164
+ lower_subtotal.must_equal 166
165
+ end
166
+ end
167
+ end
168
+ end
169
+ end