Class: Bridge::Board
- Inherits:
-
Object
- Object
- Bridge::Board
- Defined in:
- lib/bridge/board.rb
Overview
Swiped from pybridge.svn.sourceforge.net/svnroot/pybridge/trunk/pybridge/pybridge/games/bridge/board.py 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.
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_at ⇒ Object
Returns the value of attribute created_at.
22 23 24 |
# File 'lib/bridge/board.rb', line 22 def created_at @created_at end |
#deal ⇒ Object
Returns the value of attribute deal.
21 22 23 |
# File 'lib/bridge/board.rb', line 21 def deal @deal end |
#dealer ⇒ Object
Returns the value of attribute dealer.
21 22 23 |
# File 'lib/bridge/board.rb', line 21 def dealer @dealer end |
#number ⇒ Object
Returns the value of attribute number.
21 22 23 |
# File 'lib/bridge/board.rb', line 21 def number @number end |
#players ⇒ Object
Returns the value of attribute players.
21 22 23 |
# File 'lib/bridge/board.rb', line 21 def players @players end |
#vulnerability ⇒ Object
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.
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
#copy ⇒ Object
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.
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 |