bridge 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  require "helper"
2
2
 
3
- class TestDuplicate < Test::Unit::TestCase
4
- test "maximum points with unique values" do
3
+ describe Bridge::Points::Duplicate do
4
+ it "maximum points with unique values" do
5
5
  maximum = Bridge::Points::Duplicate.new(-200, -100, 620, 630, 660, 690).maximum
6
6
  assert_equal 10, maximum[690]
7
7
  assert_equal 8, maximum[660]
@@ -11,7 +11,7 @@ class TestDuplicate < Test::Unit::TestCase
11
11
  assert_equal 0, maximum[-200]
12
12
  end
13
13
 
14
- test "maximum points with non-unique values" do
14
+ it "maximum points with non-unique values" do
15
15
  maximum = Bridge::Points::Duplicate.new(420, 420, 400, 400, -50, -100).maximum
16
16
  assert_equal 9, maximum[420]
17
17
  assert_equal 5, maximum[400]
@@ -19,14 +19,14 @@ class TestDuplicate < Test::Unit::TestCase
19
19
  assert_equal 0, maximum[-100]
20
20
  end
21
21
 
22
- test "maximum points with non-unique values, without zero value" do
22
+ it "maximum points with non-unique values, without zero value" do
23
23
  maximum = Bridge::Points::Duplicate.new(430, 420, 420, 420, 300, 300).maximum
24
24
  assert_equal 10, maximum[430]
25
25
  assert_equal 6, maximum[420]
26
26
  assert_equal 1, maximum[300]
27
27
  end
28
28
 
29
- test "maximum percents with non-unique values" do
29
+ it "maximum percents with non-unique values" do
30
30
  maximum_in_percents = Bridge::Points::Duplicate.new(-630, 100, 100, -600, 200, -600, 100, 100, 100, -600, 100, 100, 100, 100).maximum_in_percents
31
31
  assert_in_delta 100.0, maximum_in_percents[200], 0.05
32
32
  assert_in_delta 61.5, maximum_in_percents[100], 0.05
@@ -34,7 +34,7 @@ class TestDuplicate < Test::Unit::TestCase
34
34
  assert_in_delta 0.0, maximum_in_percents[-630], 0.05
35
35
  end
36
36
 
37
- test "maximum percents with unique-values" do
37
+ it "maximum percents with unique-values" do
38
38
  maximum_in_percents = Bridge::Points::Duplicate.new(200, 170, 500, 430, 550, 420).maximum_in_percents
39
39
  assert_in_delta 100.0, maximum_in_percents[550], 0.005
40
40
  assert_in_delta 80.0, maximum_in_percents[500], 0.005
@@ -44,7 +44,7 @@ class TestDuplicate < Test::Unit::TestCase
44
44
  assert_in_delta 0.0, maximum_in_percents[170], 0.005
45
45
  end
46
46
 
47
- test "butler with skipping the highest and the lowest score" do
47
+ it "butler with skipping the highest and the lowest score" do
48
48
  butler = Bridge::Points::Duplicate.new(690, 660, 630, 620, -100, -200).butler
49
49
  assert_equal 6, butler[690]
50
50
  assert_equal 5, butler[660]
@@ -54,7 +54,7 @@ class TestDuplicate < Test::Unit::TestCase
54
54
  assert_equal -12, butler[-200]
55
55
  end
56
56
 
57
- test "cavendish with unique values" do
57
+ it "cavendish with unique values" do
58
58
  cavendish = Bridge::Points::Duplicate.new(690, 660, 630, 620, -100, -200).cavendish
59
59
  assert_in_delta 6.2, cavendish[690], 0.05
60
60
  assert_in_delta 5.4, cavendish[660], 0.05
@@ -64,7 +64,7 @@ class TestDuplicate < Test::Unit::TestCase
64
64
  assert_in_delta -11.0, cavendish[-200], 0.05
65
65
  end
66
66
 
67
- test "cavendish with non-unique values" do
67
+ it "cavendish with non-unique values" do
68
68
  cavendish = Bridge::Points::Duplicate.new(100, 100, 110, 140).cavendish
69
69
  assert_in_delta 1.0, cavendish[140], 0.05
70
70
  assert_in_delta -0.3, cavendish[110], 0.05
@@ -1,9 +1,6 @@
1
- Bundler.require
1
+ require "minitest/autorun"
2
2
 
3
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
- $LOAD_PATH.unshift(File.dirname(__FILE__))
5
- require 'bridge'
6
-
7
- class Test::Unit::TestCase
3
+ require "bridge"
8
4
 
5
+ class MiniTest::Unit::TestCase
9
6
  end
@@ -0,0 +1,102 @@
1
+ require "helper"
2
+
3
+ describe Bridge::Play do
4
+ it "returns deal from deal_id" do
5
+ play = Bridge::Play.new(0, "7SN", [])
6
+ assert_equal Bridge::Deal, play.deal.class
7
+ end
8
+
9
+ it "returns player positions" do
10
+ play = Bridge::Play.new(0, "7SS", [])
11
+ assert_equal "S", play.declarer
12
+ assert_equal "W", play.lho
13
+ assert_equal "N", play.dummy
14
+ assert_equal "E", play.rho
15
+ end
16
+
17
+ it "returns trump" do
18
+ play = Bridge::Play.new(0, "7SN", [])
19
+ assert_equal "S", play.trump
20
+
21
+ play = Bridge::Play.new(0, "7NTN", [])
22
+ assert_nil play.trump
23
+ end
24
+
25
+ it "returns tricks" do
26
+ play = Bridge::Play.new(0, "7SN", ["HA", "D2", "C2", "S2", "SA"])
27
+ assert_equal ["HA", "D2", "C2", "S2"], play.tricks.first.cards.map(&:to_s)
28
+ assert_equal ["SA"], play.tricks.last.cards.map(&:to_s)
29
+ end
30
+
31
+ it "returns directions of played cards" do
32
+ play = Bridge::Play.new(0, "7SN", ["HA", "D2", "C2", "S2", "SA"])
33
+ assert_equal ["E", "S", "W", "N", "N"], play.directions
34
+ end
35
+
36
+ it "returns declarer tricks number" do
37
+ cards = ["C5", "CA", "C4", "C3",
38
+ "C6", "C7", "H4", "CK",
39
+ "CJ", "C2", "C8", "S6"]
40
+ assert_equal 2, Bridge::Play.new(636839108127179982824423290, "1SN", cards).declarer_tricks_number
41
+ end
42
+
43
+ describe "#next_direction" do
44
+ it "returns direction next to dealer if first lead" do
45
+ play = Bridge::Play.new(0, "7SN", [])
46
+ assert_equal "E", play.next_direction
47
+ end
48
+
49
+ it "returns direction of last trick winner when lead" do
50
+ play = Bridge::Play.new(0, "7SN", ["HA", "D2", "C2", "S2"])
51
+ assert_equal "N", play.next_direction
52
+ end
53
+
54
+ it "returns direction next to previous if trick not complete" do
55
+ play = Bridge::Play.new(0, "7SN", ["HA", "D2", "C2"])
56
+ assert_equal "N", play.next_direction
57
+ end
58
+
59
+ it "returns last trick winner on next lead" do
60
+ play = Bridge::Play.new(0, "7HN", ["HA", "D2", "C2", "S2"])
61
+ assert_equal "E", play.next_direction
62
+ end
63
+ end
64
+
65
+ describe "#card_allowed?" do
66
+ it "returns false if no contract" do
67
+ play = Bridge::Play.new(0, nil, [])
68
+ refute play.card_allowed?("SA")
69
+ end
70
+
71
+ it "returns true if card is proper" do
72
+ play = Bridge::Play.new(0, "7SN", ["HA", "D2", "C2"])
73
+ assert play.card_allowed?("S2")
74
+ end
75
+
76
+ it "returns false if card already played" do
77
+ play = Bridge::Play.new(0, "7SN", ["HA", "D2", "C2", "S2"])
78
+ refute play.card_allowed?("S2")
79
+ end
80
+
81
+ it "returns false if card not in hand of next player" do
82
+ play = Bridge::Play.new(0, "7SN", ["HA", "D2", "C2"])
83
+ refute play.card_allowed?("CA")
84
+ end
85
+
86
+ it "returns true for other suit if no more cards in first card suit" do
87
+ play = Bridge::Play.new(636839108127179982824423290, "1SN", ["C5", "CA", "C4", "C3", "C6", "C7"])
88
+ assert play.card_allowed?("S6")
89
+ end
90
+
91
+ it "returns false if card suit other than firsts card in trick" do
92
+ play = Bridge::Play.new(636839108127179982824423290, "1SN", ["HQ"])
93
+ refute play.card_allowed?("DT")
94
+ end
95
+ end
96
+ end
97
+
98
+ # deal id: 636839108127179982824423290
99
+ # "N" => ["SA", "SK", "SQ", "S8", "S6", "HK", "H7", "H6", "H4", "DK", "DQ", "DJ", "C3"]
100
+ # "E" => ["S5", "S4", "S3", "HA", "HQ", "HJ", "H9", "D5", "D4", "CK", "CJ", "C9", "C5"]
101
+ # "S" => ["ST", "S7", "S2", "HT", "H8", "H2", "DT", "D8", "D3", "CA", "CT", "C6", "C2"]
102
+ # "W" => ["SJ", "S9", "H5", "H3", "DA", "D9", "D7", "D6", "D2", "CQ", "C8", "C7", "C4"]
@@ -0,0 +1,215 @@
1
+ require "helper"
2
+
3
+ describe Bridge::Score do
4
+ before do
5
+ @score = Bridge::Score.new("1SN", "NONE", 9)
6
+ end
7
+
8
+ it "return made contract?" do
9
+ score = Bridge::Score.new("6SN", "NONE", 9)
10
+ refute score.made?
11
+ score = Bridge::Score.new("3NTN", "NONE", 3)
12
+ refute score.made?
13
+ score = Bridge::Score.new("7NTN", "NONE", 13)
14
+ assert score.made?
15
+ score = Bridge::Score.new("3NTN", "NONE", 11)
16
+ assert score.made?
17
+ end
18
+
19
+ it "return vulnerable" do
20
+ score = Bridge::Score.new("6SN", "BOTH", 12)
21
+ assert score.vulnerable?
22
+ score = Bridge::Score.new("6SN", "EW", 12)
23
+ refute score.vulnerable?
24
+ end
25
+
26
+ it "return result" do
27
+ assert_equal 2, @score.result
28
+ score = Bridge::Score.new("6SN", "NONE", 12)
29
+ assert_equal 0, score.result
30
+ score = Bridge::Score.new("6SN", "NONE", 10)
31
+ assert_equal -2, score.result
32
+ end
33
+
34
+ it "return result string" do
35
+ assert_equal "+2", @score.result_string
36
+ score = Bridge::Score.new("6SN", "NONE", 12)
37
+ assert_equal "=", score.result_string
38
+ score = Bridge::Score.new("6SN", "NONE", 10)
39
+ assert_equal "-2", score.result_string
40
+ end
41
+
42
+ it "calculate tricks with plus" do
43
+ score = Bridge::Score.new("4SN", "NONE", "+1")
44
+ assert_equal 11, score.tricks_number
45
+ end
46
+
47
+ it "calculate tricks with minus" do
48
+ score = Bridge::Score.new("4SN", "NONE", "-4")
49
+ assert_equal 6, score.tricks_number
50
+ end
51
+
52
+ it "calculate tricks with equal sign" do
53
+ score = Bridge::Score.new("4SN", "NONE", "=")
54
+ assert_equal 10, score.tricks_number
55
+ end
56
+
57
+ describe "points" do
58
+ it "game bonus" do
59
+ score = Bridge::Score.new("3SN", "NONE", 9)
60
+ assert_equal 140, score.points
61
+ score = Bridge::Score.new("4SN", "NONE", 10)
62
+ assert_equal 420, score.points
63
+ score = Bridge::Score.new("3NTN", "BOTH", 9)
64
+ assert_equal 600, score.points
65
+ end
66
+
67
+ it "small slam bonus" do
68
+ score = Bridge::Score.new("6SN", "NONE", 12)
69
+ assert_equal 980, score.points
70
+ score = Bridge::Score.new("6SN", "BOTH", 12)
71
+ assert_equal 1430, score.points
72
+ end
73
+
74
+ it "grand slam bonus" do
75
+ score = Bridge::Score.new("7SN", "NONE", 13)
76
+ assert_equal 1510, score.points
77
+ score = Bridge::Score.new("7SN", "BOTH", 13)
78
+ assert_equal 2210, score.points
79
+ end
80
+
81
+ it "doubled and redoubled cotract made bonus" do
82
+ score = Bridge::Score.new("4SXN", "NONE", 10)
83
+ assert_equal 590, score.points
84
+ score = Bridge::Score.new("4SXXN", "NONE", 10)
85
+ assert_equal 880, score.points
86
+ end
87
+
88
+ it "vulnerable undertrick points" do
89
+ score = Bridge::Score.new("4SN", "BOTH", 9)
90
+ assert_equal -100, score.points
91
+ score = Bridge::Score.new("4SXN", "BOTH", 9)
92
+ assert_equal -200, score.points
93
+ score = Bridge::Score.new("4SXN", "BOTH", 7)
94
+ assert_equal -800, score.points
95
+ score = Bridge::Score.new("4SXXN", "BOTH", 9)
96
+ assert_equal -400, score.points
97
+ score = Bridge::Score.new("4SXXN", "BOTH", 7)
98
+ assert_equal -1600, score.points
99
+ end
100
+
101
+ it "not vulnerable undertrick points" do
102
+ score = Bridge::Score.new("4SN", "NONE", 9)
103
+ assert_equal -50, score.points
104
+ score = Bridge::Score.new("4SXN", "NONE", 9)
105
+ assert_equal -100, score.points
106
+ score = Bridge::Score.new("4SXN", "NONE", 7)
107
+ assert_equal -500, score.points
108
+ score = Bridge::Score.new("4SXXN", "NONE", 9)
109
+ assert_equal -200, score.points
110
+ score = Bridge::Score.new("4SXXN", "NONE", 7)
111
+ assert_equal -1000, score.points
112
+ score = Bridge::Score.new("4SXN", "NONE", 6)
113
+ assert_equal -800, score.points
114
+ score = Bridge::Score.new("4SXXN", "NONE", 6)
115
+ assert_equal -1600, score.points
116
+ score = Bridge::Score.new("4SN", "NONE", 6)
117
+ assert_equal -200, score.points
118
+ end
119
+
120
+ it "overtrick points" do
121
+ score = Bridge::Score.new("2SN", "NONE", 10)
122
+ assert_equal 170, score.points
123
+ score = Bridge::Score.new("2SXN", "NONE", 9)
124
+ assert_equal 570, score.points
125
+ score = Bridge::Score.new("2SXXN", "NONE", 9)
126
+ assert_equal 840, score.points
127
+ score = Bridge::Score.new("2SN", "BOTH", 9)
128
+ assert_equal 140, score.points
129
+ score = Bridge::Score.new("2SXN", "BOTH", 9)
130
+ assert_equal 870, score.points
131
+ score = Bridge::Score.new("2SXXN", "BOTH", 9)
132
+ assert_equal 1240, score.points
133
+ end
134
+
135
+ it "return 90 points for 1S=" do
136
+ score = Bridge::Score.new("3SN", "NONE", 9)
137
+ assert_equal 140, score.points
138
+ end
139
+
140
+ it "return 70 points for 2S=" do
141
+ score = Bridge::Score.new("2NTN", "NONE", 8)
142
+ assert_equal 120, score.points
143
+ end
144
+
145
+ it "return 80 points for 2D+2" do
146
+ score = Bridge::Score.new("2DN", "NONE", 10)
147
+ assert_equal 130, score.points
148
+ end
149
+
150
+ it "return 1400 points for 5CXX+1" do
151
+ score = Bridge::Score.new("5CXXN", "BOTH", 12)
152
+ assert_equal 1400, score.points
153
+ end
154
+
155
+ it "return 1700 points for 3NTX-7" do
156
+ score = Bridge::Score.new("3NTXN", "NONE", 2)
157
+ assert_equal -1700, score.points
158
+ end
159
+
160
+ it "return -7600 points for 7NTXX-13" do
161
+ score = Bridge::Score.new("7NTXXN", "BOTH", 0)
162
+ assert_equal -7600, score.points
163
+ end
164
+
165
+ it "return -7600 points for 7NT-13" do
166
+ score = Bridge::Score.new("7NTN", "NONE", 0)
167
+ assert_equal -650, score.points
168
+ end
169
+
170
+ it "return -1300 points for 7NT-13" do
171
+ score = Bridge::Score.new("7NTN", "BOTH", 0)
172
+ assert_equal -1300, score.points
173
+ end
174
+
175
+ it "return -3500 points for 7NTX-13" do
176
+ score = Bridge::Score.new("7NTXN", "NONE", 0)
177
+ assert_equal -3500, score.points
178
+ end
179
+
180
+ it "return -3800 points for 7NTX-13" do
181
+ score = Bridge::Score.new("7NTXN", "BOTH", 0)
182
+ assert_equal -3800, score.points
183
+ end
184
+
185
+ it "return -7000 points for 7NTXX-13" do
186
+ score = Bridge::Score.new("7NTXXN", "NONE", 0)
187
+ assert_equal -7000, score.points
188
+ end
189
+
190
+ it "return 1340 points for 1CX+6" do
191
+ score = Bridge::Score.new("1CXN", "BOTH", 13)
192
+ assert_equal 1340, score.points
193
+ end
194
+
195
+ it "return 740 points for 1CX+6" do
196
+ score = Bridge::Score.new("1CXN", "NONE", 13)
197
+ assert_equal 740, score.points
198
+ end
199
+ end
200
+
201
+ describe "contracts" do
202
+ it "1764 results" do
203
+ assert_equal 1764, Bridge::Score.all_contracts.size
204
+ end
205
+
206
+ it "return contracts for 1430 points" do
207
+ expected = ["1C/DXX+3v", "1C/DXX+6", "6H/S=v"]
208
+ assert_equal expected, Bridge::Score.with_points(1430)
209
+ end
210
+
211
+ it "return no contracts if not found score with given points" do
212
+ assert_equal [], Bridge::Score.with_points(100)
213
+ end
214
+ end
215
+ end
@@ -1,47 +1,47 @@
1
1
  require "helper"
2
2
 
3
- class TestTrick < Test::Unit::TestCase
4
- test "the highest H card is the winner in NT game H trick and single suit" do
3
+ describe Bridge::Trick do
4
+ it "the highest H card is the winner in NT game H trick and single suit" do
5
5
  trick = Bridge::Trick.new("H2", "H3", "H4", "H5")
6
6
  assert_equal Bridge::Card.new("H5"), trick.winner
7
7
  end
8
8
 
9
- test "the highest H card is the winner in NT game H trick and multiple suits" do
9
+ it "the highest H card is the winner in NT game H trick and multiple suits" do
10
10
  trick = Bridge::Trick.new("H2", "C3", "D4", "S5")
11
11
  assert_equal Bridge::Card.new("H2"), trick.winner
12
12
  end
13
13
 
14
- test "the highest H card is the winner in S game H trick and single suit" do
14
+ it "the highest H card is the winner in S game H trick and single suit" do
15
15
  trick = Bridge::Trick.new("H2", "H5", "H4", "H3")
16
16
  assert_equal Bridge::Card.new("H5"), trick.winner("S")
17
17
  end
18
18
 
19
- test "the highest H card is the winner in S game H trick and multiple suits" do
19
+ it "the highest H card is the winner in S game H trick and multiple suits" do
20
20
  trick = Bridge::Trick.new("H2", "D5", "C4", "H3")
21
21
  assert_equal Bridge::Card.new("H3"), trick.winner("S")
22
22
  end
23
23
 
24
- test "the only trump is the winner in S game H trick and multiple suits" do
24
+ it "the only trump is the winner in S game H trick and multiple suits" do
25
25
  trick = Bridge::Trick.new("H2", "D5", "S2", "H3")
26
26
  assert_equal Bridge::Card.new("S2"), trick.winner("S")
27
27
  end
28
28
 
29
- test "the highest trump is the winner in S game H trick and multiple suits" do
29
+ it "the highest trump is the winner in S game H trick and multiple suits" do
30
30
  trick = Bridge::Trick.new("H2", "SA", "S2", "H3")
31
31
  assert_equal Bridge::Card.new("SA"), trick.winner("S")
32
32
  end
33
33
 
34
- test "the highest trump is the winner in S game S trick and multiple suits" do
34
+ it "the highest trump is the winner in S game S trick and multiple suits" do
35
35
  trick = Bridge::Trick.new("S2", "HA", "CA", "DA")
36
36
  assert_equal Bridge::Card.new("S2"), trick.winner("S")
37
37
  end
38
38
 
39
- test "is incomplete with 3 cards" do
39
+ it "is incomplete with 3 cards" do
40
40
  trick = Bridge::Trick.new("S2", "HA", "CA")
41
41
  assert trick.incomplete?
42
42
  end
43
43
 
44
- test "is incomplete with 4 cards" do
44
+ it "is incomplete with 4 cards" do
45
45
  trick = Bridge::Trick.new("S2", "HA", "CA", "DA")
46
46
  assert trick.complete?
47
47
  end