Created
December 16, 2015 10:35
-
-
Save MasoodGit/53578f063045b5cedba8 to your computer and use it in GitHub Desktop.
Deck of cards , using class Card and Deck
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For deal & count , instead of the puts statement, try to return the value and the caller will call it. Same for output deck