Решение на Четвърта задача от Мартина Тонковска

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

Към профила на Мартина Тонковска

Резултати

  • 3 точки от тестове
  • 0 бонус точки
  • 3 точки общо
  • 32 успешни тест(а)
  • 25 неуспешни тест(а)

Код

SUITS = [:spades, :hearts, :diamonds, :clubs]
class Card
attr_accessor :rank, :suit
def initialize(rank, suit)
@rank = rank
@suit = suit
end
def to_s
"#{@rank.to_s.capitalize} of #{@suit.capitalize}"
end
def queen?
@rank == :queen
end
def king?
@rank == :king
end
end
class GenericDeck
include Enumerable
RANKS = []
def generate_deck
deck = self.class::RANKS.product(SUITS).map! do |rank, suit|
Card.new(rank, suit)
end
end
def initialize(deck = generate_deck)
@deck = deck
end
def size
@deck.length
end
def draw_top_card
@deck.pop
end
def draw_bottom_card
@deck.shift
end
def top_card
@deck.last
end
def bottom_card
@deck.first
end
def shuffle
@deck.shuffle!
end
def to_s
@deck.map(&:to_s).join("\n")
end
def sort
@deck.sort! do |x, y|
if (SUITS.index(y.suit) <=> SUITS.index(x.suit)) == 0
self.class::RANKS.index(y.rank) <=> self.class::RANKS.index(x.rank)
else
SUITS.index(y.suit) <=> SUITS.index(x.suit)
end
end
end
def each(&block)
@deck.each(&block)
end
end
class Hand
include Enumerable
attr_accessor :cards
def initialize(cards = [])
@cards = cards
end
def get_suit(suit)
@cards.select { |card| card.suit == suit }
end
def has_card?(card_rank)
@cards.any? { |card| card.rank == card_rank }
end
def king_and_queen?(suits)
suites.each do |suite|
current_suite = get_suite(suite)
if current_suite.any?(&:queen?) and current_suite.any?(&:king?)
return true
end
end
false
end
def to_s
@cards.map { |card| card.to_s }.join("\n")
end
def size
@cards.length
end
def each(&block)
@cards.each(&block)
end
end
class WarHand < Hand
def size
@cards.length
end
def play_card
@cards.delete_at(rand(0...@cards.length))
end
def allow_face_up?
@cards.length <= 3
end
end
class WarDeck < GenericDeck
RANKS = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
DEAL_SIZE = 26
def deal
WarHand.new(@deck.pop(DEAL_SIZE))
end
end
class BeloteHand < Hand
def initialize(ranks, cards = [])
@ranks = ranks
super cards
end
def highest_of_suit(suit)
get_suit(suit).sort do |x, y|
@ranks.index(x.rank) <=> @ranks.index(y.rank)
end.last
end
def belote?
king_and_queen?(SUITS)
end
def identify_sequences(length)
SUITS.each do |suit|
current_suit = get_suit(suit).map! { |card| @ranks.index card.rank }.sort!
if current_suit.each_cons(length).any? do |list|
list == (list[0]..list[-1]).to_a
end
return true
end
end
false
end
def tierce?
identify_sequences(3)
end
def quarte?
identify_sequences(4)
end
def quint?
identify_sequences(5)
end
def carre?(card_type)
@cards.select { |card| card.rank == card_type}.count == 4
end
def carre_of_jacks?
carre?(:jack)
end
def carre_of_nines?
carre?(9)
end
def carre_of_aces?
carre?(:ace)
end
end
class BeloteDeck < GenericDeck
RANKS = [7, 8, 9, :jack, :queen, :king, 10, :ace]
DEAL_SIZE = 8
def deal
BeloteHand.new(self.class::RANKS, @deck.pop(DEAL_SIZE))
end
end
class SixtySixHand < Hand
def twenty?(trump_suit)
king_and_queen?(SUITS.select { |suit| suit != trump_suit })
end
def forty?(trump_suit)
king_and_queen?([trump_suit])
end
end
class SixtySixDeck < GenericDeck
RANKS = [9, :jack, :queen, :king, 10, :ace]
DEAL_SIZE = 6
def deal
SixtySixHand.new(@deck.pop(DEAL_SIZE))
end
end

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

..F.F.FFFF..F....F.FFFF..F.FFF..............F.FFFF..F.FFF

Failures:

  1) Card #== compares two cards by their rank and suit
     Failure/Error: expect(Card.new(4, :spades)).to eq Card.new(4, :spades)
       
       expected: #<Card:0x007f4b4ec8af50 @rank=4, @suit=:spades>
            got: #<Card:0x007f4b4ec8b018 @rank=4, @suit=:spades>
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -#<Card:0x007f4b4ec8af50 @rank=4, @suit=:spades>
       +#<Card:0x007f4b4ec8b018 @rank=4, @suit=:spades>
     # /tmp/d20151112-27349-1fd5grw/spec.rb:131: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)>'

  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
       expected collection contained:  [#<Card:0x007f4b4fb3a870 @rank=2, @suit=:clubs>, #<Card:0x007f4b4fb3a848 @rank=3, @suit=:clubs>, #<Card:0x007f4b4fb3a820 @rank=4, @suit=:clubs>, #<Card:0x007f4b4fb3a7d0 @rank=5, @suit=:clubs>, #<Card:0x007f4b4fb3a7a8 @rank=6, @suit=:clubs>, #<Card:0x007f4b4fb3a758 @rank=7, @suit=:clubs>, #<Card:0x007f4b4fb3a730 @rank=8, @suit=:clubs>, #<Card:0x007f4b4fb3a708 @rank=9, @suit=:clubs>, #<Card:0x007f4b4fb3a6b8 @rank=10, @suit=:clubs>, #<Card:0x007f4b4fb3a690 @rank=:jack, @suit=:clubs>, #<Card:0x007f4b4fb3a668 @rank=:queen, @suit=:clubs>, #<Card:0x007f4b4fb3a640 @rank=:king, @suit=:clubs>, #<Card:0x007f4b4fb3a618 @rank=:ace, @suit=:clubs>, #<Card:0x007f4b4fb3a5c8 @rank=2, @suit=:diamonds>, #<Card:0x007f4b4fb3a550 @rank=3, @suit=:diamonds>, #<Card:0x007f4b4fb3a528 @rank=4, @suit=:diamonds>, #<Card:0x007f4b4fb3a500 @rank=5, @suit=:diamonds>, #<Card:0x007f4b4fb3a4d8 @rank=6, @suit=:diamonds>, #<Card:0x007f4b4fb3a4b0 @rank=7, @suit=:diamonds>, #<Card:0x007f4b4fb3a488 @rank=8, @suit=:diamonds>, #<Card:0x007f4b4fb3a460 @rank=9, @suit=:diamonds>, #<Card:0x007f4b4fb3a438 @rank=10, @suit=:diamonds>, #<Card:0x007f4b4fb3a3e8 @rank=:jack, @suit=:diamonds>, #<Card:0x007f4b4fb3a3c0 @rank=:queen, @suit=:diamonds>, #<Card:0x007f4b4fb3a370 @rank=:king, @suit=:diamonds>, #<Card:0x007f4b4fb3a348 @rank=:ace, @suit=:diamonds>, #<Card:0x007f4b4fb3a320 @rank=2, @suit=:hearts>, #<Card:0x007f4b4fb3a2d0 @rank=3, @suit=:hearts>, #<Card:0x007f4b4fb3a280 @rank=4, @suit=:hearts>, #<Card:0x007f4b4fb3a258 @rank=5, @suit=:hearts>, #<Card:0x007f4b4fb3a230 @rank=6, @suit=:hearts>, #<Card:0x007f4b4fb3a208 @rank=7, @suit=:hearts>, #<Card:0x007f4b4fb3a1e0 @rank=8, @suit=:hearts>, #<Card:0x007f4b4fb3a190 @rank=9, @suit=:hearts>, #<Card:0x007f4b4fb3a118 @rank=10, @suit=:hearts>, #<Card:0x007f4b4fb3a0f0 @rank=:jack, @suit=:hearts>, #<Card:0x007f4b4fb3a0c8 @rank=:queen, @suit=:hearts>, #<Card:0x007f4b4fb3a0a0 @rank=:king, @suit=:hearts>, #<Card:0x007f4b4fb3a078 @rank=:ace, @suit=:hearts>, #<Card:0x007f4b4fb3a050 @rank=2, @suit=:spades>, #<Card:0x007f4b4fb3a028 @rank=3, @suit=:spades>, #<Card:0x007f4b4fb3a000 @rank=4, @suit=:spades>, #<Card:0x007f4b4fb39fb0 @rank=5, @suit=:spades>, #<Card:0x007f4b4fb39f88 @rank=6, @suit=:spades>, #<Card:0x007f4b4fb39f38 @rank=7, @suit=:spades>, #<Card:0x007f4b4fb39f10 @rank=8, @suit=:spades>, #<Card:0x007f4b4fb39ee8 @rank=9, @suit=:spades>, #<Card:0x007f4b4fb39e98 @rank=10, @suit=:spades>, #<Card:0x007f4b4fb39e70 @rank=:jack, @suit=:spades>, #<Card:0x007f4b4fb39e48 @rank=:queen, @suit=:spades>, #<Card:0x007f4b4fb39e20 @rank=:king, @suit=:spades>, #<Card:0x007f4b4fb39df8 @rank=:ace, @suit=:spades>]
       actual collection contained:    [#<Card:0x007f4b4fa5ccf0 @rank=2, @suit=:spades>, #<Card:0x007f4b4fa5ccc8 @rank=2, @suit=:hearts>, #<Card:0x007f4b4fa5cca0 @rank=2, @suit=:diamonds>, #<Card:0x007f4b4fa5cc78 @rank=2, @suit=:clubs>, #<Card:0x007f4b4fa5cc50 @rank=3, @suit=:spades>, #<Card:0x007f4b4fa5cc28 @rank=3, @suit=:hearts>, #<Card:0x007f4b4fa5cbd8 @rank=3, @suit=:diamonds>, #<Card:0x007f4b4fa5cbb0 @rank=3, @suit=:clubs>, #<Card:0x007f4b4fa5cb88 @rank=4, @suit=:spades>, #<Card:0x007f4b4fa5cb60 @rank=4, @suit=:hearts>, #<Card:0x007f4b4fa5cb38 @rank=4, @suit=:diamonds>, #<Card:0x007f4b4fa5cb10 @rank=4, @suit=:clubs>, #<Card:0x007f4b4fa5cac0 @rank=5, @suit=:spades>, #<Card:0x007f4b4fa5ca98 @rank=5, @suit=:hearts>, #<Card:0x007f4b4fa5ca70 @rank=5, @suit=:diamonds>, #<Card:0x007f4b4fa5ca48 @rank=5, @suit=:clubs>, #<Card:0x007f4b4fa5ca20 @rank=6, @suit=:spades>, #<Card:0x007f4b4fa5c9d0 @rank=6, @suit=:hearts>, #<Card:0x007f4b4fa5c980 @rank=6, @suit=:diamonds>, #<Card:0x007f4b4fa5c958 @rank=6, @suit=:clubs>, #<Card:0x007f4b4fa5c908 @rank=7, @suit=:spades>, #<Card:0x007f4b4fa5c8e0 @rank=7, @suit=:hearts>, #<Card:0x007f4b4fa5c8b8 @rank=7, @suit=:diamonds>, #<Card:0x007f4b4fa5c868 @rank=7, @suit=:clubs>, #<Card:0x007f4b4fa5c840 @rank=8, @suit=:spades>, #<Card:0x007f4b4fa5c818 @rank=8, @suit=:hearts>, #<Card:0x007f4b4fa5c7f0 @rank=8, @suit=:diamonds>, #<Card:0x007f4b4fa5c7c8 @rank=8, @suit=:clubs>, #<Card:0x007f4b4fa5c7a0 @rank=9, @suit=:spades>, #<Card:0x007f4b4fa5c750 @rank=9, @suit=:hearts>, #<Card:0x007f4b4fa5c728 @rank=9, @suit=:diamonds>, #<Card:0x007f4b4fa5c700 @rank=9, @suit=:clubs>, #<Card:0x007f4b4fa5c6d8 @rank=10, @suit=:spades>, #<Card:0x007f4b4fa5c6b0 @rank=10, @suit=:hearts>, #<Card:0x007f4b4fa5c688 @rank=10, @suit=:diamonds>, #<Card:0x007f4b4fa5c638 @rank=10, @suit=:clubs>, #<Card:0x007f4b4fa5c5e8 @rank=:jack, @suit=:spades>, #<Card:0x007f4b4fa5c5c0 @rank=:jack, @suit=:hearts>, #<Card:0x007f4b4fa5c598 @rank=:jack, @suit=:diamonds>, #<Card:0x007f4b4fa5c570 @rank=:jack, @suit=:clubs>, #<Card:0x007f4b4fa5c548 @rank=:queen, @suit=:spades>, #<Card:0x007f4b4fa5c520 @rank=:queen, @suit=:hearts>, #<Card:0x007f4b4fa5c4d0 @rank=:queen, @suit=:diamonds>, #<Card:0x007f4b4fa5c4a8 @rank=:queen, @suit=:clubs>, #<Card:0x007f4b4fa5c480 @rank=:king, @suit=:spades>, #<Card:0x007f4b4fa5c458 @rank=:king, @suit=:hearts>, #<Card:0x007f4b4fa5c430 @rank=:king, @suit=:diamonds>, #<Card:0x007f4b4fa5c408 @rank=:king, @suit=:clubs>, #<Card:0x007f4b4fa5c3b8 @rank=:ace, @suit=:spades>, #<Card:0x007f4b4fa5c390 @rank=:ace, @suit=:hearts>, #<Card:0x007f4b4fa5c368 @rank=:ace, @suit=:diamonds>, #<Card:0x007f4b4fa5c340 @rank=:ace, @suit=:clubs>]
       the missing elements were:      [#<Card:0x007f4b4fb3a870 @rank=2, @suit=:clubs>, #<Card:0x007f4b4fb3a848 @rank=3, @suit=:clubs>, #<Card:0x007f4b4fb3a820 @rank=4, @suit=:clubs>, #<Card:0x007f4b4fb3a7d0 @rank=5, @suit=:clubs>, #<Card:0x007f4b4fb3a7a8 @rank=6, @suit=:clubs>, #<Card:0x007f4b4fb3a758 @rank=7, @suit=:clubs>, #<Card:0x007f4b4fb3a730 @rank=8, @suit=:clubs>, #<Card:0x007f4b4fb3a708 @rank=9, @suit=:clubs>, #<Card:0x007f4b4fb3a6b8 @rank=10, @suit=:clubs>, #<Card:0x007f4b4fb3a690 @rank=:jack, @suit=:clubs>, #<Card:0x007f4b4fb3a668 @rank=:queen, @suit=:clubs>, #<Card:0x007f4b4fb3a640 @rank=:king, @suit=:clubs>, #<Card:0x007f4b4fb3a618 @rank=:ace, @suit=:clubs>, #<Card:0x007f4b4fb3a5c8 @rank=2, @suit=:diamonds>, #<Card:0x007f4b4fb3a550 @rank=3, @suit=:diamonds>, #<Card:0x007f4b4fb3a528 @rank=4, @suit=:diamonds>, #<Card:0x007f4b4fb3a500 @rank=5, @suit=:diamonds>, #<Card:0x007f4b4fb3a4d8 @rank=6, @suit=:diamonds>, #<Card:0x007f4b4fb3a4b0 @rank=7, @suit=:diamonds>, #<Card:0x007f4b4fb3a488 @rank=8, @suit=:diamonds>, #<Card:0x007f4b4fb3a460 @rank=9, @suit=:diamonds>, #<Card:0x007f4b4fb3a438 @rank=10, @suit=:diamonds>, #<Card:0x007f4b4fb3a3e8 @rank=:jack, @suit=:diamonds>, #<Card:0x007f4b4fb3a3c0 @rank=:queen, @suit=:diamonds>, #<Card:0x007f4b4fb3a370 @rank=:king, @suit=:diamonds>, #<Card:0x007f4b4fb3a348 @rank=:ace, @suit=:diamonds>, #<Card:0x007f4b4fb3a320 @rank=2, @suit=:hearts>, #<Card:0x007f4b4fb3a2d0 @rank=3, @suit=:hearts>, #<Card:0x007f4b4fb3a280 @rank=4, @suit=:hearts>, #<Card:0x007f4b4fb3a258 @rank=5, @suit=:hearts>, #<Card:0x007f4b4fb3a230 @rank=6, @suit=:hearts>, #<Card:0x007f4b4fb3a208 @rank=7, @suit=:hearts>, #<Card:0x007f4b4fb3a1e0 @rank=8, @suit=:hearts>, #<Card:0x007f4b4fb3a190 @rank=9, @suit=:hearts>, #<Card:0x007f4b4fb3a118 @rank=10, @suit=:hearts>, #<Card:0x007f4b4fb3a0f0 @rank=:jack, @suit=:hearts>, #<Card:0x007f4b4fb3a0c8 @rank=:queen, @suit=:hearts>, #<Card:0x007f4b4fb3a0a0 @rank=:king, @suit=:hearts>, #<Card:0x007f4b4fb3a078 @rank=:ace, @suit=:hearts>, #<Card:0x007f4b4fb3a050 @rank=2, @suit=:spades>, #<Card:0x007f4b4fb3a028 @rank=3, @suit=:spades>, #<Card:0x007f4b4fb3a000 @rank=4, @suit=:spades>, #<Card:0x007f4b4fb39fb0 @rank=5, @suit=:spades>, #<Card:0x007f4b4fb39f88 @rank=6, @suit=:spades>, #<Card:0x007f4b4fb39f38 @rank=7, @suit=:spades>, #<Card:0x007f4b4fb39f10 @rank=8, @suit=:spades>, #<Card:0x007f4b4fb39ee8 @rank=9, @suit=:spades>, #<Card:0x007f4b4fb39e98 @rank=10, @suit=:spades>, #<Card:0x007f4b4fb39e70 @rank=:jack, @suit=:spades>, #<Card:0x007f4b4fb39e48 @rank=:queen, @suit=:spades>, #<Card:0x007f4b4fb39e20 @rank=:king, @suit=:spades>, #<Card:0x007f4b4fb39df8 @rank=:ace, @suit=:spades>]
       the extra elements were:        [#<Card:0x007f4b4fa5ccf0 @rank=2, @suit=:spades>, #<Card:0x007f4b4fa5ccc8 @rank=2, @suit=:hearts>, #<Card:0x007f4b4fa5cca0 @rank=2, @suit=:diamonds>, #<Card:0x007f4b4fa5cc78 @rank=2, @suit=:clubs>, #<Card:0x007f4b4fa5cc50 @rank=3, @suit=:spades>, #<Card:0x007f4b4fa5cc28 @rank=3, @suit=:hearts>, #<Card:0x007f4b4fa5cbd8 @rank=3, @suit=:diamonds>, #<Card:0x007f4b4fa5cbb0 @rank=3, @suit=:clubs>, #<Card:0x007f4b4fa5cb88 @rank=4, @suit=:spades>, #<Card:0x007f4b4fa5cb60 @rank=4, @suit=:hearts>, #<Card:0x007f4b4fa5cb38 @rank=4, @suit=:diamonds>, #<Card:0x007f4b4fa5cb10 @rank=4, @suit=:clubs>, #<Card:0x007f4b4fa5cac0 @rank=5, @suit=:spades>, #<Card:0x007f4b4fa5ca98 @rank=5, @suit=:hearts>, #<Card:0x007f4b4fa5ca70 @rank=5, @suit=:diamonds>, #<Card:0x007f4b4fa5ca48 @rank=5, @suit=:clubs>, #<Card:0x007f4b4fa5ca20 @rank=6, @suit=:spades>, #<Card:0x007f4b4fa5c9d0 @rank=6, @suit=:hearts>, #<Card:0x007f4b4fa5c980 @rank=6, @suit=:diamonds>, #<Card:0x007f4b4fa5c958 @rank=6, @suit=:clubs>, #<Card:0x007f4b4fa5c908 @rank=7, @suit=:spades>, #<Card:0x007f4b4fa5c8e0 @rank=7, @suit=:hearts>, #<Card:0x007f4b4fa5c8b8 @rank=7, @suit=:diamonds>, #<Card:0x007f4b4fa5c868 @rank=7, @suit=:clubs>, #<Card:0x007f4b4fa5c840 @rank=8, @suit=:spades>, #<Card:0x007f4b4fa5c818 @rank=8, @suit=:hearts>, #<Card:0x007f4b4fa5c7f0 @rank=8, @suit=:diamonds>, #<Card:0x007f4b4fa5c7c8 @rank=8, @suit=:clubs>, #<Card:0x007f4b4fa5c7a0 @rank=9, @suit=:spades>, #<Card:0x007f4b4fa5c750 @rank=9, @suit=:hearts>, #<Card:0x007f4b4fa5c728 @rank=9, @suit=:diamonds>, #<Card:0x007f4b4fa5c700 @rank=9, @suit=:clubs>, #<Card:0x007f4b4fa5c6d8 @rank=10, @suit=:spades>, #<Card:0x007f4b4fa5c6b0 @rank=10, @suit=:hearts>, #<Card:0x007f4b4fa5c688 @rank=10, @suit=:diamonds>, #<Card:0x007f4b4fa5c638 @rank=10, @suit=:clubs>, #<Card:0x007f4b4fa5c5e8 @rank=:jack, @suit=:spades>, #<Card:0x007f4b4fa5c5c0 @rank=:jack, @suit=:hearts>, #<Card:0x007f4b4fa5c598 @rank=:jack, @suit=:diamonds>, #<Card:0x007f4b4fa5c570 @rank=:jack, @suit=:clubs>, #<Card:0x007f4b4fa5c548 @rank=:queen, @suit=:spades>, #<Card:0x007f4b4fa5c520 @rank=:queen, @suit=:hearts>, #<Card:0x007f4b4fa5c4d0 @rank=:queen, @suit=:diamonds>, #<Card:0x007f4b4fa5c4a8 @rank=:queen, @suit=:clubs>, #<Card:0x007f4b4fa5c480 @rank=:king, @suit=:spades>, #<Card:0x007f4b4fa5c458 @rank=:king, @suit=:hearts>, #<Card:0x007f4b4fa5c430 @rank=:king, @suit=:diamonds>, #<Card:0x007f4b4fa5c408 @rank=:king, @suit=:clubs>, #<Card:0x007f4b4fa5c3b8 @rank=:ace, @suit=:spades>, #<Card:0x007f4b4fa5c390 @rank=:ace, @suit=:hearts>, #<Card:0x007f4b4fa5c368 @rank=:ace, @suit=:diamonds>, #<Card:0x007f4b4fa5c340 @rank=:ace, @suit=:clubs>]
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1fd5grw/spec.rb:140
     # /tmp/d20151112-27349-1fd5grw/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.draw_top_card).to eq ace_of_spades
       
       expected: #<Card:0x007f4b4fa0c688 @rank=:ace, @suit=:spades>
            got: #<Card:0x007f4b4fa0c660 @rank=9, @suit=:clubs>
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -#<Card:0x007f4b4fa0c688 @rank=:ace, @suit=:spades>
       +#<Card:0x007f4b4fa0c660 @rank=9, @suit=:clubs>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1fd5grw/spec.rb:140
     # /tmp/d20151112-27349-1fd5grw/spec.rb:29: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.draw_bottom_card).to eq nine_of_clubs
       
       expected: #<Card:0x007f4b4fb2e098 @rank=9, @suit=:clubs>
            got: #<Card:0x007f4b4fb2e0c0 @rank=:ace, @suit=:spades>
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -#<Card:0x007f4b4fb2e098 @rank=9, @suit=:clubs>
       +#<Card:0x007f4b4fb2e0c0 @rank=:ace, @suit=:spades>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1fd5grw/spec.rb:140
     # /tmp/d20151112-27349-1fd5grw/spec.rb:36: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.top_card).to eq ace_of_spades
       
       expected: #<Card:0x007f4b4fb17a00 @rank=:ace, @suit=:spades>
            got: #<Card:0x007f4b4fb179b0 @rank=9, @suit=:clubs>
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -#<Card:0x007f4b4fb17a00 @rank=:ace, @suit=:spades>
       +#<Card:0x007f4b4fb179b0 @rank=9, @suit=:clubs>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1fd5grw/spec.rb:140
     # /tmp/d20151112-27349-1fd5grw/spec.rb:43: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.bottom_card).to eq nine_of_clubs
       
       expected: #<Card:0x007f4b4fad8d28 @rank=9, @suit=:clubs>
            got: #<Card:0x007f4b4fad8d50 @rank=:ace, @suit=:spades>
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -#<Card:0x007f4b4fad8d28 @rank=9, @suit=:clubs>
       +#<Card:0x007f4b4fad8d50 @rank=:ace, @suit=:spades>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1fd5grw/spec.rb:140
     # /tmp/d20151112-27349-1fd5grw/spec.rb:50: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) WarDeck #sort sorts the cards in the defined order
     Failure/Error: expect(deck.sort.to_a).to eq [jack_of_spades, ten_of_hearts, ace_of_clubs, two_of_clubs]
       
       expected: [#<Card:0x007f4b4faae1b8 @rank=:jack, @suit=:spades>, #<Card:0x007f4b4faae118 @rank=10, @suit=:hearts>, #<Card:0x007f4b4faae258 @rank=:ace, @suit=:clubs>, #<Card:0x007f4b4faae168 @rank=2, @suit=:clubs>]
            got: [#<Card:0x007f4b4faae258 @rank=:ace, @suit=:clubs>, #<Card:0x007f4b4faae168 @rank=2, @suit=:clubs>, #<Card:0x007f4b4faae118 @rank=10, @suit=:hearts>, #<Card:0x007f4b4faae1b8 @rank=:jack, @suit=:spades>]
       
       (compared using ==)
       
       Diff:
       
       @@ -1,5 +1,5 @@
       -[#<Card:0x007f4b4faae1b8 @rank=:jack, @suit=:spades>,
       +[#<Card:0x007f4b4faae258 @rank=:ace, @suit=:clubs>,
       + #<Card:0x007f4b4faae168 @rank=2, @suit=:clubs>,
         #<Card:0x007f4b4faae118 @rank=10, @suit=:hearts>,
       - #<Card:0x007f4b4faae258 @rank=:ace, @suit=:clubs>,
       - #<Card:0x007f4b4faae168 @rank=2, @suit=:clubs>]
       + #<Card:0x007f4b4faae1b8 @rank=:jack, @suit=:spades>]
     # /tmp/d20151112-27349-1fd5grw/spec.rb:155: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)>'

  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
       expected collection contained:  [#<Card:0x007f4b4f9c6098 @rank=7, @suit=:clubs>, #<Card:0x007f4b4f9c6070 @rank=8, @suit=:clubs>, #<Card:0x007f4b4f9c6048 @rank=9, @suit=:clubs>, #<Card:0x007f4b4f9c6020 @rank=:jack, @suit=:clubs>, #<Card:0x007f4b4f9c5ff8 @rank=:queen, @suit=:clubs>, #<Card:0x007f4b4f9c5f30 @rank=:king, @suit=:clubs>, #<Card:0x007f4b4f9c5f08 @rank=10, @suit=:clubs>, #<Card:0x007f4b4f9c5eb8 @rank=:ace, @suit=:clubs>, #<Card:0x007f4b4f9c5e90 @rank=7, @suit=:diamonds>, #<Card:0x007f4b4f9c5da0 @rank=8, @suit=:diamonds>, #<Card:0x007f4b4f9c5d78 @rank=9, @suit=:diamonds>, #<Card:0x007f4b4f9c5cd8 @rank=:jack, @suit=:diamonds>, #<Card:0x007f4b4f9c5cb0 @rank=:queen, @suit=:diamonds>, #<Card:0x007f4b4f9c5c88 @rank=:king, @suit=:diamonds>, #<Card:0x007f4b4f9c5c10 @rank=10, @suit=:diamonds>, #<Card:0x007f4b4f9c5be8 @rank=:ace, @suit=:diamonds>, #<Card:0x007f4b4f9c5bc0 @rank=7, @suit=:hearts>, #<Card:0x007f4b4f9c5b98 @rank=8, @suit=:hearts>, #<Card:0x007f4b4f9c5b20 @rank=9, @suit=:hearts>, #<Card:0x007f4b4f9c5aa8 @rank=:jack, @suit=:hearts>, #<Card:0x007f4b4f9c59e0 @rank=:queen, @suit=:hearts>, #<Card:0x007f4b4f9c59b8 @rank=:king, @suit=:hearts>, #<Card:0x007f4b4f9c5968 @rank=10, @suit=:hearts>, #<Card:0x007f4b4f9c5940 @rank=:ace, @suit=:hearts>, #<Card:0x007f4b4f9c58a0 @rank=7, @suit=:spades>, #<Card:0x007f4b4f9c5760 @rank=8, @suit=:spades>, #<Card:0x007f4b4f9c5738 @rank=9, @suit=:spades>, #<Card:0x007f4b4f9c5710 @rank=:jack, @suit=:spades>, #<Card:0x007f4b4f9c5670 @rank=:queen, @suit=:spades>, #<Card:0x007f4b4f9c5648 @rank=:king, @suit=:spades>, #<Card:0x007f4b4f9c55f8 @rank=10, @suit=:spades>, #<Card:0x007f4b4f9c5530 @rank=:ace, @suit=:spades>]
       actual collection contained:    [#<Card:0x007f4b4f9c6ed0 @rank=7, @suit=:spades>, #<Card:0x007f4b4f9c6ea8 @rank=7, @suit=:hearts>, #<Card:0x007f4b4f9c6e80 @rank=7, @suit=:diamonds>, #<Card:0x007f4b4f9c6e58 @rank=7, @suit=:clubs>, #<Card:0x007f4b4f9c6e30 @rank=8, @suit=:spades>, #<Card:0x007f4b4f9c6e08 @rank=8, @suit=:hearts>, #<Card:0x007f4b4f9c6db8 @rank=8, @suit=:diamonds>, #<Card:0x007f4b4f9c6d90 @rank=8, @suit=:clubs>, #<Card:0x007f4b4f9c6d68 @rank=9, @suit=:spades>, #<Card:0x007f4b4f9c6d40 @rank=9, @suit=:hearts>, #<Card:0x007f4b4f9c6d18 @rank=9, @suit=:diamonds>, #<Card:0x007f4b4f9c6cf0 @rank=9, @suit=:clubs>, #<Card:0x007f4b4f9c6cc8 @rank=:jack, @suit=:spades>, #<Card:0x007f4b4f9c6ca0 @rank=:jack, @suit=:hearts>, #<Card:0x007f4b4f9c6c78 @rank=:jack, @suit=:diamonds>, #<Card:0x007f4b4f9c6c50 @rank=:jack, @suit=:clubs>, #<Card:0x007f4b4f9c6c28 @rank=:queen, @suit=:spades>, #<Card:0x007f4b4f9c6bd8 @rank=:queen, @suit=:hearts>, #<Card:0x007f4b4f9c6bb0 @rank=:queen, @suit=:diamonds>, #<Card:0x007f4b4f9c6b60 @rank=:queen, @suit=:clubs>, #<Card:0x007f4b4f9c6b38 @rank=:king, @suit=:spades>, #<Card:0x007f4b4f9c6a70 @rank=:king, @suit=:hearts>, #<Card:0x007f4b4f9c6a48 @rank=:king, @suit=:diamonds>, #<Card:0x007f4b4f9c6a20 @rank=:king, @suit=:clubs>, #<Card:0x007f4b4f9c69a8 @rank=10, @suit=:spades>, #<Card:0x007f4b4f9c6980 @rank=10, @suit=:hearts>, #<Card:0x007f4b4f9c6958 @rank=10, @suit=:diamonds>, #<Card:0x007f4b4f9c6908 @rank=10, @suit=:clubs>, #<Card:0x007f4b4f9c68e0 @rank=:ace, @suit=:spades>, #<Card:0x007f4b4f9c68b8 @rank=:ace, @suit=:hearts>, #<Card:0x007f4b4f9c6890 @rank=:ace, @suit=:diamonds>, #<Card:0x007f4b4f9c6868 @rank=:ace, @suit=:clubs>]
       the missing elements were:      [#<Card:0x007f4b4f9c6098 @rank=7, @suit=:clubs>, #<Card:0x007f4b4f9c6070 @rank=8, @suit=:clubs>, #<Card:0x007f4b4f9c6048 @rank=9, @suit=:clubs>, #<Card:0x007f4b4f9c6020 @rank=:jack, @suit=:clubs>, #<Card:0x007f4b4f9c5ff8 @rank=:queen, @suit=:clubs>, #<Card:0x007f4b4f9c5f30 @rank=:king, @suit=:clubs>, #<Card:0x007f4b4f9c5f08 @rank=10, @suit=:clubs>, #<Card:0x007f4b4f9c5eb8 @rank=:ace, @suit=:clubs>, #<Card:0x007f4b4f9c5e90 @rank=7, @suit=:diamonds>, #<Card:0x007f4b4f9c5da0 @rank=8, @suit=:diamonds>, #<Card:0x007f4b4f9c5d78 @rank=9, @suit=:diamonds>, #<Card:0x007f4b4f9c5cd8 @rank=:jack, @suit=:diamonds>, #<Card:0x007f4b4f9c5cb0 @rank=:queen, @suit=:diamonds>, #<Card:0x007f4b4f9c5c88 @rank=:king, @suit=:diamonds>, #<Card:0x007f4b4f9c5c10 @rank=10, @suit=:diamonds>, #<Card:0x007f4b4f9c5be8 @rank=:ace, @suit=:diamonds>, #<Card:0x007f4b4f9c5bc0 @rank=7, @suit=:hearts>, #<Card:0x007f4b4f9c5b98 @rank=8, @suit=:hearts>, #<Card:0x007f4b4f9c5b20 @rank=9, @suit=:hearts>, #<Card:0x007f4b4f9c5aa8 @rank=:jack, @suit=:hearts>, #<Card:0x007f4b4f9c59e0 @rank=:queen, @suit=:hearts>, #<Card:0x007f4b4f9c59b8 @rank=:king, @suit=:hearts>, #<Card:0x007f4b4f9c5968 @rank=10, @suit=:hearts>, #<Card:0x007f4b4f9c5940 @rank=:ace, @suit=:hearts>, #<Card:0x007f4b4f9c58a0 @rank=7, @suit=:spades>, #<Card:0x007f4b4f9c5760 @rank=8, @suit=:spades>, #<Card:0x007f4b4f9c5738 @rank=9, @suit=:spades>, #<Card:0x007f4b4f9c5710 @rank=:jack, @suit=:spades>, #<Card:0x007f4b4f9c5670 @rank=:queen, @suit=:spades>, #<Card:0x007f4b4f9c5648 @rank=:king, @suit=:spades>, #<Card:0x007f4b4f9c55f8 @rank=10, @suit=:spades>, #<Card:0x007f4b4f9c5530 @rank=:ace, @suit=:spades>]
       the extra elements were:        [#<Card:0x007f4b4f9c6ed0 @rank=7, @suit=:spades>, #<Card:0x007f4b4f9c6ea8 @rank=7, @suit=:hearts>, #<Card:0x007f4b4f9c6e80 @rank=7, @suit=:diamonds>, #<Card:0x007f4b4f9c6e58 @rank=7, @suit=:clubs>, #<Card:0x007f4b4f9c6e30 @rank=8, @suit=:spades>, #<Card:0x007f4b4f9c6e08 @rank=8, @suit=:hearts>, #<Card:0x007f4b4f9c6db8 @rank=8, @suit=:diamonds>, #<Card:0x007f4b4f9c6d90 @rank=8, @suit=:clubs>, #<Card:0x007f4b4f9c6d68 @rank=9, @suit=:spades>, #<Card:0x007f4b4f9c6d40 @rank=9, @suit=:hearts>, #<Card:0x007f4b4f9c6d18 @rank=9, @suit=:diamonds>, #<Card:0x007f4b4f9c6cf0 @rank=9, @suit=:clubs>, #<Card:0x007f4b4f9c6cc8 @rank=:jack, @suit=:spades>, #<Card:0x007f4b4f9c6ca0 @rank=:jack, @suit=:hearts>, #<Card:0x007f4b4f9c6c78 @rank=:jack, @suit=:diamonds>, #<Card:0x007f4b4f9c6c50 @rank=:jack, @suit=:clubs>, #<Card:0x007f4b4f9c6c28 @rank=:queen, @suit=:spades>, #<Card:0x007f4b4f9c6bd8 @rank=:queen, @suit=:hearts>, #<Card:0x007f4b4f9c6bb0 @rank=:queen, @suit=:diamonds>, #<Card:0x007f4b4f9c6b60 @rank=:queen, @suit=:clubs>, #<Card:0x007f4b4f9c6b38 @rank=:king, @suit=:spades>, #<Card:0x007f4b4f9c6a70 @rank=:king, @suit=:hearts>, #<Card:0x007f4b4f9c6a48 @rank=:king, @suit=:diamonds>, #<Card:0x007f4b4f9c6a20 @rank=:king, @suit=:clubs>, #<Card:0x007f4b4f9c69a8 @rank=10, @suit=:spades>, #<Card:0x007f4b4f9c6980 @rank=10, @suit=:hearts>, #<Card:0x007f4b4f9c6958 @rank=10, @suit=:diamonds>, #<Card:0x007f4b4f9c6908 @rank=10, @suit=:clubs>, #<Card:0x007f4b4f9c68e0 @rank=:ace, @suit=:spades>, #<Card:0x007f4b4f9c68b8 @rank=:ace, @suit=:hearts>, #<Card:0x007f4b4f9c6890 @rank=:ace, @suit=:diamonds>, #<Card:0x007f4b4f9c6868 @rank=:ace, @suit=:clubs>]
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1fd5grw/spec.rb:191
     # /tmp/d20151112-27349-1fd5grw/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.draw_top_card).to eq ace_of_spades
       
       expected: #<Card:0x007f4b4f981ec0 @rank=:ace, @suit=:spades>
            got: #<Card:0x007f4b4f981e98 @rank=9, @suit=:clubs>
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -#<Card:0x007f4b4f981ec0 @rank=:ace, @suit=:spades>
       +#<Card:0x007f4b4f981e98 @rank=9, @suit=:clubs>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1fd5grw/spec.rb:191
     # /tmp/d20151112-27349-1fd5grw/spec.rb:29: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.draw_bottom_card).to eq nine_of_clubs
       
       expected: #<Card:0x007f4b4f9561f8 @rank=9, @suit=:clubs>
            got: #<Card:0x007f4b4f956248 @rank=:ace, @suit=:spades>
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -#<Card:0x007f4b4f9561f8 @rank=9, @suit=:clubs>
       +#<Card:0x007f4b4f956248 @rank=:ace, @suit=:spades>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1fd5grw/spec.rb:191
     # /tmp/d20151112-27349-1fd5grw/spec.rb:36: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.top_card).to eq ace_of_spades
       
       expected: #<Card:0x007f4b4f92b2c8 @rank=:ace, @suit=:spades>
            got: #<Card:0x007f4b4f92b2a0 @rank=9, @suit=:clubs>
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -#<Card:0x007f4b4f92b2c8 @rank=:ace, @suit=:spades>
       +#<Card:0x007f4b4f92b2a0 @rank=9, @suit=:clubs>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1fd5grw/spec.rb:191
     # /tmp/d20151112-27349-1fd5grw/spec.rb:43: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.bottom_card).to eq nine_of_clubs
       
       expected: #<Card:0x007f4b4f908e30 @rank=9, @suit=:clubs>
            got: #<Card:0x007f4b4f908e80 @rank=:ace, @suit=:spades>
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -#<Card:0x007f4b4f908e30 @rank=9, @suit=:clubs>
       +#<Card:0x007f4b4f908e80 @rank=:ace, @suit=:spades>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1fd5grw/spec.rb:191
     # /tmp/d20151112-27349-1fd5grw/spec.rb:50: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 #sort sorts the cards in the defined order
     Failure/Error: expect(deck.sort.to_a).to eq [jack_of_spades, ten_of_hearts, ace_of_clubs, seven_of_clubs]
       
       expected: [#<Card:0x007f4b4f8daaf8 @rank=:jack, @suit=:spades>, #<Card:0x007f4b4f8daa30 @rank=10, @suit=:hearts>, #<Card:0x007f4b4f8dab20 @rank=:ace, @suit=:clubs>, #<Card:0x007f4b4f8daaa8 @rank=7, @suit=:clubs>]
            got: [#<Card:0x007f4b4f8dab20 @rank=:ace, @suit=:clubs>, #<Card:0x007f4b4f8daaa8 @rank=7, @suit=:clubs>, #<Card:0x007f4b4f8daa30 @rank=10, @suit=:hearts>, #<Card:0x007f4b4f8daaf8 @rank=:jack, @suit=:spades>]
       
       (compared using ==)
       
       Diff:
       
       @@ -1,5 +1,5 @@
       -[#<Card:0x007f4b4f8daaf8 @rank=:jack, @suit=:spades>,
       +[#<Card:0x007f4b4f8dab20 @rank=:ace, @suit=:clubs>,
       + #<Card:0x007f4b4f8daaa8 @rank=7, @suit=:clubs>,
         #<Card:0x007f4b4f8daa30 @rank=10, @suit=:hearts>,
       - #<Card:0x007f4b4f8dab20 @rank=:ace, @suit=:clubs>,
       - #<Card:0x007f4b4f8daaa8 @rank=7, @suit=:clubs>]
       + #<Card:0x007f4b4f8daaf8 @rank=:jack, @suit=:spades>]
     # /tmp/d20151112-27349-1fd5grw/spec.rb:206: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)>'

  14) 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:0x007f4b4f840728 @rank=:ace, @suit=:clubs>
            got: #<Card:0x007f4b4f8402a0 @rank=:ace, @suit=:clubs>
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -#<Card:0x007f4b4f840728 @rank=:ace, @suit=:clubs>
       +#<Card:0x007f4b4f8402a0 @rank=:ace, @suit=:clubs>
     # /tmp/d20151112-27349-1fd5grw/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)>'

  15) BeloteDeck hand #belote? returns true if there is a king and a queen of the same suit
     Failure/Error: expect(hand.belote?).to be true
     NameError:
       undefined local variable or method `suites' for #<BeloteHand:0x007f4b4f800128>
     # /tmp/d20151112-27349-1fd5grw/solution.rb:98:in `king_and_queen?'
     # /tmp/d20151112-27349-1fd5grw/solution.rb:156:in `belote?'
     # /tmp/d20151112-27349-1fd5grw/spec.rb:251: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)>'

  16) BeloteDeck hand #belote? returns false when there is no king and queen of the same suit
     Failure/Error: expect(hand.belote?).to be false
     NameError:
       undefined local variable or method `suites' for #<BeloteHand:0x007f4b4f64a090>
     # /tmp/d20151112-27349-1fd5grw/solution.rb:98:in `king_and_queen?'
     # /tmp/d20151112-27349-1fd5grw/solution.rb:156:in `belote?'
     # /tmp/d20151112-27349-1fd5grw/spec.rb:266: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)>'

  17) 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
       expected collection contained:  [#<Card:0x007f4b4effefb0 @rank=9, @suit=:clubs>, #<Card:0x007f4b4effef60 @rank=:jack, @suit=:clubs>, #<Card:0x007f4b4effef38 @rank=:queen, @suit=:clubs>, #<Card:0x007f4b4effeee8 @rank=:king, @suit=:clubs>, #<Card:0x007f4b4effeec0 @rank=10, @suit=:clubs>, #<Card:0x007f4b4effee98 @rank=:ace, @suit=:clubs>, #<Card:0x007f4b4effedd0 @rank=9, @suit=:diamonds>, #<Card:0x007f4b4effeda8 @rank=:jack, @suit=:diamonds>, #<Card:0x007f4b4effed80 @rank=:queen, @suit=:diamonds>, #<Card:0x007f4b4effed58 @rank=:king, @suit=:diamonds>, #<Card:0x007f4b4effed08 @rank=10, @suit=:diamonds>, #<Card:0x007f4b4effece0 @rank=:ace, @suit=:diamonds>, #<Card:0x007f4b4effecb8 @rank=9, @suit=:hearts>, #<Card:0x007f4b4effeba0 @rank=:jack, @suit=:hearts>, #<Card:0x007f4b4effe9c0 @rank=:queen, @suit=:hearts>, #<Card:0x007f4b4effe970 @rank=:king, @suit=:hearts>, #<Card:0x007f4b4effe920 @rank=10, @suit=:hearts>, #<Card:0x007f4b4effe8a8 @rank=:ace, @suit=:hearts>, #<Card:0x007f4b4effe768 @rank=9, @suit=:spades>, #<Card:0x007f4b4effe128 @rank=:jack, @suit=:spades>, #<Card:0x007f4b4efe29c8 @rank=:queen, @suit=:spades>, #<Card:0x007f4b4efe2950 @rank=:king, @suit=:spades>, #<Card:0x007f4b4efe2798 @rank=10, @suit=:spades>, #<Card:0x007f4b4efe26f8 @rank=:ace, @suit=:spades>]
       actual collection contained:    [#<Card:0x007f4b4f010d78 @rank=9, @suit=:spades>, #<Card:0x007f4b4f010d28 @rank=9, @suit=:hearts>, #<Card:0x007f4b4f010d00 @rank=9, @suit=:diamonds>, #<Card:0x007f4b4f010cd8 @rank=9, @suit=:clubs>, #<Card:0x007f4b4f010c88 @rank=:jack, @suit=:spades>, #<Card:0x007f4b4f010c60 @rank=:jack, @suit=:hearts>, #<Card:0x007f4b4f010be8 @rank=:jack, @suit=:diamonds>, #<Card:0x007f4b4f010b98 @rank=:jack, @suit=:clubs>, #<Card:0x007f4b4f010b70 @rank=:queen, @suit=:spades>, #<Card:0x007f4b4f010b48 @rank=:queen, @suit=:hearts>, #<Card:0x007f4b4f010b20 @rank=:queen, @suit=:diamonds>, #<Card:0x007f4b4f010ad0 @rank=:queen, @suit=:clubs>, #<Card:0x007f4b4f010aa8 @rank=:king, @suit=:spades>, #<Card:0x007f4b4f010a58 @rank=:king, @suit=:hearts>, #<Card:0x007f4b4f010a30 @rank=:king, @suit=:diamonds>, #<Card:0x007f4b4f010a08 @rank=:king, @suit=:clubs>, #<Card:0x007f4b4f0109e0 @rank=10, @suit=:spades>, #<Card:0x007f4b4f0108c8 @rank=10, @suit=:hearts>, #<Card:0x007f4b4f0108a0 @rank=10, @suit=:diamonds>, #<Card:0x007f4b4f010878 @rank=10, @suit=:clubs>, #<Card:0x007f4b4f010850 @rank=:ace, @suit=:spades>, #<Card:0x007f4b4f010800 @rank=:ace, @suit=:hearts>, #<Card:0x007f4b4f010788 @rank=:ace, @suit=:diamonds>, #<Card:0x007f4b4f010738 @rank=:ace, @suit=:clubs>]
       the missing elements were:      [#<Card:0x007f4b4effefb0 @rank=9, @suit=:clubs>, #<Card:0x007f4b4effef60 @rank=:jack, @suit=:clubs>, #<Card:0x007f4b4effef38 @rank=:queen, @suit=:clubs>, #<Card:0x007f4b4effeee8 @rank=:king, @suit=:clubs>, #<Card:0x007f4b4effeec0 @rank=10, @suit=:clubs>, #<Card:0x007f4b4effee98 @rank=:ace, @suit=:clubs>, #<Card:0x007f4b4effedd0 @rank=9, @suit=:diamonds>, #<Card:0x007f4b4effeda8 @rank=:jack, @suit=:diamonds>, #<Card:0x007f4b4effed80 @rank=:queen, @suit=:diamonds>, #<Card:0x007f4b4effed58 @rank=:king, @suit=:diamonds>, #<Card:0x007f4b4effed08 @rank=10, @suit=:diamonds>, #<Card:0x007f4b4effece0 @rank=:ace, @suit=:diamonds>, #<Card:0x007f4b4effecb8 @rank=9, @suit=:hearts>, #<Card:0x007f4b4effeba0 @rank=:jack, @suit=:hearts>, #<Card:0x007f4b4effe9c0 @rank=:queen, @suit=:hearts>, #<Card:0x007f4b4effe970 @rank=:king, @suit=:hearts>, #<Card:0x007f4b4effe920 @rank=10, @suit=:hearts>, #<Card:0x007f4b4effe8a8 @rank=:ace, @suit=:hearts>, #<Card:0x007f4b4effe768 @rank=9, @suit=:spades>, #<Card:0x007f4b4effe128 @rank=:jack, @suit=:spades>, #<Card:0x007f4b4efe29c8 @rank=:queen, @suit=:spades>, #<Card:0x007f4b4efe2950 @rank=:king, @suit=:spades>, #<Card:0x007f4b4efe2798 @rank=10, @suit=:spades>, #<Card:0x007f4b4efe26f8 @rank=:ace, @suit=:spades>]
       the extra elements were:        [#<Card:0x007f4b4f010d78 @rank=9, @suit=:spades>, #<Card:0x007f4b4f010d28 @rank=9, @suit=:hearts>, #<Card:0x007f4b4f010d00 @rank=9, @suit=:diamonds>, #<Card:0x007f4b4f010cd8 @rank=9, @suit=:clubs>, #<Card:0x007f4b4f010c88 @rank=:jack, @suit=:spades>, #<Card:0x007f4b4f010c60 @rank=:jack, @suit=:hearts>, #<Card:0x007f4b4f010be8 @rank=:jack, @suit=:diamonds>, #<Card:0x007f4b4f010b98 @rank=:jack, @suit=:clubs>, #<Card:0x007f4b4f010b70 @rank=:queen, @suit=:spades>, #<Card:0x007f4b4f010b48 @rank=:queen, @suit=:hearts>, #<Card:0x007f4b4f010b20 @rank=:queen, @suit=:diamonds>, #<Card:0x007f4b4f010ad0 @rank=:queen, @suit=:clubs>, #<Card:0x007f4b4f010aa8 @rank=:king, @suit=:spades>, #<Card:0x007f4b4f010a58 @rank=:king, @suit=:hearts>, #<Card:0x007f4b4f010a30 @rank=:king, @suit=:diamonds>, #<Card:0x007f4b4f010a08 @rank=:king, @suit=:clubs>, #<Card:0x007f4b4f0109e0 @rank=10, @suit=:spades>, #<Card:0x007f4b4f0108c8 @rank=10, @suit=:hearts>, #<Card:0x007f4b4f0108a0 @rank=10, @suit=:diamonds>, #<Card:0x007f4b4f010878 @rank=10, @suit=:clubs>, #<Card:0x007f4b4f010850 @rank=:ace, @suit=:spades>, #<Card:0x007f4b4f010800 @rank=:ace, @suit=:hearts>, #<Card:0x007f4b4f010788 @rank=:ace, @suit=:diamonds>, #<Card:0x007f4b4f010738 @rank=:ace, @suit=:clubs>]
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1fd5grw/spec.rb:400
     # /tmp/d20151112-27349-1fd5grw/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)>'

  18) SixtySixDeck behaves like a deck #draw_top_card pops the top-most card
     Failure/Error: expect(small_deck.draw_top_card).to eq ace_of_spades
       
       expected: #<Card:0x007f4b4ec8abb8 @rank=:ace, @suit=:spades>
            got: #<Card:0x007f4b4ec8ab90 @rank=9, @suit=:clubs>
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -#<Card:0x007f4b4ec8abb8 @rank=:ace, @suit=:spades>
       +#<Card:0x007f4b4ec8ab90 @rank=9, @suit=:clubs>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1fd5grw/spec.rb:400
     # /tmp/d20151112-27349-1fd5grw/spec.rb:29: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 #draw_bottom_card pops the bottom-most card
     Failure/Error: expect(small_deck.draw_bottom_card).to eq nine_of_clubs
       
       expected: #<Card:0x007f4b4ec63270 @rank=9, @suit=:clubs>
            got: #<Card:0x007f4b4ec63298 @rank=:ace, @suit=:spades>
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -#<Card:0x007f4b4ec63270 @rank=9, @suit=:clubs>
       +#<Card:0x007f4b4ec63298 @rank=:ace, @suit=:spades>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1fd5grw/spec.rb:400
     # /tmp/d20151112-27349-1fd5grw/spec.rb:36: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)>'

  20) SixtySixDeck behaves like a deck #top peeks at the top-most card
     Failure/Error: expect(small_deck.top_card).to eq ace_of_spades
       
       expected: #<Card:0x007f4b4ec5d190 @rank=:ace, @suit=:spades>
            got: #<Card:0x007f4b4ec5d140 @rank=9, @suit=:clubs>
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -#<Card:0x007f4b4ec5d190 @rank=:ace, @suit=:spades>
       +#<Card:0x007f4b4ec5d140 @rank=9, @suit=:clubs>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1fd5grw/spec.rb:400
     # /tmp/d20151112-27349-1fd5grw/spec.rb:43: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)>'

  21) SixtySixDeck behaves like a deck #bottom peeks at the bottom-most card
     Failure/Error: expect(small_deck.bottom_card).to eq nine_of_clubs
       
       expected: #<Card:0x007f4b4ec96760 @rank=9, @suit=:clubs>
            got: #<Card:0x007f4b4ec967d8 @rank=:ace, @suit=:spades>
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -#<Card:0x007f4b4ec96760 @rank=9, @suit=:clubs>
       +#<Card:0x007f4b4ec967d8 @rank=:ace, @suit=:spades>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1fd5grw/spec.rb:400
     # /tmp/d20151112-27349-1fd5grw/spec.rb:50: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)>'

  22) SixtySixDeck #sort sorts the cards in the defined order
     Failure/Error: expect(deck.sort.to_a).to eq [jack_of_spades, ten_of_hearts, ace_of_clubs, two_of_clubs]
       
       expected: [#<Card:0x007f4b4ef67ca0 @rank=:jack, @suit=:spades>, #<Card:0x007f4b4ef67bb0 @rank=10, @suit=:hearts>, #<Card:0x007f4b4ef6c160 @rank=:ace, @suit=:clubs>, #<Card:0x007f4b4ef67c00 @rank=9, @suit=:clubs>]
            got: [#<Card:0x007f4b4ef6c160 @rank=:ace, @suit=:clubs>, #<Card:0x007f4b4ef67c00 @rank=9, @suit=:clubs>, #<Card:0x007f4b4ef67bb0 @rank=10, @suit=:hearts>, #<Card:0x007f4b4ef67ca0 @rank=:jack, @suit=:spades>]
       
       (compared using ==)
       
       Diff:
       
       @@ -1,5 +1,5 @@
       -[#<Card:0x007f4b4ef67ca0 @rank=:jack, @suit=:spades>,
       +[#<Card:0x007f4b4ef6c160 @rank=:ace, @suit=:clubs>,
       + #<Card:0x007f4b4ef67c00 @rank=9, @suit=:clubs>,
         #<Card:0x007f4b4ef67bb0 @rank=10, @suit=:hearts>,
       - #<Card:0x007f4b4ef6c160 @rank=:ace, @suit=:clubs>,
       - #<Card:0x007f4b4ef67c00 @rank=9, @suit=:clubs>]
       + #<Card:0x007f4b4ef67ca0 @rank=:jack, @suit=:spades>]
     # /tmp/d20151112-27349-1fd5grw/spec.rb:415: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)>'

  23) SixtySixDeck hand #twenty? returns true for king and queen not of the trump suit
     Failure/Error: expect(hand.twenty?(:hearts)).to be true
     NameError:
       undefined local variable or method `suites' for #<SixtySixHand:0x007f4b4f442310>
     # /tmp/d20151112-27349-1fd5grw/solution.rb:98:in `king_and_queen?'
     # /tmp/d20151112-27349-1fd5grw/solution.rb:212:in `twenty?'
     # /tmp/d20151112-27349-1fd5grw/spec.rb:439: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)>'

  24) SixtySixDeck hand #twenty? returns false for king and queen of the trump suit
     Failure/Error: expect(hand.twenty?(:clubs)).to be false
     NameError:
       undefined local variable or method `suites' for #<SixtySixHand:0x007f4b4f4bdee8>
     # /tmp/d20151112-27349-1fd5grw/solution.rb:98:in `king_and_queen?'
     # /tmp/d20151112-27349-1fd5grw/solution.rb:212:in `twenty?'
     # /tmp/d20151112-27349-1fd5grw/spec.rb:452: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)>'

  25) SixtySixDeck hand #twenty? returns false for hands without a king and queen of the same suit
     Failure/Error: expect(hand.twenty?(:hearts)).to be false
     NameError:
       undefined local variable or method `suites' for #<SixtySixHand:0x007f4b4f4af690>
     # /tmp/d20151112-27349-1fd5grw/solution.rb:98:in `king_and_queen?'
     # /tmp/d20151112-27349-1fd5grw/solution.rb:212:in `twenty?'
     # /tmp/d20151112-27349-1fd5grw/spec.rb:465: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)>'

Finished in 0.06146 seconds
57 examples, 25 failures

Failed examples:

rspec /tmp/d20151112-27349-1fd5grw/spec.rb:130 # Card #== compares two cards by their rank and suit
rspec /tmp/d20151112-27349-1fd5grw/spec.rb:14 # WarDeck behaves like a deck fills the deck if no initialize parameters are given
rspec /tmp/d20151112-27349-1fd5grw/spec.rb:28 # WarDeck behaves like a deck #draw_top_card pops the top-most card
rspec /tmp/d20151112-27349-1fd5grw/spec.rb:35 # WarDeck behaves like a deck #draw_bottom_card pops the bottom-most card
rspec /tmp/d20151112-27349-1fd5grw/spec.rb:42 # WarDeck behaves like a deck #top peeks at the top-most card
rspec /tmp/d20151112-27349-1fd5grw/spec.rb:49 # WarDeck behaves like a deck #bottom peeks at the bottom-most card
rspec /tmp/d20151112-27349-1fd5grw/spec.rb:145 # WarDeck #sort sorts the cards in the defined order
rspec /tmp/d20151112-27349-1fd5grw/spec.rb:14 # BeloteDeck behaves like a deck fills the deck if no initialize parameters are given
rspec /tmp/d20151112-27349-1fd5grw/spec.rb:28 # BeloteDeck behaves like a deck #draw_top_card pops the top-most card
rspec /tmp/d20151112-27349-1fd5grw/spec.rb:35 # BeloteDeck behaves like a deck #draw_bottom_card pops the bottom-most card
rspec /tmp/d20151112-27349-1fd5grw/spec.rb:42 # BeloteDeck behaves like a deck #top peeks at the top-most card
rspec /tmp/d20151112-27349-1fd5grw/spec.rb:49 # BeloteDeck behaves like a deck #bottom peeks at the bottom-most card
rspec /tmp/d20151112-27349-1fd5grw/spec.rb:196 # BeloteDeck #sort sorts the cards in the defined order
rspec /tmp/d20151112-27349-1fd5grw/spec.rb:220 # BeloteDeck hand #highest_of_suit returns the strongest card of the specified suit
rspec /tmp/d20151112-27349-1fd5grw/spec.rb:239 # BeloteDeck hand #belote? returns true if there is a king and a queen of the same suit
rspec /tmp/d20151112-27349-1fd5grw/spec.rb:254 # BeloteDeck hand #belote? returns false when there is no king and queen of the same suit
rspec /tmp/d20151112-27349-1fd5grw/spec.rb:14 # SixtySixDeck behaves like a deck fills the deck if no initialize parameters are given
rspec /tmp/d20151112-27349-1fd5grw/spec.rb:28 # SixtySixDeck behaves like a deck #draw_top_card pops the top-most card
rspec /tmp/d20151112-27349-1fd5grw/spec.rb:35 # SixtySixDeck behaves like a deck #draw_bottom_card pops the bottom-most card
rspec /tmp/d20151112-27349-1fd5grw/spec.rb:42 # SixtySixDeck behaves like a deck #top peeks at the top-most card
rspec /tmp/d20151112-27349-1fd5grw/spec.rb:49 # SixtySixDeck behaves like a deck #bottom peeks at the bottom-most card
rspec /tmp/d20151112-27349-1fd5grw/spec.rb:405 # SixtySixDeck #sort sorts the cards in the defined order
rspec /tmp/d20151112-27349-1fd5grw/spec.rb:429 # SixtySixDeck hand #twenty? returns true for king and queen not of the trump suit
rspec /tmp/d20151112-27349-1fd5grw/spec.rb:442 # SixtySixDeck hand #twenty? returns false for king and queen of the trump suit
rspec /tmp/d20151112-27349-1fd5grw/spec.rb:455 # SixtySixDeck hand #twenty? returns false for hands without a king and queen of the same suit

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

Мартина обнови решението на 06.11.2015 21:52 (преди около 9 години)

+SUITES = [:spades, :hearts, :diamonds, :clubs]
+
+class Card
+
+ attr_accessor :rank, :suite
+
+ def initialize(rank, suite)
+ @rank = rank
+ @suite = suite
+ end
+
+ def to_s
+ "#{@rank.to_s.capitalize} of #{@suite.capitalize}"
+ end
+
+ def number?
+ @rank.is_a? Integer
+ end
+
+ def ace?
+ @rank == :ace
+ end
+
+ def queen?
+ @rank == :queen
+ end
+
+ def king?
+ @rank == :king
+ end
+
+ def jack?
+ @rank == :jack
+ end
+end
+
+class GenericDeck
+ include Enumerable
+
+ RANKS = []
+
+ def generate_deck
+ deck = self.class::RANKS.product(SUITES).map! do |rank, suit|
+ Card.new(rank, suit)
+ end
+ end
+
+ def initialize(deck = generate_deck)
+ @deck = deck
+ end
+
+ def size
+ @deck.length
+ end
+
+ def draw_top_card
+ @deck.pop
+ end
+
+ def draw_bottom_card
+ @deck.shift
+ end
+
+ def top_card
+ @deck.last
+ end
+
+ def bottom_card
+ @deck.first
+ end
+
+ def shuffle
+ @deck.shuffle!
+ end
+
+ def to_s
+ @deck.map(&:to_s).join("\n")
+ end
+
+ def sort
+ cards_by_suites = @deck.group_by { |card| card.suite }
+ @deck.clear
+ SUITES.select{ |s| cards_by_suites.has_key? s }.each do |suite|
+ @deck.push(*cards_by_suites[suite].sort { | x, y |
+ self.class::RANKS.index(y.rank) <=> self.class::RANKS.index(x.rank) })
+ end
+ @deck
+ end
+
+ def each(&block)
+ @deck.each(&block)
+ end
+
+end
+
+class Hand
+ include Enumerable
+ attr_accessor :cards
+
+ def initialize(cards = [])
+ @cards = cards
+ end
+
+ def get_suite(suite)
+ @cards.select { |card| card.suite == suite }
+ end
+
+ def has_card?(card_rank)
+ @cards.any? { |card| card.rank == card_rank }
+ end
+
+ def has_king_and_queen(suites)
+ suites.each do |suite|
+ current_suite = get_suite(suite)
+ if current_suite.any?(&:queen?) and current_suite.any?(&:king?)
+ return true
+ end
+ end
+ false
+ end
+
+ def to_s
+ @cards.map { |card| card.to_s }.join("\n")
+ end
+
+ def size
+ @cards.length
+ end
+
+ def each(&block)
+ @cards.each(&block)
+ end
+end
+
+class WarHand < Hand
+ def size
+ @cards.length
+ end
+
+ def play_card
+ @cards.delete_at(rand(0...@cards.length))
+ end
+
+ def allow_face_up?
+ @cards.length <= 3
+ end
+end
+
+class WarDeck < GenericDeck
+ RANKS = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
+ DEAL_SIZE = 26
+
+ def deal
+ WarHand.new(@deck.pop(DEAL_SIZE))
+ end
+
+end
+
+class BeloteHand < Hand
+
+ def initialize(ranks, cards = [])
+ @ranks = ranks
+ super cards
+ end
+
+ def highest_of_suit(suit)
+ get_suite(suit).sort { |x, y|
+ @ranks.index(x.rank) <=> @ranks.index(y.rank) }.last
+ end
+
+ def belote?
+ has_king_and_queen(SUITES)
+ end
+
+ def identify_sequences(length)
+ SUITES.each do |suite|
+ current_suite = get_suite(suite)
+ current_suite.map! { |card| @ranks.index(card.rank) }.sort!
+ if current_suite.each_cons(length).any? { |list|
+ list == (list[0]..list[-1]).to_a }
+ return true
+ end
+ end
+ false
+ end
+
+ def tierce?
+ identify_sequences(3)
+ end
+
+ def quarte?
+ identify_sequences(4)
+ end
+
+ def quint?
+ identify_sequences(5)
+ end
+
+ def carre?(card_type)
+ @cards.select { |card| card.rank == card_type}.count == 4
+ end
+
+ def carre_of_jacks?
+ carre?(:jack)
+ end
+
+ def carre_of_nines?
+ carre?(9)
+ end
+
+ def carre_of_aces?
+ carre?(:ace)
+ end
+end
+
+class BeloteDeck < GenericDeck
+ RANKS = [7, 8, 9, :jack, :queen, :king, 10, :ace]
+ DEAL_SIZE = 8
+
+ def deal
+ BeloteHand.new(self.class::RANKS, @deck.pop(DEAL_SIZE))
+ end
+end
+
+
+class SixtySixHand < Hand
+ def twenty?(trump_suit)
+ has_king_and_queen(SUITES.select { |suit| suit != trump_suit })
+ end
+
+ def forty?(trump_suit)
+ has_king_and_queen([trump_suit])
+ end
+end
+
+class SixtySixDeck < GenericDeck
+ RANKS = [9, :jack, :queen, :king, 10, :ace]
+ DEAL_SIZE = 6
+
+ def deal
+ SixtySixHand.new(@deck.pop(DEAL_SIZE))
+ end
+end

Здравей :) Ето няколко коментара:

  • На някои места използваш { и } за блокове на много редове. Това е в разрез с конвенциите и повод за взимане на точки. :)
  • self.class::RANKS е голяма магия. Не е очевидно коя константа взима. Можеш ли да измислиш как да дадеш на GenericDeck този масив по по-прост начин?
  • Удивителната при map-а не ти е нужна.
  • sort-ът е ненужно сложен. Подсказвам, че има метод #sort!. Също, в самия блок на sort-a можеш да вземеш предвид и suits, вместо да сортираш на части. :)
  • has_king_and_queen може да се имплементира по-лесно с any? вместо each. Спестяваш си return-ите и if-овете. Същото важи и за identify_sequences. Също, хубаво е булевите методи да са с ? накрая и да нямат думи като has, is и подобни. king_and_queen?.
  • Множественото число на suit е suits, не suites :)

Мартина обнови решението на 08.11.2015 12:45 (преди около 9 години)

-SUITES = [:spades, :hearts, :diamonds, :clubs]
+SUITS = [:spades, :hearts, :diamonds, :clubs]
class Card
- attr_accessor :rank, :suite
+ attr_accessor :rank, :suit
- def initialize(rank, suite)
+ def initialize(rank, suit)
@rank = rank
- @suite = suite
+ @suit = suit
end
def to_s
- "#{@rank.to_s.capitalize} of #{@suite.capitalize}"
+ "#{@rank.to_s.capitalize} of #{@suit.capitalize}"
end
- def number?
- @rank.is_a? Integer
- end
-
- def ace?
- @rank == :ace
- end
-
def queen?
@rank == :queen
end
def king?
@rank == :king
end
- def jack?
- @rank == :jack
- end
end
class GenericDeck
+
include Enumerable
RANKS = []
def generate_deck
- deck = self.class::RANKS.product(SUITES).map! do |rank, suit|
+ deck = self.class::RANKS.product(SUITS).map! do |rank, suit|
Card.new(rank, suit)
end
end
def initialize(deck = generate_deck)
@deck = deck
end
def size
@deck.length
end
def draw_top_card
@deck.pop
end
def draw_bottom_card
@deck.shift
end
def top_card
@deck.last
end
def bottom_card
@deck.first
end
def shuffle
@deck.shuffle!
end
def to_s
@deck.map(&:to_s).join("\n")
end
def sort
- cards_by_suites = @deck.group_by { |card| card.suite }
- @deck.clear
- SUITES.select{ |s| cards_by_suites.has_key? s }.each do |suite|
- @deck.push(*cards_by_suites[suite].sort { | x, y |
- self.class::RANKS.index(y.rank) <=> self.class::RANKS.index(x.rank) })
+ @deck.sort! do |x, y|
+ if (SUITS.index(y.suit) <=> SUITS.index(x.suit)) == 0
+ self.class::RANKS.index(y.rank) <=> self.class::RANKS.index(x.rank)
+ else
+ SUITS.index(y.suit) <=> SUITS.index(x.suit)
+ end
end
- @deck
end
def each(&block)
@deck.each(&block)
end
end
class Hand
+
include Enumerable
attr_accessor :cards
def initialize(cards = [])
@cards = cards
end
- def get_suite(suite)
- @cards.select { |card| card.suite == suite }
+ def get_suit(suit)
+ @cards.select { |card| card.suit == suit }
end
def has_card?(card_rank)
@cards.any? { |card| card.rank == card_rank }
end
- def has_king_and_queen(suites)
- suites.each do |suite|
- current_suite = get_suite(suite)
- if current_suite.any?(&:queen?) and current_suite.any?(&:king?)
- return true
- end
+ def king_and_queen?(suits)
+ @cards.any? do |x, y| x.queen? and y.king? and x.suit == y.suit and
+ suits.include?(x.suit)
end
- false
end
def to_s
@cards.map { |card| card.to_s }.join("\n")
end
def size
@cards.length
end
def each(&block)
@cards.each(&block)
end
+
end
class WarHand < Hand
+
def size
@cards.length
end
def play_card
@cards.delete_at(rand(0...@cards.length))
end
def allow_face_up?
@cards.length <= 3
end
+
end
class WarDeck < GenericDeck
+
RANKS = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
DEAL_SIZE = 26
def deal
WarHand.new(@deck.pop(DEAL_SIZE))
end
end
class BeloteHand < Hand
def initialize(ranks, cards = [])
@ranks = ranks
super cards
end
def highest_of_suit(suit)
- get_suite(suit).sort { |x, y|
- @ranks.index(x.rank) <=> @ranks.index(y.rank) }.last
+ get_suit(suit).sort do |x, y|
+ @ranks.index(x.rank) <=> @ranks.index(y.rank)
+ end.last
end
def belote?
- has_king_and_queen(SUITES)
+ king_and_queen?(SUITS)
end
def identify_sequences(length)
- SUITES.each do |suite|
- current_suite = get_suite(suite)
- current_suite.map! { |card| @ranks.index(card.rank) }.sort!
- if current_suite.each_cons(length).any? { |list|
- list == (list[0]..list[-1]).to_a }
+ SUITS.each do |suit|
+ current_suit = get_suit(suit).map! { |card| @ranks.index card.rank }.sort!
+ if current_suit.each_cons(length).any? do |list|
+ list == (list[0]..list[-1]).to_a
+ end
return true
end
end
false
end
def tierce?
identify_sequences(3)
end
def quarte?
identify_sequences(4)
end
def quint?
identify_sequences(5)
end
def carre?(card_type)
@cards.select { |card| card.rank == card_type}.count == 4
end
def carre_of_jacks?
carre?(:jack)
end
def carre_of_nines?
carre?(9)
end
def carre_of_aces?
carre?(:ace)
end
+
end
class BeloteDeck < GenericDeck
+
RANKS = [7, 8, 9, :jack, :queen, :king, 10, :ace]
DEAL_SIZE = 8
def deal
BeloteHand.new(self.class::RANKS, @deck.pop(DEAL_SIZE))
end
+
end
class SixtySixHand < Hand
+
def twenty?(trump_suit)
- has_king_and_queen(SUITES.select { |suit| suit != trump_suit })
+ king_and_queen?(SUITS.select { |suit| suit != trump_suit })
end
def forty?(trump_suit)
- has_king_and_queen([trump_suit])
+ king_and_queen?([trump_suit])
end
+
end
class SixtySixDeck < GenericDeck
+
RANKS = [9, :jack, :queen, :king, 10, :ace]
DEAL_SIZE = 6
def deal
SixtySixHand.new(@deck.pop(DEAL_SIZE))
end
-end
+
+end

Здравей :)

Update-нах решението си с поправки по отношение на повечето забележки, които си ми писал. Нямам претенции, че решението ми е изпипано и благодаря за насоките, но имам няколко коментара.

  1. Във връзка с решението да използвам self.class::RANKS - наистина не е очевидно, може би в известна степен грозно, може да бъде и class instance variable, но в крайна сметка RANKS е точно константа за класа и не ми се струва много удачно да е @ranks.

  2. Удивителната на map - не съм сигурна кой ред визираш, но да кажем, че е извикването в метода generate_deck. Разбира се, може и без нея, но причината, поради която използвам map!, е че искам да си спестя създаването на нов Array обект.

  1. Наистина, това е много подходящо за константа. Не те карам да я махаш, просто да я подадеш по друг начин на базовия клас. Както подаваш картите, например. Не е задължително конструкторите на двата класа да приемат едни и същи аргументи.

  2. Създаването на един допълнителен обект не трябва да те интересува :) Времето, което ще спестиш е незначително. Мястото - още по-незначително. Кофти момент е, че неминуемо ще се случи да използваш такъв метод върху аргумент на функцията и ще ти трябват часове да дебъгнеш после. :) Също, някои от тези методи не винаги връщат промененото нещо. Например, "TEST".upcase!.sub('TEST', 'baba') ще гръмне, защото #upcase! връща nil, ако не се наложат промени.

Все още има някои стилови проблеми. Например, празните места в началото и в края на всеки клас, и условието след аргументите на блока в king_and_queen?.

Мартина обнови решението на 11.11.2015 16:13 (преди около 9 години)

SUITS = [:spades, :hearts, :diamonds, :clubs]
class Card
-
attr_accessor :rank, :suit
def initialize(rank, suit)
@rank = rank
@suit = suit
end
def to_s
"#{@rank.to_s.capitalize} of #{@suit.capitalize}"
end
def queen?
@rank == :queen
end
def king?
@rank == :king
end
-
end
class GenericDeck
-
include Enumerable
-
RANKS = []
def generate_deck
deck = self.class::RANKS.product(SUITS).map! do |rank, suit|
Card.new(rank, suit)
end
end
def initialize(deck = generate_deck)
@deck = deck
end
def size
@deck.length
end
def draw_top_card
@deck.pop
end
def draw_bottom_card
@deck.shift
end
def top_card
@deck.last
end
def bottom_card
@deck.first
end
def shuffle
@deck.shuffle!
end
def to_s
@deck.map(&:to_s).join("\n")
end
def sort
@deck.sort! do |x, y|
if (SUITS.index(y.suit) <=> SUITS.index(x.suit)) == 0
self.class::RANKS.index(y.rank) <=> self.class::RANKS.index(x.rank)
else
SUITS.index(y.suit) <=> SUITS.index(x.suit)
end
end
end
def each(&block)
@deck.each(&block)
end
-
end
class Hand
-
include Enumerable
attr_accessor :cards
def initialize(cards = [])
@cards = cards
end
def get_suit(suit)
@cards.select { |card| card.suit == suit }
end
def has_card?(card_rank)
@cards.any? { |card| card.rank == card_rank }
end
def king_and_queen?(suits)
- @cards.any? do |x, y| x.queen? and y.king? and x.suit == y.suit and
- suits.include?(x.suit)
+ suites.each do |suite|
+ current_suite = get_suite(suite)
+ if current_suite.any?(&:queen?) and current_suite.any?(&:king?)
+ return true
+ end
end
+ false
end
def to_s
@cards.map { |card| card.to_s }.join("\n")
end
def size
@cards.length
end
def each(&block)
@cards.each(&block)
end
-
end
class WarHand < Hand
-
def size
@cards.length
end
def play_card
@cards.delete_at(rand(0...@cards.length))
end
def allow_face_up?
@cards.length <= 3
end
-
end
class WarDeck < GenericDeck
-
RANKS = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
DEAL_SIZE = 26
def deal
WarHand.new(@deck.pop(DEAL_SIZE))
end
-
end
class BeloteHand < Hand
-
def initialize(ranks, cards = [])
@ranks = ranks
super cards
end
def highest_of_suit(suit)
get_suit(suit).sort do |x, y|
@ranks.index(x.rank) <=> @ranks.index(y.rank)
end.last
end
def belote?
king_and_queen?(SUITS)
end
def identify_sequences(length)
SUITS.each do |suit|
current_suit = get_suit(suit).map! { |card| @ranks.index card.rank }.sort!
if current_suit.each_cons(length).any? do |list|
list == (list[0]..list[-1]).to_a
end
return true
end
end
false
end
def tierce?
identify_sequences(3)
end
def quarte?
identify_sequences(4)
end
def quint?
identify_sequences(5)
end
def carre?(card_type)
@cards.select { |card| card.rank == card_type}.count == 4
end
def carre_of_jacks?
carre?(:jack)
end
def carre_of_nines?
carre?(9)
end
def carre_of_aces?
carre?(:ace)
end
-
end
class BeloteDeck < GenericDeck
-
RANKS = [7, 8, 9, :jack, :queen, :king, 10, :ace]
DEAL_SIZE = 8
def deal
BeloteHand.new(self.class::RANKS, @deck.pop(DEAL_SIZE))
end
-
end
class SixtySixHand < Hand
-
def twenty?(trump_suit)
king_and_queen?(SUITS.select { |suit| suit != trump_suit })
end
def forty?(trump_suit)
king_and_queen?([trump_suit])
end
-
end
class SixtySixDeck < GenericDeck
-
RANKS = [9, :jack, :queen, :king, 10, :ace]
DEAL_SIZE = 6
def deal
SixtySixHand.new(@deck.pop(DEAL_SIZE))
end
-
end