Class: Bridge::Board
- Inherits:
-
Object
- Object
- Bridge::Board
- Defined in:
- lib/bridge/board.rb
Overview
An encapsulation of board information.
Instance Attribute Summary collapse
-
#created_at ⇒ Object
Returns the value of attribute created_at.
-
#deal ⇒ Object
Returns the value of attribute deal.
-
#dealer ⇒ Object
Returns the value of attribute dealer.
-
#number ⇒ Object
Returns the value of attribute number.
-
#players ⇒ Object
Returns the value of attribute players.
-
#vulnerability ⇒ Object
Returns the value of attribute vulnerability.
Class Method Summary collapse
-
.first(deal = nil) ⇒ Object
Builds and returns a successor board to this board.
Instance Method Summary collapse
- #copy ⇒ Object
-
#initialize(opts = {}) ⇒ Board
constructor
A new instance of Board.
-
#next(deal = nil) ⇒ Object
Builds and returns a successor board to this board.
- #to_json(opts = {}) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Board
Returns a new instance of Board.
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/bridge/board.rb', line 23 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_at ⇒ Object
Returns the value of attribute created_at.
21 22 23 |
# File 'lib/bridge/board.rb', line 21 def created_at @created_at end |
#deal ⇒ Object
Returns the value of attribute deal.
20 21 22 |
# File 'lib/bridge/board.rb', line 20 def deal @deal end |
#dealer ⇒ Object
Returns the value of attribute dealer.
20 21 22 |
# File 'lib/bridge/board.rb', line 20 def dealer @dealer end |
#number ⇒ Object
Returns the value of attribute number.
20 21 22 |
# File 'lib/bridge/board.rb', line 20 def number @number end |
#players ⇒ Object
Returns the value of attribute players.
20 21 22 |
# File 'lib/bridge/board.rb', line 20 def players @players end |
#vulnerability ⇒ Object
Returns the value of attribute vulnerability.
20 21 22 |
# File 'lib/bridge/board.rb', line 20 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.
40 41 42 43 44 45 46 47 |
# File 'lib/bridge/board.rb', line 40 def self.first deal = nil self.new( :deal => deal || Deal.new, :number => 1, :dealer => Direction.north, :vulnerability => Vulnerability.none ) end |
Instance Method Details
#copy ⇒ Object
79 80 81 |
# File 'lib/bridge/board.rb', line 79 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.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/bridge/board.rb', line 54 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
71 72 73 74 75 76 77 |
# File 'lib/bridge/board.rb', line 71 def to_json(opts = {}) h = {} [:vulnerability, :players, :dealer, :deal, :number, :created_at].each do |a| h[a] = send(a) end h.to_json end |