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

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

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

Резултати

  • 3 точки от тестове
  • 1 отнета точка
  • 2 точки общо
  • 28 успешни тест(а)
  • 29 неуспешни тест(а)

Код

class Card
include Comparable
SUITS = [:clubs, :diamonds, :hearts, :spades]
RANKS = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
def initialize(rank,suit)
@rank = rank
@suit = suit
end
def rank
return @rank
end
def suit
return @suit
end
def to_s
return @rank.to_s.capitalize + " of " + @suit.to_s.capitalize
end
def ==(other)
return self.to_s == other.to_s
end
def <=>(other)
(RANKS.find_index(self.rank) <=> RANKS.find_index(other.rank)).nonzero? ||
(SUITS.find_index(self.suit) <=> SUITS.find_index(other.suit))
end
end
class Deck
include Enumerable
def standard_deck
suits = [:clubs, :diamonds, :hearts, :spades]
ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
ranks.product(suits).collect {|rank, suit| Card.new(rank,
suit)}
end
def initialize(deck = standard_deck)
@deck = deck.dup
end
def size
@deck.size
end
def draw_bottom_card
popped_card = @deck.last
@deck.delete_at(self.size - 1)
popped_card
end
def draw_top_card
popped_card = @deck.first
@deck.delete_at(0)
popped_card
end
def bottom_card
popped_card = @deck.last
end
def top_card
popped_card = @deck.first
end
def shuffle
@deck.shuffle!
end
def sort
@deck.sort!.reverse!
end
def to_a
@deck
end
def to_s
for item in @deck
item.to_s
end
end
def deal
end
end
class HandForWarDeck
def cards
@cards
end
def size
@cards.size
end
def initialize(cards = [])
@cards = cards
end
def play_card
popped_card = @cards.first
@cards.delete_at(0)
popped_card
end
def allow_face_up?
return @cards.size <= 3
end
end
class WarDeck < Deck
def deal
count = 26
cards = []
while count > 0
cards << self.draw_top_card
count -= 1
end
HandForWarDeck.new(cards)
end
end
class HandForBeloteDeck
def cards
@cards
end
def size
@cards.size
end
def initialize(cards = [])
@cards = cards
end
def highest_of_suit(suit)
@cards.max.filter{ |a| a.suit == suit }
end
def belote?
for suit in suits
if @cards.include? Card(:queen,suit) and @cards.include? Card(:king,suit)
return true
end
end
false
end
end
class BeloteDeck < Deck
def standard_deck
ranks = [7, 8, 9, :jack, :queen, :king, 10, :ace]
suits = [:clubs, :diamonds, :hearts, :spades]
cards = suits.product(ranks).map {|x| x.reverse }.collect {|rank,
suit| Card.new(rank, suit)}
end
def sort
look_up = {}
standard_deck.each_with_index do |item, index|
lookup[item] = index
end
@deck.sort_by do |item|
look_up.fetch(item)
end
@deck
end
end
class HandForSixtySixDeck
def cards
@cards
end
def size
@cards.size
end
def initialize(cards = [])
@cards = cards
end
def twenty?(trump_suit)
suit_for_twenty = [:clubs, :diamonds, :hearts, :spades] - trump_suit
for suit in suits
if @cards.include? Card(:queen,suit) and @cards.include? Card(:king,suit)
return true
end
end
false
end
def forty?(trump_suit)
return ( @cards.include? Card(:queen,trump_suit) and
@cards.include? Card(:king,trump_suit) )
end
end
class SixtySixDeck < Deck
def standard_deck
ranks = [9, :jack, :queen, :king, 10, :ace]
suits = [:clubs, :diamonds, :hearts, :spades]
cards = suits.product(ranks).map {|x| x.reverse }.collect {|rank,
suit| Card.new(rank, suit)}
end
def deal
count = 6
cards = []
while count > 0
cards << self.draw_top_card
count -= 1
end
HandForSixtySixDeck.new(cards)
end
end

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

...F.......FF...F.......FFFFFFFFFFFFFFFFFFFF.......FF.FFF

Failures:

  1) WarDeck behaves like a deck implements Enumerable
     Failure/Error: expect(small_deck).to respond_to(:each)
       expected #<WarDeck:0x007f30a4fb22b0 @deck=[#<Card:0x007f30a4fb2328 @rank=:ace, @suit=:spades>, #<Card:0x007f30a4fb2300 @rank=9, @suit=:clubs>]> to respond to :each
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1a174v7/spec.rb:140
     # /tmp/d20151112-27349-1a174v7/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 #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:0x007f30a4952f78>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1a174v7/spec.rb:140
     # /tmp/d20151112-27349-1a174v7/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)>'

  3) 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:0x007f30a4949978 @rank=:jack, @suit=:spades>, #<Card:0x007f30a4949928 @rank=10, @suit=:hearts>, #<Card:0x007f30a49499a0 @rank=:ace, @suit=:clubs>, #<Card:0x007f30a4949950 @rank=2, @suit=:clubs>]
            got: [#<Card:0x007f30a49499a0 @rank=:ace, @suit=:clubs>, #<Card:0x007f30a4949978 @rank=:jack, @suit=:spades>, #<Card:0x007f30a4949928 @rank=10, @suit=:hearts>, #<Card:0x007f30a4949950 @rank=2, @suit=:clubs>]
       
       (compared using ==)
       
       Diff:
       
       @@ -1,5 +1,5 @@
       -[#<Card:0x007f30a4949978 @rank=:jack, @suit=:spades>,
       +[#<Card:0x007f30a49499a0 @rank=:ace, @suit=:clubs>,
       + #<Card:0x007f30a4949978 @rank=:jack, @suit=:spades>,
         #<Card:0x007f30a4949928 @rank=10, @suit=:hearts>,
       - #<Card:0x007f30a49499a0 @rank=:ace, @suit=:clubs>,
         #<Card:0x007f30a4949950 @rank=2, @suit=:clubs>]
     # /tmp/d20151112-27349-1a174v7/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)>'

  4) BeloteDeck behaves like a deck implements Enumerable
     Failure/Error: expect(small_deck).to respond_to(:each)
       expected #<BeloteDeck:0x007f30a4a76aa8 @deck=[#<Card:0x007f30a4a76c10 @rank=:ace, @suit=:spades>, #<Card:0x007f30a4a76be8 @rank=9, @suit=:clubs>]> to respond to :each
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1a174v7/spec.rb:191
     # /tmp/d20151112-27349-1a174v7/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)>'

  5) 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:0x007f30a4dc9c78>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1a174v7/spec.rb:191
     # /tmp/d20151112-27349-1a174v7/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)>'

  6) 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]
     NameError:
       undefined local variable or method `lookup' for #<BeloteDeck:0x007f30a4db6e70>
     # /tmp/d20151112-27349-1a174v7/solution.rb:182:in `block in sort'
     # /tmp/d20151112-27349-1a174v7/solution.rb:181:in `each'
     # /tmp/d20151112-27349-1a174v7/solution.rb:181:in `each_with_index'
     # /tmp/d20151112-27349-1a174v7/solution.rb:181:in `sort'
     # /tmp/d20151112-27349-1a174v7/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)>'

  7) BeloteDeck hand #deal deals 8 cards
     Failure/Error: expect(hand.size).to eq 8
     NoMethodError:
       undefined method `size' for nil:NilClass
     # /tmp/d20151112-27349-1a174v7/spec.rb:215: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)>'

  8) 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)
     NoMethodError:
       undefined method `highest_of_suit' for nil:NilClass
     # /tmp/d20151112-27349-1a174v7/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)>'

  9) 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
     NoMethodError:
       undefined method `belote?' for nil:NilClass
     # /tmp/d20151112-27349-1a174v7/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)>'

  10) BeloteDeck hand #belote? returns false when there is no king and queen of the same suit
     Failure/Error: expect(hand.belote?).to be false
     NoMethodError:
       undefined method `belote?' for nil:NilClass
     # /tmp/d20151112-27349-1a174v7/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)>'

  11) BeloteDeck hand #tierce? with tierce returns true for cards with names
     Failure/Error: expect(hand.tierce?).to be true
     NoMethodError:
       undefined method `tierce?' for nil:NilClass
     # /tmp/d20151112-27349-1a174v7/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)>'

  12) BeloteDeck hand #tierce? with tierce returns true for cards with numbers
     Failure/Error: expect(hand.tierce?).to be true
     NoMethodError:
       undefined method `tierce?' for nil:NilClass
     # /tmp/d20151112-27349-1a174v7/spec.rb:299: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)>'

  13) BeloteDeck hand #tierce? without tierce does not confuse cards with different suits
     Failure/Error: expect(hand.tierce?).to be false
     NoMethodError:
       undefined method `tierce?' for nil:NilClass
     # /tmp/d20151112-27349-1a174v7/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)>'

  14) BeloteDeck hand #quarte? detects four cards with increasing ranks
     Failure/Error: expect(hand.quarte?).to be true
     NoMethodError:
       undefined method `quarte?' for nil:NilClass
     # /tmp/d20151112-27349-1a174v7/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)>'

  15) BeloteDeck hand #quarte? does not return true if there is no quarte
     Failure/Error: expect(hand.quarte?).to be false
     NoMethodError:
       undefined method `quarte?' for nil:NilClass
     # /tmp/d20151112-27349-1a174v7/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)>'

  16) BeloteDeck hand #quint? detects five cards with increasing ranks
     Failure/Error: expect(hand.quint?).to be true
     NoMethodError:
       undefined method `quint?' for nil:NilClass
     # /tmp/d20151112-27349-1a174v7/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)>'

  17) BeloteDeck hand #quint? does not return true if there is no quint
     Failure/Error: expect(hand.quint?).to be false
     NoMethodError:
       undefined method `quint?' for nil:NilClass
     # /tmp/d20151112-27349-1a174v7/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)>'

  18) 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
     NoMethodError:
       undefined method `carre_of_jacks?' for nil:NilClass
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1a174v7/spec.rb:386
     # /tmp/d20151112-27349-1a174v7/spec.rb:86:in `public_send'
     # /tmp/d20151112-27349-1a174v7/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)>'

  19) 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
     NoMethodError:
       undefined method `carre_of_jacks?' for nil:NilClass
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1a174v7/spec.rb:386
     # /tmp/d20151112-27349-1a174v7/spec.rb:101:in `public_send'
     # /tmp/d20151112-27349-1a174v7/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)>'

  20) 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
     NoMethodError:
       undefined method `carre_of_nines?' for nil:NilClass
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1a174v7/spec.rb:390
     # /tmp/d20151112-27349-1a174v7/spec.rb:86:in `public_send'
     # /tmp/d20151112-27349-1a174v7/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)>'

  21) 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
     NoMethodError:
       undefined method `carre_of_nines?' for nil:NilClass
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1a174v7/spec.rb:390
     # /tmp/d20151112-27349-1a174v7/spec.rb:101:in `public_send'
     # /tmp/d20151112-27349-1a174v7/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)>'

  22) 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
     NoMethodError:
       undefined method `carre_of_aces?' for nil:NilClass
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1a174v7/spec.rb:394
     # /tmp/d20151112-27349-1a174v7/spec.rb:86:in `public_send'
     # /tmp/d20151112-27349-1a174v7/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_aces? behaves like carre-checking method returns false when there is no carre
     Failure/Error: expect(hand.public_send(method)).to be false
     NoMethodError:
       undefined method `carre_of_aces?' for nil:NilClass
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1a174v7/spec.rb:394
     # /tmp/d20151112-27349-1a174v7/spec.rb:101:in `public_send'
     # /tmp/d20151112-27349-1a174v7/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) SixtySixDeck behaves like a deck implements Enumerable
     Failure/Error: expect(small_deck).to respond_to(:each)
       expected #<SixtySixDeck:0x007f30a4f2b918 @deck=[#<Card:0x007f30a4f2b990 @rank=:ace, @suit=:spades>, #<Card:0x007f30a4f2b968 @rank=9, @suit=:clubs>]> to respond to :each
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1a174v7/spec.rb:400
     # /tmp/d20151112-27349-1a174v7/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)>'

  25) 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:0x007f30a4fd73a8>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1a174v7/spec.rb:400
     # /tmp/d20151112-27349-1a174v7/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)>'

  26) 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:0x007f30a4fd4748 @rank=:jack, @suit=:spades>, #<Card:0x007f30a4fd45e0 @rank=10, @suit=:hearts>, #<Card:0x007f30a4fd4770 @rank=:ace, @suit=:clubs>, #<Card:0x007f30a4fd46f8 @rank=9, @suit=:clubs>]
            got: [#<Card:0x007f30a4fd4770 @rank=:ace, @suit=:clubs>, #<Card:0x007f30a4fd4748 @rank=:jack, @suit=:spades>, #<Card:0x007f30a4fd45e0 @rank=10, @suit=:hearts>, #<Card:0x007f30a4fd46f8 @rank=9, @suit=:clubs>]
       
       (compared using ==)
       
       Diff:
       
       @@ -1,5 +1,5 @@
       -[#<Card:0x007f30a4fd4748 @rank=:jack, @suit=:spades>,
       +[#<Card:0x007f30a4fd4770 @rank=:ace, @suit=:clubs>,
       + #<Card:0x007f30a4fd4748 @rank=:jack, @suit=:spades>,
         #<Card:0x007f30a4fd45e0 @rank=10, @suit=:hearts>,
       - #<Card:0x007f30a4fd4770 @rank=:ace, @suit=:clubs>,
         #<Card:0x007f30a4fd46f8 @rank=9, @suit=:clubs>]
     # /tmp/d20151112-27349-1a174v7/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)>'

  27) SixtySixDeck hand #twenty? returns true for king and queen not of the trump suit
     Failure/Error: expect(hand.twenty?(:hearts)).to be true
     TypeError:
       no implicit conversion of Symbol into Array
     # /tmp/d20151112-27349-1a174v7/solution.rb:208:in `-'
     # /tmp/d20151112-27349-1a174v7/solution.rb:208:in `twenty?'
     # /tmp/d20151112-27349-1a174v7/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)>'

  28) SixtySixDeck hand #twenty? returns false for king and queen of the trump suit
     Failure/Error: expect(hand.twenty?(:clubs)).to be false
     TypeError:
       no implicit conversion of Symbol into Array
     # /tmp/d20151112-27349-1a174v7/solution.rb:208:in `-'
     # /tmp/d20151112-27349-1a174v7/solution.rb:208:in `twenty?'
     # /tmp/d20151112-27349-1a174v7/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)>'

  29) 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
     TypeError:
       no implicit conversion of Symbol into Array
     # /tmp/d20151112-27349-1a174v7/solution.rb:208:in `-'
     # /tmp/d20151112-27349-1a174v7/solution.rb:208:in `twenty?'
     # /tmp/d20151112-27349-1a174v7/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.05984 seconds
57 examples, 29 failures

Failed examples:

rspec /tmp/d20151112-27349-1a174v7/spec.rb:8 # WarDeck behaves like a deck implements Enumerable
rspec /tmp/d20151112-27349-1a174v7/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-1a174v7/spec.rb:145 # WarDeck #sort sorts the cards in the defined order
rspec /tmp/d20151112-27349-1a174v7/spec.rb:8 # BeloteDeck behaves like a deck implements Enumerable
rspec /tmp/d20151112-27349-1a174v7/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-1a174v7/spec.rb:196 # BeloteDeck #sort sorts the cards in the defined order
rspec /tmp/d20151112-27349-1a174v7/spec.rb:212 # BeloteDeck hand #deal deals 8 cards
rspec /tmp/d20151112-27349-1a174v7/spec.rb:220 # BeloteDeck hand #highest_of_suit returns the strongest card of the specified suit
rspec /tmp/d20151112-27349-1a174v7/spec.rb:239 # BeloteDeck hand #belote? returns true if there is a king and a queen of the same suit
rspec /tmp/d20151112-27349-1a174v7/spec.rb:254 # BeloteDeck hand #belote? returns false when there is no king and queen of the same suit
rspec /tmp/d20151112-27349-1a174v7/spec.rb:272 # BeloteDeck hand #tierce? with tierce returns true for cards with names
rspec /tmp/d20151112-27349-1a174v7/spec.rb:287 # BeloteDeck hand #tierce? with tierce returns true for cards with numbers
rspec /tmp/d20151112-27349-1a174v7/spec.rb:304 # BeloteDeck hand #tierce? without tierce does not confuse cards with different suits
rspec /tmp/d20151112-27349-1a174v7/spec.rb:322 # BeloteDeck hand #quarte? detects four cards with increasing ranks
rspec /tmp/d20151112-27349-1a174v7/spec.rb:337 # BeloteDeck hand #quarte? does not return true if there is no quarte
rspec /tmp/d20151112-27349-1a174v7/spec.rb:354 # BeloteDeck hand #quint? detects five cards with increasing ranks
rspec /tmp/d20151112-27349-1a174v7/spec.rb:369 # BeloteDeck hand #quint? does not return true if there is no quint
rspec /tmp/d20151112-27349-1a174v7/spec.rb:74 # BeloteDeck hand #carre_of_jacks? behaves like carre-checking method returns true when there is a carre
rspec /tmp/d20151112-27349-1a174v7/spec.rb:89 # BeloteDeck hand #carre_of_jacks? behaves like carre-checking method returns false when there is no carre
rspec /tmp/d20151112-27349-1a174v7/spec.rb:74 # BeloteDeck hand #carre_of_nines? behaves like carre-checking method returns true when there is a carre
rspec /tmp/d20151112-27349-1a174v7/spec.rb:89 # BeloteDeck hand #carre_of_nines? behaves like carre-checking method returns false when there is no carre
rspec /tmp/d20151112-27349-1a174v7/spec.rb:74 # BeloteDeck hand #carre_of_aces? behaves like carre-checking method returns true when there is a carre
rspec /tmp/d20151112-27349-1a174v7/spec.rb:89 # BeloteDeck hand #carre_of_aces? behaves like carre-checking method returns false when there is no carre
rspec /tmp/d20151112-27349-1a174v7/spec.rb:8 # SixtySixDeck behaves like a deck implements Enumerable
rspec /tmp/d20151112-27349-1a174v7/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-1a174v7/spec.rb:405 # SixtySixDeck #sort sorts the cards in the defined order
rspec /tmp/d20151112-27349-1a174v7/spec.rb:429 # SixtySixDeck hand #twenty? returns true for king and queen not of the trump suit
rspec /tmp/d20151112-27349-1a174v7/spec.rb:442 # SixtySixDeck hand #twenty? returns false for king and queen of the trump suit
rspec /tmp/d20151112-27349-1a174v7/spec.rb:455 # SixtySixDeck hand #twenty? returns false for hands without a king and queen of the same suit

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

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

+class Card
+
+ include Comparable
+ SUITS = [:clubs, :diamonds, :hearts, :spades]
+ RANKS = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
+
+ def initialize(rank,suit)
+ @rank = rank
+ @suit = suit
+ end
+
+ def rank
+ return @rank
+ end
+
+ def suit
+ return @suit
+ end
+
+ def to_s
+ return @rank.to_s.capitalize + " of " + @suit.to_s.capitalize
+ end
+
+ def ==(other)
+ return self.to_s == other.to_s
+ end
+
+ def <=>(other)
+ (RANKS.find_index(self.rank) <=> RANKS.find_index(other.rank)).nonzero? ||
+ (SUITS.find_index(self.suit) <=> SUITS.find_index(other.suit))
+ end
+
+end
+
+class Deck
+
+ include Enumerable
+ def standard_deck
+ suits = [:clubs, :diamonds, :hearts, :spades]
+ ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
+
+ ranks.product(suits).collect {|rank, suit| Card.new(rank,
+suit)}
+
+ end
+
+ def initialize(deck = standard_deck)
+ @deck = deck.dup
+ end
+
+ def size
+ @deck.size
+ end
+
+ def draw_bottom_card
+ popped_card = @deck.last
+ @deck.delete_at(self.size - 1)
+ popped_card
+ end
+
+ def draw_top_card
+ popped_card = @deck.first
+ @deck.delete_at(0)
+ popped_card
+ end
+
+ def bottom_card
+ popped_card = @deck.last
+ end
+
+ def top_card
+ popped_card = @deck.first
+ end
+
+ def shuffle
+ @deck.shuffle!
+ end
+
+ def sort
+ @deck.sort!.reverse!
+ end
+
+ def to_a
+ @deck
+ end
+
+ def to_s
+ for item in @deck
+ item.to_s
+ end
+ end
+
+ def deal
+
+ end
+
+end
+
+class HandForWarDeck
+
+ def cards
+ @cards
+ end
+
+ def size
+ @cards.size
+ end
+
+ def initialize(cards = [])
+ @cards = cards
+ end
+
+ def play_card
+
+ popped_card = @cards.first
+ @cards.delete_at(0)
+ popped_card
+
+ end
+
+ def allow_face_up?
+ return @cards.size <= 3
+ end
+
+end
+
+class WarDeck < Deck
+ def deal
+ count = 26
+ cards = []
+ while count > 0
+ cards << self.draw_top_card
+ count -= 1
+ end
+
+ HandForWarDeck.new(cards)
+ end
+
+end
+
+class HandForBeloteDeck
+
+ def cards
+ @cards
+ end
+
+ def size
+ @cards.size
+ end
+
+ def initialize(cards = [])
+ @cards = cards
+ end
+
+ def highest_of_suit(suit)
+ @cards.max.filter{ |a| a.suit == suit }
+ end
+
+ def belote?
+ for suit in suits
+ if @cards.include? Card(:queen,suit) and @cards.include? Card(:king,suit)
+ return true
+ end
+ end
+ false
+ end
+
+end
+
+class BeloteDeck < Deck
+
+ def standard_deck
+ ranks = [7, 8, 9, :jack, :queen, :king, 10, :ace]
+ suits = [:clubs, :diamonds, :hearts, :spades]
+ cards = suits.product(ranks).map {|x| x.reverse }.collect {|rank,
+ suit| Card.new(rank, suit)}
+ end
+
+ def sort
+ look_up = {}
+ standard_deck.each_with_index do |item, index|
+ lookup[item] = index
+ end
+ @deck.sort_by do |item|
+ look_up.fetch(item)
+ end
+ @deck
+ end
+
+end
+
+class HandForSixtySixDeck
+
+ def cards
+ @cards
+ end
+
+ def size
+ @cards.size
+ end
+
+ def initialize(cards = [])
+ @cards = cards
+ end
+
+ def twenty?(trump_suit)
+
+ suit_for_twenty = [:clubs, :diamonds, :hearts, :spades] - trump_suit
+ for suit in suits
+ if @cards.include? Card(:queen,suit) and @cards.include? Card(:king,suit)
+ return true
+ end
+ end
+ false
+ end
+
+ def forty?(trump_suit)
+ return ( @cards.include? Card(:queen,trump_suit) and
+@cards.include? Card(:king,trump_suit) )
+ end
+
+end
+
+class SixtySixDeck < Deck
+
+ def standard_deck
+ ranks = [9, :jack, :queen, :king, 10, :ace]
+ suits = [:clubs, :diamonds, :hearts, :spades]
+ cards = suits.product(ranks).map {|x| x.reverse }.collect {|rank,
+ suit| Card.new(rank, suit)}
+ end
+
+ def deal
+ count = 6
+ cards = []
+ while count > 0
+ cards << self.draw_top_card
+ count -= 1
+ end
+
+ HandForSixtySixDeck.new(cards)
+ end
+
+end