Created
November 6, 2017 18:33
-
-
Save iseitz/0d32d5af660aa6e4c035276244b53102 to your computer and use it in GitHub Desktop.
Creating a card class with specific rank and suit, then create a hand of several cards and checking if it has any face cards
This file contains hidden or 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 PlayingCard | |
attr_accessor :rank, :suite | |
def initialize (rank, suite) | |
@rank = rank | |
@suite = suite | |
end | |
def face_card? | |
['K', 'Q', 'J'].include?(@rank) | |
end | |
class << self | |
def hand (hash_of_cards) | |
@hand = [] | |
hash_of_cards.each do |rank, suite| | |
hand_part = PlayingCard.new(rank, suite) | |
@hand << hand_part | |
end | |
return @hand | |
end | |
def has_face_card? | |
@hand = PlayingCard.hand({'7'=>'spades', '4'=>'diamonds', 'K'=>'hearts'}) | |
@hand.each do |card| | |
if card.face_card? | |
puts "Face card!" | |
else | |
puts "Not a face card!" | |
end | |
end | |
end | |
end | |
end | |
hand1 = PlayingCard.hand({'7'=>'spades', '4'=>'diamonds', 'K'=>'hearts'}) | |
puts PlayingCard.has_face_card? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment