Last active
August 6, 2020 20:04
-
-
Save KGZM/48f90948953f17aad811a791fae15b10 to your computer and use it in GitHub Desktop.
cards.rb
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
vendor | |
.bundle |
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 "pry" | |
SUITS = [:spades, :diamonds, :hearts, :clubs] | |
VALUES = (1..13).to_a | |
FACES = ["Jack", "Queen", "King", "Ace"] | |
class Card | |
attr_accessor :suit, :value | |
def initialize(suit, value) | |
self.suit = suit | |
self.value = value | |
end | |
end | |
class Deck | |
def initialize | |
cards = VALUES.flat_map do |value| | |
SUITS.map do |suit| | |
Card.new(suit, value) | |
end | |
end | |
@cards = cards | |
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 'minitest/autorun' | |
require './cards' | |
describe "A deck of cards" do | |
let(:deck) { Deck.new() } | |
it "has 52 cards" do | |
value(deck.cards.length).must_equal(52) | |
end | |
it "shuffles" do | |
original_order = deck.cards.map(&:value) | |
deck.shuffle | |
value(deck.cards.map(&:value)).wont_equal(original_order) | |
end | |
end | |
describe "A hand of cards" do | |
def h(*args) | |
Hand.new(args.map { |c| Card.new(*c) }) | |
end | |
let (:hands) { | |
{ | |
royal_flush: h([:spades, 13], [:spades, 12], [:spades, 11], [:spades, 10], [:spades, 9]), | |
straight_flush: h([:spades, 1], [:spades, 2], [:spades, 3], [:spades, 4], [:spades, 5]), | |
full_house: h([:spades, 8], [:hearts, 8], [:diamonds, 8], [:spades, 7], [:clubs, 7]), | |
flush: h([:spades, 1], [:spades, 5], [:spades, 9], [:spades, 10], [:spades, 4]), | |
straight: h([:spades, 8], [:hearts, 5], [:spades, 6], [:spades, 7], [:spades, 4]), | |
three_of_kind: h([:hearts, 5], [:spades, 5], [:clubs, 5], [:clubs, 1], [:diamonds, 3]), | |
two_pair: h([:hearts, 5], [:spades, 5], [:clubs, 2], [:clubs, 1], [:diamonds, 1]), | |
pair: h([:hearts, 5], [:spades, 5], [:clubs, 3], [:clubs, 1], [:diamonds, 2]), | |
high_card: h([:hearts, 5], [:spades, 4], [:clubs, 13], [:clubs, 1], [:diamonds, 2]), | |
nothing: h([:hearts, 5], [:spades, 4], [:clubs, 1], [:clubs, 8], [:diamonds, 2]) | |
} | |
} | |
def self.beats(hand, others) | |
others.each do |other| | |
it "#{hand} must beat #{other}" do | |
value(hands[hand].score).must_be :>, hands[other].score | |
end | |
end | |
end | |
beats(:royal_flush, [:straight_flush, :full_house, :flush, :straight, :three_of_kind, :two_pair, :pair, :high_card, :nothing]) | |
beats(:straight_flush, [:full_house, :flush, :straight, :three_of_kind, :two_pair, :pair, :high_card, :nothing]) | |
beats(:full_house, [:flush, :straight, :three_of_kind, :two_pair, :pair, :high_card, :nothing]) | |
beats(:flush, [:straight, :three_of_kind, :two_pair, :pair, :high_card, :nothing]) | |
beats(:straight, [:three_of_kind, :two_pair, :pair, :high_card, :nothing]) | |
beats(:three_of_kind, [:two_pair, :pair, :high_card, :nothing]) | |
beats(:two_pair, [:pair, :high_card, :nothing]) | |
beats(:pair, [:high_card, :nothing]) | |
beats(:high_card, [:nothing]) | |
describe "two_pair" do | |
it "musnt be a full house" do | |
value(hands[:two_pair].is_full_house).must_equal false | |
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
# frozen_string_literal: true | |
source "https://rubygems.org" | |
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } | |
# gem "rails" | |
gem "minitest", "~> 5.11" | |
gem "pry", "~> 0.12.2" | |
gem "pry-doc", "~> 1.0" |
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
GEM | |
remote: https://rubygems.org/ | |
specs: | |
coderay (1.1.2) | |
method_source (0.9.2) | |
minitest (5.11.3) | |
pry (0.12.2) | |
coderay (~> 1.1.0) | |
method_source (~> 0.9.0) | |
pry-doc (1.0.0) | |
pry (~> 0.11) | |
yard (~> 0.9.11) | |
yard (0.9.18) | |
PLATFORMS | |
ruby | |
DEPENDENCIES | |
minitest (~> 5.11) | |
pry (~> 0.12.2) | |
pry-doc (~> 1.0) | |
BUNDLED WITH | |
2.0.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment