Решение на Четвърта задача от Изтрит профил

Обратно към всички решения

Към профила на Изтрит профил

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 38 успешни тест(а)
  • 19 неуспешни тест(а)

Код

class Card
attr_accessor :rank, :suit
def initialize(rank, suit)
@rank = rank
@suit = suit
end
def ==(other)
@rank == other.rank and @suit == other.suit
end
alias :eql? :==
def hash
[@rank, @suit].hash
end
def to_s()
"%s of %s" % [@rank.to_s.capitalize, @suit.capitalize]
end
end
class Deck
include Enumerable
class Hand
def initialize(cards)
@cards = cards
end
def has_rank?(rank, cards = @cards)
cards.any? { |card| card.rank == rank }
end
def size
@cards.length
end
end
def initialize(cards = nil)
@suits = [:spades, :hearts, :diamonds, :clubs]
rank_suit_pairs = @suits.product(@ranks.reverse)
default_cards = rank_suit_pairs.map { |suit, rank| Card.new(rank, suit) }
@cards = cards || default_cards
@suit_hash = Hash[default_cards.map.with_index.to_a]
end
def size
@cards.length
end
def draw_top_card
@cards.shift
end
def draw_bottom_card
@cards.pop
end
def top_card
@cards[0]
end
def bottom_card
@cards[-1]
end
def shuffle()
@cards.shuffle!
end
def sort
@cards.sort! { |a, b| @suit_hash[a] <=> @suit_hash[b] }
end
def to_s()
@cards.join("\n")
end
end
class WarDeck < Deck
class Hand < Deck::Hand
def play_card
@cards.pop
end
def allow_face_up?
@cards.length <= 3
end
end
def initialize(cards = nil)
@cards_in_hand = 26
@ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
super(cards)
end
def deal
Hand.new(@cards[0..@cards_in_hand - 1])
end
end
class BeloteDeck < Deck
class Hand < Deck::Hand
def initialize(cards, ranks)
super(cards)
@ranks = ranks
end
def highest_of_suit(suit)
cards_of_suit = @cards.select { |card| card.suit == suit }
@ranks.reverse.each do |rank|
return rank if cards_of_suit.any? { |card| card.rank == rank }
end
nil
end
def belote?
groups_by_suit = @cards.group_by { |card| card.suit }
groups_by_suit.each do |suit, cards|
return true if has_rank?(:king, cards) && has_rank?(:queen, cards)
end
false
end
def has_ranks?(ranks, cards)
ranks.all? { |rank| has_rank?(rank, cards) }
end
def check_sequence(count, suit, cards)
@ranks.each_cons(count) do |ranks|
return true if has_ranks?(ranks, cards)
end
false
end
def has_successive_ranks?(count)
groups_by_suit = @cards.group_by { |card| card.suit }
groups_by_suit.each do |suit, cards|
return true if check_sequence(count, suit, cards)
end
false
end
def tierce?
has_successive_ranks?(3)
end
def quarte?
has_successive_ranks?(4)
end
def quint?
has_successive_ranks?(5)
end
def carre_of_rank?(rank)
@cards.count { |card| card.rank == rank } == 4
end
def carre_of_jacks?
carre_of_rank?(:jack)
end
def carre_of_nines?
carre_of_rank?(9)
end
def carre_of_aces?
carre_of_rank?(:ace)
end
end
def initialize(cards = nil)
@cards_in_hand = 8
@ranks = [7, 8, 9, :jack, :queen, :king, 10, :ace]
super(cards)
end
def deal
Hand.new(@cards[0..@cards_in_hand - 1], @ranks)
end
end
class SixtySixDeck < Deck
class Hand < Deck::Hand
def twenty?(trump_suit)
groups_by_suit = @cards.group_by { |card| card.suit }
groups_by_suit.delete(trump_suit)
groups_by_suit.each do |suit, cards|
return true if has_rank?(:king, cards) &&
has_rank?(:queen, cards)
end
false
end
def forty?(trump_suit)
groups_by_suit = @cards.group_by { |card| card.suit }
trump_suit_cards = groups_by_suit[trump_suit]
trump_suit_cards.any? { |card| card.rank == :king } &&
trump_suit_cards.any? { |card| card.rank == :queen }
end
end
def initialize(cards = nil)
@cards_in_hand = 6
@ranks = [9, :jack, :queen, :king, 10, :ace]
super(cards)
end
def deal
Hand.new(@cards[0..@cards_in_hand - 1])
end
end

