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

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

Към профила на Мила Русева

Резултати

  • 2 точки от тестове
  • 0 бонус точки
  • 2 точки общо
  • 21 успешни тест(а)
  • 36 неуспешни тест(а)

Код

class Card
include Comparable
attr_accessor :rank, :suit, :deck
def initialize (rank, suit)
@rank = rank
@suit = suit
@deck = Deck.new([])
end
def to_s
"#{rank.to_s.capitalize} of #{suit.to_s.capitalize}"
end
def <=> (other)
suits_comparison = deck.suits.index(@suit) <=> deck.suits.index(other.suit)
if suits_comparison == 0
return rank_index <=> other.rank_index
end
return suits_comparison
end
def rank_index
deck.ranks.index(@rank)
end
end
class Deck
include Enumerable
attr_accessor :cards
def initialize(cards = nil)
if cards == nil
all_cards_deck
else
@cards = cards
end
@cards.each { |card| card.deck = self }
end
def all_cards_deck
@cards = Array.new
suits.each do |suit|
ranks.each do |rank|
@cards << Card.new(rank, suit)
end
end
end
def ranks
[2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :qeen, :king, :ace].reverse
end
def suits
[:spades, :hearts, :diamonds, :clubs]
end
def size
@cards.size
end
def draw_top_card
@cards.slice!(0)
end
def draw_bottom_card
@cards.pop
end
def top_card
@cards.at(0)
end
def bottom_card
@cards.at(-1)
end
def shuffle
@cards.shuffle!
end
def sort
@cards.sort!
end
def to_s
@cards.join("\n")
end
end
class Hand
attr_accessor :cards
def initialize(cards = nil)
@cards = cards
end
def size
@cards.size
end
end
class WarHand < Hand
def play_card
@cards.pop
end
def allow_face_up?
size <= 3
end
end
class WarDeck < Deck
def deal
hand = Array.new(26) { draw_top_card }
WarHand.new(hand)
end
end
class BeloteHand < Hand
def highest_of_suit(suit)
suit_cards = @cards.select { |card| card.suit == suit }
suit_cards.sort.first
end
def belote?
@cards.sort.each_cons(2) do |cons|
if cons[0] == :king and cons[1] == :qeen and cons[0].suit == cons[1].suit
return true
end
end
return false
end
def tierce?
@cards.sort.each_cons(3) do |cons|
same_rank = cons[0].rank_index == (cons[-1].rank_index - 2)
same_suit = cons.all? { |card| card.suit == cons[0].suit }
if same_rank and same_suit
return true
end
end
return false
end
def quarte?
@cards.sort.each_cons(4) do |cons|
same_rank = cons[0].rank_index == (cons[-1].rank_index - 3)
same_suit = cons.all? { |card| card.suit == cons[0].suit }
if same_rank and same_suit
return true
end
end
return false
end
def quint?
@cards.sort.each_cons(5) do |cons|
same_rank = cons[0].rank_index == (cons[-1].rank_index - 4)
same_suit = cons.all? { |card| card.suit == cons[0].suit }
if same_rank and same_suit
return true
end
end
return false
end
def carre_of_jacks?
has_carre? do |card|
card.rank == :jack
end
end
def carre_of_nines?
has_carre? do |card|
card.rank == 9
end
end
def carre_of_aces?
has_carre? do |card|
card.rank == :ace
end
end
def has_carre? (&block)
@cards.sort.each_cons(4) do |cons|
if cons.all? block
return true
end
end
return false
end
end
class BeloteDeck < Deck
def ranks
[7, 8, 9, :jack, :qeen, :king, 10, :ace].reverse
end
def deal
hand = Array.new(8) { draw_top_card }
BeloteHand.new(hand)
end
end
class SixtySixHand < Hand
def twenty?(trump_suit)
@cards.sort.each_cons(2) do |cons|
rank_cards = cons[0] == :king and cons[1] == :qeen
suit_cards = cons[0].suit == cons[1].suit
if rank_cards and (suit_cards != trump_suit)
return true
end
end
return false
end
def forty?(trump_suit)
@cards.sort.each_cons(2) do |cons|
rank_cards = cons[0] == :king and cons[1] == :qeen
suit_cards = cons[0].suit == cons[1].suit
if rank_cards and (suit_cards == trump_suit)
return true
end
end
return false
end
end
class SixtySixDeck < Deck
def ranks
[9, :jack, :qeen, :king, 10, :ace].reverse
end
def deal
hand = Array.new(6) { draw_top_card }
SixtySixHand.new(hand)
end
end

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

...FF.FFFF......FF.FFFF....FFFF.FFFFFFFFFFFFF.FFFF....FFF

Failures:

  1) WarDeck behaves like a deck implements Enumerable
     Failure/Error: expect(small_deck).to respond_to(:each)
       expected #<WarDeck:0x007fade93fe5a0 @cards=[#<Card:0x007fade93fea50 @rank=:ace, @suit=:spades, @deck=#<WarDeck:0x007fade93fe5a0 ...>>, #<Card:0x007fade93fe870 @rank=9, @suit=:clubs, @deck=#<WarDeck:0x007fade93fe5a0 ...>>]> to respond to :each
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-39p1jf/spec.rb:140
     # /tmp/d20151112-27349-39p1jf/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:0x007fadea1f0ca8>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-39p1jf/spec.rb:140
     # /tmp/d20151112-27349-39p1jf/spec.rb:18:in `to_a'
     # /tmp/d20151112-27349-39p1jf/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:0x007fadea28f6c8>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-39p1jf/spec.rb:140
     # /tmp/d20151112-27349-39p1jf/spec.rb:30:in `to_a'
     # /tmp/d20151112-27349-39p1jf/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:0x007fadea28cc48>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-39p1jf/spec.rb:140
     # /tmp/d20151112-27349-39p1jf/spec.rb:37:in `to_a'
     # /tmp/d20151112-27349-39p1jf/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:0x007fadea279530>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-39p1jf/spec.rb:140
     # /tmp/d20151112-27349-39p1jf/spec.rb:44:in `to_a'
     # /tmp/d20151112-27349-39p1jf/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:0x007fadea1fe268>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-39p1jf/spec.rb:140
     # /tmp/d20151112-27349-39p1jf/spec.rb:51:in `to_a'
     # /tmp/d20151112-27349-39p1jf/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:0x007fadea24d4d0 @cards=[#<Card:0x007fadea24d5e8 @rank=:ace, @suit=:spades, @deck=#<BeloteDeck:0x007fadea24d4d0 ...>>, #<Card:0x007fadea24d570 @rank=9, @suit=:clubs, @deck=#<BeloteDeck:0x007fadea24d4d0 ...>>]> to respond to :each
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-39p1jf/spec.rb:191
     # /tmp/d20151112-27349-39p1jf/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:0x007fadea2423f0>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-39p1jf/spec.rb:191
     # /tmp/d20151112-27349-39p1jf/spec.rb:18:in `to_a'
     # /tmp/d20151112-27349-39p1jf/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:0x007fadea222c30>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-39p1jf/spec.rb:191
     # /tmp/d20151112-27349-39p1jf/spec.rb:30:in `to_a'
     # /tmp/d20151112-27349-39p1jf/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:0x007fadea21b7a0>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-39p1jf/spec.rb:191
     # /tmp/d20151112-27349-39p1jf/spec.rb:37:in `to_a'
     # /tmp/d20151112-27349-39p1jf/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:0x007fadea218668>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-39p1jf/spec.rb:191
     # /tmp/d20151112-27349-39p1jf/spec.rb:44:in `to_a'
     # /tmp/d20151112-27349-39p1jf/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:0x007fadea2095c8>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-39p1jf/spec.rb:191
     # /tmp/d20151112-27349-39p1jf/spec.rb:51:in `to_a'
     # /tmp/d20151112-27349-39p1jf/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)
     ArgumentError:
       comparison of Card with Card failed
     # /tmp/d20151112-27349-39p1jf/solution.rb:125:in `sort'
     # /tmp/d20151112-27349-39p1jf/solution.rb:125:in `highest_of_suit'
     # /tmp/d20151112-27349-39p1jf/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) 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
     ArgumentError:
       comparison of Card with Card failed
     # /tmp/d20151112-27349-39p1jf/solution.rb:129:in `sort'
     # /tmp/d20151112-27349-39p1jf/solution.rb:129:in `belote?'
     # /tmp/d20151112-27349-39p1jf/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)>'

  15) BeloteDeck hand #belote? returns false when there is no king and queen of the same suit
     Failure/Error: expect(hand.belote?).to be false
     ArgumentError:
       comparison of Card with Card failed
     # /tmp/d20151112-27349-39p1jf/solution.rb:129:in `sort'
     # /tmp/d20151112-27349-39p1jf/solution.rb:129:in `belote?'
     # /tmp/d20151112-27349-39p1jf/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)>'

  16) BeloteDeck hand #tierce? with tierce returns true for cards with names
     Failure/Error: expect(hand.tierce?).to be true
     ArgumentError:
       comparison of Card with Card failed
     # /tmp/d20151112-27349-39p1jf/solution.rb:138:in `sort'
     # /tmp/d20151112-27349-39p1jf/solution.rb:138:in `tierce?'
     # /tmp/d20151112-27349-39p1jf/spec.rb:284:in `block (5 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) BeloteDeck hand #tierce? without tierce does not confuse cards with different suits
     Failure/Error: expect(hand.tierce?).to be false
     ArgumentError:
       comparison of Card with Card failed
     # /tmp/d20151112-27349-39p1jf/solution.rb:138:in `sort'
     # /tmp/d20151112-27349-39p1jf/solution.rb:138:in `tierce?'
     # /tmp/d20151112-27349-39p1jf/spec.rb:316:in `block (5 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) BeloteDeck hand #quarte? detects four cards with increasing ranks
     Failure/Error: expect(hand.quarte?).to be true
     ArgumentError:
       comparison of Card with Card failed
     # /tmp/d20151112-27349-39p1jf/solution.rb:149:in `sort'
     # /tmp/d20151112-27349-39p1jf/solution.rb:149:in `quarte?'
     # /tmp/d20151112-27349-39p1jf/spec.rb:334: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)>'

  19) BeloteDeck hand #quarte? does not return true if there is no quarte
     Failure/Error: expect(hand.quarte?).to be false
     ArgumentError:
       comparison of Card with Card failed
     # /tmp/d20151112-27349-39p1jf/solution.rb:149:in `sort'
     # /tmp/d20151112-27349-39p1jf/solution.rb:149:in `quarte?'
     # /tmp/d20151112-27349-39p1jf/spec.rb:349: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)>'

  20) BeloteDeck hand #quint? detects five cards with increasing ranks
     Failure/Error: expect(hand.quint?).to be true
     ArgumentError:
       comparison of Card with Card failed
     # /tmp/d20151112-27349-39p1jf/solution.rb:160:in `sort'
     # /tmp/d20151112-27349-39p1jf/solution.rb:160:in `quint?'
     # /tmp/d20151112-27349-39p1jf/spec.rb:366: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)>'

  21) BeloteDeck hand #quint? does not return true if there is no quint
     Failure/Error: expect(hand.quint?).to be false
     ArgumentError:
       comparison of Card with Card failed
     # /tmp/d20151112-27349-39p1jf/solution.rb:160:in `sort'
     # /tmp/d20151112-27349-39p1jf/solution.rb:160:in `quint?'
     # /tmp/d20151112-27349-39p1jf/spec.rb:381: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)>'

  22) BeloteDeck hand #carre_of_jacks? behaves like carre-checking method returns true when there is a carre
     Failure/Error: expect(hand.public_send(method)).to be true
     ArgumentError:
       wrong number of arguments (1 for 0)
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-39p1jf/spec.rb:386
     # /tmp/d20151112-27349-39p1jf/solution.rb:190:in `all?'
     # /tmp/d20151112-27349-39p1jf/solution.rb:190:in `block in has_carre?'
     # /tmp/d20151112-27349-39p1jf/solution.rb:189:in `each'
     # /tmp/d20151112-27349-39p1jf/solution.rb:189:in `each_cons'
     # /tmp/d20151112-27349-39p1jf/solution.rb:189:in `has_carre?'
     # /tmp/d20151112-27349-39p1jf/solution.rb:171:in `carre_of_jacks?'
     # /tmp/d20151112-27349-39p1jf/spec.rb:86:in `public_send'
     # /tmp/d20151112-27349-39p1jf/spec.rb:86: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)>'

  23) BeloteDeck hand #carre_of_jacks? behaves like carre-checking method returns false when there is no carre
     Failure/Error: expect(hand.public_send(method)).to be false
     ArgumentError:
       wrong number of arguments (1 for 0)
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-39p1jf/spec.rb:386
     # /tmp/d20151112-27349-39p1jf/solution.rb:190:in `all?'
     # /tmp/d20151112-27349-39p1jf/solution.rb:190:in `block in has_carre?'
     # /tmp/d20151112-27349-39p1jf/solution.rb:189:in `each'
     # /tmp/d20151112-27349-39p1jf/solution.rb:189:in `each_cons'
     # /tmp/d20151112-27349-39p1jf/solution.rb:189:in `has_carre?'
     # /tmp/d20151112-27349-39p1jf/solution.rb:171:in `carre_of_jacks?'
     # /tmp/d20151112-27349-39p1jf/spec.rb:101:in `public_send'
     # /tmp/d20151112-27349-39p1jf/spec.rb:101: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)>'

  24) BeloteDeck hand #carre_of_nines? behaves like carre-checking method returns true when there is a carre
     Failure/Error: expect(hand.public_send(method)).to be true
     ArgumentError:
       wrong number of arguments (1 for 0)
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-39p1jf/spec.rb:390
     # /tmp/d20151112-27349-39p1jf/solution.rb:190:in `all?'
     # /tmp/d20151112-27349-39p1jf/solution.rb:190:in `block in has_carre?'
     # /tmp/d20151112-27349-39p1jf/solution.rb:189:in `each'
     # /tmp/d20151112-27349-39p1jf/solution.rb:189:in `each_cons'
     # /tmp/d20151112-27349-39p1jf/solution.rb:189:in `has_carre?'
     # /tmp/d20151112-27349-39p1jf/solution.rb:177:in `carre_of_nines?'
     # /tmp/d20151112-27349-39p1jf/spec.rb:86:in `public_send'
     # /tmp/d20151112-27349-39p1jf/spec.rb:86: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)>'

  25) BeloteDeck hand #carre_of_nines? behaves like carre-checking method returns false when there is no carre
     Failure/Error: expect(hand.public_send(method)).to be false
     ArgumentError:
       wrong number of arguments (1 for 0)
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-39p1jf/spec.rb:390
     # /tmp/d20151112-27349-39p1jf/solution.rb:190:in `all?'
     # /tmp/d20151112-27349-39p1jf/solution.rb:190:in `block in has_carre?'
     # /tmp/d20151112-27349-39p1jf/solution.rb:189:in `each'
     # /tmp/d20151112-27349-39p1jf/solution.rb:189:in `each_cons'
     # /tmp/d20151112-27349-39p1jf/solution.rb:189:in `has_carre?'
     # /tmp/d20151112-27349-39p1jf/solution.rb:177:in `carre_of_nines?'
     # /tmp/d20151112-27349-39p1jf/spec.rb:101:in `public_send'
     # /tmp/d20151112-27349-39p1jf/spec.rb:101: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)>'

  26) BeloteDeck hand #carre_of_aces? behaves like carre-checking method returns true when there is a carre
     Failure/Error: expect(hand.public_send(method)).to be true
     ArgumentError:
       wrong number of arguments (1 for 0)
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-39p1jf/spec.rb:394
     # /tmp/d20151112-27349-39p1jf/solution.rb:190:in `all?'
     # /tmp/d20151112-27349-39p1jf/solution.rb:190:in `block in has_carre?'
     # /tmp/d20151112-27349-39p1jf/solution.rb:189:in `each'
     # /tmp/d20151112-27349-39p1jf/solution.rb:189:in `each_cons'
     # /tmp/d20151112-27349-39p1jf/solution.rb:189:in `has_carre?'
     # /tmp/d20151112-27349-39p1jf/solution.rb:183:in `carre_of_aces?'
     # /tmp/d20151112-27349-39p1jf/spec.rb:86:in `public_send'
     # /tmp/d20151112-27349-39p1jf/spec.rb:86: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)>'

  27) BeloteDeck hand #carre_of_aces? behaves like carre-checking method returns false when there is no carre
     Failure/Error: expect(hand.public_send(method)).to be false
     ArgumentError:
       wrong number of arguments (1 for 0)
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-39p1jf/spec.rb:394
     # /tmp/d20151112-27349-39p1jf/solution.rb:190:in `all?'
     # /tmp/d20151112-27349-39p1jf/solution.rb:190:in `block in has_carre?'
     # /tmp/d20151112-27349-39p1jf/solution.rb:189:in `each'
     # /tmp/d20151112-27349-39p1jf/solution.rb:189:in `each_cons'
     # /tmp/d20151112-27349-39p1jf/solution.rb:189:in `has_carre?'
     # /tmp/d20151112-27349-39p1jf/solution.rb:183:in `carre_of_aces?'
     # /tmp/d20151112-27349-39p1jf/spec.rb:101:in `public_send'
     # /tmp/d20151112-27349-39p1jf/spec.rb:101: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)>'

  28) SixtySixDeck behaves like a deck implements Enumerable
     Failure/Error: expect(small_deck).to respond_to(:each)
       expected #<SixtySixDeck:0x007fadea00e188 @cards=[#<Card:0x007fadea00e2c8 @rank=:ace, @suit=:spades, @deck=#<SixtySixDeck:0x007fadea00e188 ...>>, #<Card:0x007fadea00e250 @rank=9, @suit=:clubs, @deck=#<SixtySixDeck:0x007fadea00e188 ...>>]> to respond to :each
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-39p1jf/spec.rb:400
     # /tmp/d20151112-27349-39p1jf/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)>'

  29) 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:0x007fadea00f510>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-39p1jf/spec.rb:400
     # /tmp/d20151112-27349-39p1jf/spec.rb:18:in `to_a'
     # /tmp/d20151112-27349-39p1jf/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)>'

  30) 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:0x007fade9fc6fe0>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-39p1jf/spec.rb:400
     # /tmp/d20151112-27349-39p1jf/spec.rb:30:in `to_a'
     # /tmp/d20151112-27349-39p1jf/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)>'

  31) 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:0x007fade9fc48a8>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-39p1jf/spec.rb:400
     # /tmp/d20151112-27349-39p1jf/spec.rb:37:in `to_a'
     # /tmp/d20151112-27349-39p1jf/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)>'

  32) 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:0x007fade9f99090>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-39p1jf/spec.rb:400
     # /tmp/d20151112-27349-39p1jf/spec.rb:44:in `to_a'
     # /tmp/d20151112-27349-39p1jf/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)>'

  33) 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:0x007fade9de1ba8>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-39p1jf/spec.rb:400
     # /tmp/d20151112-27349-39p1jf/spec.rb:51:in `to_a'
     # /tmp/d20151112-27349-39p1jf/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)>'

  34) SixtySixDeck hand #twenty? returns true for king and queen not of the trump suit
     Failure/Error: expect(hand.twenty?(:hearts)).to be true
     ArgumentError:
       comparison of Card with Card failed
     # /tmp/d20151112-27349-39p1jf/solution.rb:211:in `sort'
     # /tmp/d20151112-27349-39p1jf/solution.rb:211:in `twenty?'
     # /tmp/d20151112-27349-39p1jf/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)>'

  35) SixtySixDeck hand #twenty? returns false for king and queen of the trump suit
     Failure/Error: expect(hand.twenty?(:clubs)).to be false
     ArgumentError:
       comparison of Card with Card failed
     # /tmp/d20151112-27349-39p1jf/solution.rb:211:in `sort'
     # /tmp/d20151112-27349-39p1jf/solution.rb:211:in `twenty?'
     # /tmp/d20151112-27349-39p1jf/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)>'

  36) 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
     ArgumentError:
       comparison of Card with Card failed
     # /tmp/d20151112-27349-39p1jf/solution.rb:211:in `sort'
     # /tmp/d20151112-27349-39p1jf/solution.rb:211:in `twenty?'
     # /tmp/d20151112-27349-39p1jf/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.03955 seconds
