-
-
Save framallo/fd2d8f5dd2456a0221b8 to your computer and use it in GitHub Desktop.
Firehose Card Challenge
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 | |
puts "#{self.rank} of #{self.suit}" | |
end | |
end | |
class Deck | |
attr_accessor :cards | |
def cards | |
@cards ||= populate_deck | |
end | |
def initialize | |
end | |
def ranks | |
%w{2 3 4 5 6 7 8 9 10 Jack Queen King Ace} | |
end | |
def suits | |
['spades', 'clubs', 'hearts', 'diamonds' ] | |
end | |
def shuffle | |
cards.shuffle! | |
end | |
def deal | |
cards.shift | |
end | |
# private | |
def populate_deck | |
possible_combinations.map do |rank, suit| | |
Card.new rank, suit | |
end | |
end | |
def possible_combinations | |
@card_combinations ||= ranks.product suits | |
end | |
end |
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
require 'rspec_helper' | |
RSpec.describe Deck do | |
it 'returns all possible card combinations' do | |
expect(Deck.new.possible_combinations).to eq [ | |
%w(2 spades), %w(2 clubs), %w(2 hearts), %w(2 diamonds), | |
%w(3 spades), %w(3 clubs), %w(3 hearts), %w(3 diamonds), | |
%w(4 spades), %w(4 clubs), %w(4 hearts), %w(4 diamonds), | |
%w(5 spades), %w(5 clubs), %w(5 hearts), %w(5 diamonds), | |
%w(6 spades), %w(6 clubs), %w(6 hearts), %w(6 diamonds), | |
%w(7 spades), %w(7 clubs), %w(7 hearts), %w(7 diamonds), | |
%w(8 spades), %w(8 clubs), %w(8 hearts), %w(8 diamonds), | |
%w(9 spades), %w(9 clubs), %w(9 hearts), %w(9 diamonds), | |
%w(10 spades), %w(10 clubs), %w(10 hearts), %w(10 diamonds), | |
%w(Jack spades), %w(Jack clubs), %w(Jack hearts), %w(Jack diamonds), | |
%w(Queen spades), %w(Queen clubs), %w(Queen hearts), %w(Queen diamonds), | |
%w(King spades), %w(King clubs), %w(King hearts), %w(King diamonds), | |
%w(Ace spades), %w(Ace clubs), %w(Ace hearts), %w(Ace diamonds) | |
] | |
end | |
it 'has 52 cards' do | |
expect(Deck.new.cards.size).to eq 52 | |
end | |
it 'has 13 ranks' do | |
expect(Deck.new.ranks.size).to eq 13 | |
end | |
it 'has 4 suits' do | |
expect(Deck.new.suits.size).to eq 4 | |
end | |
describe '#deal' do | |
let(:deck) { Deck.new } | |
it 'can deal a card' do | |
expect { deck.deal }.to change { | |
deck.cards | |
} | |
end | |
it 'reduces the amount of cards' do | |
expect { deck.deal }.to change { | |
deck.cards.size | |
}.from(52).to(51) | |
end | |
it 'can deal more than 52 cards' do | |
52.times { deck.deal } | |
expect { deck.deal }.not_to raise_exception | |
end | |
it 'returns a card' do | |
expect(deck.deal).to be_kind_of Card | |
end | |
end | |
end |
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
require_relative 'card' | |
require 'rspec' | |
# gem install rspec |
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
require_relative 'card' | |
deck = Deck.new | |
deck.deal | |
deck.deal.output_card |
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
require_relative 'card' | |
puts 'possible combinations ' | |
puts Deck.new.possible_combinations == [ | |
%w(2 spades), %w(2 clubs), %w(2 hearts), %w(2 diamonds), | |
%w(3 spades), %w(3 clubs), %w(3 hearts), %w(3 diamonds), | |
%w(4 spades), %w(4 clubs), %w(4 hearts), %w(4 diamonds), | |
%w(5 spades), %w(5 clubs), %w(5 hearts), %w(5 diamonds), | |
%w(6 spades), %w(6 clubs), %w(6 hearts), %w(6 diamonds), | |
%w(7 spades), %w(7 clubs), %w(7 hearts), %w(7 diamonds), | |
%w(8 spades), %w(8 clubs), %w(8 hearts), %w(8 diamonds), | |
%w(9 spades), %w(9 clubs), %w(9 hearts), %w(9 diamonds), | |
%w(10 spades), %w(10 clubs), %w(10 hearts), %w(10 diamonds), | |
%w(Jack spades), %w(Jack clubs), %w(Jack hearts), %w(Jack diamonds), | |
%w(Queen spades), %w(Queen clubs), %w(Queen hearts), %w(Queen diamonds), | |
%w(King spades), %w(King clubs), %w(King hearts), %w(King diamonds), | |
%w(Ace spades), %w(Ace clubs), %w(Ace hearts), %w(Ace diamonds) | |
] | |
puts 'cards should be 52' | |
puts Deck.new.cards.size == 52 | |
puts 'should have 13 ranks' | |
puts Deck.new.ranks.size == 13 | |
puts 'should have 4 suits' | |
puts Deck.new.suits.size == 4 | |
puts 'testing dealing' | |
deck = Deck.new | |
before_dealing = deck.cards.join | |
deck.deal | |
after_dealing = deck.cards.join | |
puts before_dealing != after_dealing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment