bridge 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,118 @@
1
+ require "helper"
2
+
3
+ describe Bridge do
4
+ it "negative number is not valid deal id" do
5
+ refute Bridge.deal_id?(-1)
6
+ end
7
+
8
+ it "number of possible bridge deals is not valid deal id" do
9
+ refute Bridge.deal_id?(Bridge::DEALS)
10
+ end
11
+
12
+ it "return partner of direction" do
13
+ assert_equal "N", Bridge.partner_of("S")
14
+ assert_equal "S", Bridge.partner_of("N")
15
+ assert_equal "E", Bridge.partner_of("W")
16
+ assert_equal "W", Bridge.partner_of("E")
17
+ end
18
+
19
+ it "return side of direction" do
20
+ assert_equal "NS", Bridge.side_of("S")
21
+ assert_equal "NS", Bridge.side_of("N")
22
+ assert_equal "EW", Bridge.side_of("W")
23
+ assert_equal "EW", Bridge.side_of("E")
24
+ end
25
+
26
+ it "return next direction" do
27
+ assert_equal "E", Bridge.next_direction("N")
28
+ assert_equal "S", Bridge.next_direction("E")
29
+ assert_equal "W", Bridge.next_direction("S")
30
+ assert_equal "N", Bridge.next_direction("W")
31
+ assert_equal "N", Bridge.next_direction(nil)
32
+ end
33
+
34
+ it "return vulnerable for given deal nr" do
35
+ assert_equal "NONE", Bridge.vulnerable_in_deal(nil)
36
+
37
+ assert_equal "NONE", Bridge.vulnerable_in_deal(1)
38
+ assert_equal "NS", Bridge.vulnerable_in_deal(2)
39
+ assert_equal "EW", Bridge.vulnerable_in_deal(3)
40
+ assert_equal "BOTH", Bridge.vulnerable_in_deal(4)
41
+
42
+ assert_equal "NS", Bridge.vulnerable_in_deal(5)
43
+ assert_equal "EW", Bridge.vulnerable_in_deal(6)
44
+ assert_equal "BOTH", Bridge.vulnerable_in_deal(7)
45
+ assert_equal "NONE", Bridge.vulnerable_in_deal(8)
46
+
47
+ assert_equal "EW", Bridge.vulnerable_in_deal(9)
48
+ assert_equal "BOTH", Bridge.vulnerable_in_deal(10)
49
+ assert_equal "NONE", Bridge.vulnerable_in_deal(11)
50
+ assert_equal "NS", Bridge.vulnerable_in_deal(12)
51
+
52
+ assert_equal "BOTH", Bridge.vulnerable_in_deal(13)
53
+ assert_equal "NONE", Bridge.vulnerable_in_deal(14)
54
+ assert_equal "NS", Bridge.vulnerable_in_deal(15)
55
+ assert_equal "EW", Bridge.vulnerable_in_deal(16)
56
+
57
+ assert_equal "NONE", Bridge.vulnerable_in_deal(17)
58
+ assert_equal "NS", Bridge.vulnerable_in_deal(18)
59
+ assert_equal "EW", Bridge.vulnerable_in_deal(19)
60
+ assert_equal "BOTH", Bridge.vulnerable_in_deal(20)
61
+ end
62
+
63
+ describe "bid regexp" do
64
+ it "matches 1S" do
65
+ assert_equal "1S", "1SS".match(Bridge::BID_REGEXP)[:bid]
66
+ end
67
+
68
+ it "matches 7NT" do
69
+ assert_equal "7NT", "7NTE".match(Bridge::BID_REGEXP)[:bid]
70
+ end
71
+
72
+ it "does not match 8NT" do
73
+ assert_nil "8NTE".match(Bridge::BID_REGEXP)
74
+ end
75
+ end
76
+
77
+ describe "modifier regexp" do
78
+ it "matches X" do
79
+ assert_equal "X", "1SXS".match(Bridge::MODIFIER_REGEXP)[:modifier]
80
+ end
81
+
82
+ it "matches XX" do
83
+ assert_equal "XX", "1NTXXS".match(Bridge::MODIFIER_REGEXP)[:modifier]
84
+ end
85
+ end
86
+
87
+ describe "result regexp" do
88
+ it "matches =" do
89
+ assert_equal "=", "1SNXE=".match(Bridge::RESULT_REGEXP)[:result]
90
+ end
91
+
92
+ it "matches -12" do
93
+ assert_equal "-12", "12NTNXE-12".match(Bridge::RESULT_REGEXP)[:result]
94
+ end
95
+ end
96
+
97
+ describe "contract regexp" do
98
+ it "matches 1SN" do
99
+ assert_equal "1SXN", "1SXN-2".match(Bridge::CONTRACT_REGEXP)[:contract]
100
+ end
101
+
102
+ it "matches 7NTXXS" do
103
+ assert_equal "7NTXXS", "7NTXXS=".match(Bridge::CONTRACT_REGEXP)[:contract]
104
+ end
105
+ end
106
+
107
+ describe "score regexp" do
108
+ it "matches 7NTXXS-2" do
109
+ matched = "7NTXXS-2".match(Bridge::SCORE_REGEXP)
110
+ assert_equal "7NTXXS-2", matched[:score]
111
+ assert_equal "7NTXXS", matched[:contract]
112
+ assert_equal "7NT", matched[:bid]
113
+ assert_equal "XX", matched[:modifier]
114
+ assert_equal "S", matched[:direction]
115
+ assert_equal "-2", matched[:result]
116
+ end
117
+ end
118
+ end
@@ -1,57 +1,57 @@
1
1
  require "helper"
2
2
 
3
- class TestCard < Test::Unit::TestCase
4
- test "H2 is a valid card" do
3
+ describe Bridge::Card do
4
+ it "H2 is a valid card" do
5
5
  card = Bridge::Card.new("H2")
6
6
  assert "H", card.suit
7
7
  assert "2", card.value
8
8
  end
9
9
 
10
- test "ST is a valid card" do
10
+ it "ST is a valid card" do
11
11
  card = Bridge::Card.new("ST")
12
12
  assert "S", card.suit
13
13
  assert "T", card.value
14
14
  end
15
15
 
16
- test "CQ is a valid card" do
16
+ it "CQ is a valid card" do
17
17
  card = Bridge::Card.new("CQ")
18
18
  assert "C", card.suit
19
19
  assert "Q", card.value
20
20
  end
21
21
 
22
- test "NT1 is not a valid card" do
22
+ it "NT1 is not a valid card" do
23
23
  assert_raises(ArgumentError) do
24
24
  Bridge::Card.new("NT1")
25
25
  end
26
26
  end
27
27
 
28
- test "1H is not a valid card" do
28
+ it "1H is not a valid card" do
29
29
  assert_raises(ArgumentError) do
30
30
  Bridge::Card.new("1H")
31
31
  end
32
32
  end
33
33
 
34
- test "ST is lower than SJ" do
34
+ it "ST is lower than SJ" do
35
35
  assert Bridge::Card.new("ST") < Bridge::Card.new("SJ")
36
36
  end
37
37
 
38
- test "H2 has no hcp" do
38
+ it "H2 has no hcp" do
39
39
  assert_equal 0, Bridge::Card.new("H2").hcp
40
40
  end
41
41
 
42
- test "SJ has 1 hcp" do
42
+ it "SJ has 1 hcp" do
43
43
  assert_equal 1, Bridge::Card.new("SJ").hcp
44
44
  end
45
45
 
46
- test "CQ has 2 hcp" do
46
+ it "CQ has 2 hcp" do
47
47
  assert_equal 2, Bridge::Card.new("CQ").hcp
48
48
  end
49
49
 
50
- test "CK has 3 hcp" do
50
+ it "CK has 3 hcp" do
51
51
  assert_equal 3, Bridge::Card.new("CK").hcp
52
52
  end
53
53
 
54
- test "DA has 4 hcp" do
54
+ it "DA has 4 hcp" do
55
55
  assert_equal 4, Bridge::Card.new("DA").hcp
56
56
  end
57
57
  end
@@ -1,48 +1,48 @@
1
1
  require "helper"
2
2
 
3
- class TestChicago < Test::Unit::TestCase
4
- test "raises ArgumentError when invalid honour card points provided" do
3
+ describe Bridge::Points::Chicago do
4
+ it "raises ArgumentError when invalid honour card points provided" do
5
5
  assert_raises(ArgumentError) do
6
6
  Bridge::Points::Chicago.new(:hcp => 15, :points => 100)
7
7
  end
8
8
  end
9
9
 
10
- test "set default vulnerable to false" do
10
+ it "set default vulnerable to false" do
11
11
  imp = Bridge::Points::Chicago.new(:hcp => 40, :points => -100)
12
- assert_false imp.vulnerable
12
+ refute imp.vulnerable
13
13
  end
14
14
 
15
- test "return vulnerable boolean" do
15
+ it "return vulnerable boolean" do
16
16
  imp = Bridge::Points::Chicago.new(:hcp => 20, :points => 100, :vulnerable => true)
17
17
  assert imp.vulnerable?
18
18
  end
19
19
 
20
- test "return points to make when vulnerable" do
20
+ it "return points to make when vulnerable" do
21
21
  imp = Bridge::Points::Chicago.new(:hcp => 23, :points => 100, :vulnerable => true)
22
22
  assert_equal 110, imp.points_to_make
23
23
  end
24
24
 
25
- test "return nil when points are not in range" do
25
+ it "return nil when points are not in range" do
26
26
  imp = Bridge::Points::Chicago.new(:hcp => 20, :points => 45)
27
27
  assert_equal nil, imp.imps
28
28
  end
29
29
 
30
- test "return high value of imp range" do
30
+ it "return high value of imp range" do
31
31
  imp = Bridge::Points::Chicago.new(:hcp => 22, :points => 110)
32
32
  assert_equal 1, imp.imps
33
33
  end
34
34
 
35
- test "return points to make when not vulnerable" do
35
+ it "return points to make when not vulnerable" do
36
36
  imp = Bridge::Points::Chicago.new(:hcp => 23, :points => 100, :vulnerable => false)
37
37
  assert_equal 110, imp.points_to_make
38
38
  end
39
39
 
40
- test "return positive imps" do
40
+ it "return positive imps" do
41
41
  imp = Bridge::Points::Chicago.new(:hcp => 21, :points => 100)
42
42
  assert_equal 2, imp.imps
43
43
  end
44
44
 
45
- test "return negative imps" do
45
+ it "return negative imps" do
46
46
  imp = Bridge::Points::Chicago.new(:hcp => 21, :points => -100)
47
47
  assert_equal -4, imp.imps
48
48
  end
@@ -0,0 +1,225 @@
1
+ require "helper"
2
+
3
+ describe Bridge::Deal do
4
+ it "first deal conversion" do
5
+ id = 0
6
+ deal = Bridge::Deal.from_id(id)
7
+ assert deal.valid?
8
+ assert_equal %w(SA SK SQ SJ ST S9 S8 S7 S6 S5 S4 S3 S2).map { |c| Bridge::Card.new(c) }, deal.n
9
+ assert_equal %w(HA HK HQ HJ HT H9 H8 H7 H6 H5 H4 H3 H2).map { |c| Bridge::Card.new(c) }, deal.e
10
+ assert_equal %w(DA DK DQ DJ DT D9 D8 D7 D6 D5 D4 D3 D2).map { |c| Bridge::Card.new(c) }, deal.s
11
+ assert_equal %w(CA CK CQ CJ CT C9 C8 C7 C6 C5 C4 C3 C2).map { |c| Bridge::Card.new(c) }, deal.w
12
+ assert_equal id, deal.id
13
+ end
14
+
15
+ it "first deal hcp" do
16
+ deal = Bridge::Deal.from_id(0)
17
+ assert_equal 10, deal.hcp("N")
18
+ assert_equal 10, deal.hcp("E")
19
+ assert_equal 10, deal.hcp("S")
20
+ assert_equal 10, deal.hcp("W")
21
+ assert_equal 20, deal.hcp("NS")
22
+ assert_equal 20, deal.hcp("EW")
23
+ end
24
+
25
+ it "last deal conversion" do
26
+ id = Bridge::DEALS - 1
27
+ deal = Bridge::Deal.from_id(id)
28
+ assert deal.valid?
29
+ assert_equal %w(CA CK CQ CJ CT C9 C8 C7 C6 C5 C4 C3 C2).map { |c| Bridge::Card.new(c) }, deal.n
30
+ assert_equal %w(DA DK DQ DJ DT D9 D8 D7 D6 D5 D4 D3 D2).map { |c| Bridge::Card.new(c) }, deal.e
31
+ assert_equal %w(HA HK HQ HJ HT H9 H8 H7 H6 H5 H4 H3 H2).map { |c| Bridge::Card.new(c) }, deal.s
32
+ assert_equal %w(SA SK SQ SJ ST S9 S8 S7 S6 S5 S4 S3 S2).map { |c| Bridge::Card.new(c) }, deal.w
33
+ assert_equal id, deal.id
34
+ end
35
+
36
+ it "last deal hcp" do
37
+ deal = Bridge::Deal.from_id(Bridge::DEALS - 1)
38
+ assert_equal 10, deal.hcp("N")
39
+ assert_equal 10, deal.hcp("E")
40
+ assert_equal 10, deal.hcp("S")
41
+ assert_equal 10, deal.hcp("W")
42
+ assert_equal 20, deal.hcp("NS")
43
+ assert_equal 20, deal.hcp("EW")
44
+ end
45
+
46
+ it "deal no 1 000 000 000 000" do
47
+ id = 1_000_000_000_000
48
+ deal = Bridge::Deal.from_id(id)
49
+ assert deal.valid?
50
+ assert_equal id, deal.id
51
+ end
52
+
53
+ it "sample deal to id conversion" do
54
+ deal = Bridge::Deal.new("N" => %w(SA SK SQ SJ HA HK HQ DA DK DQ CA CK CQ),
55
+ "E" => %w(ST S9 S8 S7 S6 S5 S4 S3 S2 HJ HT H9 H8),
56
+ "S" => %w(H7 H6 H5 H4 H3 H2 DJ DT D9 D8 D7 D6 D5),
57
+ "W" => %w(D4 D3 D2 CJ CT C9 C8 C7 C6 C5 C4 C3 C2))
58
+ assert deal.valid?
59
+ id = deal.id
60
+ assert_equal deal, Bridge::Deal.from_id(id)
61
+ end
62
+
63
+ it "deal with doubled cards is not valid deal" do
64
+ deal = Bridge::Deal.new("N" => %w(SA SA SQ SJ HA HK HQ DA DK DQ CA CK CQ),
65
+ "E" => %w(ST S9 S8 S7 S6 S5 S4 S3 S2 HJ HT H9 H8),
66
+ "S" => %w(H7 H6 H5 H4 H3 H2 DJ DT D9 D8 D7 D6 D5),
67
+ "W" => %w(D4 D3 D2 CJ CT C9 C8 C7 C6 C5 C4 C3 C2))
68
+ refute deal.valid?
69
+ end
70
+
71
+ it "deal with different length hands is not valid deal" do
72
+ deal = Bridge::Deal.new("N" => %w(SA SK SQ SJ HA HK HQ DA DK DQ CA CK CQ ST),
73
+ "E" => %w(S9 S8 S7 S6 S5 S4 S3 S2 HJ HT H9 H8),
74
+ "S" => %w(H7 H6 H5 H4 H3 H2 DJ DT D9 D8 D7 D6 D5),
75
+ "W" => %w(D4 D3 D2 CJ CT C9 C8 C7 C6 C5 C4 C3 C2))
76
+ refute deal.valid?
77
+ end
78
+
79
+ it "random deal id is valid deal id" do
80
+ id = Bridge::Deal.random_id
81
+ assert Bridge.deal_id?(id)
82
+ end
83
+
84
+ it "random deal is valid deal" do
85
+ deal = Bridge::Deal.random
86
+ assert deal.valid?
87
+ end
88
+
89
+ it "comparison of two deals" do
90
+ deal1 = Bridge::Deal.from_id(1_000_000_000_000_000)
91
+ deal2 = Bridge::Deal.from_id(9_000_000_000_000_000)
92
+ assert deal1 < deal2
93
+ end
94
+
95
+ it "fetching wrong direction raises an error" do
96
+ deal = Bridge::Deal.random
97
+ assert_raises(ArgumentError) do
98
+ deal[:h]
99
+ end
100
+ end
101
+
102
+ it "passing incorrect id to from_id raises an error" do
103
+ assert_raises(ArgumentError) do
104
+ Bridge::Deal.from_id(-1)
105
+ end
106
+ end
107
+
108
+ it "owner returns correct direction" do
109
+ deal = Bridge::Deal.new({ "N" => ["SK", "S9", "S7", "S6", "S4", "S2", "HA", "HJ", "H2", "D7", "D2", "C6", "C3"],
110
+ "E" => ["SA", "SJ", "ST", "H3", "DA", "DK", "DT", "D9", "D8", "D5", "D4", "CK", "CJ"],
111
+ "S" => ["S8", "S5", "S3", "HK", "HQ", "HT", "H8", "H4", "DQ", "D3", "CQ", "C4", "C2"],
112
+ "W" => ["SQ", "H9", "H7", "H6", "H5", "DJ", "D6", "CA", "CT", "C9", "C8", "C7", "C5"] })
113
+
114
+ assert_equal "N", deal.owner("SK")
115
+ assert_equal "N", deal.owner("C3")
116
+ assert_equal "E", deal.owner("SJ")
117
+ assert_equal "E", deal.owner("CK")
118
+ assert_equal "S", deal.owner("S3")
119
+ assert_equal "S", deal.owner("CQ")
120
+ assert_equal "W", deal.owner("H6")
121
+ assert_equal "W", deal.owner("C9")
122
+ end
123
+
124
+ it "to_hash returns hash" do
125
+ deal = Bridge::Deal.from_id(0)
126
+ assert_equal Hash, deal.to_hash.class
127
+ end
128
+
129
+ it "to_hash returns hash with arrays of strings" do
130
+ deal = Bridge::Deal.from_id(0)
131
+ assert_equal String, deal.to_hash["N"].first.class
132
+ end
133
+
134
+ describe "sort" do
135
+ before do
136
+ @deal = Bridge::Deal.new({ "N" => ["SK", "S9", "S7", "S6", "S4", "HA", "HJ", "H3", "H2", "D7", "D2", "C6", "C3"],
137
+ "E" => ["SA", "SQ", "SJ", "ST", "S2", "DA", "DK", "DT", "D9", "D8", "D5", "CK", "CJ"],
138
+ "S" => ["S8", "S5", "S3", "HK", "HQ", "HT", "H8", "H4", "DQ", "D3", "CQ", "C4", "C2"],
139
+ "W" => ["H9", "H7", "H6", "H5", "DJ", "D6", "D4", "CA", "CT", "C9", "C8", "C7", "C5"] })
140
+ end
141
+
142
+ it "split cards by color for given direction" do
143
+ expected = { "S" => @deal.n.select { |c| c.suit == "S" },
144
+ "H" => @deal.n.select { |c| c.suit == "H" },
145
+ "D" => @deal.n.select { |c| c.suit == "D" },
146
+ "C" => @deal.n.select { |c| c.suit == "C" } }
147
+ assert_equal expected, @deal.cards_for("N")
148
+ end
149
+
150
+ it "return empty array if no cards" do
151
+ expected = { "S" => @deal.e.select { |c| c.suit == "S" },
152
+ "H" => [],
153
+ "D" => @deal.e.select { |c| c.suit == "D" },
154
+ "C" => @deal.e.select { |c| c.suit == "C" } }
155
+ assert_equal expected, @deal.cards_for("E")
156
+ end
157
+
158
+ it "return sorted 4 colors" do
159
+ assert_equal ["S", "H", "C", "D"], @deal.send(:sort_colors, ["S", "H", "D", "C"])
160
+ assert_equal ["S", "H", "C", "D"], @deal.send(:sort_colors, ["H", "D", "S", "C"])
161
+ assert_equal ["S", "H", "C", "D"], @deal.send(:sort_colors, ["D", "S", "C", "H"])
162
+ end
163
+
164
+ it "return sorted 3 colors" do
165
+ assert_equal ["S", "H", "C"], @deal.send(:sort_colors, ["S", "H", "C"])
166
+ assert_equal ["H", "C", "D"], @deal.send(:sort_colors, ["H", "D", "C"])
167
+ assert_equal ["H", "S", "D"], @deal.send(:sort_colors, ["S", "H", "D"])
168
+ assert_equal ["S", "D", "C"], @deal.send(:sort_colors, ["S", "D", "C"])
169
+ end
170
+
171
+ it "return sorted 2 colors" do
172
+ assert_equal ["S", "H"], @deal.send(:sort_colors, ["S", "H"])
173
+ assert_equal ["S", "D"], @deal.send(:sort_colors, ["S", "D"])
174
+ assert_equal ["C", "D"], @deal.send(:sort_colors, ["D", "C"])
175
+ # assert_equal ["H", "C"], @deal.send(:sort_colors, ["H", "C"])
176
+ end
177
+
178
+ it "return sorted 4 colors with trump" do
179
+ assert_equal ["S", "H", "C", "D"], @deal.send(:sort_colors, ["S", "H", "D", "C"], "S")
180
+ assert_equal ["H", "S", "D", "C"], @deal.send(:sort_colors, ["H", "D", "S", "C"], "H")
181
+ assert_equal ["D", "S", "H", "C"], @deal.send(:sort_colors, ["D", "S", "C", "H"], "D")
182
+ assert_equal ["C", "H", "S", "D"], @deal.send(:sort_colors, ["D", "S", "C", "H"], "C")
183
+ end
184
+
185
+ it "return sorted 3 colors with trump" do
186
+ assert_equal ["S", "H", "C"], @deal.send(:sort_colors, ["S", "H", "C"], "S")
187
+ assert_equal ["H", "S", "C"], @deal.send(:sort_colors, ["S", "H", "C"], "H")
188
+ assert_equal ["S", "H", "D"], @deal.send(:sort_colors, ["S", "H", "D"], "S")
189
+ assert_equal ["D", "S", "H"], @deal.send(:sort_colors, ["S", "H", "D"], "D")
190
+ end
191
+
192
+ it "return sorted 2 colors with trump" do
193
+ assert_equal ["S", "H"], @deal.send(:sort_colors, ["S", "H"], "S")
194
+ assert_equal ["H", "S"], @deal.send(:sort_colors, ["S", "H"], "H")
195
+ assert_equal ["C", "S"], @deal.send(:sort_colors, ["S", "C"], "C")
196
+ assert_equal ["D", "H"], @deal.send(:sort_colors, ["H", "D"], "D")
197
+ end
198
+
199
+ it "sort hands by colors" do
200
+ expected = {
201
+ "N" => ["SK", "S9", "S7", "S6", "S4", "HA", "HJ", "H3", "H2", "C6", "C3", "D7", "D2"],
202
+ "E" => ["SA", "SQ", "SJ", "ST", "S2", "DA", "DK", "DT", "D9", "D8", "D5", "CK", "CJ"],
203
+ "S" => ["S8", "S5", "S3", "HK", "HQ", "HT", "H8", "H4", "CQ", "C4", "C2", "DQ", "D3"],
204
+ "W" => ["H9", "H7", "H6", "H5", "CA", "CT", "C9", "C8", "C7", "C5", "DJ", "D6", "D4"]
205
+ }
206
+ assert_equal expected, @deal.sort_by_color!.to_hash
207
+ end
208
+
209
+ it "sort hands by colors with trump" do
210
+ expected = {
211
+ "N" => ["C6", "C3", "HA", "HJ", "H3", "H2", "SK", "S9", "S7", "S6", "S4", "D7", "D2"],
212
+ "E" => ["CK", "CJ", "DA", "DK", "DT", "D9", "D8", "D5", "SA", "SQ", "SJ", "ST", "S2"],
213
+ "S" => ["CQ", "C4", "C2", "HK", "HQ", "HT", "H8", "H4", "S8", "S5", "S3", "DQ", "D3"],
214
+ "W" => ["CA", "CT", "C9", "C8", "C7", "C5", "H9", "H7", "H6", "H5", "DJ", "D6", "D4"]
215
+ }
216
+ assert_equal expected, @deal.sort_by_color!("C").to_hash
217
+ end
218
+
219
+ it "not modify hands if sorted by color without bang" do
220
+ old_deal = @deal.to_hash
221
+ @deal.sort_by_color
222
+ assert_equal old_deal, @deal.to_hash
223
+ end
224
+ end
225
+ end