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

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

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

Резултати

  • 1 точка от тестове
  • 1 отнета точка
  • 0 точки общо
  • 14 успешни тест(а)
  • 43 неуспешни тест(а)

Код

class Card
attr_accessor :rank, :suit
def initialize(rank, suit)
@rank = rank
@suit = suit
end
def rank
@rank
end
def suit
@suit
end
def to_s
@rank.to_s.capitalize + " of " + @suit.to_s.capitalize
end
def ==(other)
@suit == other.suit && @rank == other.rank
end
def suit_in_strings
case @suit
when 4 then @suit = :spades
when 3 then @suit = :hearts
when 2 then @suit = :diamonds
when 1 then @suit = :clubs
end
end
def suit_in_digit
case @suit
when :spades then @suit = 4
when :hearts then @suit = 3
when :diamonds then @suit = 2
when :clubs then @suit = 1
end
end
def rank_in_digit
case @rank
when :jack then @rank = 11
when :queen then @rank = 12
when :king then @rank = 13
when :ace then @rank = 14
end
end
def rank_in_strings
case @rank
when 11 then @rank = :jack
when 12 then @rank = :queen
when 13 then @rank = :king
when 14 then @rank = :ace
end
end
end
class Deck
def initialize(deck = full_deck)
@deck = deck
end
def full_deck
full = []
(1..4).each do |i|
(2..14).each{|j| full.push(Card.new(j,i))}
end
full.each do |card|
card.suit_in_strings
card.rank_in_strings
end
full
end
def size
@deck.size
end
def draw_top_card
@deck.delete_at(0)
end
def draw_bottom_card
@deck.delete_at(-1)
end
def bottom_card
@deck[0]
end
def bottom_card
@deck[-1]
end
def shuffle
@deck.shuffle!
end
def to_s
@deck.each{|card| puts card.to_s}
end
def sort
@deck.each{|card| card.suit_in_digit}
@deck.sort_by!{|card| [card.suit, card.rank]}
@deck.each{|card| card.suit_in_strings}
end
def king?(suit)
@deck.include? Card.new(:king, suit)
end
def queen?(suit)
Card.new(:queen, suit)
end
end
class WarDeck < Deck
def initialize(cards = full_deck)
super cards
end
def full_deck
full = []
(1..4).each do |i|
(2..14).each {full.push(Card.new(j,i))}
end
full.each do |card|
card.suit_in_strings
card.rank_in_strings
end
full
end
def deal
cards = []
i = 0
while(i < 26)
cards.push @deck.delete_at(0)
i = i + 1
end
WarDeck.new(cards)
end
def play_card
@deck.delete_at(7)
end
def allow_face_up?
@deck.size < 4
end
end
class BeloteDeck < Deck
def initialize(cards = full_deck)
super cards
end
def full_deck
full = []
(1..4).each do |i|
(7..14).each {|j| full.push(Card.new(j,i))}
end
full.each do |card|
card.suit_in_strings
card.rank_in_strings
end
full
end
def deck
@deck
end
def deal
cards = []
i = 0
while(i < 8)
cards.push @deck.delete_at(0)
i = i + 1
end
BeloteDeck(cards)
end
def highest_of_suit(suit)
cards = []
@deck.each do |card|
if(card.suit == suit)
cards.push card.rank_in_digit
end
end
#cards.max_by{|card| card.rank}
end
def carre_of_jacks?
carre? :jacks
end
def carre_of_nines?
carre? 9
end
def carre_of_aces?
carre? :ace
end
def carre?(some_rank)
counter = 0
@deck.each do |card|
if(card.rank == some_rank)
counter = counter + 1
end
return counter == 4
end
end
def belote?
return true if king?(:spades) and queen?(:spades)
return true if king?(:clubs) and queen?(:clubs)
return true if king?(:diamonds) and queen?(:diamonds)
return true if king?(:hearts) and queen?(:hearts)
end
end
class SixtySixDeck < Deck
def initialize(cards = full_deck)
super cards
end
def full_deck
full = []
(1..4).each do |i|
(9..14).each{|j| full.push(Card.new(j,i))}
end
full.each do |card|
card.suit_in_strings
card.rank_in_strings
end
full
end
def deal
cards = []
i = 0
while(i < 6)
cards.push @deck.delete_at(0)
i = i + 1
end
SixtySixDeck.new(cards)
end
def forty?(trump_suit)
return true if king?(trump_suit) and queen?(trump_suit)
end
def twenty?(trump_suit)
return false if forty?(trump_suit)
king?(:spades) and queen?(:spades)
king?(:clubs) and queen?(:clubs)
king?(:diamonds) and queen?(:diamonds)
king?(:hearts) and queen?(:hearts)
end
end

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

...FF.FFFF.Ace of Spades
9 of Clubs
FF..FFF.FFFF.Ace of Spades
9 of Clubs
FFFFFFFFFFFFFFFFFFFFF.FFFF.Ace of Spades
9 of Clubs
FF.F..

Failures:

  1) WarDeck behaves like a deck implements Enumerable
     Failure/Error: expect(deck_class).to include(Enumerable)
       expected WarDeck to include Enumerable
       Diff:
       @@ -1,2 +1,2 @@
       -[Enumerable]
       +WarDeck
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1um7w3a/spec.rb:140
     # /tmp/d20151112-27349-1um7w3a/spec.rb:9: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 `to_a' for #<WarDeck:0x007f5600f58770>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1um7w3a/spec.rb:140
     # /tmp/d20151112-27349-1um7w3a/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 `to_a' for #<WarDeck:0x007f5600f41700>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1um7w3a/spec.rb:140
     # /tmp/d20151112-27349-1um7w3a/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 `to_a' for #<WarDeck:0x007f5600f2b4f0>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1um7w3a/spec.rb:140
     # /tmp/d20151112-27349-1um7w3a/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.top_card).to eq ace_of_spades
     NoMethodError:
       undefined method `top_card' for #<WarDeck:0x007f5600f118c0>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1um7w3a/spec.rb:140
     # /tmp/d20151112-27349-1um7w3a/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.to_a).to eq [ace_of_spades, nine_of_clubs]
     NoMethodError:
       undefined method `to_a' for #<WarDeck:0x007f5600efe9a0>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1um7w3a/spec.rb:140
     # /tmp/d20151112-27349-1um7w3a/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) WarDeck behaves like a deck #to_s returns the names of the cards, each on its own line
     Failure/Error: expect(small_deck.to_s.strip).to eq "Ace of Spades\n9 of Clubs"
     NoMethodError:
       undefined method `strip' for #<Array:0x007f5600ed8688>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1um7w3a/spec.rb:140
     # /tmp/d20151112-27349-1um7w3a/spec.rb:68: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) 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]
     ArgumentError:
       comparison of Array with Array failed
     # /tmp/d20151112-27349-1um7w3a/solution.rb:91:in `sort_by'
     # /tmp/d20151112-27349-1um7w3a/solution.rb:91:in `sort_by!'
     # /tmp/d20151112-27349-1um7w3a/solution.rb:91:in `sort'
     # /tmp/d20151112-27349-1um7w3a/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)>'

  9) WarDeck hand #allow_face_up? returns true if the cards are less than or equal to 3
     Failure/Error: expect(hand.allow_face_up?).to eq true
       
       expected: true
            got: false
       
       (compared using ==)
     # /tmp/d20151112-27349-1um7w3a/spec.rb:178: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)>'

  10) BeloteDeck behaves like a deck implements Enumerable
     Failure/Error: expect(deck_class).to include(Enumerable)
       expected BeloteDeck to include Enumerable
       Diff:
       @@ -1,2 +1,2 @@
       -[Enumerable]
       +BeloteDeck
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1um7w3a/spec.rb:191
     # /tmp/d20151112-27349-1um7w3a/spec.rb:9: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)>'

  11) 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 `to_a' for #<BeloteDeck:0x007f5600e31158>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1um7w3a/spec.rb:191
     # /tmp/d20151112-27349-1um7w3a/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)>'

  12) 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 `to_a' for #<BeloteDeck:0x007f5600e15c50>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1um7w3a/spec.rb:191
     # /tmp/d20151112-27349-1um7w3a/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)>'

  13) 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 `to_a' for #<BeloteDeck:0x007f5600e0a328>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1um7w3a/spec.rb:191
     # /tmp/d20151112-27349-1um7w3a/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)>'

  14) BeloteDeck behaves like a deck #top peeks at the top-most card
     Failure/Error: expect(small_deck.top_card).to eq ace_of_spades
     NoMethodError:
       undefined method `top_card' for #<BeloteDeck:0x007f5600e02d30>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1um7w3a/spec.rb:191
     # /tmp/d20151112-27349-1um7w3a/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)>'

  15) 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 `to_a' for #<BeloteDeck:0x007f5600e001c0>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1um7w3a/spec.rb:191
     # /tmp/d20151112-27349-1um7w3a/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)>'

  16) BeloteDeck behaves like a deck #to_s returns the names of the cards, each on its own line
     Failure/Error: expect(small_deck.to_s.strip).to eq "Ace of Spades\n9 of Clubs"
     NoMethodError:
       undefined method `strip' for #<Array:0x007f5600dd4de0>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1um7w3a/spec.rb:191
     # /tmp/d20151112-27349-1um7w3a/spec.rb:68:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  17) 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]
     ArgumentError:
       comparison of Array with Array failed
     # /tmp/d20151112-27349-1um7w3a/solution.rb:91:in `sort_by'
     # /tmp/d20151112-27349-1um7w3a/solution.rb:91:in `sort_by!'
     # /tmp/d20151112-27349-1um7w3a/solution.rb:91:in `sort'
     # /tmp/d20151112-27349-1um7w3a/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)>'

  18) BeloteDeck hand #deal deals 8 cards
     Failure/Error: hand = BeloteDeck.new.deal
     NoMethodError:
       undefined method `BeloteDeck' for #<BeloteDeck:0x007f5600d9d908>
     # /tmp/d20151112-27349-1um7w3a/solution.rb:158:in `deal'
     # /tmp/d20151112-27349-1um7w3a/spec.rb:213: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 #highest_of_suit returns the strongest card of the specified suit
     Failure/Error: ]).deal
     NoMethodError:
       undefined method `BeloteDeck' for #<BeloteDeck:0x007f5600d965b8 @deck=[]>
     # /tmp/d20151112-27349-1um7w3a/solution.rb:158:in `deal'
     # /tmp/d20151112-27349-1um7w3a/spec.rb:230: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 #belote? returns true if there is a king and a queen of the same suit
     Failure/Error: ]).deal
     NoMethodError:
       undefined method `BeloteDeck' for #<BeloteDeck:0x007f5600d6b598 @deck=[]>
     # /tmp/d20151112-27349-1um7w3a/solution.rb:158:in `deal'
     # /tmp/d20151112-27349-1um7w3a/spec.rb:249: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 #belote? returns false when there is no king and queen of the same suit
     Failure/Error: ]).deal
     NoMethodError:
       undefined method `BeloteDeck' for #<BeloteDeck:0x007f5600d5ed70 @deck=[]>
     # /tmp/d20151112-27349-1um7w3a/solution.rb:158:in `deal'
     # /tmp/d20151112-27349-1um7w3a/spec.rb:264: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 #tierce? with tierce returns true for cards with names
     Failure/Error: ]).deal
     NoMethodError:
       undefined method `BeloteDeck' for #<BeloteDeck:0x007f5600d4bc98 @deck=[]>
     # /tmp/d20151112-27349-1um7w3a/solution.rb:158:in `deal'
     # /tmp/d20151112-27349-1um7w3a/spec.rb:282: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)>'

  23) BeloteDeck hand #tierce? with tierce returns true for cards with numbers
     Failure/Error: ]).deal
     NoMethodError:
       undefined method `BeloteDeck' for #<BeloteDeck:0x007f5600d48cf0 @deck=[]>
     # /tmp/d20151112-27349-1um7w3a/solution.rb:158:in `deal'
     # /tmp/d20151112-27349-1um7w3a/spec.rb:297: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)>'

  24) BeloteDeck hand #tierce? without tierce does not confuse cards with different suits
     Failure/Error: ]).deal
     NoMethodError:
       undefined method `BeloteDeck' for #<BeloteDeck:0x007f5600ccd258 @deck=[]>
     # /tmp/d20151112-27349-1um7w3a/solution.rb:158:in `deal'
     # /tmp/d20151112-27349-1um7w3a/spec.rb:314: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)>'

  25) BeloteDeck hand #quarte? detects four cards with increasing ranks
     Failure/Error: ]).deal
     NoMethodError:
       undefined method `BeloteDeck' for #<BeloteDeck:0x007f5600c76160 @deck=[]>
     # /tmp/d20151112-27349-1um7w3a/solution.rb:158:in `deal'
     # /tmp/d20151112-27349-1um7w3a/spec.rb:332: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)>'

  26) BeloteDeck hand #quarte? does not return true if there is no quarte
     Failure/Error: ]).deal
     NoMethodError:
       undefined method `BeloteDeck' for #<BeloteDeck:0x007f5600b52a90 @deck=[]>
     # /tmp/d20151112-27349-1um7w3a/solution.rb:158:in `deal'
     # /tmp/d20151112-27349-1um7w3a/spec.rb:347: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)>'

  27) BeloteDeck hand #quint? detects five cards with increasing ranks
     Failure/Error: ]).deal
     NoMethodError:
       undefined method `BeloteDeck' for #<BeloteDeck:0x007f5600b25220 @deck=[]>
     # /tmp/d20151112-27349-1um7w3a/solution.rb:158:in `deal'
     # /tmp/d20151112-27349-1um7w3a/spec.rb:364: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)>'

  28) BeloteDeck hand #quint? does not return true if there is no quint
     Failure/Error: ]).deal
     NoMethodError:
       undefined method `BeloteDeck' for #<BeloteDeck:0x007f5600b16900 @deck=[]>
     # /tmp/d20151112-27349-1um7w3a/solution.rb:158:in `deal'
     # /tmp/d20151112-27349-1um7w3a/spec.rb:379: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)>'

  29) BeloteDeck hand #carre_of_jacks? behaves like carre-checking method returns true when there is a carre
     Failure/Error: ]).deal
     NoMethodError:
       undefined method `BeloteDeck' for #<BeloteDeck:0x007f5600b0f038 @deck=[]>
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1um7w3a/spec.rb:386
     # /tmp/d20151112-27349-1um7w3a/solution.rb:158:in `deal'
     # /tmp/d20151112-27349-1um7w3a/spec.rb:84: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) BeloteDeck hand #carre_of_jacks? behaves like carre-checking method returns false when there is no carre
     Failure/Error: ]).deal
     NoMethodError:
       undefined method `BeloteDeck' for #<BeloteDeck:0x007f5600b034e0 @deck=[]>
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1um7w3a/spec.rb:386
     # /tmp/d20151112-27349-1um7w3a/solution.rb:158:in `deal'
     # /tmp/d20151112-27349-1um7w3a/spec.rb:99: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)>'

  31) BeloteDeck hand #carre_of_nines? behaves like carre-checking method returns true when there is a carre
     Failure/Error: ]).deal
     NoMethodError:
       undefined method `BeloteDeck' for #<BeloteDeck:0x007f5600ad3f38 @deck=[]>
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1um7w3a/spec.rb:390
     # /tmp/d20151112-27349-1um7w3a/solution.rb:158:in `deal'
     # /tmp/d20151112-27349-1um7w3a/spec.rb:84: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)>'

  32) BeloteDeck hand #carre_of_nines? behaves like carre-checking method returns false when there is no carre
     Failure/Error: ]).deal
     NoMethodError:
       undefined method `BeloteDeck' for #<BeloteDeck:0x007f5600ac71e8 @deck=[]>
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1um7w3a/spec.rb:390
     # /tmp/d20151112-27349-1um7w3a/solution.rb:158:in `deal'
     # /tmp/d20151112-27349-1um7w3a/spec.rb:99: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)>'

  33) BeloteDeck hand #carre_of_aces? behaves like carre-checking method returns true when there is a carre
     Failure/Error: ]).deal
     NoMethodError:
       undefined method `BeloteDeck' for #<BeloteDeck:0x007f5600aa9c60 @deck=[]>
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1um7w3a/spec.rb:394
     # /tmp/d20151112-27349-1um7w3a/solution.rb:158:in `deal'
     # /tmp/d20151112-27349-1um7w3a/spec.rb:84: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)>'

  34) BeloteDeck hand #carre_of_aces? behaves like carre-checking method returns false when there is no carre
     Failure/Error: ]).deal
     NoMethodError:
       undefined method `BeloteDeck' for #<BeloteDeck:0x007f5600a9e978 @deck=[]>
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1um7w3a/spec.rb:394
     # /tmp/d20151112-27349-1um7w3a/solution.rb:158:in `deal'
     # /tmp/d20151112-27349-1um7w3a/spec.rb:99: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)>'

  35) SixtySixDeck behaves like a deck implements Enumerable
     Failure/Error: expect(deck_class).to include(Enumerable)
       expected SixtySixDeck to include Enumerable
       Diff:
       @@ -1,2 +1,2 @@
       -[Enumerable]
       +SixtySixDeck
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1um7w3a/spec.rb:400
     # /tmp/d20151112-27349-1um7w3a/spec.rb:9: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)>'

  36) 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 `to_a' for #<SixtySixDeck:0x007f5600a50ca0>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1um7w3a/spec.rb:400
     # /tmp/d20151112-27349-1um7w3a/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)>'

  37) 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 `to_a' for #<SixtySixDeck:0x007f5600a32570>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1um7w3a/spec.rb:400
     # /tmp/d20151112-27349-1um7w3a/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)>'

  38) 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 `to_a' for #<SixtySixDeck:0x007f5600a06678>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1um7w3a/spec.rb:400
     # /tmp/d20151112-27349-1um7w3a/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)>'

  39) SixtySixDeck behaves like a deck #top peeks at the top-most card
     Failure/Error: expect(small_deck.top_card).to eq ace_of_spades
     NoMethodError:
       undefined method `top_card' for #<SixtySixDeck:0x007f56009e4a28>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1um7w3a/spec.rb:400
     # /tmp/d20151112-27349-1um7w3a/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)>'

  40) 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 `to_a' for #<SixtySixDeck:0x007f5600991fa8>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1um7w3a/spec.rb:400
     # /tmp/d20151112-27349-1um7w3a/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)>'

  41) SixtySixDeck behaves like a deck #to_s returns the names of the cards, each on its own line
     Failure/Error: expect(small_deck.to_s.strip).to eq "Ace of Spades\n9 of Clubs"
     NoMethodError:
       undefined method `strip' for #<Array:0x007f5600983160>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1um7w3a/spec.rb:400
     # /tmp/d20151112-27349-1um7w3a/spec.rb:68: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)>'

  42) 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]
     ArgumentError:
       comparison of Array with Array failed
     # /tmp/d20151112-27349-1um7w3a/solution.rb:91:in `sort_by'
     # /tmp/d20151112-27349-1um7w3a/solution.rb:91:in `sort_by!'
     # /tmp/d20151112-27349-1um7w3a/solution.rb:91:in `sort'
     # /tmp/d20151112-27349-1um7w3a/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)>'

  43) SixtySixDeck hand #twenty? returns true for king and queen not of the trump suit
     Failure/Error: expect(hand.twenty?(:hearts)).to be true
       
       expected #<TrueClass:20> => true
            got #<FalseClass:0> => false
       
       Compared using equal?, which compares object identity,
       but expected and actual are not the same object. Use
       `expect(actual).to eq(expected)` if you don't care about
       object identity in this example.
     # /tmp/d20151112-27349-1um7w3a/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)>'

Finished in 0.04727 seconds
57 examples, 43 failures

Failed examples:

rspec /tmp/d20151112-27349-1um7w3a/spec.rb:8 # WarDeck behaves like a deck implements Enumerable
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:14 # WarDeck behaves like a deck fills the deck if no initialize parameters are given
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:28 # WarDeck behaves like a deck #draw_top_card pops the top-most card
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:35 # WarDeck behaves like a deck #draw_bottom_card pops the bottom-most card
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:42 # WarDeck behaves like a deck #top peeks at the top-most card
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:49 # WarDeck behaves like a deck #bottom peeks at the bottom-most card
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:67 # WarDeck behaves like a deck #to_s returns the names of the cards, each on its own line
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:145 # WarDeck #sort sorts the cards in the defined order
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:175 # WarDeck hand #allow_face_up? returns true if the cards are less than or equal to 3
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:8 # BeloteDeck behaves like a deck implements Enumerable
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:14 # BeloteDeck behaves like a deck fills the deck if no initialize parameters are given
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:28 # BeloteDeck behaves like a deck #draw_top_card pops the top-most card
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:35 # BeloteDeck behaves like a deck #draw_bottom_card pops the bottom-most card
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:42 # BeloteDeck behaves like a deck #top peeks at the top-most card
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:49 # BeloteDeck behaves like a deck #bottom peeks at the bottom-most card
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:67 # BeloteDeck behaves like a deck #to_s returns the names of the cards, each on its own line
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:196 # BeloteDeck #sort sorts the cards in the defined order
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:212 # BeloteDeck hand #deal deals 8 cards
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:220 # BeloteDeck hand #highest_of_suit returns the strongest card of the specified suit
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:239 # BeloteDeck hand #belote? returns true if there is a king and a queen of the same suit
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:254 # BeloteDeck hand #belote? returns false when there is no king and queen of the same suit
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:272 # BeloteDeck hand #tierce? with tierce returns true for cards with names
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:287 # BeloteDeck hand #tierce? with tierce returns true for cards with numbers
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:304 # BeloteDeck hand #tierce? without tierce does not confuse cards with different suits
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:322 # BeloteDeck hand #quarte? detects four cards with increasing ranks
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:337 # BeloteDeck hand #quarte? does not return true if there is no quarte
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:354 # BeloteDeck hand #quint? detects five cards with increasing ranks
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:369 # BeloteDeck hand #quint? does not return true if there is no quint
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:74 # BeloteDeck hand #carre_of_jacks? behaves like carre-checking method returns true when there is a carre
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:89 # BeloteDeck hand #carre_of_jacks? behaves like carre-checking method returns false when there is no carre
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:74 # BeloteDeck hand #carre_of_nines? behaves like carre-checking method returns true when there is a carre
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:89 # BeloteDeck hand #carre_of_nines? behaves like carre-checking method returns false when there is no carre
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:74 # BeloteDeck hand #carre_of_aces? behaves like carre-checking method returns true when there is a carre
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:89 # BeloteDeck hand #carre_of_aces? behaves like carre-checking method returns false when there is no carre
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:8 # SixtySixDeck behaves like a deck implements Enumerable
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:14 # SixtySixDeck behaves like a deck fills the deck if no initialize parameters are given
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:28 # SixtySixDeck behaves like a deck #draw_top_card pops the top-most card
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:35 # SixtySixDeck behaves like a deck #draw_bottom_card pops the bottom-most card
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:42 # SixtySixDeck behaves like a deck #top peeks at the top-most card
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:49 # SixtySixDeck behaves like a deck #bottom peeks at the bottom-most card
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:67 # SixtySixDeck behaves like a deck #to_s returns the names of the cards, each on its own line
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:405 # SixtySixDeck #sort sorts the cards in the defined order
rspec /tmp/d20151112-27349-1um7w3a/spec.rb:429 # SixtySixDeck hand #twenty? returns true for king and queen not of the trump suit

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

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

+class Card
+ attr_accessor :rank, :suit
+ def initialize(rank, suit)
+ @rank = rank
+ @suit = suit
+ end
+ def rank
+ @rank
+ end
+ def suit
+ @suit
+ end
+ def to_s
+ @rank.to_s.capitalize + " of " + @suit.to_s.capitalize
+ end
+ def ==(other)
+ @suit == other.suit && @rank == other.rank
+ end
+ def suit_in_strings
+ case @suit
+ when 4 then @suit = :spades
+ when 3 then @suit = :hearts
+ when 2 then @suit = :diamonds
+ when 1 then @suit = :clubs
+ end
+ end
+ def suit_in_digit
+ case @suit
+ when :spades then @suit = 4
+ when :hearts then @suit = 3
+ when :diamonds then @suit = 2
+ when :clubs then @suit = 1
+ end
+ end
+ def rank_in_digit
+ case @rank
+ when :jack then @rank = 11
+ when :queen then @rank = 12
+ when :king then @rank = 13
+ when :ace then @rank = 14
+ end
+ end
+ def rank_in_strings
+ case @rank
+ when 11 then @rank = :jack
+ when 12 then @rank = :queen
+ when 13 then @rank = :king
+ when 14 then @rank = :ace
+ end
+ end
+end
+
+class Deck
+ def initialize(deck = full_deck)
+ @deck = deck
+ end
+ def full_deck
+ full = []
+ (1..4).each do |i|
+ (2..14).each{|j| full.push(Card.new(j,i))}
+ end
+ full.each do |card|
+ card.suit_in_strings
+ card.rank_in_strings
+ end
+ full
+ end
+ def size
+ @deck.size
+ end
+ def draw_top_card
+ @deck.delete_at(0)
+ end
+ def draw_bottom_card
+ @deck.delete_at(-1)
+ end
+ def bottom_card
+ @deck[0]
+ end
+ def bottom_card
+ @deck[-1]
+ end
+ def shuffle
+ @deck.shuffle!
+ end
+ def to_s
+ @deck.each{|card| puts card.to_s}
+ end
+ def sort
+ @deck.each{|card| card.suit_in_digit}
+ @deck.sort_by!{|card| [card.suit, card.rank]}
+ @deck.each{|card| card.suit_in_strings}
+ end
+ def king?(suit)
+ @deck.include? Card.new(:king, suit)
+ end
+ def queen?(suit)
+ Card.new(:queen, suit)
+ end
+end
+class WarDeck < Deck
+ def initialize(cards = full_deck)
+ super cards
+ end
+ def full_deck
+ full = []
+ (1..4).each do |i|
+ (2..14).each {full.push(Card.new(j,i))}
+ end
+ full.each do |card|
+ card.suit_in_strings
+ card.rank_in_strings
+ end
+ full
+ end
+ def deal
+ cards = []
+ i = 0
+ while(i < 26)
+ cards.push @deck.delete_at(0)
+ i = i + 1
+ end
+ WarDeck.new(cards)
+ end
+ def play_card
+ @deck.delete_at(7)
+ end
+ def allow_face_up?
+ @deck.size < 4
+ end
+end
+
+class BeloteDeck < Deck
+ def initialize(cards = full_deck)
+ super cards
+ end
+ def full_deck
+ full = []
+ (1..4).each do |i|
+ (7..14).each {|j| full.push(Card.new(j,i))}
+ end
+ full.each do |card|
+ card.suit_in_strings
+ card.rank_in_strings
+ end
+ full
+ end
+ def deck
+ @deck
+ end
+ def deal
+ cards = []
+ i = 0
+ while(i < 8)
+ cards.push @deck.delete_at(0)
+ i = i + 1
+ end
+ BeloteDeck(cards)
+ end
+ def highest_of_suit(suit)
+ cards = []
+ @deck.each do |card|
+ if(card.suit == suit)
+ cards.push card.rank_in_digit
+ end
+ end
+ #cards.max_by{|card| card.rank}
+ end
+ def carre_of_jacks?
+ carre? :jacks
+ end
+ def carre_of_nines?
+ carre? 9
+ end
+ def carre_of_aces?
+ carre? :ace
+ end
+ def carre?(some_rank)
+ counter = 0
+ @deck.each do |card|
+ if(card.rank == some_rank)
+ counter = counter + 1
+ end
+ return counter == 4
+ end
+ end
+ def belote?
+ return true if king?(:spades) and queen?(:spades)
+ return true if king?(:clubs) and queen?(:clubs)
+ return true if king?(:diamonds) and queen?(:diamonds)
+ return true if king?(:hearts) and queen?(:hearts)
+ end
+end
+class SixtySixDeck < Deck
+ def initialize(cards = full_deck)
+ super cards
+ end
+ def full_deck
+ full = []
+ (1..4).each do |i|
+ (9..14).each{|j| full.push(Card.new(j,i))}
+ end
+ full.each do |card|
+ card.suit_in_strings
+ card.rank_in_strings
+ end
+ full
+ end
+ def deal
+ cards = []
+ i = 0
+ while(i < 6)
+ cards.push @deck.delete_at(0)
+ i = i + 1
+ end
+ SixtySixDeck.new(cards)
+ end
+ def forty?(trump_suit)
+ return true if king?(trump_suit) and queen?(trump_suit)
+ end
+ def twenty?(trump_suit)
+ return false if forty?(trump_suit)
+ king?(:spades) and queen?(:spades)
+ king?(:clubs) and queen?(:clubs)
+ king?(:diamonds) and queen?(:diamonds)
+ king?(:hearts) and queen?(:hearts)
+ end
+end

Имаш значителни проблеми със спазването на конвенциите на места, направи справка с ръководството за стил.

Освен това, вадиш на екрана текст, вместо да го връщаш като стойност. Това води до наказателна точка.

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