Class: Bridge::Board

Inherits:
Object
  • Object
show all
Defined in:
lib/bridge/board.rb

Overview

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Board

Returns a new instance of Board.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bridge/board.rb', line 24

def initialize opts = {}
  opts = {
    :deal => Deal.new,
    :number => 1,
    :dealer => Direction.north,
    :vulnerability => Vulnerability.none
  }.merge(opts)
  
  opts.map { |k,v| self.send(:"#{k}=",v) if self.respond_to?(k) }
  self.created_at = Time.now
end

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at.



22
23
24
# File 'lib/bridge/board.rb', line 22

def created_at
  @created_at
end

#dealObject

Returns the value of attribute deal.



21
22
23
# File 'lib/bridge/board.rb', line 21

def deal
  @deal
end

#dealerObject

Returns the value of attribute dealer.



21
22
23
# File 'lib/bridge/board.rb', line 21

def dealer
  @dealer
end

#numberObject

Returns the value of attribute number.



21
22
23
# File 'lib/bridge/board.rb', line 21

def number
  @number
end

#playersObject

Returns the value of attribute players.



21
22
23
# File 'lib/bridge/board.rb', line 21

def players
  @players
end

#vulnerabilityObject

Returns the value of attribute vulnerability.



21
22
23
# File 'lib/bridge/board.rb', line 21

def vulnerability
  @vulnerability
end

Class Method Details

.first(deal = nil) ⇒ Object

Builds and returns a successor board to this board. The dealer and vulnerability of the successor board are determined from the board number, according to the rotation scheme for duplicate bridge. Otherwise, a randomly-generated deal is wrapped.

Parameters:

  • deal:

    if provided, the deal to be wrapped by next board.



41
42
43
44
45
46
47
48
# File 'lib/bridge/board.rb', line 41

def self.first deal = nil
  self.new(
    :deal => deal || Deal.new,
    :number => 1,
    :dealer => Direction.north,
    :vulnerability => Vulnerability.none
  )
end

Instance Method Details

#copyObject



80
81
82
# File 'lib/bridge/board.rb', line 80

def copy
  self.clone
end

#next(deal = nil) ⇒ Object

Builds and returns a successor board to this board. The dealer and vulnerability of the successor board are determined from the board number, according to the rotation scheme for duplicate bridge. Otherwise, a randomly-generated deal is wrapped.

Parameters:

  • deal:

    if provided, the deal to be wrapped by next board.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/bridge/board.rb', line 55

def next deal = nil
  board = Board.new
  board.deal = deal || Deal.new
  board.number = self.number + 1
  board.created_at = Time.now

  # Dealer rotates clockwise.
  board.dealer = Direction.next(self.dealer)

  # Map from duplicate board index range 1..16 to vulnerability.
  # See http://www.d21acbl.com/References/Laws/node5.html#law2
  i = (board.number - 1) % 16
  board.vulnerability = Vulnerability[(i%4 + i/4)%4]

  return board
end

#to_json(opts = {}) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/bridge/board.rb', line 72

def to_json(opts = {})
  h = {}
  [:vulnerability, :players, :dealer, :deal, :number, :created_at].each do |a|
    h[a] = send(a)
  end
  h.to_json
end