Лог от изпълнението

...FF.FFFF......FF.FFFF....F...............FF.FFFF.......

Failures:

  1) WarDeck behaves like a deck implements Enumerable
     Failure/Error: expect(small_deck).to respond_to(:each)
       expected #<WarDeck:0x007f8d4455aa18 @cards_in_hand=26, @ranks=[2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace], @suits=[:spades, :hearts, :diamonds, :clubs], @cards=[#<Card:0x007f8d4455ab58 @rank=:ace, @suit=:spades>, #<Card:0x007f8d4455ab30 @rank=9, @suit=:clubs>], @suit_hash={#<Card:0x007f8d44558d80 @rank=:ace, @suit=:spades>=>0, #<Card:0x007f8d44558d58 @rank=:king, @suit=:spades>=>1, #<Card:0x007f8d44558d30 @rank=:queen, @suit=:spades>=>2, #<Card:0x007f8d44558c68 @rank=:jack, @suit=:spades>=>3, #<Card:0x007f8d44558c40 @rank=10, @suit=:spades>=>4, #<Card:0x007f8d44558ab0 @rank=9, @suit=:spades>=>5, #<Card:0x007f8d44558a88 @rank=8, @suit=:spades>=>6, #<Card:0x007f8d44558858 @rank=7, @suit=:spades>=>7, #<Card:0x007f8d44558718 @rank=6, @suit=:spades>=>8, #<Card:0x007f8d445584e8 @rank=5, @suit=:spades>=>9, #<Card:0x007f8d445583f8 @rank=4, @suit=:spades>=>10, #<Card:0x007f8d445583d0 @rank=3, @suit=:spades>=>11, #<Card:0x007f8d44558218 @rank=2, @suit=:spades>=>12, #<Card:0x007f8d44558b78 @rank=:ace, @suit=:hearts>=>13, #<Card:0x007f8d44557e80 @rank=:king, @suit=:hearts>=>14, #<Card:0x007f8d44557a70 @rank=:queen, @suit=:hearts>=>15, #<Card:0x007f8d445579f8 @rank=:jack, @suit=:hearts>=>16, #<Card:0x007f8d445578b8 @rank=10, @suit=:hearts>=>17, #<Card:0x007f8d44557778 @rank=9, @suit=:hearts>=>18, #<Card:0x007f8d445576b0 @rank=8, @suit=:hearts>=>19, #<Card:0x007f8d44557660 @rank=7, @suit=:hearts>=>20, #<Card:0x007f8d445574f8 @rank=6, @suit=:hearts>=>21, #<Card:0x007f8d445574d0 @rank=5, @suit=:hearts>=>22, #<Card:0x007f8d445573b8 @rank=4, @suit=:hearts>=>23, #<Card:0x007f8d44556f08 @rank=3, @suit=:hearts>=>24, #<Card:0x007f8d44556a58 @rank=2, @suit=:hearts>=>25, #<Card:0x007f8d44556940 @rank=:ace, @suit=:diamonds>=>26, #<Card:0x007f8d44556918 @rank=:king, @suit=:diamonds>=>27, #<Card:0x007f8d445567d8 @rank=:queen, @suit=:diamonds>=>28, #<Card:0x007f8d445567b0 @rank=:jack, @suit=:diamonds>=>29, #<Card:0x007f8d44556788 @rank=10, @suit=:diamonds>=>30, #<Card:0x007f8d44556760 @rank=9, @suit=:diamonds>=>31, #<Card:0x007f8d445565d0 @rank=8, @suit=:diamonds>=>32, #<Card:0x007f8d445565a8 @rank=7, @suit=:diamonds>=>33, #<Card:0x007f8d44556580 @rank=6, @suit=:diamonds>=>34, #<Card:0x007f8d44556558 @rank=5, @suit=:diamonds>=>35, #<Card:0x007f8d445562b0 @rank=4, @suit=:diamonds>=>36, #<Card:0x007f8d44556260 @rank=3, @suit=:diamonds>=>37, #<Card:0x007f8d44556210 @rank=2, @suit=:diamonds>=>38, #<Card:0x007f8d445560a8 @rank=:ace, @suit=:clubs>=>39, #<Card:0x007f8d44555c98 @rank=:king, @suit=:clubs>=>40, #<Card:0x007f8d44555bf8 @rank=:queen, @suit=:clubs>=>41, #<Card:0x007f8d44555ba8 @rank=:jack, @suit=:clubs>=>42, #<Card:0x007f8d44555ae0 @rank=10, @suit=:clubs>=>43, #<Card:0x007f8d44555a40 @rank=9, @suit=:clubs>=>44, #<Card:0x007f8d445559c8 @rank=8, @suit=:clubs>=>45, #<Card:0x007f8d445558b0 @rank=7, @suit=:clubs>=>46, #<Card:0x007f8d44555770 @rank=6, @suit=:clubs>=>47, #<Card:0x007f8d44555518 @rank=5, @suit=:clubs>=>48, #<Card:0x007f8d44554f28 @rank=4, @suit=:clubs>=>49, #<Card:0x007f8d44554b90 @rank=3, @suit=:clubs>=>50, #<Card:0x007f8d44554500 @rank=2, @suit=:clubs>=>51}> to respond to :each
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1xzs6s7/spec.rb:140
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:10:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) WarDeck behaves like a deck fills the deck if no initialize parameters are given
     Failure/Error: expect(deck.to_a).to match_array all_available_cards
     NoMethodError:
       undefined method `each' for #<WarDeck:0x007f8d453ba1f8>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1xzs6s7/spec.rb:140
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:18:in `to_a'
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:18:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) WarDeck behaves like a deck #draw_top_card pops the top-most card
     Failure/Error: expect(small_deck.to_a).to eq [nine_of_clubs]
     NoMethodError:
       undefined method `each' for #<WarDeck:0x007f8d453c46f8>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1xzs6s7/spec.rb:140
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:30:in `to_a'
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:30:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) WarDeck behaves like a deck #draw_bottom_card pops the bottom-most card
     Failure/Error: expect(small_deck.to_a).to eq [ace_of_spades]
     NoMethodError:
       undefined method `each' for #<WarDeck:0x007f8d45413ca8>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1xzs6s7/spec.rb:140
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:37:in `to_a'
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:37:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  5) WarDeck behaves like a deck #top peeks at the top-most card
     Failure/Error: expect(small_deck.to_a).to eq [ace_of_spades, nine_of_clubs]
     NoMethodError:
       undefined method `each' for #<WarDeck:0x007f8d45402110>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1xzs6s7/spec.rb:140
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:44:in `to_a'
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:44:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  6) WarDeck behaves like a deck #bottom peeks at the bottom-most card
     Failure/Error: expect(small_deck.to_a).to eq [ace_of_spades, nine_of_clubs]
     NoMethodError:
       undefined method `each' for #<WarDeck:0x007f8d453f43a8>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1xzs6s7/spec.rb:140
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:51:in `to_a'
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:51:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  7) BeloteDeck behaves like a deck implements Enumerable
     Failure/Error: expect(small_deck).to respond_to(:each)
       expected #<BeloteDeck:0x007f8d452f4980 @cards_in_hand=8, @ranks=[7, 8, 9, :jack, :queen, :king, 10, :ace], @suits=[:spades, :hearts, :diamonds, :clubs], @cards=[#<Card:0x007f8d452f4a48 @rank=:ace, @suit=:spades>, #<Card:0x007f8d452f4a20 @rank=9, @suit=:clubs>], @suit_hash={#<Card:0x007f8d452de6d0 @rank=:ace, @suit=:spades>=>0, #<Card:0x007f8d452de6a8 @rank=10, @suit=:spades>=>1, #<Card:0x007f8d452de658 @rank=:king, @suit=:spades>=>2, #<Card:0x007f8d452de630 @rank=:queen, @suit=:spades>=>3, #<Card:0x007f8d452de5e0 @rank=:jack, @suit=:spades>=>4, #<Card:0x007f8d452de540 @rank=9, @suit=:spades>=>5, #<Card:0x007f8d452de518 @rank=8, @suit=:spades>=>6, #<Card:0x007f8d452de4f0 @rank=7, @suit=:spades>=>7, #<Card:0x007f8d452de4c8 @rank=:ace, @suit=:hearts>=>8, #<Card:0x007f8d452de478 @rank=10, @suit=:hearts>=>9, #<Card:0x007f8d452de450 @rank=:king, @suit=:hearts>=>10, #<Card:0x007f8d452de428 @rank=:queen, @suit=:hearts>=>11, #<Card:0x007f8d452de3d8 @rank=:jack, @suit=:hearts>=>12, #<Card:0x007f8d452de158 @rank=9, @suit=:hearts>=>13, #<Card:0x007f8d452de130 @rank=8, @suit=:hearts>=>14, #<Card:0x007f8d452de108 @rank=7, @suit=:hearts>=>15, #<Card:0x007f8d452de018 @rank=:ace, @suit=:diamonds>=>16, #<Card:0x007f8d452ddff0 @rank=10, @suit=:diamonds>=>17, #<Card:0x007f8d452ddfa0 @rank=:king, @suit=:diamonds>=>18, #<Card:0x007f8d452ddf50 @rank=:queen, @suit=:diamonds>=>19, #<Card:0x007f8d452ddf28 @rank=:jack, @suit=:diamonds>=>20, #<Card:0x007f8d452ddf00 @rank=9, @suit=:diamonds>=>21, #<Card:0x007f8d452dded8 @rank=8, @suit=:diamonds>=>22, #<Card:0x007f8d452ddeb0 @rank=7, @suit=:diamonds>=>23, #<Card:0x007f8d452ddde8 @rank=:ace, @suit=:clubs>=>24, #<Card:0x007f8d452ddcd0 @rank=10, @suit=:clubs>=>25, #<Card:0x007f8d452ddc80 @rank=:king, @suit=:clubs>=>26, #<Card:0x007f8d452ddc58 @rank=:queen, @suit=:clubs>=>27, #<Card:0x007f8d452ddc08 @rank=:jack, @suit=:clubs>=>28, #<Card:0x007f8d452ddb68 @rank=9, @suit=:clubs>=>29, #<Card:0x007f8d452ddb18 @rank=8, @suit=:clubs>=>30, #<Card:0x007f8d452ddaf0 @rank=7, @suit=:clubs>=>31}> to respond to :each
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1xzs6s7/spec.rb:191
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:10:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  8) BeloteDeck behaves like a deck fills the deck if no initialize parameters are given
     Failure/Error: expect(deck.to_a).to match_array all_available_cards
     NoMethodError:
       undefined method `each' for #<BeloteDeck:0x007f8d452b1a90>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1xzs6s7/spec.rb:191
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:18:in `to_a'
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:18:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  9) BeloteDeck behaves like a deck #draw_top_card pops the top-most card
     Failure/Error: expect(small_deck.to_a).to eq [nine_of_clubs]
     NoMethodError:
       undefined method `each' for #<BeloteDeck:0x007f8d45282470>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1xzs6s7/spec.rb:191
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:30:in `to_a'
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:30:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  10) BeloteDeck behaves like a deck #draw_bottom_card pops the bottom-most card
     Failure/Error: expect(small_deck.to_a).to eq [ace_of_spades]
     NoMethodError:
       undefined method `each' for #<BeloteDeck:0x007f8d45274848>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1xzs6s7/spec.rb:191
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:37:in `to_a'
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:37:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  11) BeloteDeck behaves like a deck #top peeks at the top-most card
     Failure/Error: expect(small_deck.to_a).to eq [ace_of_spades, nine_of_clubs]
     NoMethodError:
       undefined method `each' for #<BeloteDeck:0x007f8d4524bce0>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1xzs6s7/spec.rb:191
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:44:in `to_a'
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:44:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  12) BeloteDeck behaves like a deck #bottom peeks at the bottom-most card
     Failure/Error: expect(small_deck.to_a).to eq [ace_of_spades, nine_of_clubs]
     NoMethodError:
       undefined method `each' for #<BeloteDeck:0x007f8d45238f00>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1xzs6s7/spec.rb:191
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:51:in `to_a'
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:51:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  13) BeloteDeck hand #highest_of_suit returns the strongest card of the specified suit
     Failure/Error: expect(hand.highest_of_suit(:clubs)).to eq Card.new(:ace, :clubs)
       
       expected: #<Card:0x007f8d451c5140 @rank=:ace, @suit=:clubs>
            got: :ace
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -#<Card:0x007f8d451c5140 @rank=:ace, @suit=:clubs>
       +:ace
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:232:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  14) SixtySixDeck behaves like a deck implements Enumerable
     Failure/Error: expect(small_deck).to respond_to(:each)
       expected #<SixtySixDeck:0x007f8d44794d10 @cards_in_hand=6, @ranks=[9, :jack, :queen, :king, 10, :ace], @suits=[:spades, :hearts, :diamonds, :clubs], @cards=[#<Card:0x007f8d44794f68 @rank=:ace, @suit=:spades>, #<Card:0x007f8d44794ef0 @rank=9, @suit=:clubs>], @suit_hash={#<Card:0x007f8d4478b350 @rank=:ace, @suit=:spades>=>0, #<Card:0x007f8d4478b260 @rank=10, @suit=:spades>=>1, #<Card:0x007f8d4478b080 @rank=:king, @suit=:spades>=>2, #<Card:0x007f8d4478ae28 @rank=:queen, @suit=:spades>=>3, #<Card:0x007f8d4478aba8 @rank=:jack, @suit=:spades>=>4, #<Card:0x007f8d4478aa90 @rank=9, @suit=:spades>=>5, #<Card:0x007f8d4478a860 @rank=:ace, @suit=:hearts>=>6, #<Card:0x007f8d4478a6f8 @rank=10, @suit=:hearts>=>7, #<Card:0x007f8d4478a478 @rank=:king, @suit=:hearts>=>8, #<Card:0x007f8d4478a360 @rank=:queen, @suit=:hearts>=>9, #<Card:0x007f8d44789988 @rank=:jack, @suit=:hearts>=>10, #<Card:0x007f8d447895f0 @rank=9, @suit=:hearts>=>11, #<Card:0x007f8d44788d30 @rank=:ace, @suit=:diamonds>=>12, #<Card:0x007f8d44788650 @rank=10, @suit=:diamonds>=>13, #<Card:0x007f8d44773de0 @rank=:king, @suit=:diamonds>=>14, #<Card:0x007f8d44773a20 @rank=:queen, @suit=:diamonds>=>15, #<Card:0x007f8d44773890 @rank=:jack, @suit=:diamonds>=>16, #<Card:0x007f8d447734f8 @rank=9, @suit=:diamonds>=>17, #<Card:0x007f8d447733e0 @rank=:ace, @suit=:clubs>=>18, #<Card:0x007f8d44773318 @rank=10, @suit=:clubs>=>19, #<Card:0x007f8d44772e68 @rank=:king, @suit=:clubs>=>20, #<Card:0x007f8d44772d28 @rank=:queen, @suit=:clubs>=>21, #<Card:0x007f8d44772940 @rank=:jack, @suit=:clubs>=>22, #<Card:0x007f8d44772328 @rank=9, @suit=:clubs>=>23}> to respond to :each
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1xzs6s7/spec.rb:400
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:10:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  15) SixtySixDeck behaves like a deck fills the deck if no initialize parameters are given
     Failure/Error: expect(deck.to_a).to match_array all_available_cards
     NoMethodError:
       undefined method `each' for #<SixtySixDeck:0x007f8d44735e50>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1xzs6s7/spec.rb:400
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:18:in `to_a'
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:18:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  16) SixtySixDeck behaves like a deck #draw_top_card pops the top-most card
     Failure/Error: expect(small_deck.to_a).to eq [nine_of_clubs]
     NoMethodError:
       undefined method `each' for #<SixtySixDeck:0x007f8d44570890>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1xzs6s7/spec.rb:400
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:30:in `to_a'
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:30:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  17) SixtySixDeck behaves like a deck #draw_bottom_card pops the bottom-most card
     Failure/Error: expect(small_deck.to_a).to eq [ace_of_spades]
     NoMethodError:
       undefined method `each' for #<SixtySixDeck:0x007f8d44560df0>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1xzs6s7/spec.rb:400
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:37:in `to_a'
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:37:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  18) SixtySixDeck behaves like a deck #top peeks at the top-most card
     Failure/Error: expect(small_deck.to_a).to eq [ace_of_spades, nine_of_clubs]
     NoMethodError:
       undefined method `each' for #<SixtySixDeck:0x007f8d4454bb08>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1xzs6s7/spec.rb:400
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:44:in `to_a'
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:44:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  19) SixtySixDeck behaves like a deck #bottom peeks at the bottom-most card
     Failure/Error: expect(small_deck.to_a).to eq [ace_of_spades, nine_of_clubs]
     NoMethodError:
       undefined method `each' for #<SixtySixDeck:0x007f8d4455fe50>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1xzs6s7/spec.rb:400
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:51:in `to_a'
     # /tmp/d20151112-27349-1xzs6s7/spec.rb:51:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.05002 seconds
57 examples, 19 failures

Failed examples:

rspec /tmp/d20151112-27349-1xzs6s7/spec.rb:8 # WarDeck behaves like a deck implements Enumerable
rspec /tmp/d20151112-27349-1xzs6s7/spec.rb:14 # WarDeck behaves like a deck fills the deck if no initialize parameters are given
rspec /tmp/d20151112-27349-1xzs6s7/spec.rb:28 # WarDeck behaves like a deck #draw_top_card pops the top-most card
rspec /tmp/d20151112-27349-1xzs6s7/spec.rb:35 # WarDeck behaves like a deck #draw_bottom_card pops the bottom-most card
rspec /tmp/d20151112-27349-1xzs6s7/spec.rb:42 # WarDeck behaves like a deck #top peeks at the top-most card
rspec /tmp/d20151112-27349-1xzs6s7/spec.rb:49 # WarDeck behaves like a deck #bottom peeks at the bottom-most card
rspec /tmp/d20151112-27349-1xzs6s7/spec.rb:8 # BeloteDeck behaves like a deck implements Enumerable
rspec /tmp/d20151112-27349-1xzs6s7/spec.rb:14 # BeloteDeck behaves like a deck fills the deck if no initialize parameters are given
rspec /tmp/d20151112-27349-1xzs6s7/spec.rb:28 # BeloteDeck behaves like a deck #draw_top_card pops the top-most card
rspec /tmp/d20151112-27349-1xzs6s7/spec.rb:35 # BeloteDeck behaves like a deck #draw_bottom_card pops the bottom-most card
rspec /tmp/d20151112-27349-1xzs6s7/spec.rb:42 # BeloteDeck behaves like a deck #top peeks at the top-most card
rspec /tmp/d20151112-27349-1xzs6s7/spec.rb:49 # BeloteDeck behaves like a deck #bottom peeks at the bottom-most card
rspec /tmp/d20151112-27349-1xzs6s7/spec.rb:220 # BeloteDeck hand #highest_of_suit returns the strongest card of the specified suit
rspec /tmp/d20151112-27349-1xzs6s7/spec.rb:8 # SixtySixDeck behaves like a deck implements Enumerable
rspec /tmp/d20151112-27349-1xzs6s7/spec.rb:14 # SixtySixDeck behaves like a deck fills the deck if no initialize parameters are given
rspec /tmp/d20151112-27349-1xzs6s7/spec.rb:28 # SixtySixDeck behaves like a deck #draw_top_card pops the top-most card
rspec /tmp/d20151112-27349-1xzs6s7/spec.rb:35 # SixtySixDeck behaves like a deck #draw_bottom_card pops the bottom-most card
rspec /tmp/d20151112-27349-1xzs6s7/spec.rb:42 # SixtySixDeck behaves like a deck #top peeks at the top-most card
rspec /tmp/d20151112-27349-1xzs6s7/spec.rb:49 # SixtySixDeck behaves like a deck #bottom peeks at the bottom-most card

