Skip to content

Instantly share code, notes, and snippets.

@matthutchinson
Created September 22, 2015 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthutchinson/e0687187ebf421f3c7c8 to your computer and use it in GitHub Desktop.
Save matthutchinson/e0687187ebf421f3c7c8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
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
attr_accessor :cards
def initialize
@suits = %w(Hearts Spades Diamonds Clubs)
@ranks = [1,2,3,4,5,6,7,8,9,10,'Jack', 'King', 'Queen', 'Ace']
@cards = []
@suits.each do |suit|
@ranks.each do |rank|
@cards << Card.new(rank, suit)
end
end
end
def shuffle
@cards.shuffle!
end
def deal
@cards.shift
end
def output_deck
puts "Here is the deck"
@cards.each do |card|
card.output_card
end
puts "(#{@cards.length} cards left in deck)"
end
end
#### testing
deck = Deck.new # create a deck
#deck.shuffle # shuffle the deck
#deck.output_deck # print the whole deck
#deck.deal.output_card # deal one card and show it
# deal 5 cards and show each one
5.times do
deck.deal.output_card
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment