Skip to content

Instantly share code, notes, and snippets.

@bstolte
Last active August 29, 2015 14:28
Show Gist options
  • Save bstolte/dd4c4680dc459bada787 to your computer and use it in GitHub Desktop.
Save bstolte/dd4c4680dc459bada787 to your computer and use it in GitHub Desktop.
Lesson 12 [Self Directed] - Building a Deck of Cards
class Card
attr_accessor :rank, :suit
def initialize(rank, suit)
self.rank = rank
self.suit = suit
end
def output_card
puts "#{self.rank} of #{self.suit}"
end
def self.random_card
Card.new(rand(10), :spades)
end
end
class Deck
def initialize
@stack_of_cards = []
rank = %w{A 2 3 4 5 6 7 8 9 10 J Q K}
suit = %w{Spades Hearts Diamonds Clubs}
suit.each do |suit|
rank.each do |rank|
@stack_of_cards << Card.new(rank, suit)
end
end
end
def shuffle
@stack_of_cards.shuffle!
end
def deal
@card_dealt = @stack_of_cards.shift
@card_dealt.output_card
end
end
deck = Deck.new
deck.shuffle
deck.deal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment