Class: Bridge::Play
- Inherits:
-
Object
- Object
- Bridge::Play
- Defined in:
- lib/bridge/play.rb
Instance Attribute Summary collapse
-
#cards ⇒ Object
readonly
Returns the value of attribute cards.
-
#contract ⇒ Object
readonly
Returns the value of attribute contract.
-
#deal_id ⇒ Object
readonly
Returns the value of attribute deal_id.
Instance Method Summary collapse
- #card_allowed?(card) ⇒ Boolean
- #deal ⇒ Object
- #declarer ⇒ Object
- #declarer_tricks_number ⇒ Object
- #directions ⇒ Object
- #dummy ⇒ Object
- #finished? ⇒ Boolean
-
#initialize(deal_id, contract, cards) ⇒ Play
constructor
A new instance of Play.
- #left_hand_opponent ⇒ Object (also: #lho)
- #next_direction ⇒ Object
- #right_hand_opponent ⇒ Object (also: #rho)
- #tricks ⇒ Object
- #trump ⇒ Object
Constructor Details
Instance Attribute Details
#cards ⇒ Object (readonly)
Returns the value of attribute cards.
3 4 5 |
# File 'lib/bridge/play.rb', line 3 def cards @cards end |
#contract ⇒ Object (readonly)
Returns the value of attribute contract.
3 4 5 |
# File 'lib/bridge/play.rb', line 3 def contract @contract end |
#deal_id ⇒ Object (readonly)
Returns the value of attribute deal_id.
3 4 5 |
# File 'lib/bridge/play.rb', line 3 def deal_id @deal_id end |
Instance Method Details
#card_allowed?(card) ⇒ Boolean
37 38 39 40 41 42 43 44 |
# File 'lib/bridge/play.rb', line 37 def card_allowed?(card) card = Card.new(card.to_s) case when !contract, cards.include?(card), !deal[next_direction].include?(card) then false when tricks.none?, tricks.last.complete? then true else (deal[next_direction] - cards).map(&:suit).uniq.include?(last_lead.suit) ? card.suit == last_lead.suit : true end end |
#deal ⇒ Object
62 63 64 |
# File 'lib/bridge/play.rb', line 62 def deal @deal ||= Deal.from_id(deal_id) end |
#declarer ⇒ Object
15 16 17 |
# File 'lib/bridge/play.rb', line 15 def declarer contract[-1] if contract end |
#declarer_tricks_number ⇒ Object
58 59 60 |
# File 'lib/bridge/play.rb', line 58 def declarer_tricks_number tricks.map { |trick| deal.owner(trick.winner(trump)) }.count { |direction| [declarer, dummy].include?(direction) } end |
#directions ⇒ Object
46 47 48 |
# File 'lib/bridge/play.rb', line 46 def directions @directions ||= tricks.map { |trick| trick.cards }.flatten.map { |card| deal.owner(card) } end |
#dummy ⇒ Object
19 20 21 |
# File 'lib/bridge/play.rb', line 19 def dummy Bridge.partner_of(declarer) if contract end |
#finished? ⇒ Boolean
11 12 13 |
# File 'lib/bridge/play.rb', line 11 def finished? cards.length == 52 end |
#left_hand_opponent ⇒ Object Also known as: lho
23 24 25 |
# File 'lib/bridge/play.rb', line 23 def left_hand_opponent Bridge.next_direction(declarer) if contract end |
#next_direction ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/bridge/play.rb', line 50 def next_direction case when tricks.none? then Bridge.next_direction(declarer) when tricks.last.complete? then deal.owner(tricks.last.winner(trump)) else Bridge.next_direction(directions.last) end end |
#right_hand_opponent ⇒ Object Also known as: rho
28 29 30 |
# File 'lib/bridge/play.rb', line 28 def right_hand_opponent Bridge.partner_of(left_hand_opponent) if contract end |