57 examples, 36 failures

Failed examples:

rspec /tmp/d20151112-27349-39p1jf/spec.rb:8 # WarDeck behaves like a deck implements Enumerable
rspec /tmp/d20151112-27349-39p1jf/spec.rb:14 # WarDeck behaves like a deck fills the deck if no initialize parameters are given
rspec /tmp/d20151112-27349-39p1jf/spec.rb:28 # WarDeck behaves like a deck #draw_top_card pops the top-most card
rspec /tmp/d20151112-27349-39p1jf/spec.rb:35 # WarDeck behaves like a deck #draw_bottom_card pops the bottom-most card
rspec /tmp/d20151112-27349-39p1jf/spec.rb:42 # WarDeck behaves like a deck #top peeks at the top-most card
rspec /tmp/d20151112-27349-39p1jf/spec.rb:49 # WarDeck behaves like a deck #bottom peeks at the bottom-most card
rspec /tmp/d20151112-27349-39p1jf/spec.rb:8 # BeloteDeck behaves like a deck implements Enumerable
rspec /tmp/d20151112-27349-39p1jf/spec.rb:14 # BeloteDeck behaves like a deck fills the deck if no initialize parameters are given
rspec /tmp/d20151112-27349-39p1jf/spec.rb:28 # BeloteDeck behaves like a deck #draw_top_card pops the top-most card
rspec /tmp/d20151112-27349-39p1jf/spec.rb:35 # BeloteDeck behaves like a deck #draw_bottom_card pops the bottom-most card
rspec /tmp/d20151112-27349-39p1jf/spec.rb:42 # BeloteDeck behaves like a deck #top peeks at the top-most card
rspec /tmp/d20151112-27349-39p1jf/spec.rb:49 # BeloteDeck behaves like a deck #bottom peeks at the bottom-most card
rspec /tmp/d20151112-27349-39p1jf/spec.rb:220 # BeloteDeck hand #highest_of_suit returns the strongest card of the specified suit
rspec /tmp/d20151112-27349-39p1jf/spec.rb:239 # BeloteDeck hand #belote? returns true if there is a king and a queen of the same suit
rspec /tmp/d20151112-27349-39p1jf/spec.rb:254 # BeloteDeck hand #belote? returns false when there is no king and queen of the same suit
rspec /tmp/d20151112-27349-39p1jf/spec.rb:272 # BeloteDeck hand #tierce? with tierce returns true for cards with names
rspec /tmp/d20151112-27349-39p1jf/spec.rb:304 # BeloteDeck hand #tierce? without tierce does not confuse cards with different suits
rspec /tmp/d20151112-27349-39p1jf/spec.rb:322 # BeloteDeck hand #quarte? detects four cards with increasing ranks
rspec /tmp/d20151112-27349-39p1jf/spec.rb:337 # BeloteDeck hand #quarte? does not return true if there is no quarte
rspec /tmp/d20151112-27349-39p1jf/spec.rb:354 # BeloteDeck hand #quint? detects five cards with increasing ranks
rspec /tmp/d20151112-27349-39p1jf/spec.rb:369 # BeloteDeck hand #quint? does not return true if there is no quint
rspec /tmp/d20151112-27349-39p1jf/spec.rb:74 # BeloteDeck hand #carre_of_jacks? behaves like carre-checking method returns true when there is a carre
rspec /tmp/d20151112-27349-39p1jf/spec.rb:89 # BeloteDeck hand #carre_of_jacks? behaves like carre-checking method returns false when there is no carre
rspec /tmp/d20151112-27349-39p1jf/spec.rb:74 # BeloteDeck hand #carre_of_nines? behaves like carre-checking method returns true when there is a carre
rspec /tmp/d20151112-27349-39p1jf/spec.rb:89 # BeloteDeck hand #carre_of_nines? behaves like carre-checking method returns false when there is no carre
rspec /tmp/d20151112-27349-39p1jf/spec.rb:74 # BeloteDeck hand #carre_of_aces? behaves like carre-checking method returns true when there is a carre
rspec /tmp/d20151112-27349-39p1jf/spec.rb:89 # BeloteDeck hand #carre_of_aces? behaves like carre-checking method returns false when there is no carre
rspec /tmp/d20151112-27349-39p1jf/spec.rb:8 # SixtySixDeck behaves like a deck implements Enumerable
rspec /tmp/d20151112-27349-39p1jf/spec.rb:14 # SixtySixDeck behaves like a deck fills the deck if no initialize parameters are given
rspec /tmp/d20151112-27349-39p1jf/spec.rb:28 # SixtySixDeck behaves like a deck #draw_top_card pops the top-most card
rspec /tmp/d20151112-27349-39p1jf/spec.rb:35 # SixtySixDeck behaves like a deck #draw_bottom_card pops the bottom-most card
rspec /tmp/d20151112-27349-39p1jf/spec.rb:42 # SixtySixDeck behaves like a deck #top peeks at the top-most card
rspec /tmp/d20151112-27349-39p1jf/spec.rb:49 # SixtySixDeck behaves like a deck #bottom peeks at the bottom-most card
rspec /tmp/d20151112-27349-39p1jf/spec.rb:429 # SixtySixDeck hand #twenty? returns true for king and queen not of the trump suit
rspec /tmp/d20151112-27349-39p1jf/spec.rb:442 # SixtySixDeck hand #twenty? returns false for king and queen of the trump suit
rspec /tmp/d20151112-27349-39p1jf/spec.rb:455 # SixtySixDeck hand #twenty? returns false for hands without a king and queen of the same suit

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

