Skip to content

Instantly share code, notes, and snippets.

@bstolte
Created August 24, 2015 04:08
Show Gist options
  • Save bstolte/b17c54ffed471d11ddbe to your computer and use it in GitHub Desktop.
Save bstolte/b17c54ffed471d11ddbe 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
# card = Card.random_card
# card.output_card
class Deck
def initialize
@cards = []
# This is shitty!!
@cards << Card.new(2, :spades)
@cards << Card.new(3, :spades)
@cards << Card.new(4, :spades)
@cards << Card.new(5, :spades)
@cards << Card.new(6, :spades)
@cards << Card.new(7, :spades)
@cards << Card.new(8, :spades)
@cards << Card.new(9, :spades)
@cards << Card.new(10, :spades)
@cards << Card.new(:jack, :spades)
@cards << Card.new(:queen, :spades)
@cards << Card.new(:king, :spades)
@cards << Card.new(:ace, :spades)
end
def shuffle
@cards.shuffle!
end
def deal
@card = @cards.shift
@card.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