Class: CardGame::Player
- Inherits:
-
Object
- Object
- CardGame::Player
- Extended by:
- Forwardable
- Defined in:
- lib/cardgame/player.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#initialize(name) ⇒ Player
constructor
A new instance of Player.
-
#play(top_card) ⇒ Object
Select one or more of our cards (according to our playing strategy).
- #to_s ⇒ Object
Constructor Details
#initialize(name) ⇒ Player
Returns a new instance of Player.
8 9 10 11 |
# File 'lib/cardgame/player.rb', line 8 def initialize(name) @name = name @hand = [] end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/cardgame/player.rb', line 6 def name @name end |
Instance Method Details
#play(top_card) ⇒ Object
Select one or more of our cards (according to our playing strategy)
Note the the player has no further knowledge about the state of the game except the topmost card of the pile that is passed. If a player wants to implement more advanced strategies (e.g. by observing and counting the cards already played), we need to implement a notification mechanism that tells every player which card the current player played.
TODO Need to obey if top_card is a trump, unless we can trump that one TODO If the game system allows it, return two identical cards
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/cardgame/player.rb', line 27 def play(top_card) raise "There must be a card on the pile" if top_card.nil? # puts # puts "#{self} plays:" # puts "#{@hand.size} in hand and sees the top card #{top_card}" candidates = @hand.select{|card| top_card.rank == card.rank || top_card.suit == card.suit || card.trump?} selected = candidates.sort_by{rand}.pop if selected.nil? # puts "Decided NOT to play" else selected.suit = Uno.suits.sample if selected.trump? # TODO Improve suit selection to take our current hand into account # puts "Decided to play #{selected}" @hand.delete_if{|card| selected == card} # puts "Has #{@hand.size} left in hand." end # raise Uno.new(selected) if 1 == @hand.size raise UnoUno.new(self, selected) if 0 == @hand.size selected end |
#to_s ⇒ Object
13 14 15 |
# File 'lib/cardgame/player.rb', line 13 def to_s "Player #{@name}" end |