Last active
May 12, 2017 23:56
-
-
Save pdxwolfy/35bcbd073c0976202fd16a4f4b90162f to your computer and use it in GitHub Desktop.
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_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