bridge 0.0.16 → 0.0.17

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -2,6 +2,25 @@
2
2
 
3
3
  Contract bridge utils.
4
4
 
5
+ == Score
6
+
7
+ You can create Score object passing 3 arguments:
8
+ Bridge::Score.new(:contract => "6NTX", :vulnerable => true, :tricks => "=")
9
+
10
+ Arguments:
11
+ * :contract -- String, where first sign is level, second suit (C D H S NT) and optional double or redouble (X or XX)
12
+ * :vulnerable -- Boolean, declarer is vulnerable? (default is false)
13
+ * :tricks -- Integer or String, when Integer is passed it's number of tricks taken by declarer side, String can be relative to contract level i.e. "+1", "-2", "="
14
+
15
+ Methods:
16
+ * Score#made? -- Boolean
17
+ * Score#result -- Integer, relative to contract level i.e. -1 (one down), 1 (overtrick), 0 (contract made)
18
+ * Score#points -- Integer, calculated full value
19
+
20
+ You can also ask for all possible contracts finished with given points:
21
+ Bridge::Score.with_points(980)
22
+ #=> ["1NTX-11-vulnerable", "2C/DX-12-vulnerable", "6H/S-12"]
23
+
5
24
  == Note on Patches/Pull Requests
6
25
 
7
26
  * Fork the project.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.16
1
+ 0.0.17
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.16"
8
+ s.version = "0.0.17"
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-03}
12
+ s.date = %q{2010-03-05}
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/score.rb CHANGED
@@ -27,7 +27,13 @@ module Bridge
27
27
  made? ? (made_contract_points + overtrick_points + bonus) : undertrick_points
28
28
  end
29
29
 
30
- #private
30
+ # Returns all possible contracts with given points
31
+ def self.with_points(points)
32
+ contracts = all_contracts.select { |contract, result| result == points }
33
+ contracts.respond_to?(:keys) ? contracts.keys.sort : contracts.map { |c| c.first }.sort # Ruby 1.8.* compatibility
34
+ end
35
+
36
+ # private
31
37
 
32
38
  def vulnerable?
33
39
  @vulnerable == true
@@ -159,5 +165,20 @@ module Bridge
159
165
  modifier = $1.nil? ? 1 : $1.to_s.size * 2
160
166
  [Bridge::Bid.new(contract), modifier]
161
167
  end
168
+
169
+ def self.all_contracts
170
+ result = {}
171
+ contracts = %w(1 2 3 4 5 6 7).inject([]) do |b, l|
172
+ b += ["H/S", "C/D", "NT"].map { |s| l + s }
173
+ end
174
+ (contracts + contracts.map { |c| c + "X" } + contracts.map { |c| c + "XX" }).each do |contract|
175
+ [true, false].each do |vulnerable|
176
+ (0..13).each do |tricks|
177
+ result["#{contract}-#{tricks}#{vulnerable == true ? "-vulnerable" : ""}"] = new(:contract => contract.sub("H/S", "S").sub("C/D", "C"), :tricks => tricks, :vulnerable => vulnerable).points
178
+ end
179
+ end
180
+ end
181
+ result
182
+ end
162
183
  end
163
184
  end
data/test/test_score.rb CHANGED
@@ -239,4 +239,19 @@ class TestScorePoints < Test::Unit::TestCase
239
239
  score = Bridge::Score.new(:contract => "1CX", :vulnerable => false, :tricks => 13)
240
240
  assert_equal 740, score.points
241
241
  end
242
+ end
243
+
244
+ class TestScoreContracts < Test::Unit::TestCase
245
+ test "1764 results" do
246
+ assert_equal 1764, Bridge::Score.all_contracts.size
247
+ end
248
+
249
+ test "return contracts for 1430 points" do
250
+ expected = ["1C/DXX-10-vulnerable", "1C/DXX-13", "6H/S-12-vulnerable"]
251
+ assert_equal expected, Bridge::Score.with_points(1430)
252
+ end
253
+
254
+ test "return [] if not found" do
255
+ assert_equal [], Bridge::Score.with_points(100)
256
+ end
242
257
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 16
9
- version: 0.0.16
8
+ - 17
9
+ version: 0.0.17
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-03 00:00:00 +01:00
17
+ date: 2010-03-05 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency