Class: Bridge::Auction

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dealer, bids) ⇒ Auction

Returns a new instance of Auction.

Raises:

  • (ArgumentError)


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

#bidsObject (readonly)

Returns the value of attribute bids.



3
4
5
# File 'lib/bridge/auction.rb', line 3

def bids
  @bids
end

#dealerObject (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

Returns:

  • (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

#contractObject



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

#declarerObject



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

#directionsObject



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

Returns:

  • (Boolean)


11
12
13
# File 'lib/bridge/auction.rb', line 11

def finished?
  bids.length > 3 && bids[-3..-1].all?(&:pass?)
end

#next_directionObject



48
49
50
# File 'lib/bridge/auction.rb', line 48

def next_direction
  @next_direction ||= directions.none? ? dealer : Bridge.next_direction(directions.last)
end