bridge 0.0.22 → 0.0.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -27,7 +27,7 @@ Methods:
27
27
 
28
28
  You can also ask for all possible contracts finished with given points:
29
29
  Bridge::Score.with_points(980)
30
- #=> ["1NTX-11-vulnerable", "2C/DX-12-vulnerable", "6H/S-12"]
30
+ #=> ["1NTX+4v", "2C/DX+4v", "6H/S="]
31
31
 
32
32
  You can use regexp to check if contract with result is valid:
33
33
  Bridge::Score::REGEXP
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.22
1
+ 0.0.23
data/bridge.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{bridge}
8
- s.version = "0.0.22"
8
+ s.version = "0.0.23"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jakub Kuźma"]
12
- s.date = %q{2010-03-27}
12
+ s.date = %q{2010-03-28}
13
13
  s.description = %q{Useful contract bridge utilities - deal generator, id to deal and deal to id conversion}
14
14
  s.email = %q{qoobaa+github@gmail.com}
15
15
  s.extra_rdoc_files = [
data/lib/bridge/deal.rb CHANGED
@@ -133,6 +133,22 @@ module Bridge
133
133
  end
134
134
  alias :hcp :honour_card_points
135
135
 
136
+ def sort_by_color!(trump = nil)
137
+ sort_by_color(trump).each do |direction, hand|
138
+ self[direction] = hand
139
+ end
140
+ self
141
+ end
142
+
143
+ def sort_by_color(trump = nil)
144
+ DIRECTIONS.inject({}) do |sorted, direction|
145
+ splitted_colors = split_colors(direction)
146
+ sorted_colors = sort_colors(splitted_colors.keys, trump)
147
+ sorted[direction] = sorted_colors.inject([]) { |cards, color| cards << splitted_colors.delete(color) }.flatten
148
+ sorted
149
+ end
150
+ end
151
+
136
152
  private
137
153
 
138
154
  def must_be_direction!(string)
@@ -143,5 +159,28 @@ module Bridge
143
159
  must_be_direction!(direction)
144
160
  instance_variable_set("@#{direction.to_s.downcase}", cards)
145
161
  end
162
+
163
+ def split_colors(direction)
164
+ TRUMPS.inject({}) do |colors, trump|
165
+ cards = self[direction].select { |card| card.suit == trump }
166
+ colors[trump] = cards unless cards.empty?
167
+ colors
168
+ end
169
+ end
170
+
171
+ def sort_colors(colors, trump = nil)
172
+ black = ["S", "C"] & colors
173
+ red = ["H", "D"] & colors
174
+ sorted = if black.delete(trump)
175
+ [trump] << red.shift << black.shift << red.shift
176
+ elsif red.delete(trump)
177
+ [trump] << black.shift << red.shift << black.shift
178
+ elsif black.size >= red.size
179
+ [black.shift] << red.shift << black.shift << red.shift
180
+ else
181
+ [red.shift] << black.shift << red.shift << black.shift
182
+ end
183
+ sorted.compact
184
+ end
146
185
  end
147
186
  end
data/test/test_deal.rb CHANGED
@@ -131,3 +131,94 @@ class TestDeal < Test::Unit::TestCase
131
131
  assert_equal String, deal.to_hash["N"].first.class
132
132
  end
133
133
  end
134
+
135
+ class TestDealSort < Test::Unit::TestCase
136
+ def setup
137
+ @deal = Bridge::Deal.new({ "N" => ["SK", "S9", "S7", "S6", "S4", "HA", "HJ", "H3", "H2", "D7", "D2", "C6", "C3"],
138
+ "E" => ["SA", "SQ", "SJ", "ST", "S2", "DA", "DK", "DT", "D9", "D8", "D5", "CK", "CJ"],
139
+ "S" => ["S8", "S5", "S3", "HK", "HQ", "HT", "H8", "H4", "DQ", "D3", "CQ", "C4", "C2"],
140
+ "W" => ["H9", "H7", "H6", "H5", "DJ", "D6", "D4", "CA", "CT", "C9", "C8", "C7", "C5"] })
141
+ end
142
+
143
+ test "split cards by color for given direction" do
144
+ expected = { "S" => @deal.n.select { |c| c.suit == "S" },
145
+ "H" => @deal.n.select { |c| c.suit == "H" },
146
+ "D" => @deal.n.select { |c| c.suit == "D" },
147
+ "C" => @deal.n.select { |c| c.suit == "C" } }
148
+ assert_equal expected, @deal.send(:split_colors, "N")
149
+ end
150
+
151
+ test "return colors as key only if cards are present on hand" do
152
+ expected = { "S" => @deal.e.select { |c| c.suit == "S" },
153
+ "D" => @deal.e.select { |c| c.suit == "D" },
154
+ "C" => @deal.e.select { |c| c.suit == "C" } }
155
+ assert_equal expected, @deal.send(:split_colors, "E")
156
+ end
157
+
158
+ test "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
+ test "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
+ test "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.sort_colors(["H", "C"])
176
+ end
177
+
178
+ test "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
+ test "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
+ test "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
+ test "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
+ test "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
+ test "not modify hands if sorted by color without !" do
220
+ old_deal = @deal.to_hash
221
+ @deal.sort_by_color
222
+ assert_equal old_deal, @deal.to_hash
223
+ end
224
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 22
9
- version: 0.0.22
8
+ - 23
9
+ version: 0.0.23
10
10
  platform: ruby
11
11
  authors:
12
12
  - "Jakub Ku\xC5\xBAma"
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-27 00:00:00 +01:00
17
+ date: 2010-03-28 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency