Class: Hand

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeHand

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

#cardsArray<CardDeck::Card> Also known as: inspect

Returns:



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.

Parameters:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/hand.rb', line 15

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
		end
		i += 1
	end
	card
end

#view_cardsvoid

This method returns an undefined value.

Displays cards



42
43
44
45
# File 'lib/hand.rb', line 42

def view_cards
		print "These are your cards: "
   	@cards.each {|card| print "\t#{card}"}
end