Skip to content

Instantly share code, notes, and snippets.

@MasoodGit
Created December 16, 2015 10:35
Show Gist options
  • Save MasoodGit/53578f063045b5cedba8 to your computer and use it in GitHub Desktop.
Save MasoodGit/53578f063045b5cedba8 to your computer and use it in GitHub Desktop.
Deck of cards , using class Card and Deck
class Card
attr_accessor :rank, :suit
def initialize(rank, suit)
self.rank = rank
self.suit = suit
end
def output_card
return "Card: #{self.rank} of #{self.suit}"
end
end
class Deck
def initialize()
ranks = %w{ A 2 3 4 5 6 7 8 9 10 J Q K}
suits = %w{Spades Hearts Diamonds Clubs}
@cards = []
suits.each do |suit|
ranks.each do |rank|
@cards << Card.new(rank,suit)
end
end
end
def shuffle
@cards.shuffle!
end
def deal
card = @cards.shift
puts "Dealing - " + card.output_card
end
def count
puts @cards.size
end
def output_deck
result = ''
@cards.each do |card|
result += card.output_card + "\n"
end
puts result
end
end
deck = Deck.new
#deck.output_deck
deck.shuffle
deck.deal
deck.count
deck.deal
deck.count
@binhngoc17
Copy link

For deal & count , instead of the puts statement, try to return the value and the caller will call it. Same for output deck

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment