Решение на Четвърта задача от Виктор Божилов

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

Към профила на Виктор Божилов

Резултати

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

Код

module WarCardValue
def war_suit
suit_value = {clubs: 1, diamonds: 2, hearts: 3, spades: 4}
suit_value[self.suit]
end
def war_rank
rank_value = {jack: 11, queen: 12, king: 13, ace: 14}
current_index = 2
while current_index <= 10
rank_value[current_index] = current_index
current_index += 1
end
rank_value[self.rank]
end
end
module BeloteCardValue #belote and 66
def belote_suit
suit_value = {clubs: 1, diamonds: 2, hearts: 3, spades: 4}
suit_value[self.suit]
end
def belote_rank
rank_value = {jack: 10, queen: 11, king: 12, ace: 14}
rank_value[0] = 0 #hack
rank_value[7] = 7
rank_value[8] = 8
rank_value[9] = 9
rank_value[10] = 13
rank_value[self.rank]
end
end
class Card
include WarCardValue
include BeloteCardValue
attr_reader :suit
attr_reader :rank
def initialize (rank, suit)
@rank = rank
@suit = suit
end
def to_s
rank.to_s.capitalize + " of " + suit.to_s.capitalize
end
def == (other)
if other.rank == @rank and other.suit == suit
return true
end
return false
end
end
class Deck
include WarCardValue
include BeloteCardValue
include Enumerable
def initialize (cards)
@cards = cards
end
def each
current_index = 0
while current_index < @cards.size
yield @cards[current_index]
current_index += 1
end
end
def size
@cards.size
end
def top_card
@cards[0]
end
def bottom_card
@cards[-1]
end
def draw_top_card
@cards.delete_at(0)
end
def draw_bottom_card
@cards.delete_at(-1)
end
def shuffle
@cards.sort_by! {rand}
end
def to_s
@cards.each{|card| p card.to_s}
end
def sort
@cards.sort_by!{|a| [a.war_rank, a.war_suit]}
end
def deal(hand_size)
#@cards = @cards.take(hand_size) #hacks
@cards.slice!(hand_size, @cards.size - 1)
end
end
class WarDeck < Deck
alias old_sort sort
alias old_deal deal
def play_card
draw_top_card
end
def allow_face_up?
if (size <= 3) then true
else false
end
end
def deal
old_deal(26)
end
end
class SixtySixDeck < Deck
alias old_sort sort
alias old_deal deal
def deal
old_deal(6)
end
def sort
@cards.sort_by!{|a| [a.belote_rank, a.belote_suit]}
end
def sort_by_suits
@cards.sort_by!{|a| [a.belote_suit, a.belote_rank]}
end
def king_and_queen
self.sort_by_suits
current = 0
until current == 5
if @cards[current].rank == :queen and @cards[current + 1].rank == :king and
@cards[current].suit == @cards[current + 1].suit
return current
end
current += 1
end
return false
end
def twenty? (trump_suit)
if king_and_queen and @cards[king_and_queen].suit != trump_suit
return true
else
return false
end
end
def forty? (trump_suit)
if king_and_queen and @cards[king_and_queen].suit == trump_suit
return true
else
return false
end
end
end
class BeloteDeck < Deck
alias old_sort sort
alias old_deal deal
def deal
old_deal(8)
end
def sort
@cards.sort_by!{|a| [a.belote_rank, a.belote_suit]}
end
def sort_by_suits
@cards.sort_by!{|a| [a.belote_suit, a.belote_rank]}
end
def highest_of_suit(suit)
highest = Card.new(0, suit)
@cards.each {|x| if x.belote_rank > highest.belote_rank and
x.suit == suit
highest = Card.new(x.rank, suit)
end
}
if(highest.rank == 0)
return nil
else
return highest
end
end
def belote?
self.sort_by_suits
current = 0
until current_index == 7
if @cards[current].rank == :queen and @cards[current + 1].rank == :king and
@cards[current].suit == @cards[current + 1].suit
return true
end
current += 1
end
return false
end
def three_in_a_roll(index)
if @cards[index].belote_rank + 1 == @cards[index + 1].belote_rank and
@cards[index].belote_rank + 2 == @cards[index + 2].belote_rank
return true
else return false
end
end
def four_in_a_roll(index)
if @cards[index].belote_rank + 1 == @cards[index + 1].belote_rank and
@cards[index].belote_rank + 2 == @cards[index + 2].belote_rank and
@cards[index].belote_rank + 3 == @cards[index + 3].belote_rank
return true
else return false
end
end
def five_in_a_roll(index)
if @cards[index].belote_rank + 1 == @cards[index + 1].belote_rank and
@cards[index].belote_rank + 2 == @cards[index + 2].belote_rank and
@cards[index].belote_rank + 3 == @cards[index + 3].belote_rank and
@cards[index].belote_rank + 4 == @cards[index + 4].belote_rank
return true
else return false
end
end
def tierce?
self.sort_by_suits
current = 0
until current == 6
if three_in_a_roll(current)
return true
end
current += 1
end
return false
end
def quarte?
self.sort_by_suits
current = 0
until current == 5
if four_in_a_roll(current)
return true
end
current += 1
end
return false
end
def quint?
self.sort_by_suits
current = 0
until current == 4
if five_in_a_roll(current)
return true
end
current += 1
end
return false
end
def carre_of_jacks?
if @cards.include?(Card.new(:jack, :clubs)) and
@cards.include?(Card.new(:jack, :diamonds)) and
@cards.include?(Card.new(:jack, :hearts)) and
@cards.include?(Card.new(:jack, :spades))
return true
else
return false
end
end
def carre_of_nines?
if @cards.include?(Card.new(9, :clubs)) and
@cards.include?(Card.new(9, :diamonds)) and
@cards.include?(Card.new(9, :hearts)) and
@cards.include?(Card.new(9, :spades))
return true
else
return false
end
end
def carre_of_ace?
if @cards.include?(Card.new(:ace, :clubs)) and
@cards.include?(Card.new(:ace, :diamonds)) and
@cards.include?(Card.new(:ace, :hearts)) and
@cards.include?(Card.new(:ace, :spades))
return true
else
return false
end
end
end

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

....F.....F"Ace of Spades"
"9 of Clubs"
FFFFF.F.....F"Ace of Spades"
"9 of Clubs"
FFFFFFFFFFFFFFFFFFF.F.....F"Ace of Spades"
"9 of Clubs"
FFFFFF

Failures:

  1) WarDeck behaves like a deck fills the deck if no initialize parameters are given
     Failure/Error: deck = deck_class.new
     ArgumentError:
       wrong number of arguments (0 for 1)
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-x244cf/spec.rb:140
     # /tmp/d20151112-27349-x244cf/solution.rb:58:in `initialize'
     # /tmp/d20151112-27349-x244cf/spec.rb:15:in `new'
     # /tmp/d20151112-27349-x244cf/spec.rb:15: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 #shuffle does not remove cards from the deck
     Failure/Error: deck = deck_class.new
     ArgumentError:
       wrong number of arguments (0 for 1)
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-x244cf/spec.rb:140
     # /tmp/d20151112-27349-x244cf/solution.rb:58:in `initialize'
     # /tmp/d20151112-27349-x244cf/spec.rb:57:in `new'
     # /tmp/d20151112-27349-x244cf/spec.rb:57: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 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:0x007fbd051c7c98>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-x244cf/spec.rb:140
     # /tmp/d20151112-27349-x244cf/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)>'

  4) 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:0x007fbd051c49d0 @rank=:jack, @suit=:spades>, #<Card:0x007fbd051c4980 @rank=10, @suit=:hearts>, #<Card:0x007fbd051c49f8 @rank=:ace, @suit=:clubs>, #<Card:0x007fbd051c49a8 @rank=2, @suit=:clubs>]
            got: [#<Card:0x007fbd051c49a8 @rank=2, @suit=:clubs>, #<Card:0x007fbd051c4980 @rank=10, @suit=:hearts>, #<Card:0x007fbd051c49d0 @rank=:jack, @suit=:spades>, #<Card:0x007fbd051c49f8 @rank=:ace, @suit=:clubs>]
       
       (compared using ==)
       
       Diff:
       
       @@ -1,5 +1,5 @@
       -[#<Card:0x007fbd051c49d0 @rank=:jack, @suit=:spades>,
       +[#<Card:0x007fbd051c49a8 @rank=2, @suit=:clubs>,
         #<Card:0x007fbd051c4980 @rank=10, @suit=:hearts>,
       - #<Card:0x007fbd051c49f8 @rank=:ace, @suit=:clubs>,
       - #<Card:0x007fbd051c49a8 @rank=2, @suit=:clubs>]
       + #<Card:0x007fbd051c49d0 @rank=:jack, @suit=:spades>,
       + #<Card:0x007fbd051c49f8 @rank=:ace, @suit=:clubs>]
     # /tmp/d20151112-27349-x244cf/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)>'

  5) WarDeck hand #deal deals 26 cards
     Failure/Error: hand = WarDeck.new.deal
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20151112-27349-x244cf/solution.rb:58:in `initialize'
     # /tmp/d20151112-27349-x244cf/spec.rb:162:in `new'
     # /tmp/d20151112-27349-x244cf/spec.rb:162: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)>'

  6) WarDeck hand #allow_face_up? returns false if the cards are more than 3
     Failure/Error: let(:hand) { WarDeck.new.deal }
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20151112-27349-x244cf/solution.rb:58:in `initialize'
     # /tmp/d20151112-27349-x244cf/spec.rb:169:in `new'
     # /tmp/d20151112-27349-x244cf/spec.rb:169:in `block (4 levels) in <top (required)>'
     # /tmp/d20151112-27349-x244cf/spec.rb:172: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)>'

  7) WarDeck hand #allow_face_up? returns true if the cards are less than or equal to 3
     Failure/Error: let(:hand) { WarDeck.new.deal }
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20151112-27349-x244cf/solution.rb:58:in `initialize'
     # /tmp/d20151112-27349-x244cf/spec.rb:169:in `new'
     # /tmp/d20151112-27349-x244cf/spec.rb:169:in `block (4 levels) in <top (required)>'
     # /tmp/d20151112-27349-x244cf/spec.rb:176:in `block (5 levels) in <top (required)>'
     # /tmp/d20151112-27349-x244cf/spec.rb:176:in `times'
     # /tmp/d20151112-27349-x244cf/spec.rb:176: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 behaves like a deck fills the deck if no initialize parameters are given
     Failure/Error: deck = deck_class.new
     ArgumentError:
       wrong number of arguments (0 for 1)
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-x244cf/spec.rb:191
     # /tmp/d20151112-27349-x244cf/solution.rb:58:in `initialize'
     # /tmp/d20151112-27349-x244cf/spec.rb:15:in `new'
     # /tmp/d20151112-27349-x244cf/spec.rb:15: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 #shuffle does not remove cards from the deck
     Failure/Error: deck = deck_class.new
     ArgumentError:
       wrong number of arguments (0 for 1)
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-x244cf/spec.rb:191
     # /tmp/d20151112-27349-x244cf/solution.rb:58:in `initialize'
     # /tmp/d20151112-27349-x244cf/spec.rb:57:in `new'
     # /tmp/d20151112-27349-x244cf/spec.rb:57: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 #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:0x007fbd05164940>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-x244cf/spec.rb:191
     # /tmp/d20151112-27349-x244cf/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)>'

  11) BeloteDeck #sort sorts the cards in the defined order
     Failure/Error: expect(deck.sort.to_a).to eq [jack_of_spades, ten_of_hearts, ace_of_clubs, seven_of_clubs]
       
       expected: [#<Card:0x007fbd0511f278 @rank=:jack, @suit=:spades>, #<Card:0x007fbd0511f200 @rank=10, @suit=:hearts>, #<Card:0x007fbd0511f2c8 @rank=:ace, @suit=:clubs>, #<Card:0x007fbd0511f228 @rank=7, @suit=:clubs>]
            got: [#<Card:0x007fbd0511f228 @rank=7, @suit=:clubs>, #<Card:0x007fbd0511f278 @rank=:jack, @suit=:spades>, #<Card:0x007fbd0511f200 @rank=10, @suit=:hearts>, #<Card:0x007fbd0511f2c8 @rank=:ace, @suit=:clubs>]
       
       (compared using ==)
       
       Diff:
       
       @@ -1,5 +1,5 @@
       -[#<Card:0x007fbd0511f278 @rank=:jack, @suit=:spades>,
       +[#<Card:0x007fbd0511f228 @rank=7, @suit=:clubs>,
       + #<Card:0x007fbd0511f278 @rank=:jack, @suit=:spades>,
         #<Card:0x007fbd0511f200 @rank=10, @suit=:hearts>,
       - #<Card:0x007fbd0511f2c8 @rank=:ace, @suit=:clubs>,
       - #<Card:0x007fbd0511f228 @rank=7, @suit=:clubs>]
       + #<Card:0x007fbd0511f2c8 @rank=:ace, @suit=:clubs>]
     # /tmp/d20151112-27349-x244cf/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)>'

  12) BeloteDeck hand #deal deals 8 cards
     Failure/Error: hand = BeloteDeck.new.deal
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20151112-27349-x244cf/solution.rb:58:in `initialize'
     # /tmp/d20151112-27349-x244cf/spec.rb:213:in `new'
     # /tmp/d20151112-27349-x244cf/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)>'

  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)
     NoMethodError:
       undefined method `highest_of_suit' for []:Array
     # /tmp/d20151112-27349-x244cf/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
     NoMethodError:
       undefined method `belote?' for []:Array
     # /tmp/d20151112-27349-x244cf/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
     NoMethodError:
       undefined method `belote?' for []:Array
     # /tmp/d20151112-27349-x244cf/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
     NoMethodError:
       undefined method `tierce?' for []:Array
     # /tmp/d20151112-27349-x244cf/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? with tierce returns true for cards with numbers
     Failure/Error: expect(hand.tierce?).to be true
     NoMethodError:
       undefined method `tierce?' for []:Array
     # /tmp/d20151112-27349-x244cf/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)>'

  18) 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 []:Array
     # /tmp/d20151112-27349-x244cf/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)>'

  19) BeloteDeck hand #quarte? detects four cards with increasing ranks
     Failure/Error: expect(hand.quarte?).to be true
     NoMethodError:
       undefined method `quarte?' for []:Array
     # /tmp/d20151112-27349-x244cf/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)>'

  20) 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 []:Array
     # /tmp/d20151112-27349-x244cf/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)>'

  21) BeloteDeck hand #quint? detects five cards with increasing ranks
     Failure/Error: expect(hand.quint?).to be true
     NoMethodError:
       undefined method `quint?' for []:Array
     # /tmp/d20151112-27349-x244cf/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)>'

  22) 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 []:Array
     # /tmp/d20151112-27349-x244cf/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)>'

  23) 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 []:Array
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-x244cf/spec.rb:386
     # /tmp/d20151112-27349-x244cf/spec.rb:86:in `public_send'
     # /tmp/d20151112-27349-x244cf/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)>'

  24) 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 []:Array
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-x244cf/spec.rb:386
     # /tmp/d20151112-27349-x244cf/spec.rb:101:in `public_send'
     # /tmp/d20151112-27349-x244cf/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)>'

  25) 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 []:Array
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-x244cf/spec.rb:390
     # /tmp/d20151112-27349-x244cf/spec.rb:86:in `public_send'
     # /tmp/d20151112-27349-x244cf/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)>'

  26) 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 []:Array
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-x244cf/spec.rb:390
     # /tmp/d20151112-27349-x244cf/spec.rb:101:in `public_send'
     # /tmp/d20151112-27349-x244cf/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)>'

  27) 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 []:Array
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-x244cf/spec.rb:394
     # /tmp/d20151112-27349-x244cf/spec.rb:86:in `public_send'
     # /tmp/d20151112-27349-x244cf/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)>'

  28) 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 []:Array
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-x244cf/spec.rb:394
     # /tmp/d20151112-27349-x244cf/spec.rb:101:in `public_send'
     # /tmp/d20151112-27349-x244cf/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)>'

  29) SixtySixDeck behaves like a deck fills the deck if no initialize parameters are given
     Failure/Error: deck = deck_class.new
     ArgumentError:
       wrong number of arguments (0 for 1)
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-x244cf/spec.rb:400
     # /tmp/d20151112-27349-x244cf/solution.rb:58:in `initialize'
     # /tmp/d20151112-27349-x244cf/spec.rb:15:in `new'
     # /tmp/d20151112-27349-x244cf/spec.rb:15: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 #shuffle does not remove cards from the deck
     Failure/Error: deck = deck_class.new
     ArgumentError:
       wrong number of arguments (0 for 1)
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-x244cf/spec.rb:400
     # /tmp/d20151112-27349-x244cf/solution.rb:58:in `initialize'
     # /tmp/d20151112-27349-x244cf/spec.rb:57:in `new'
     # /tmp/d20151112-27349-x244cf/spec.rb:57: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 #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:0x007fbd04f30868>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-x244cf/spec.rb:400
     # /tmp/d20151112-27349-x244cf/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)>'

  32) 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:0x007fbd04f1dee8 @rank=:jack, @suit=:spades>, #<Card:0x007fbd04f1de98 @rank=10, @suit=:hearts>, #<Card:0x007fbd04f1df10 @rank=:ace, @suit=:clubs>, #<Card:0x007fbd04f1dec0 @rank=9, @suit=:clubs>]
            got: [#<Card:0x007fbd04f1dec0 @rank=9, @suit=:clubs>, #<Card:0x007fbd04f1dee8 @rank=:jack, @suit=:spades>, #<Card:0x007fbd04f1de98 @rank=10, @suit=:hearts>, #<Card:0x007fbd04f1df10 @rank=:ace, @suit=:clubs>]
       
       (compared using ==)
       
       Diff:
       
       @@ -1,5 +1,5 @@
       -[#<Card:0x007fbd04f1dee8 @rank=:jack, @suit=:spades>,
       +[#<Card:0x007fbd04f1dec0 @rank=9, @suit=:clubs>,
       + #<Card:0x007fbd04f1dee8 @rank=:jack, @suit=:spades>,
         #<Card:0x007fbd04f1de98 @rank=10, @suit=:hearts>,
       - #<Card:0x007fbd04f1df10 @rank=:ace, @suit=:clubs>,
       - #<Card:0x007fbd04f1dec0 @rank=9, @suit=:clubs>]
       + #<Card:0x007fbd04f1df10 @rank=:ace, @suit=:clubs>]
     # /tmp/d20151112-27349-x244cf/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)>'

  33) SixtySixDeck hand #deal deals 6 cards
     Failure/Error: hand = SixtySixDeck.new.deal
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20151112-27349-x244cf/solution.rb:58:in `initialize'
     # /tmp/d20151112-27349-x244cf/spec.rb:422:in `new'
     # /tmp/d20151112-27349-x244cf/spec.rb:422: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)>'

  34) SixtySixDeck hand #twenty? returns true for king and queen not of the trump suit
     Failure/Error: expect(hand.twenty?(:hearts)).to be true
     NoMethodError:
       undefined method `twenty?' for []:Array
     # /tmp/d20151112-27349-x244cf/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
     NoMethodError:
       undefined method `twenty?' for []:Array
     # /tmp/d20151112-27349-x244cf/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
     NoMethodError:
       undefined method `twenty?' for []:Array
     # /tmp/d20151112-27349-x244cf/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.0432 seconds
57 examples, 36 failures

Failed examples:

rspec /tmp/d20151112-27349-x244cf/spec.rb:14 # WarDeck behaves like a deck fills the deck if no initialize parameters are given
rspec /tmp/d20151112-27349-x244cf/spec.rb:56 # WarDeck behaves like a deck #shuffle does not remove cards from the deck
rspec /tmp/d20151112-27349-x244cf/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-x244cf/spec.rb:145 # WarDeck #sort sorts the cards in the defined order
rspec /tmp/d20151112-27349-x244cf/spec.rb:161 # WarDeck hand #deal deals 26 cards
rspec /tmp/d20151112-27349-x244cf/spec.rb:171 # WarDeck hand #allow_face_up? returns false if the cards are more than 3
rspec /tmp/d20151112-27349-x244cf/spec.rb:175 # WarDeck hand #allow_face_up? returns true if the cards are less than or equal to 3
rspec /tmp/d20151112-27349-x244cf/spec.rb:14 # BeloteDeck behaves like a deck fills the deck if no initialize parameters are given
rspec /tmp/d20151112-27349-x244cf/spec.rb:56 # BeloteDeck behaves like a deck #shuffle does not remove cards from the deck
rspec /tmp/d20151112-27349-x244cf/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-x244cf/spec.rb:196 # BeloteDeck #sort sorts the cards in the defined order
rspec /tmp/d20151112-27349-x244cf/spec.rb:212 # BeloteDeck hand #deal deals 8 cards
rspec /tmp/d20151112-27349-x244cf/spec.rb:220 # BeloteDeck hand #highest_of_suit returns the strongest card of the specified suit
rspec /tmp/d20151112-27349-x244cf/spec.rb:239 # BeloteDeck hand #belote? returns true if there is a king and a queen of the same suit
rspec /tmp/d20151112-27349-x244cf/spec.rb:254 # BeloteDeck hand #belote? returns false when there is no king and queen of the same suit
rspec /tmp/d20151112-27349-x244cf/spec.rb:272 # BeloteDeck hand #tierce? with tierce returns true for cards with names
rspec /tmp/d20151112-27349-x244cf/spec.rb:287 # BeloteDeck hand #tierce? with tierce returns true for cards with numbers
rspec /tmp/d20151112-27349-x244cf/spec.rb:304 # BeloteDeck hand #tierce? without tierce does not confuse cards with different suits
rspec /tmp/d20151112-27349-x244cf/spec.rb:322 # BeloteDeck hand #quarte? detects four cards with increasing ranks
rspec /tmp/d20151112-27349-x244cf/spec.rb:337 # BeloteDeck hand #quarte? does not return true if there is no quarte
rspec /tmp/d20151112-27349-x244cf/spec.rb:354 # BeloteDeck hand #quint? detects five cards with increasing ranks
rspec /tmp/d20151112-27349-x244cf/spec.rb:369 # BeloteDeck hand #quint? does not return true if there is no quint
rspec /tmp/d20151112-27349-x244cf/spec.rb:74 # BeloteDeck hand #carre_of_jacks? behaves like carre-checking method returns true when there is a carre
rspec /tmp/d20151112-27349-x244cf/spec.rb:89 # BeloteDeck hand #carre_of_jacks? behaves like carre-checking method returns false when there is no carre
rspec /tmp/d20151112-27349-x244cf/spec.rb:74 # BeloteDeck hand #carre_of_nines? behaves like carre-checking method returns true when there is a carre
rspec /tmp/d20151112-27349-x244cf/spec.rb:89 # BeloteDeck hand #carre_of_nines? behaves like carre-checking method returns false when there is no carre
rspec /tmp/d20151112-27349-x244cf/spec.rb:74 # BeloteDeck hand #carre_of_aces? behaves like carre-checking method returns true when there is a carre
rspec /tmp/d20151112-27349-x244cf/spec.rb:89 # BeloteDeck hand #carre_of_aces? behaves like carre-checking method returns false when there is no carre
rspec /tmp/d20151112-27349-x244cf/spec.rb:14 # SixtySixDeck behaves like a deck fills the deck if no initialize parameters are given
rspec /tmp/d20151112-27349-x244cf/spec.rb:56 # SixtySixDeck behaves like a deck #shuffle does not remove cards from the deck
rspec /tmp/d20151112-27349-x244cf/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-x244cf/spec.rb:405 # SixtySixDeck #sort sorts the cards in the defined order
rspec /tmp/d20151112-27349-x244cf/spec.rb:421 # SixtySixDeck hand #deal deals 6 cards
rspec /tmp/d20151112-27349-x244cf/spec.rb:429 # SixtySixDeck hand #twenty? returns true for king and queen not of the trump suit
rspec /tmp/d20151112-27349-x244cf/spec.rb:442 # SixtySixDeck hand #twenty? returns false for king and queen of the trump suit
rspec /tmp/d20151112-27349-x244cf/spec.rb:455 # SixtySixDeck hand #twenty? returns false for hands without a king and queen of the same suit

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

Виктор обнови решението на 08.11.2015 16:59 (преди около 9 години)

+module WarCardValue
+ def war_suit
+ suit_value = {clubs: 1, diamonds: 2, hearts: 3, spades: 4}
+
+ suit_value[self.suit]
+ end
+ def war_rank
+ rank_value = {jack: 11, queen: 12, king: 13, ace: 14}
+ current_index = 2
+ while current_index <= 10
+ rank_value[current_index] = current_index
+ current_index += 1
+ end
+ rank_value[self.rank]
+ end
+end
+
+module BeloteCardValue #belote and 66
+ def belote_suit
+ suit_value = {clubs: 1, diamonds: 2, hearts: 3, spades: 4}
+ suit_value[self.suit]
+ end
+ def belote_rank
+ rank_value = {jack: 10, queen: 11, king: 12, ace: 14}
+ rank_value[0] = 0 #hack
+ rank_value[7] = 7
+ rank_value[8] = 8
+ rank_value[9] = 9
+ rank_value[10] = 13
+ rank_value[self.rank]
+ end
+end
+
+class Card
+ include WarCardValue
+ include BeloteCardValue
+ attr_reader :suit
+ attr_reader :rank
+ def initialize (rank, suit)
+ @rank = rank
+ @suit = suit
+ end
+ def to_s
+ rank.to_s.capitalize + " of " + suit.to_s.capitalize
+ end
+ def == (other)
+ if other.rank == @rank and other.suit == suit
+ return true
+ end
+ return false
+ end
+end
+
+class Deck
+ include WarCardValue
+ include BeloteCardValue
+ include Enumerable
+ def initialize (cards)
+ @cards = cards
+ end
+ def each
+ current_index = 0
+ while current_index < @cards.size
+ yield @cards[current_index]
+ current_index += 1
+ end
+ end
+ def size
+ @cards.size
+ end
+ def top_card
+ @cards[0]
+ end
+ def bottom_card
+ @cards[-1]
+ end
+ def draw_top_card
+ @cards.delete_at(0)
+ end
+ def draw_bottom_card
+ @cards.delete_at(-1)
+ end
+ def shuffle
+ @cards.sort_by! {rand}
+ end
+ def to_s
+ @cards.each{|card| p card.to_s}
+ end
+ def sort
+ @cards.sort_by!{|a| [a.war_rank, a.war_suit]}
+ end
+ def deal(hand_size)
+ #@cards = @cards.take(hand_size) #hacks
+ @cards.slice!(hand_size, @cards.size - 1)
+ end
+end
+
+class WarDeck < Deck
+ alias old_sort sort
+ alias old_deal deal
+ def play_card
+ draw_top_card
+ end
+ def allow_face_up?
+ if (size <= 3) then true
+ else false
+ end
+ end
+ def deal
+ old_deal(26)
+ end
+end
+
+class SixtySixDeck < Deck
+ alias old_sort sort
+ alias old_deal deal
+ def deal
+ old_deal(6)
+ end
+ def sort
+ @cards.sort_by!{|a| [a.belote_rank, a.belote_suit]}
+ end
+ def sort_by_suits
+ @cards.sort_by!{|a| [a.belote_suit, a.belote_rank]}
+ end
+
+ def king_and_queen
+ self.sort_by_suits
+ current = 0
+ until current == 5
+ if @cards[current].rank == :queen and @cards[current + 1].rank == :king and
+ @cards[current].suit == @cards[current + 1].suit
+ return current
+ end
+ current += 1
+ end
+ return false
+ end
+ def twenty? (trump_suit)
+ if king_and_queen and @cards[king_and_queen].suit != trump_suit
+ return true
+ else
+ return false
+ end
+ end
+ def forty? (trump_suit)
+ if king_and_queen and @cards[king_and_queen].suit == trump_suit
+ return true
+ else
+ return false
+ end
+ end
+end
+
+class BeloteDeck < Deck
+ alias old_sort sort
+ alias old_deal deal
+ def deal
+ old_deal(8)
+ end
+ def sort
+ @cards.sort_by!{|a| [a.belote_rank, a.belote_suit]}
+ end
+ def sort_by_suits
+ @cards.sort_by!{|a| [a.belote_suit, a.belote_rank]}
+ end
+ def highest_of_suit(suit)
+ highest = Card.new(0, suit)
+ @cards.each {|x| if x.belote_rank > highest.belote_rank and
+ x.suit == suit
+ highest = Card.new(x.rank, suit)
+ end
+ }
+ if(highest.rank == 0)
+ return nil
+ else
+ return highest
+ end
+ end
+ def belote?
+ self.sort_by_suits
+ current = 0
+ until current_index == 7
+ if @cards[current].rank == :queen and @cards[current + 1].rank == :king and
+ @cards[current].suit == @cards[current + 1].suit
+ return true
+ end
+ current += 1
+ end
+ return false
+ end
+ def three_in_a_roll(index)
+ if @cards[index].belote_rank + 1 == @cards[index + 1].belote_rank and
+ @cards[index].belote_rank + 2 == @cards[index + 2].belote_rank
+ return true
+ else return false
+ end
+ end
+ def four_in_a_roll(index)
+ if @cards[index].belote_rank + 1 == @cards[index + 1].belote_rank and
+ @cards[index].belote_rank + 2 == @cards[index + 2].belote_rank and
+ @cards[index].belote_rank + 3 == @cards[index + 3].belote_rank
+ return true
+ else return false
+ end
+ end
+ def five_in_a_roll(index)
+ if @cards[index].belote_rank + 1 == @cards[index + 1].belote_rank and
+ @cards[index].belote_rank + 2 == @cards[index + 2].belote_rank and
+ @cards[index].belote_rank + 3 == @cards[index + 3].belote_rank and
+ @cards[index].belote_rank + 4 == @cards[index + 4].belote_rank
+ return true
+ else return false
+ end
+ end
+
+ def tierce?
+ self.sort_by_suits
+ current = 0
+ until current == 6
+ if three_in_a_roll(current)
+ return true
+ end
+ current += 1
+ end
+ return false
+ end
+ def quarte?
+ self.sort_by_suits
+ current = 0
+ until current == 5
+ if four_in_a_roll(current)
+ return true
+ end
+ current += 1
+ end
+ return false
+ end
+ def quint?
+ self.sort_by_suits
+ current = 0
+ until current == 4
+ if five_in_a_roll(current)
+ return true
+ end
+ current += 1
+ end
+ return false
+ end
+ def carre_of_jacks?
+ if @cards.include?(Card.new(:jack, :clubs)) and
+ @cards.include?(Card.new(:jack, :diamonds)) and
+ @cards.include?(Card.new(:jack, :hearts)) and
+ @cards.include?(Card.new(:jack, :spades))
+ return true
+ else
+ return false
+ end
+ end
+ def carre_of_nines?
+ if @cards.include?(Card.new(9, :clubs)) and
+ @cards.include?(Card.new(9, :diamonds)) and
+ @cards.include?(Card.new(9, :hearts)) and
+ @cards.include?(Card.new(9, :spades))
+ return true
+ else
+ return false
+ end
+ end
+ def carre_of_ace?
+ if @cards.include?(Card.new(:ace, :clubs)) and
+ @cards.include?(Card.new(:ace, :diamonds)) and
+ @cards.include?(Card.new(:ace, :hearts)) and
+ @cards.include?(Card.new(:ace, :spades))
+ return true
+ else
+ return false
+ end
+ end
+end

Имаш проблеми със спазването на конвенциите на доста места, направи справка с ръководството за стил – излишни return-и, излишни if-конструкции, лош whitespace и други.

Оставяй празни редове между дефинициите на методи и между другите секции в класа.

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