Class: Bridge::Deal

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/bridge/deal.rb

Overview

Class representing bridge deal

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hands) ⇒ Deal

Creates new deal object with cards given in hash of directions

Example

Bridge::Deal.new(:n => ["HA", ...], :s => ["SA"], ...)


23
24
25
26
27
# File 'lib/bridge/deal.rb', line 23

def initialize(hands)
  hands.each do |hand, cards|
    self[hand] = cards.map { |c| Card.new(c) }
  end
end

Instance Attribute Details

#eObject (readonly)

Returns the value of attribute e.



6
7
8
# File 'lib/bridge/deal.rb', line 6

def e
  @e
end

#nObject (readonly)

Returns the value of attribute n.



6
7
8
# File 'lib/bridge/deal.rb', line 6

def n
  @n
end

#sObject (readonly)

Returns the value of attribute s.



6
7
8
# File 'lib/bridge/deal.rb', line 6

def s
  @s
end

#wObject (readonly)

Returns the value of attribute w.



6
7
8
# File 'lib/bridge/deal.rb', line 6

def w
  @w
end

Class Method Details

.from_id(id) ⇒ Object

Converts given id to deal

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bridge/deal.rb', line 30

def self.from_id(id)
  raise ArgumentError, "invalid deal id: #{id}" unless Bridge.deal_id?(id)
  n = []; e = []; s = []; w = []; k = DEALS
  DECK.each_with_index do |card, i|
    card = Card.new(card)
    x = k * (13 - n.size) / (52 - i)
    if id < x
      n << card
    else
      id -= x
      x = k * (13 - e.size) / (52 - i)
      if id < x
        e << card
      else
        id -= x
        x = k * (13 - s.size) / (52 - i)
        if id < x
          s << card
        else
          id -= x
          x = k * (13 - w.size) / (52 - i)
          w << card
        end
      end
    end
    k = x
  end
  new(:n => n, :e => e, :s => s, :w => w)
end

.randomObject

Returns a random deal



94
95
96
# File 'lib/bridge/deal.rb', line 94

def self.random
  from_id(random_id)
end

.random_idObject

Returns a random deal id



89
90
91
# File 'lib/bridge/deal.rb', line 89

def self.random_id
  rand(DEALS)
end

Instance Method Details

#<=>(other) ⇒ Object

Compares the deal with given deal



15
16
17
# File 'lib/bridge/deal.rb', line 15

def <=>(other)
  id <=> other.id
end

#[](direction) ⇒ Object

Returns cards of given direction



9
10
11
12
# File 'lib/bridge/deal.rb', line 9

def [](direction)
  must_be_direction!(direction)
  send("#{direction.to_s.downcase}")
end

#cards_for(direction) ⇒ Object



149
150
151
152
153
154
# File 'lib/bridge/deal.rb', line 149

def cards_for(direction)
  TRUMPS.each_with_object({}) do |trump, colors|
    cards = self[direction].select { |card| card.suit == trump }
    colors[trump] = cards
  end
end

#honour_card_points(side = nil) ⇒ Object Also known as: hcp



121
122
123
124
125
126
127
128
129
130
# File 'lib/bridge/deal.rb', line 121

def honour_card_points(side = nil)
  hash = DIRECTIONS.each_with_object({}) do |direction, h|
    h[direction] = self[direction].inject(0) { |sum, card| sum += card.honour_card_points }
  end
  if side
    side.to_s.upcase.split("").inject(0) { |sum, direction| sum += hash[direction] }
  else
    hash
  end
end

#idObject

Converts given deal (hash) to id



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/bridge/deal.rb', line 61

def id
  k = DEALS; id = 0; n = self.n.dup; e = self.e.dup; s = self.s.dup; w = self.w.dup
  DECK.each_with_index do |card, i|
    x = k * n.size / (52 - i)
    unless n.delete(card)
      id += x
      x = k * e.size / (52 - i)
      unless e.delete(card)
        id += x
        x = k * s.size / (52 - i)
        unless s.delete(card)
          id += x
          x = k * w.size / (52 - i)
          w.delete(card)
        end
      end
    end
    k = x
  end
  id
end

#inspectObject



117
118
119
# File 'lib/bridge/deal.rb', line 117

def inspect
  to_hash.inspect
end

#owner(card) ⇒ Object

Returns the direction that owns the card



84
85
86
# File 'lib/bridge/deal.rb', line 84

def owner(card)
  DIRECTIONS.find { |direction| self[direction].include?(card) }
end

#sort_by_color(trump = nil) ⇒ Object



140
141
142
143
144
145
146
147
# File 'lib/bridge/deal.rb', line 140

def sort_by_color(trump = nil)
  DIRECTIONS.each_with_object({}) do |direction, sorted|
    splitted_colors = cards_for(direction)
    splitted_colors.reject! { |color, cards| cards.empty? }
    sorted_colors = sort_colors(splitted_colors.keys, trump)
    sorted[direction] = sorted_colors.map { |color| splitted_colors.delete(color) }.flatten
  end
end

#sort_by_color!(trump = nil) ⇒ Object



133
134
135
136
137
138
# File 'lib/bridge/deal.rb', line 133

def sort_by_color!(trump = nil)
  sort_by_color(trump).each do |direction, hand|
    self[direction] = hand
  end
  self
end

#to_hashObject

Returns hash with hands



113
114
115
# File 'lib/bridge/deal.rb', line 113

def to_hash
  {"N" => n.map(&:to_s), "E" => e.map(&:to_s), "S" => s.map(&:to_s), "W" => w.map(&:to_s)}
end

#valid?Boolean

Checks if the deal is a valid deal

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/bridge/deal.rb', line 99

def valid?
  if DIRECTIONS.all? { |d| self[d] && self[d].size == 13 }
    cards = (n + e + s + w).uniq
    if cards.size == 52
      cards.all? { |card| Bridge.card?(card.to_s) }
    else
      false
    end
  else
    false
  end
end