Class: Hand
- Inherits:
-
Object
- Object
- Hand
- Defined in:
- lib/99_game.rb
Overview
Creates a object that holds and can play cards
Instance Attribute Summary collapse
-
#hand ⇒ Object
readonly
Returns the value of attribute hand.
Instance Method Summary collapse
-
#initialize ⇒ Hand
constructor
A new instance of Hand.
- #play(card) ⇒ Object
- #view ⇒ Object
Constructor Details
#initialize ⇒ Hand
Returns a new instance of Hand.
33 |
# File 'lib/99_game.rb', line 33 def initialize; @hand = [$deck.shift, $deck.shift, $deck.shift]; end |
Instance Attribute Details
#hand ⇒ Object (readonly)
Returns the value of attribute hand.
32 33 34 |
# File 'lib/99_game.rb', line 32 def hand @hand end |
Instance Method Details
#play(card) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/99_game.rb', line 34 def play(card) legal = false for cards in @hand; legal = true if cards.num == card.num; end raise CardError, "\aCard not allowed\a" unless legal if card.num == "King"; $value = 99 elsif card.num == "Joker"; $value = 0 else; $value += card.value end i, done = 0, false for index in @hand if index.num == card.num and not done discard = @hand[i] @hand.delete_at(i) draw = $deck.shift @hand.push(draw) $deck.push(discard) done = true end i += 1 end end |
#view ⇒ Object
55 56 57 58 |
# File 'lib/99_game.rb', line 55 def view print "\tThese are your cards: " @hand.each {|card| print "\t#{card.num}"} end |