Мила обнови решението на 11.11.2015 10:25 (преди над 8 години)

+class Card
+ include Comparable
+
+ attr_accessor :rank, :suit, :deck
+
+ def initialize (rank, suit)
+ @rank = rank
+ @suit = suit
+ @deck = Deck.new([])
+ end
+
+ def to_s
+ "#{rank.to_s.capitalize} of #{suit.to_s.capitalize}"
+ end
+
+ def <=> (other)
+ suits_comparison = deck.suits.index(@suit) <=> deck.suits.index(other.suit)
+ if suits_comparison == 0
+ return rank_index <=> other.rank_index
+ end
+ return suits_comparison
+ end
+
+ def rank_index
+ deck.ranks.index(@rank)
+ end
+end
+
+class Deck
+ include Enumerable
+
+ attr_accessor :cards
+
+ def initialize(cards = nil)
+ if cards == nil
+ all_cards_deck
+ else
+ @cards = cards
+ end
+ @cards.each { |card| card.deck = self }
+ end
+
+ def all_cards_deck
+ @cards = Array.new
+ suits.each do |suit|
+ ranks.each do |rank|
+ @cards << Card.new(rank, suit)
+ end
+ end
+ end
+
+ def ranks
+ [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :qeen, :king, :ace].reverse
+ end
+
+ def suits
+ [:spades, :hearts, :diamonds, :clubs]
+ end
+
+ def size
+ @cards.size
+ end
+
+ def draw_top_card
+ @cards.slice!(0)
+ end
+
+ def draw_bottom_card
+ @cards.pop
+ end
+
+ def top_card
+ @cards.at(0)
+ end
+
+ def bottom_card
+ @cards.at(-1)
+ end
+
+ def shuffle
+ @cards.shuffle!
+ end
+
+ def sort
+ @cards.sort!
+ end
+
+ def to_s
+ @cards.join("\n")
+ end
+end
+
+class Hand
+ attr_accessor :cards
+
+ def initialize(cards = nil)
+ @cards = cards
+ end
+
+ def size
+ @cards.size
+ end
+end
+
+class WarHand < Hand
+ def play_card
+ @cards.pop
+ end
+
+ def allow_face_up?
+ size <= 3
+ end
+end
+
+class WarDeck < Deck
+ def deal
+ hand = Array.new(26) { draw_top_card }
+ WarHand.new(hand)
+ end
+end
+
+class BeloteHand < Hand
+ def highest_of_suit(suit)
+ suit_cards = @cards.select { |card| card.suit == suit }
+ suit_cards.sort.first
+ end
+
+ def belote?
+ @cards.sort.each_cons(2) do |cons|
+ if cons[0] == :king and cons[1] == :qeen and cons[0].suit == cons[1].suit
+ return true
+ end
+ end
+ return false
+ end
+
+ def tierce?
+ @cards.sort.each_cons(3) do |cons|
+ same_rank = cons[0].rank_index == (cons[-1].rank_index - 2)
+ same_suit = cons.all? { |card| card.suit == cons[0].suit }
+ if same_rank and same_suit
+ return true
+ end
+ end
+ return false
+ end
+
+ def quarte?
+ @cards.sort.each_cons(4) do |cons|
+ same_rank = cons[0].rank_index == (cons[-1].rank_index - 3)
+ same_suit = cons.all? { |card| card.suit == cons[0].suit }
+ if same_rank and same_suit
+ return true
+ end
+ end
+ return false
+ end
+
+ def quint?
+ @cards.sort.each_cons(5) do |cons|
+ same_rank = cons[0].rank_index == (cons[-1].rank_index - 4)
+ same_suit = cons.all? { |card| card.suit == cons[0].suit }
+ if same_rank and same_suit
+ return true
+ end
+ end
+ return false
+ end
+
+ def carre_of_jacks?
+ has_carre? do |card|
+ card.rank == :jack
+ end
+ end
+
+ def carre_of_nines?
+ has_carre? do |card|
+ card.rank == 9
+ end
+ end
+
+ def carre_of_aces?
+ has_carre? do |card|
+ card.rank == :ace
+ end
+ end
+
+ def has_carre? (&block)
+ @cards.sort.each_cons(4) do |cons|
+ if cons.all? block
+ return true
+ end
+ end
+ return false
+ end
+end
+
+class BeloteDeck < Deck
+ def ranks
+ [7, 8, 9, :jack, :qeen, :king, 10, :ace].reverse
+ end
+
+ def deal
+ hand = Array.new(8) { draw_top_card }
+ BeloteHand.new(hand)
+ end
+end
+
+class SixtySixHand < Hand
+ def twenty?(trump_suit)
+ @cards.sort.each_cons(2) do |cons|
+ rank_cards = cons[0] == :king and cons[1] == :qeen
+ suit_cards = cons[0].suit == cons[1].suit
+ if rank_cards and (suit_cards != trump_suit)
+ return true
+ end
+ end
+ return false
+ end
+
+ def forty?(trump_suit)
+ @cards.sort.each_cons(2) do |cons|
+ rank_cards = cons[0] == :king and cons[1] == :qeen
+ suit_cards = cons[0].suit == cons[1].suit
+ if rank_cards and (suit_cards == trump_suit)
+ return true
+ end
+ end
+ return false
+ end
+end
+
+class SixtySixDeck < Deck
+ def ranks
+ [9, :jack, :qeen, :king, 10, :ace].reverse
+ end
+
+ def deal
+ hand = Array.new(6) { draw_top_card }
+ SixtySixHand.new(hand)
+ end
+end

Имаш проблеми с идентацията на места. В следващата задача ще ти отнемем 2 точки за такива нарушения.

В някои методи има възможност за много по-елегантен код. Прегледай решенията на колеги и нашето примерно решение за алтернативни идеи.