bridge 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.5
1
+ 0.0.6
data/bridge.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{bridge}
8
- s.version = "0.0.5"
8
+ s.version = "0.0.6"
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"]
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
25
25
  "VERSION",
26
26
  "bridge.gemspec",
27
27
  "lib/bridge.rb",
28
+ "lib/bridge/card.rb",
28
29
  "lib/bridge/contract.rb",
29
30
  "lib/bridge/deal.rb",
30
31
  "test/helper.rb",
@@ -35,7 +36,7 @@ Gem::Specification.new do |s|
35
36
  s.homepage = %q{http://github.com/qoobaa/bridge}
36
37
  s.rdoc_options = ["--charset=UTF-8"]
37
38
  s.require_paths = ["lib"]
38
- s.rubygems_version = %q{1.3.5}
39
+ s.rubygems_version = %q{1.3.6}
39
40
  s.summary = %q{Contract bridge utilities}
40
41
  s.test_files = [
41
42
  "test/test_bridge.rb",
data/lib/bridge.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require "bridge/card"
1
2
  require "bridge/deal"
2
3
 
3
4
  module Bridge
@@ -44,4 +45,8 @@ module Bridge
44
45
  def self.card?(string)
45
46
  DECK.include?(string)
46
47
  end
48
+
49
+ def self.compare_cards(first, second)
50
+ DECK.index(second) <=> DECK.index(first)
51
+ end
47
52
  end
@@ -0,0 +1,64 @@
1
+ module Bridge
2
+ class Card
3
+ include Comparable
4
+
5
+ attr_reader :card
6
+
7
+ # Creates a new card
8
+ def initialize(card)
9
+ @card = card.to_s.upcase
10
+ raise ArgumentError, "invalid card: #{card}" unless Bridge.card?(@card)
11
+ end
12
+
13
+ # Returns the suit of the card
14
+ def suit
15
+ card[0]
16
+ end
17
+
18
+ # Returns the suit of the card
19
+ def value
20
+ card[1]
21
+ end
22
+
23
+ # Compares the card with the other card
24
+ def <=>(other)
25
+ case other
26
+ when Card
27
+ raise ArgumentError, "comparing card of suit #{suit} with suit #{other.suit}" unless suit == other.suit
28
+ Bridge.compare_cards(self.card, other.card)
29
+ when String
30
+ self <=> Card.new(other)
31
+ else
32
+ begin
33
+ a, b = other.coerce(self)
34
+ a <=> b
35
+ rescue
36
+ end
37
+ end
38
+ end
39
+
40
+ def eql?(other)
41
+ self == other && other.instance_of?(Card)
42
+ end
43
+
44
+ def hash
45
+ @card.hash
46
+ end
47
+
48
+ def coerce(other)
49
+ [Card.new(other.to_s), self]
50
+ end
51
+
52
+ def inspect
53
+ card.inspect
54
+ end
55
+
56
+ def to_s
57
+ @card
58
+ end
59
+
60
+ def to_str
61
+ @card
62
+ end
63
+ end
64
+ end
data/lib/bridge/deal.rb CHANGED
@@ -22,7 +22,9 @@ module Bridge
22
22
  # ==== Example
23
23
  # Bridge::Deal.new(:n => ["HA", ...], :s => ["SA"], ...)
24
24
  def initialize(hands)
25
- hands.each { |hand, cards| self[hand] = cards }
25
+ hands.each do |hand, cards|
26
+ self[hand] = cards.map { |c| Card.new(c) }
27
+ end
26
28
  end
27
29
 
28
30
  # Converts given id to deal
@@ -30,6 +32,7 @@ module Bridge
30
32
  raise ArgumentError, "invalid deal id: #{id}" unless Bridge.deal_id?(id)
31
33
  n = []; e = []; s = []; w = []; k = DEALS
32
34
  DECK.each_with_index do |card, i|
35
+ card = Card.new(card)
33
36
  x = k * (13 - n.size) / (52 - i)
34
37
  if id < x
35
38
  n << card
@@ -93,7 +96,7 @@ module Bridge
93
96
  if DIRECTIONS.all? { |d| self[d] && self[d].size == 13 }
94
97
  cards = (n + e + s + w).uniq
95
98
  if cards.size == 52
96
- cards.all? { |card| Bridge.card?(card) }
99
+ cards.all? { |card| Bridge.card?(card.to_s) }
97
100
  else
98
101
  false
99
102
  end
@@ -102,6 +105,10 @@ module Bridge
102
105
  end
103
106
  end
104
107
 
108
+ def inspect
109
+ { "N" => n, "E" => e, "S" => s, "W" => w }.inspect
110
+ end
111
+
105
112
  private
106
113
 
107
114
  def must_be_direction!(string)
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bridge
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 6
9
+ version: 0.0.6
5
10
  platform: ruby
6
11
  authors:
7
12
  - "Jakub Ku\xC5\xBAma"
@@ -14,14 +19,16 @@ default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: test-unit
17
- type: :development
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
20
24
  requirements:
21
25
  - - ">="
22
26
  - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
23
29
  version: "2"
24
- version:
30
+ type: :development
31
+ version_requirements: *id001
25
32
  description: Useful contract bridge utilities - deal generator, id to deal and deal to id conversion
26
33
  email: qoobaa+github@gmail.com
27
34
  executables: []
@@ -40,6 +47,7 @@ files:
40
47
  - VERSION
41
48
  - bridge.gemspec
42
49
  - lib/bridge.rb
50
+ - lib/bridge/card.rb
43
51
  - lib/bridge/contract.rb
44
52
  - lib/bridge/deal.rb
45
53
  - test/helper.rb
@@ -59,18 +67,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
59
67
  requirements:
60
68
  - - ">="
61
69
  - !ruby/object:Gem::Version
70
+ segments:
71
+ - 0
62
72
  version: "0"
63
- version:
64
73
  required_rubygems_version: !ruby/object:Gem::Requirement
65
74
  requirements:
66
75
  - - ">="
67
76
  - !ruby/object:Gem::Version
77
+ segments:
78
+ - 0
68
79
  version: "0"
69
- version:
70
80
  requirements: []
71
81
 
72
82
  rubyforge_project:
73
- rubygems_version: 1.3.5
83
+ rubygems_version: 1.3.6
74
84
  signing_key:
75
85
  specification_version: 3
76
86
  summary: Contract bridge utilities