Class: Bridge::Auction
- Inherits:
-
Object
- Object
- Bridge::Auction
- Defined in:
- lib/bridge/auction.rb
Instance Attribute Summary collapse
-
#bids ⇒ Object
readonly
Returns the value of attribute bids.
-
#dealer ⇒ Object
readonly
Returns the value of attribute dealer.
Instance Method Summary collapse
- #bid_allowed?(bid) ⇒ Boolean
- #contract ⇒ Object
- #declarer ⇒ Object
- #directions ⇒ Object
- #finished? ⇒ Boolean
-
#initialize(dealer, bids) ⇒ Auction
constructor
A new instance of Auction.
- #next_direction ⇒ Object
Constructor Details
#initialize(dealer, bids) ⇒ Auction
Returns a new instance of Auction.
5 6 7 8 9 |
# File 'lib/bridge/auction.rb', line 5 def initialize(dealer, bids) raise ArgumentError, "invalid direction: #{dealer}" unless Bridge.direction?(dealer) @dealer = dealer @bids = bids.map { |bid| Bid.new(bid.to_s) } end |
Instance Attribute Details
#bids ⇒ Object (readonly)
Returns the value of attribute bids.
3 4 5 |
# File 'lib/bridge/auction.rb', line 3 def bids @bids end |
#dealer ⇒ Object (readonly)
Returns the value of attribute dealer.
3 4 5 |
# File 'lib/bridge/auction.rb', line 3 def dealer @dealer end |
Instance Method Details
#bid_allowed?(bid) ⇒ Boolean
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/bridge/auction.rb', line 22 def bid_allowed?(bid) bid = Bid.new(bid.to_s) return false if finished? case when bid.pass? then true when bid.contract? then contract_allowed?(bid) when bid.double? then double_allowed? when bid.redouble? then redouble_allowed? end end |
#contract ⇒ Object
15 16 17 18 19 20 |
# File 'lib/bridge/auction.rb', line 15 def contract if last_contract_index modifier = bids[last_contract_index..-1].select(&:modifier?).last bids[last_contract_index].to_s + modifier.to_s + declarer end end |
#declarer ⇒ Object
33 34 35 36 37 |
# File 'lib/bridge/auction.rb', line 33 def declarer direction = dealer last_contract_index.times { direction = Bridge.next_direction(direction) } direction end |
#directions ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/bridge/auction.rb', line 39 def directions @directions ||= begin return [] if bids.none? directions = [dealer] (bids.count - 1).times { directions << Bridge.next_direction(directions.last) } directions end end |
#finished? ⇒ Boolean
11 12 13 |
# File 'lib/bridge/auction.rb', line 11 def finished? bids.length > 3 && bids[-3..-1].all?(&:pass?) end |
#next_direction ⇒ Object
48 49 50 |
# File 'lib/bridge/auction.rb', line 48 def next_direction @next_direction ||= directions.none? ? dealer : Bridge.next_direction(directions.last) end |