История (1 версия и 0 коментара)

Изтрит обнови решението на 07.11.2015 00:02 (преди около 9 години)

+class Card
+ attr_accessor :rank, :suit
+
+ def initialize(rank, suit)
+ @rank = rank
+ @suit = suit
+ end
+
+ def ==(other)
+ @rank == other.rank and @suit == other.suit
+ end
+ alias :eql? :==
+
+ def hash
+ [@rank, @suit].hash
+ end
+
+ def to_s()
+ "%s of %s" % [@rank.to_s.capitalize, @suit.capitalize]
+ end
+end
+
+class Deck
+ include Enumerable
+
+ class Hand
+ def initialize(cards)
+ @cards = cards
+ end
+
+ def has_rank?(rank, cards = @cards)
+ cards.any? { |card| card.rank == rank }
+ end
+
+ def size
+ @cards.length
+ end
+ end
+
+ def initialize(cards = nil)
+ @suits = [:spades, :hearts, :diamonds, :clubs]
+ rank_suit_pairs = @suits.product(@ranks.reverse)
+ default_cards = rank_suit_pairs.map { |suit, rank| Card.new(rank, suit) }
+ @cards = cards || default_cards
+ @suit_hash = Hash[default_cards.map.with_index.to_a]
+ end
+
+ def size
+ @cards.length
+ end
+
+ def draw_top_card
+ @cards.shift
+ end
+
+ def draw_bottom_card
+ @cards.pop
+ end
+
+ def top_card
+ @cards[0]
+ end
+
+ def bottom_card
+ @cards[-1]
+ end
+
+ def shuffle()
+ @cards.shuffle!
+ end
+
+ def sort
+ @cards.sort! { |a, b| @suit_hash[a] <=> @suit_hash[b] }
+ end
+
+ def to_s()
+ @cards.join("\n")
+ end
+end
+
+class WarDeck < Deck
+ class Hand < Deck::Hand
+ def play_card
+ @cards.pop
+ end
+
+ def allow_face_up?
+ @cards.length <= 3
+ end
+ end
+
+ def initialize(cards = nil)
+ @cards_in_hand = 26
+ @ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
+ super(cards)
+ end
+
+ def deal
+ Hand.new(@cards[0..@cards_in_hand - 1])
+ end
+end
+
+class BeloteDeck < Deck
+ class Hand < Deck::Hand
+ def initialize(cards, ranks)
+ super(cards)
+ @ranks = ranks
+ end
+
+ def highest_of_suit(suit)
+ cards_of_suit = @cards.select { |card| card.suit == suit }
+
+ @ranks.reverse.each do |rank|
+ return rank if cards_of_suit.any? { |card| card.rank == rank }
+ end
+ nil
+ end
+
+ def belote?
+ groups_by_suit = @cards.group_by { |card| card.suit }
+ groups_by_suit.each do |suit, cards|
+ return true if has_rank?(:king, cards) && has_rank?(:queen, cards)
+ end
+
+ false
+ end
+
+ def has_ranks?(ranks, cards)
+ ranks.all? { |rank| has_rank?(rank, cards) }
+ end
+
+ def check_sequence(count, suit, cards)
+ @ranks.each_cons(count) do |ranks|
+ return true if has_ranks?(ranks, cards)
+ end
+ false
+ end
+
+ def has_successive_ranks?(count)
+ groups_by_suit = @cards.group_by { |card| card.suit }
+ groups_by_suit.each do |suit, cards|
+ return true if check_sequence(count, suit, cards)
+ end
+
+ false
+ end
+
+ def tierce?
+ has_successive_ranks?(3)
+ end
+
+ def quarte?
+ has_successive_ranks?(4)
+ end
+
+ def quint?
+ has_successive_ranks?(5)
+ end
+
+ def carre_of_rank?(rank)
+ @cards.count { |card| card.rank == rank } == 4
+ end
+
+ def carre_of_jacks?
+ carre_of_rank?(:jack)
+ end
+
+ def carre_of_nines?
+ carre_of_rank?(9)
+ end
+
+ def carre_of_aces?
+ carre_of_rank?(:ace)
+ end
+ end
+
+ def initialize(cards = nil)
+ @cards_in_hand = 8
+ @ranks = [7, 8, 9, :jack, :queen, :king, 10, :ace]
+ super(cards)
+ end
+
+ def deal
+ Hand.new(@cards[0..@cards_in_hand - 1], @ranks)
+ end
+end
+
+class SixtySixDeck < Deck
+ class Hand < Deck::Hand
+ def twenty?(trump_suit)
+ groups_by_suit = @cards.group_by { |card| card.suit }
+ groups_by_suit.delete(trump_suit)
+ groups_by_suit.each do |suit, cards|
+ return true if has_rank?(:king, cards) &&
+ has_rank?(:queen, cards)
+ end
+
+ false
+ end
+
+ def forty?(trump_suit)
+ groups_by_suit = @cards.group_by { |card| card.suit }
+ trump_suit_cards = groups_by_suit[trump_suit]
+ trump_suit_cards.any? { |card| card.rank == :king } &&
+ trump_suit_cards.any? { |card| card.rank == :queen }
+ end
+ end
+
+ def initialize(cards = nil)
+ @cards_in_hand = 6
+ @ranks = [9, :jack, :queen, :king, 10, :ace]
+ super(cards)
+ end
+
+ def deal
+ Hand.new(@cards[0..@cards_in_hand - 1])
+ end
+end