bridge 0.0.6 → 0.0.7

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.6
1
+ 0.0.7
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.6"
8
+ s.version = "0.0.7"
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-02-23}
12
+ s.date = %q{2010-02-24}
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 = [
@@ -25,12 +25,13 @@ Gem::Specification.new do |s|
25
25
  "VERSION",
26
26
  "bridge.gemspec",
27
27
  "lib/bridge.rb",
28
+ "lib/bridge/bid.rb",
28
29
  "lib/bridge/card.rb",
29
- "lib/bridge/contract.rb",
30
30
  "lib/bridge/deal.rb",
31
31
  "test/helper.rb",
32
+ "test/test_bid.rb",
32
33
  "test/test_bridge.rb",
33
- "test/test_contract.rb",
34
+ "test/test_card.rb",
34
35
  "test/test_deal.rb"
35
36
  ]
36
37
  s.homepage = %q{http://github.com/qoobaa/bridge}
@@ -39,10 +40,11 @@ Gem::Specification.new do |s|
39
40
  s.rubygems_version = %q{1.3.6}
40
41
  s.summary = %q{Contract bridge utilities}
41
42
  s.test_files = [
42
- "test/test_bridge.rb",
43
+ "test/test_card.rb",
43
44
  "test/test_deal.rb",
45
+ "test/test_bid.rb",
44
46
  "test/helper.rb",
45
- "test/test_contract.rb"
47
+ "test/test_bridge.rb"
46
48
  ]
47
49
 
48
50
  if s.respond_to? :specification_version then
data/lib/bridge.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "bridge/card"
2
2
  require "bridge/deal"
3
+ require "bridge/bid"
3
4
 
4
5
  module Bridge
5
6
  # Number of possible deals in bridge
@@ -47,6 +48,44 @@ module Bridge
47
48
  end
48
49
 
49
50
  def self.compare_cards(first, second)
51
+ # DECK has reversed order
50
52
  DECK.index(second) <=> DECK.index(first)
51
53
  end
54
+
55
+ def self.compare_contracts(first, second)
56
+ CONTRACTS.index(first) <=> CONTRACTS.index(second)
57
+ end
58
+
59
+ def self.pass?(string)
60
+ PASS == string
61
+ end
62
+
63
+ def self.double?(string)
64
+ DOUBLE == string
65
+ end
66
+
67
+ def self.redouble?(string)
68
+ REDOUBLE == string
69
+ end
70
+
71
+ def self.modifier?(string)
72
+ MODIFIERS.include?(string)
73
+ end
74
+
75
+ def self.contract?(string)
76
+ CONTRACTS.include?(string)
77
+ end
78
+
79
+ def self.bid?(string)
80
+ BIDS.include?(string)
81
+ end
82
+ end
83
+
84
+ # Constructor shortcuts
85
+ def Bid(string)
86
+ Bridge::Bid.new(string)
87
+ end
88
+
89
+ def Card(string)
90
+ Bridge::Card.new(string)
52
91
  end
data/lib/bridge/bid.rb ADDED
@@ -0,0 +1,90 @@
1
+ module Bridge
2
+ class Bid
3
+ include Comparable
4
+
5
+ attr_reader :bid
6
+
7
+ # Creates a new bid
8
+ def initialize(bid)
9
+ @bid = bid.to_s.upcase
10
+ raise ArgumentError, "invalid bid: #{bid}" unless Bridge.bid?(@bid)
11
+ end
12
+
13
+ # Returns the level of the bid
14
+ def level
15
+ bid[0] if contract?
16
+ end
17
+
18
+ # Returns the suit of the bid
19
+ def suit
20
+ bid[1..-1] if contract?
21
+ end
22
+
23
+ def pass?
24
+ Bridge.pass?(bid)
25
+ end
26
+
27
+ def double?
28
+ Bridge.double?(bid)
29
+ end
30
+
31
+ def redouble?
32
+ Bridge.redouble?(bid)
33
+ end
34
+
35
+ def modifier?
36
+ Bridge.modifier?(bid)
37
+ end
38
+
39
+ def contract?
40
+ Bridge.contract?(bid)
41
+ end
42
+
43
+ def <=>(other)
44
+ case other
45
+ when Bid
46
+ if contract?
47
+ raise ArgumentError, "could not compare contract bid with non-contract bid #{other}" unless other.contract?
48
+ Bridge.compare_contracts(self.bid, other.bid)
49
+ elsif pass?
50
+ raise ArgumentError, "could not compare pass bid with non-pass bid #{other}" unless other.pass?
51
+ bid <=> other.bid
52
+ elsif double?
53
+ raise ArgumentError, "could not compare double bid with non-double bid #{other}" unless other.double?
54
+ bid <=> other.bid
55
+ elsif redouble?
56
+ raise ArgumentError, "could not compare redouble bid with non-redouble bid #{other}" unless other.redouble?
57
+ bid <=> other.bid
58
+ end
59
+ when String
60
+ self <=> Bid.new(other)
61
+ else
62
+ begin
63
+ a, b = other.coerce(self)
64
+ a <=> b
65
+ rescue
66
+ end
67
+ end
68
+ end
69
+
70
+ def coerce(other)
71
+ [Bid.new(other.to_s), self]
72
+ end
73
+
74
+ def eql?(other)
75
+ self == other && other.instance_of?(Bid)
76
+ end
77
+
78
+ def hash
79
+ bid.hash
80
+ end
81
+
82
+ def to_s
83
+ bid
84
+ end
85
+
86
+ def inspect
87
+ bid.inspect
88
+ end
89
+ end
90
+ end
data/lib/bridge/card.rb CHANGED
@@ -42,7 +42,7 @@ module Bridge
42
42
  end
43
43
 
44
44
  def hash
45
- @card.hash
45
+ card.hash
46
46
  end
47
47
 
48
48
  def coerce(other)
@@ -54,11 +54,7 @@ module Bridge
54
54
  end
55
55
 
56
56
  def to_s
57
- @card
58
- end
59
-
60
- def to_str
61
- @card
57
+ card
62
58
  end
63
59
  end
64
60
  end
data/test/test_bid.rb ADDED
@@ -0,0 +1,103 @@
1
+ require "helper"
2
+
3
+ class TestBid < Test::Unit::TestCase
4
+ test "pas is not a valid bid" do
5
+ assert_raises(ArgumentError) do
6
+ Bridge::Bid.new("pas")
7
+ end
8
+ end
9
+
10
+ test "case doesn't matter in bid" do
11
+ Bridge::Bid.new("pass")
12
+ Bridge::Bid.new("x")
13
+ Bridge::Bid.new("xx")
14
+ Bridge::Bid.new("1nt")
15
+ end
16
+
17
+ test "pass is a valid bid" do
18
+ bid = Bridge::Bid.new("PASS")
19
+ assert bid.pass?
20
+ assert_false bid.double?
21
+ assert_false bid.redouble?
22
+ assert_false bid.modifier?
23
+ assert_false bid.contract?
24
+ assert_nil bid.level
25
+ assert_nil bid.suit
26
+ end
27
+
28
+ test "double is a valid bid" do
29
+ bid = Bridge::Bid.new("X")
30
+ assert_false bid.pass?
31
+ assert bid.double?
32
+ assert_false bid.redouble?
33
+ assert bid.modifier?
34
+ assert_false bid.contract?
35
+ assert_nil bid.level
36
+ assert_nil bid.suit
37
+ end
38
+
39
+ test "redouble is a valid bid" do
40
+ bid = Bridge::Bid.new("XX")
41
+ assert_false bid.pass?
42
+ assert_false bid.double?
43
+ assert bid.redouble?
44
+ assert bid.modifier?
45
+ assert_false bid.contract?
46
+ assert_nil bid.level
47
+ assert_nil bid.suit
48
+ end
49
+
50
+ test "1H is a valid bid" do
51
+ bid = Bridge::Bid.new("1H")
52
+ assert_false bid.pass?
53
+ assert_false bid.double?
54
+ assert_false bid.redouble?
55
+ assert_false bid.modifier?
56
+ assert bid.contract?
57
+ assert_equal "1", bid.level
58
+ assert_equal "H", bid.suit
59
+ end
60
+
61
+ test "7NT is a valid bid" do
62
+ bid = Bridge::Bid.new("7NT")
63
+ assert_false bid.pass?
64
+ assert_false bid.double?
65
+ assert_false bid.redouble?
66
+ assert_false bid.modifier?
67
+ assert bid.contract?
68
+ assert_equal "7", bid.level
69
+ assert_equal "NT", bid.suit
70
+ end
71
+
72
+ test "7NT is greater than 1C" do
73
+ assert Bridge::Bid.new("7NT") > Bridge::Bid.new("1C")
74
+ assert_false Bridge::Bid.new("7NT") < Bridge::Bid.new("1C")
75
+ assert_false Bridge::Bid.new("7NT") == Bridge::Bid.new("1C")
76
+ end
77
+
78
+ test "1S is greater than 1H" do
79
+ assert Bridge::Bid.new("1S") > Bridge::Bid.new("1H")
80
+ assert_false Bridge::Bid.new("1S") < Bridge::Bid.new("1H")
81
+ assert_false Bridge::Bid.new("1S") == Bridge::Bid.new("1H")
82
+ end
83
+
84
+ test "initialize works using shortcut" do
85
+ Bid("pass")
86
+ end
87
+
88
+ test "comparison of PASS and 1S raises an error" do
89
+ assert_raises(ArgumentError) do
90
+ Bridge::Bid.new("PASS") > Bridge::Bid.new("1S")
91
+ end
92
+ end
93
+
94
+ test "comparison of X and PASS raises an error" do
95
+ assert_raises(ArgumentError) do
96
+ Bridge::Bid.new("X") > Bridge::Bid.new("PASS")
97
+ end
98
+ end
99
+
100
+ test "PASS and XX are not equal" do
101
+ assert_not_equal Bridge::Bid.new("PASS"), Bridge::Bid.new("XX")
102
+ end
103
+ end
data/test/test_card.rb ADDED
@@ -0,0 +1,33 @@
1
+ require "helper"
2
+
3
+ class TestCard < Test::Unit::TestCase
4
+ test "H2 is a valid card" do
5
+ card = Bridge::Card.new("H2")
6
+ assert "H", card.suit
7
+ assert "2", card.value
8
+ end
9
+
10
+ test "ST is a valid card" do
11
+ card = Bridge::Card.new("ST")
12
+ assert "S", card.suit
13
+ assert "T", card.value
14
+ end
15
+
16
+ test "CQ is a valid card" do
17
+ card = Bridge::Card.new("CQ")
18
+ assert "C", card.suit
19
+ assert "Q", card.value
20
+ end
21
+
22
+ test "NT1 is not a valid card" do
23
+ assert_raises(ArgumentError) do
24
+ Bridge::Card.new("NT1")
25
+ end
26
+ end
27
+
28
+ test "1H is not a valid card" do
29
+ assert_raises(ArgumentError) do
30
+ Bridge::Card.new("1H")
31
+ end
32
+ end
33
+ end
data/test/test_deal.rb CHANGED
@@ -5,10 +5,10 @@ class TestDeal < Test::Unit::TestCase
5
5
  id = 0
6
6
  deal = Bridge::Deal.from_id(id)
7
7
  assert deal.valid?
8
- assert_equal %w(SA SK SQ SJ ST S9 S8 S7 S6 S5 S4 S3 S2), deal.n
9
- assert_equal %w(HA HK HQ HJ HT H9 H8 H7 H6 H5 H4 H3 H2), deal.e
10
- assert_equal %w(DA DK DQ DJ DT D9 D8 D7 D6 D5 D4 D3 D2), deal.s
11
- assert_equal %w(CA CK CQ CJ CT C9 C8 C7 C6 C5 C4 C3 C2), deal.w
8
+ assert_equal %w(SA SK SQ SJ ST S9 S8 S7 S6 S5 S4 S3 S2).map { |c| Card(c) }, deal.n
9
+ assert_equal %w(HA HK HQ HJ HT H9 H8 H7 H6 H5 H4 H3 H2).map { |c| Card(c) }, deal.e
10
+ assert_equal %w(DA DK DQ DJ DT D9 D8 D7 D6 D5 D4 D3 D2).map { |c| Card(c) }, deal.s
11
+ assert_equal %w(CA CK CQ CJ CT C9 C8 C7 C6 C5 C4 C3 C2).map { |c| Card(c) }, deal.w
12
12
  assert_equal id, deal.id
13
13
  end
14
14
 
@@ -16,10 +16,10 @@ class TestDeal < Test::Unit::TestCase
16
16
  id = Bridge::DEALS - 1
17
17
  deal = Bridge::Deal.from_id(id)
18
18
  assert deal.valid?
19
- assert_equal %w(CA CK CQ CJ CT C9 C8 C7 C6 C5 C4 C3 C2), deal.n
20
- assert_equal %w(DA DK DQ DJ DT D9 D8 D7 D6 D5 D4 D3 D2), deal.e
21
- assert_equal %w(HA HK HQ HJ HT H9 H8 H7 H6 H5 H4 H3 H2), deal.s
22
- assert_equal %w(SA SK SQ SJ ST S9 S8 S7 S6 S5 S4 S3 S2), deal.w
19
+ assert_equal %w(CA CK CQ CJ CT C9 C8 C7 C6 C5 C4 C3 C2).map { |c| Card(c) }, deal.n
20
+ assert_equal %w(DA DK DQ DJ DT D9 D8 D7 D6 D5 D4 D3 D2).map { |c| Card(c) }, deal.e
21
+ assert_equal %w(HA HK HQ HJ HT H9 H8 H7 H6 H5 H4 H3 H2).map { |c| Card(c) }, deal.s
22
+ assert_equal %w(SA SK SQ SJ ST S9 S8 S7 S6 S5 S4 S3 S2).map { |c| Card(c) }, deal.w
23
23
  assert_equal id, deal.id
24
24
  end
25
25
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 6
9
- version: 0.0.6
8
+ - 7
9
+ version: 0.0.7
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-02-23 00:00:00 +01:00
17
+ date: 2010-02-24 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -47,12 +47,13 @@ files:
47
47
  - VERSION
48
48
  - bridge.gemspec
49
49
  - lib/bridge.rb
50
+ - lib/bridge/bid.rb
50
51
  - lib/bridge/card.rb
51
- - lib/bridge/contract.rb
52
52
  - lib/bridge/deal.rb
53
53
  - test/helper.rb
54
+ - test/test_bid.rb
54
55
  - test/test_bridge.rb
55
- - test/test_contract.rb
56
+ - test/test_card.rb
56
57
  - test/test_deal.rb
57
58
  has_rdoc: true
58
59
  homepage: http://github.com/qoobaa/bridge
@@ -85,7 +86,8 @@ signing_key:
85
86
  specification_version: 3
86
87
  summary: Contract bridge utilities
87
88
  test_files:
88
- - test/test_bridge.rb
89
+ - test/test_card.rb
89
90
  - test/test_deal.rb
91
+ - test/test_bid.rb
90
92
  - test/helper.rb
91
- - test/test_contract.rb
93
+ - test/test_bridge.rb
@@ -1,23 +0,0 @@
1
- module Bridge
2
- module Contract
3
- def self.contracts_compare(first, second)
4
- CONTRACTS.index(first) <=> CONTRACTS.index(second)
5
- end
6
-
7
- def self.contract?(contract)
8
- CONTRACTS.include?(value)
9
- end
10
-
11
- def self.pass?(value)
12
- value == PASS
13
- end
14
-
15
- def self.double?(value)
16
- value == DOUBLE
17
- end
18
-
19
- def self.redouble?(value)
20
- value == REDOUBLE
21
- end
22
- end
23
- end
@@ -1,5 +0,0 @@
1
- require "helper"
2
-
3
- class TestContract < Test::Unit::TestCase
4
-
5
- end