Class: Hand
- Inherits:
-
Object
- Object
- Hand
- Defined in:
- lib/hand.rb
Overview
Creates an object that holds and can play cards. Interacts with Deck objects.
Constant Summary collapse
- @@deck =
Deck.new.cards.shuffle!
Instance Attribute Summary collapse
- #cards ⇒ Array<CardDeck::Card> (also: #inspect)
Instance Method Summary collapse
-
#initialize ⇒ Hand
constructor
A new instance of Hand.
- #play(card) ⇒ void
- #test_outcomes ⇒ Object
-
#view_cards ⇒ void
Displays cards.
Constructor Details
#initialize ⇒ Hand
Returns a new instance of Hand.
6 7 8 |
# File 'lib/hand.rb', line 6 def initialize @cards = Array.new(3) {$deck.shift} end |
Instance Attribute Details
#cards ⇒ Array<CardDeck::Card> Also known as: inspect
5 6 7 |
# File 'lib/hand.rb', line 5 def cards @cards end |
Instance Method Details
#play(card) ⇒ void
Note:
Gameplay method
This method returns an undefined value.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/hand.rb', line 13 def play(card) raise "Card not found" unless @cards.include? card if card.num == "King" $value = 99 elsif card.num == "Joker" $value = 0 else $value += card.value end i, done = 0, false for index in @cards if index.num == card.num and not done discard = @cards[i] @cards.delete_at i @cards.push $deck.shift $deck.push discard done = true $deck.shuffle! end i += 1 end card end |
#test_outcomes ⇒ Object
Note:
Used by the CPU to determine which card to play. Parameter card needs to be an instance of Card.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/hand.rb', line 46 def test_outcomes outcomes = Array.new @cards.each do |card| test_value = case card.num when "King" if (@cards - [card]).any? {|card| [4, 9, "Jack", "Queen", "King", "Joker"].include? card.num} || rand < 0.1 99 else -99 end when "Joker" then 0 else $value + card.value end test_value = -100 if test_value > 99 outcomes << test_value end return outcomes end |
#view_cards ⇒ void
This method returns an undefined value.
Displays cards
39 40 41 42 |
# File 'lib/hand.rb', line 39 def view_cards print "These are your cards: " @cards.each {|card| print "#{card} "} end |