Class: Brigitte::Player
- Inherits:
-
Object
- Object
- Brigitte::Player
- Defined in:
- lib/brigitte/player.rb
Overview
A Player in Brigitte has a: hand
where from player can only throw cards from visible_cards
where from player can draw from if hands are empty blind_cards
cards that are face down where from player can take only one if all cards are played
A player is ready if cards are swapped between it’s hand
and visible_cards
Instance Attribute Summary collapse
-
#blind_cards ⇒ Object
Returns the value of attribute blind_cards.
-
#hand ⇒ Object
Returns the value of attribute hand.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
Returns the value of attribute name.
-
#ready ⇒ Object
Returns the value of attribute ready.
-
#visible_cards ⇒ Object
Returns the value of attribute visible_cards.
Class Method Summary collapse
-
.from_h(hash) ⇒ Object
rubocop:disable Metrics/AbcSize.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(name, id = nil) {|_self| ... } ⇒ Player
constructor
A new instance of Player.
- #pull_blind_card(index) ⇒ Object
- #ready! ⇒ Object
- #ready? ⇒ Boolean
- #sort_hand! ⇒ Object
- #swap(hand_card, visible_card) ⇒ Object
- #throw(card) ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(name, id = nil) {|_self| ... } ⇒ Player
Returns a new instance of Player.
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/brigitte/player.rb', line 19 def initialize(name, id = nil) @id = id || SecureRandom.uuid @name = name @hand = [] @blind_cards = [] @visible_cards = [] @ready = false yield self if block_given? end |
Instance Attribute Details
#blind_cards ⇒ Object
Returns the value of attribute blind_cards.
16 17 18 |
# File 'lib/brigitte/player.rb', line 16 def blind_cards @blind_cards end |
#hand ⇒ Object
Returns the value of attribute hand.
16 17 18 |
# File 'lib/brigitte/player.rb', line 16 def hand @hand end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
17 18 19 |
# File 'lib/brigitte/player.rb', line 17 def id @id end |
#name ⇒ Object
Returns the value of attribute name.
16 17 18 |
# File 'lib/brigitte/player.rb', line 16 def name @name end |
#ready ⇒ Object
Returns the value of attribute ready.
16 17 18 |
# File 'lib/brigitte/player.rb', line 16 def ready @ready end |
#visible_cards ⇒ Object
Returns the value of attribute visible_cards.
16 17 18 |
# File 'lib/brigitte/player.rb', line 16 def visible_cards @visible_cards end |
Class Method Details
.from_h(hash) ⇒ Object
rubocop:disable Metrics/AbcSize
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/brigitte/player.rb', line 87 def self.from_h(hash) # rubocop:disable Metrics/AbcSize return if hash.empty? new(hash[:name], hash[:id]) do |p| p.hand = hash[:hand].map { |h| Card.from_h(h) } p.blind_cards = hash[:blind_cards].map { |h| Card.from_h(h) } p.visible_cards = hash[:visible_cards].map { |h| Card.from_h(h) } p.ready = hash[:ready] end end |
Instance Method Details
#==(other) ⇒ Object
39 40 41 |
# File 'lib/brigitte/player.rb', line 39 def ==(other) id == other&.id end |
#pull_blind_card(index) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/brigitte/player.rb', line 55 def pull_blind_card(index) return false if hand.any? return false if visible_cards.any? blind_card = blind_cards[index] return false unless blind_card blind_cards[index] = nil hand << blind_card sort_hand! true end |
#ready! ⇒ Object
30 31 32 33 |
# File 'lib/brigitte/player.rb', line 30 def ready! sort_hand! @ready = true end |
#ready? ⇒ Boolean
35 36 37 |
# File 'lib/brigitte/player.rb', line 35 def ready? @ready end |
#sort_hand! ⇒ Object
72 73 74 |
# File 'lib/brigitte/player.rb', line 72 def sort_hand! hand.sort_by!(&:weight).reverse! end |
#swap(hand_card, visible_card) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/brigitte/player.rb', line 43 def swap(hand_card, visible_card) return if @ready hand_card_index = hand.find_index(hand_card) visible_card_index = visible_cards.find_index(visible_card) return unless hand_card_index return unless visible_card_index visible_cards[visible_card_index] = hand_card hand[hand_card_index] = visible_card end |
#throw(card) ⇒ Object
68 69 70 |
# File 'lib/brigitte/player.rb', line 68 def throw(card) hand.delete(card) end |
#to_h ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/brigitte/player.rb', line 76 def to_h { id: id, name: name, hand: hand.map(&:to_h), blind_cards: blind_cards.map(&:to_h), visible_cards: visible_cards.map(&:to_h), ready: ready } end |