Skip to content

Instantly share code, notes, and snippets.

@pdxwolfy
Last active May 12, 2017 23:56
Show Gist options
  • Save pdxwolfy/35bcbd073c0976202fd16a4f4b90162f to your computer and use it in GitHub Desktop.
Save pdxwolfy/35bcbd073c0976202fd16a4f4b90162f to your computer and use it in GitHub Desktop.
class Card
attr_reader :rank, :suit
include Comparable
VALUES = { 'Jack' => 11, 'Queen' => 12, 'King' => 13, 'Ace' => 14 }
def initialize(rank, suit)
@rank = rank
@suit = suit
end
def to_s
"#{rank} of #{suit}"
end
def value
VALUES.fetch(@rank, @rank)
end
def <=>(other_card)
value <=> other_card.value
end
end
class Deck
RANKS = (2..10).to_a + %w(Jack Queen King Ace).freeze
SUITS = %w(Hearts Clubs Diamonds Spades).freeze
def intialize()
require 'pry'; binding.pry
@deck = RANKS.product(SUITS).map do |rank, suit|
Card.new(rank, suit)
end
@deck.shuffle!
end
def draw
p @deck.class
selected_card = @deck.pop
selected_card
end
end
deck = Deck.new
drawn = []
52.times { drawn << deck.draw }
p drawn.count { |card| card.rank == 5 } == 4
p drawn.count { |card| card.suit == 'Hearts' } == 13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment