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

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

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

Резултати

  • 4 точки от тестове
  • 1 отнета точка
  • 3 точки общо
  • 37 успешни тест(а)
  • 20 неуспешни тест(а)

Код

SUITS = [:spades, :hearts, :diamonds, :clubs]
class Card
attr_reader :rank, :suit
def initialize(rank, suit)
@rank = rank
@suit = suit
end
def to_s
rank.to_s.capitalize + " of " + suit.to_s.capitalize
end
def ==(other)
rank == other.rank && suit == other.suit
end
alias_method :eql?, :==
end
class Deck
include Enumerable
RANKS = [:ace, :king, :queen, :jack, 10, 9, 8, 7, 6, 5, 4, 3, 2]
attr_reader :cards
def initialize(cards = all_cards)
@cards = cards.shuffle
end
def each(&block)
@cards.each(&block)
end
def size
@cards.size
end
def draw_top_card
@cards.delete(@cards.first)
end
def draw_bottom_card
@cards.delete(@cards.last)
end
def top_card
@cards.first
end
def bottom_card
@cards.last
end
def shuffle
@cards.shuffle!
end
def sort
sorted_deck = []
SUITS.each { |suit| sorted_deck << sort_by_ranks(suit) }
@cards = sorted_deck.flatten
end
def to_s
@cards.each { |card| p card.to_s }
end
def deal
end
private
def all_cards
name = self.class
cards = []
SUITS.each {|suit| name::RANKS.each {|rank| cards << Card.new(rank, suit)}}
cards
end
def sort_by_ranks(suit)
carts_to_sort = @cards.select { |card| card.suit == suit }
carts_to_sort.sort_by! { |card| self.class::RANKS.index(card.rank)}
end
end
class WarDeck < Deck
def initialize(*arguments)
super
end
def deal
WarDeck.new(draw)
end
def play_card
draw_top_card
end
def allow_face_up?
size <= 3
end
private
def draw
cards = []
(size / 2).times{ |i| cards << draw_top_card }
cards
end
end
class BeloteDeck < Deck
RANKS = [:ace, :king, :queen, :jack, 10, 9, 8, 7]
THREE_CONSECUTIVE = [
[:ace, :king, :queen],
[:king, :queen, :jack],
[:queen, :jack, 10],
[:jack, 10, 9],
[10, 9, 8],
[9, 8, 7]
]
FOUR_CONSECUTIVE = [
[:ace, :king, :queen, :jack],
[:king, :queen, :jack, 10],
[:queen, :jack, 10, 9],
[:jack, 10, 9, 8],
[10, 9, 8, 7]
]
FIVE_CONSECUTIVE = [
[:ace, :king, :queen, :jack, 10],
[:king, :queen, :jack, 10, 9],
[:queen, :jack, 10, 9, 8],
[:jack, 10, 9, 8, 7]
]
def initialize(*arguments)
super
end
def deal
BeloteDeck.new(draw)
end
def highest_of_suit(suit)
sort.select { |card| card.suit == suit }.first
end
def belote?
twenty = []
SUITS.each do |suit|
twenty << @cards.include?(Card.new(:king, suit)) &&
@cards.include?(Card.new(:queen, suit))
end
twenty.any?
end
def tierce?
three_consecutive = []
THREE_CONSECUTIVE.each do |tierce|
split_hand.each {|element| three_consecutive << (element == tierce) }
end
three_consecutive.any?
end
def quarte?
four_consecutive = []
FOUR_CONSECUTIVE.each do |quarte|
split_hand.each {|element| four_consecutive << (element == quarte) }
end
four_consecutive.any?
end
def quint?
five_consecutive = []
FIVE_CONSECUTIVE.each do |quint|
split_hand.each {|element| five_consecutive << (element == quint) }
end
five_consecutive.any?
end
def split_hand
splits = []
SUITS.each do |suit|
splits << sort.select{ |card| card.suit == suit }.map(&:rank)
end
splits
end
def carre_of_jacks?
@cards.select { |card| card.rank == :jack }.size == 4
end
def carre_of_nines?
@cards.select { |card| card.rank == 9 }.size == 4
end
def carre_of_aces?
@cards.select { |card| card.rank == :ace }.size == 4
end
private
def draw
cards = []
(size / 4).times{ |i| cards << draw_top_card }
cards
end
end
class SixtySixDeck < Deck
RANKS = [:ace, :king, :queen, :jack, 10, 9]
def initialize(*arguments)
super
end
def deal
SixtySixDeck.new(draw)
end
def twenty?(trump_suit)
twenty = []
(SUITS - [trump_suit]).each do |suit|
twenty << @cards.include?(Card.new(:king, suit)) &&
@cards.include?(Card.new(:queen, suit))
end
twenty.any?
end
def forty?(trump_suit)
@cards.include?(Card.new(:king, trump_suit)) &&
@cards.include?(Card.new(:queen, trump_suit))
end
private
def draw
cards = []
6.times{ |i| cards << draw_top_card }
cards
end
end

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

.......FFF."Ace of Spades"
"9 of Clubs"
F........F..."9 of Clubs"
"Ace of Spades"
F..FF.FF.F.F.F.F.F.F..FF..."9 of Clubs"
"Ace of Spades"
F....F

Failures:

  1) WarDeck behaves like a deck #draw_bottom_card pops the bottom-most card
     Failure/Error: expect(small_deck.draw_bottom_card).to eq nine_of_clubs
       
       expected: #<Card:0x007fab9e5e8598 @rank=9, @suit=:clubs>
            got: #<Card:0x007fab9e5e85c0 @rank=:ace, @suit=:spades>
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -#<Card:0x007fab9e5e8598 @rank=9, @suit=:clubs>
       +#<Card:0x007fab9e5e85c0 @rank=:ace, @suit=:spades>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1ixor1r/spec.rb:140
     # /tmp/d20151112-27349-1ixor1r/spec.rb:36:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

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

  3) WarDeck behaves like a deck #bottom peeks at the bottom-most card
     Failure/Error: expect(small_deck.bottom_card).to eq nine_of_clubs
       
       expected: #<Card:0x007fab9e4b85b0 @rank=9, @suit=:clubs>
            got: #<Card:0x007fab9e4b85d8 @rank=:ace, @suit=:spades>
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -#<Card:0x007fab9e4b85b0 @rank=9, @suit=:clubs>
       +#<Card:0x007fab9e4b85d8 @rank=:ace, @suit=:spades>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1ixor1r/spec.rb:140
     # /tmp/d20151112-27349-1ixor1r/spec.rb:50:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) 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:0x007fab9e5d0f10>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1ixor1r/spec.rb:140
     # /tmp/d20151112-27349-1ixor1r/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)>'

  5) BeloteDeck behaves like a deck #draw_bottom_card pops the bottom-most card
     Failure/Error: expect(small_deck.draw_bottom_card).to eq nine_of_clubs
       
       expected: #<Card:0x007fab9e54a370 @rank=9, @suit=:clubs>
            got: #<Card:0x007fab9e54a398 @rank=:ace, @suit=:spades>
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -#<Card:0x007fab9e54a370 @rank=9, @suit=:clubs>
       +#<Card:0x007fab9e54a398 @rank=:ace, @suit=:spades>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1ixor1r/spec.rb:191
     # /tmp/d20151112-27349-1ixor1r/spec.rb:36:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

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

  7) BeloteDeck hand #highest_of_suit returns the strongest card of the specified suit
     Failure/Error: expect(hand.highest_of_suit(:spades)).to eq Card.new(:king, :spades)
       
       expected: #<Card:0x007fab9e488720 @rank=:king, @suit=:spades>
            got: nil
       
       (compared using ==)
     # /tmp/d20151112-27349-1ixor1r/spec.rb:233: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 #belote? returns true if there is a king and a queen of the same suit
     Failure/Error: expect(hand.belote?).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-1ixor1r/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)>'

  9) BeloteDeck hand #tierce? with tierce returns true for cards with names
     Failure/Error: expect(hand.tierce?).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-1ixor1r/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)>'

  10) BeloteDeck hand #tierce? with tierce returns true for cards with numbers
     Failure/Error: expect(hand.tierce?).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-1ixor1r/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)>'

  11) BeloteDeck hand #quarte? detects four cards with increasing ranks
     Failure/Error: expect(hand.quarte?).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-1ixor1r/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)>'

  12) BeloteDeck hand #quint? detects five cards with increasing ranks
     Failure/Error: expect(hand.quint?).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-1ixor1r/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)>'

  13) 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
       
       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.
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1ixor1r/spec.rb:386
     # /tmp/d20151112-27349-1ixor1r/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)>'

  14) 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
       
       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.
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1ixor1r/spec.rb:390
     # /tmp/d20151112-27349-1ixor1r/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)>'

  15) 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
       
       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.
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1ixor1r/spec.rb:394
     # /tmp/d20151112-27349-1ixor1r/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)>'

  16) SixtySixDeck behaves like a deck implements Enumerable
     Failure/Error: expect(small_deck.to_a).to eq [ace_of_spades, nine_of_clubs]
       
       expected: [#<Card:0x007fab9df85660 @rank=:ace, @suit=:spades>, #<Card:0x007fab9df85638 @rank=9, @suit=:clubs>]
            got: [#<Card:0x007fab9df85638 @rank=9, @suit=:clubs>, #<Card:0x007fab9df85660 @rank=:ace, @suit=:spades>]
       
       (compared using ==)
       
       Diff:
       @@ -1,3 +1,3 @@
       -[#<Card:0x007fab9df85660 @rank=:ace, @suit=:spades>,
       - #<Card:0x007fab9df85638 @rank=9, @suit=:clubs>]
       +[#<Card:0x007fab9df85638 @rank=9, @suit=:clubs>,
       + #<Card:0x007fab9df85660 @rank=:ace, @suit=:spades>]
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1ixor1r/spec.rb:400
     # /tmp/d20151112-27349-1ixor1r/spec.rb:11: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)>'

  17) SixtySixDeck behaves like a deck #draw_top_card pops the top-most card
     Failure/Error: expect(small_deck.draw_top_card).to eq ace_of_spades
       
       expected: #<Card:0x007fab9decc2c8 @rank=:ace, @suit=:spades>
            got: #<Card:0x007fab9decc2a0 @rank=9, @suit=:clubs>
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -#<Card:0x007fab9decc2c8 @rank=:ace, @suit=:spades>
       +#<Card:0x007fab9decc2a0 @rank=9, @suit=:clubs>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1ixor1r/spec.rb:400
     # /tmp/d20151112-27349-1ixor1r/spec.rb:29:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  18) SixtySixDeck behaves like a deck #draw_bottom_card pops the bottom-most card
     Failure/Error: expect(small_deck.draw_bottom_card).to eq nine_of_clubs
       
       expected: #<Card:0x007fab9dab5ec8 @rank=9, @suit=:clubs>
            got: #<Card:0x007fab9dab6058 @rank=:ace, @suit=:spades>
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -#<Card:0x007fab9dab5ec8 @rank=9, @suit=:clubs>
       +#<Card:0x007fab9dab6058 @rank=:ace, @suit=:spades>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1ixor1r/spec.rb:400
     # /tmp/d20151112-27349-1ixor1r/spec.rb:36:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

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

  20) 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
       
       expected #<FalseClass:0> => false
            got #<TrueClass:20> => true
       
       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-1ixor1r/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.04996 seconds
57 examples, 20 failures

Failed examples:

rspec /tmp/d20151112-27349-1ixor1r/spec.rb:35 # WarDeck behaves like a deck #draw_bottom_card pops the bottom-most card
rspec /tmp/d20151112-27349-1ixor1r/spec.rb:42 # WarDeck behaves like a deck #top peeks at the top-most card
rspec /tmp/d20151112-27349-1ixor1r/spec.rb:49 # WarDeck behaves like a deck #bottom peeks at the bottom-most card
rspec /tmp/d20151112-27349-1ixor1r/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-1ixor1r/spec.rb:35 # BeloteDeck behaves like a deck #draw_bottom_card pops the bottom-most card
rspec /tmp/d20151112-27349-1ixor1r/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-1ixor1r/spec.rb:220 # BeloteDeck hand #highest_of_suit returns the strongest card of the specified suit
rspec /tmp/d20151112-27349-1ixor1r/spec.rb:239 # BeloteDeck hand #belote? returns true if there is a king and a queen of the same suit
rspec /tmp/d20151112-27349-1ixor1r/spec.rb:272 # BeloteDeck hand #tierce? with tierce returns true for cards with names
rspec /tmp/d20151112-27349-1ixor1r/spec.rb:287 # BeloteDeck hand #tierce? with tierce returns true for cards with numbers
rspec /tmp/d20151112-27349-1ixor1r/spec.rb:322 # BeloteDeck hand #quarte? detects four cards with increasing ranks
rspec /tmp/d20151112-27349-1ixor1r/spec.rb:354 # BeloteDeck hand #quint? detects five cards with increasing ranks
rspec /tmp/d20151112-27349-1ixor1r/spec.rb:74 # BeloteDeck hand #carre_of_jacks? behaves like carre-checking method returns true when there is a carre
rspec /tmp/d20151112-27349-1ixor1r/spec.rb:74 # BeloteDeck hand #carre_of_nines? behaves like carre-checking method returns true when there is a carre
rspec /tmp/d20151112-27349-1ixor1r/spec.rb:74 # BeloteDeck hand #carre_of_aces? behaves like carre-checking method returns true when there is a carre
rspec /tmp/d20151112-27349-1ixor1r/spec.rb:8 # SixtySixDeck behaves like a deck implements Enumerable
rspec /tmp/d20151112-27349-1ixor1r/spec.rb:28 # SixtySixDeck behaves like a deck #draw_top_card pops the top-most card
rspec /tmp/d20151112-27349-1ixor1r/spec.rb:35 # SixtySixDeck behaves like a deck #draw_bottom_card pops the bottom-most card
rspec /tmp/d20151112-27349-1ixor1r/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-1ixor1r/spec.rb:455 # SixtySixDeck hand #twenty? returns false for hands without a king and queen of the same suit

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

Даяна обнови решението на 11.11.2015 15:38 (преди около 9 години)

+SUITS = [:spades, :hearts, :diamonds, :clubs]
+
+class Card
+ attr_reader :rank, :suit
+
+ def initialize(rank, suit)
+ @rank = rank
+ @suit = suit
+ end
+
+ def to_s
+ rank.to_s.capitalize + " of " + suit.to_s.capitalize
+ end
+
+ def ==(other)
+ rank == other.rank && suit == other.suit
+ end
+
+ alias_method :eql?, :==
+end
+
+class Deck
+ include Enumerable
+
+ RANKS = [:ace, :king, :queen, :jack, 10, 9, 8, 7, 6, 5, 4, 3, 2]
+
+ attr_reader :cards
+
+ def initialize(cards = all_cards)
+ @cards = cards.shuffle
+ end
+
+ def each(&block)
+ each(&block)
+ end
+
+ def size
+ @cards.size
+ end
+
+ def draw_top_card
+ @cards.delete(@cards.first)
+ end
+
+ def draw_bottom_card
+ @cards.delete(@cards.last)
+ end
+
+ def top_card
+ @cards.first
+ end
+
+ def bottom_card
+ @cards.last
+ end
+
+ def shuffle
+ @cards.shuffle!
+ end
+
+ def sort
+ sorted_deck = []
+ SUITS.each { |suit| sorted_deck << sort_by_ranks(suit) }
+ @cards = sorted_deck.flatten
+ end
+
+ def to_s
+ @cards.each { |card| p card.to_s }
+ end
+
+ def deal
+ end
+
+ private
+
+ def all_cards
+ name = self.class
+ cards = []
+ SUITS.each {|suit| name::RANKS.each {|rank| cards << Card.new(rank, suit)}}
+ cards
+ end
+
+ def sort_by_ranks(suit)
+ carts_to_sort = @cards.select { |card| card.suit == suit }
+ carts_to_sort.sort_by! { |card| self.class::RANKS.index(card.rank)}
+ end
+end
+
+class WarDeck < Deck
+
+ def initialize(*arguments)
+ super
+ end
+
+ def deal
+ WarDeck.new(draw)
+ end
+
+ def play_card
+ draw_top_card
+ end
+
+ def allow_face_up?
+ size <= 3
+ end
+
+ private
+
+ def draw
+ cards = []
+ (size / 2).times{ |i| cards << draw_top_card }
+ cards
+ end
+end
+
+class BeloteDeck < Deck
+
+ RANKS = [:ace, :king, :queen, :jack, 10, 9, 8, 7]
+
+ three_consecutive = [
+ [:ace, :king, :queen],
+ [:king, :queen, :jack],
+ [:queen, :jack, 10],
+ [:jack, 10, 9],
+ [10, 9, 8],
+ [9, 8, 7]
+ ]
+
+ four_consecutive = [
+ [:ace, :king, :queen, :jack],
+ [:king, :queen, :jack, 10],
+ [:queen, :jack, 10, 9],
+ [:jack, 10, 9, 8],
+ [10, 9, 8, 7]
+ ]
+
+ five_consecutive = [
+ [:ace, :king, :queen, :jack, 10],
+ [:king, :queen, :jack, 10, 9],
+ [:queen, :jack, 10, 9, 8],
+ [:jack, 10, 9, 8, 7]
+ ]
+
+
+ def initialize(*arguments)
+ super
+ end
+
+ def deal
+ BeloteDeck.new(draw)
+ end
+
+ def highest_of_suit(suit)
+ sort.select { |card| card.suit == suit }.first
+ end
+
+ def belote?
+ twenty = []
+ SUITS.each do |suit|
+ twenty << @cards.include?(Card.new(:king, suit)) &&
+ @cards.include?(Card.new(:queen, suit))
+ end
+ twenty.any?
+ end
+
+ def tierce?
+ end
+
+ def quarte?
+ end
+
+ def quint?
+ end
+
+ def carre_of_jacks?
+ @cards.select { |card| card.rank == :jack }.size == 4
+ end
+
+ def carre_of_nines?
+ @cards.select { |card| card.rank == 9 }.size == 4
+ end
+
+ def carre_of_aces?
+ @cards.select { |card| card.rank == :ace }.size == 4
+ end
+
+ private
+
+ def draw
+ cards = []
+ (size / 4).times{ |i| cards << draw_top_card }
+ cards
+ end
+end
+
+class SixtySixDeck < Deck
+
+ RANKS = [:ace, :king, :queen, :jack, 10, 9]
+
+ def initialize(*arguments)
+ super
+ end
+
+ def deal
+ SixtySixDeck.new(draw)
+ end
+
+ def twenty?(trump_suit)
+ twenty = []
+ (SUITS - [trump_suit]).each do |suit|
+ twenty << @cards.include?(Card.new(:king, suit)) &&
+ @cards.include?(Card.new(:queen, suit))
+ end
+ twenty.any?
+ end
+
+ def forty?(trump_suit)
+ @cards.include?(Card.new(:king, trump_suit)) &&
+ @cards.include?(Card.new(:queen, trump_suit))
+ end
+
+ private
+
+ def draw
+ cards = []
+ 6.times{ |i| cards << draw_top_card }
+ cards
+ end
+end

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

SUITS = [:spades, :hearts, :diamonds, :clubs]
class Card
attr_reader :rank, :suit
def initialize(rank, suit)
@rank = rank
@suit = suit
end
def to_s
rank.to_s.capitalize + " of " + suit.to_s.capitalize
end
def ==(other)
rank == other.rank && suit == other.suit
end
alias_method :eql?, :==
end
class Deck
include Enumerable
RANKS = [:ace, :king, :queen, :jack, 10, 9, 8, 7, 6, 5, 4, 3, 2]
attr_reader :cards
def initialize(cards = all_cards)
@cards = cards.shuffle
end
def each(&block)
each(&block)
end
def size
@cards.size
end
def draw_top_card
@cards.delete(@cards.first)
end
def draw_bottom_card
@cards.delete(@cards.last)
end
def top_card
@cards.first
end
def bottom_card
@cards.last
end
def shuffle
@cards.shuffle!
end
def sort
sorted_deck = []
SUITS.each { |suit| sorted_deck << sort_by_ranks(suit) }
@cards = sorted_deck.flatten
end
def to_s
@cards.each { |card| p card.to_s }
end
def deal
end
private
def all_cards
name = self.class
cards = []
SUITS.each {|suit| name::RANKS.each {|rank| cards << Card.new(rank, suit)}}
cards
end
def sort_by_ranks(suit)
carts_to_sort = @cards.select { |card| card.suit == suit }
carts_to_sort.sort_by! { |card| self.class::RANKS.index(card.rank)}
end
end
class WarDeck < Deck
def initialize(*arguments)
super
end
def deal
WarDeck.new(draw)
end
def play_card
draw_top_card
end
def allow_face_up?
size <= 3
end
private
def draw
cards = []
(size / 2).times{ |i| cards << draw_top_card }
cards
end
end
class BeloteDeck < Deck
RANKS = [:ace, :king, :queen, :jack, 10, 9, 8, 7]
- three_consecutive = [
- [:ace, :king, :queen],
- [:king, :queen, :jack],
- [:queen, :jack, 10],
- [:jack, 10, 9],
- [10, 9, 8],
- [9, 8, 7]
- ]
-
- four_consecutive = [
- [:ace, :king, :queen, :jack],
- [:king, :queen, :jack, 10],
- [:queen, :jack, 10, 9],
- [:jack, 10, 9, 8],
- [10, 9, 8, 7]
- ]
-
- five_consecutive = [
- [:ace, :king, :queen, :jack, 10],
- [:king, :queen, :jack, 10, 9],
- [:queen, :jack, 10, 9, 8],
- [:jack, 10, 9, 8, 7]
- ]
-
-
def initialize(*arguments)
super
end
def deal
BeloteDeck.new(draw)
end
def highest_of_suit(suit)
sort.select { |card| card.suit == suit }.first
end
def belote?
twenty = []
SUITS.each do |suit|
twenty << @cards.include?(Card.new(:king, suit)) &&
@cards.include?(Card.new(:queen, suit))
end
twenty.any?
end
def tierce?
end
def quarte?
end
def quint?
end
def carre_of_jacks?
@cards.select { |card| card.rank == :jack }.size == 4
end
def carre_of_nines?
@cards.select { |card| card.rank == 9 }.size == 4
end
def carre_of_aces?
@cards.select { |card| card.rank == :ace }.size == 4
end
private
def draw
cards = []
(size / 4).times{ |i| cards << draw_top_card }
cards
end
end
class SixtySixDeck < Deck
RANKS = [:ace, :king, :queen, :jack, 10, 9]
def initialize(*arguments)
super
end
def deal
SixtySixDeck.new(draw)
end
def twenty?(trump_suit)
twenty = []
(SUITS - [trump_suit]).each do |suit|
twenty << @cards.include?(Card.new(:king, suit)) &&
@cards.include?(Card.new(:queen, suit))
end
twenty.any?
end
def forty?(trump_suit)
@cards.include?(Card.new(:king, trump_suit)) &&
@cards.include?(Card.new(:queen, trump_suit))
end
private
def draw
cards = []
6.times{ |i| cards << draw_top_card }
cards
end
end

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

SUITS = [:spades, :hearts, :diamonds, :clubs]
class Card
attr_reader :rank, :suit
def initialize(rank, suit)
@rank = rank
@suit = suit
end
def to_s
rank.to_s.capitalize + " of " + suit.to_s.capitalize
end
def ==(other)
rank == other.rank && suit == other.suit
end
alias_method :eql?, :==
end
class Deck
include Enumerable
RANKS = [:ace, :king, :queen, :jack, 10, 9, 8, 7, 6, 5, 4, 3, 2]
attr_reader :cards
def initialize(cards = all_cards)
@cards = cards.shuffle
end
def each(&block)
- each(&block)
+ @cards.each(&block)
end
def size
@cards.size
end
def draw_top_card
@cards.delete(@cards.first)
end
def draw_bottom_card
@cards.delete(@cards.last)
end
def top_card
@cards.first
end
def bottom_card
@cards.last
end
def shuffle
@cards.shuffle!
end
def sort
sorted_deck = []
SUITS.each { |suit| sorted_deck << sort_by_ranks(suit) }
@cards = sorted_deck.flatten
end
def to_s
@cards.each { |card| p card.to_s }
end
def deal
end
private
def all_cards
name = self.class
cards = []
SUITS.each {|suit| name::RANKS.each {|rank| cards << Card.new(rank, suit)}}
cards
end
def sort_by_ranks(suit)
carts_to_sort = @cards.select { |card| card.suit == suit }
carts_to_sort.sort_by! { |card| self.class::RANKS.index(card.rank)}
end
end
class WarDeck < Deck
def initialize(*arguments)
super
end
def deal
WarDeck.new(draw)
end
def play_card
draw_top_card
end
def allow_face_up?
size <= 3
end
private
def draw
cards = []
(size / 2).times{ |i| cards << draw_top_card }
cards
end
end
class BeloteDeck < Deck
RANKS = [:ace, :king, :queen, :jack, 10, 9, 8, 7]
+ THREE_CONSECUTIVE = [
+ [:ace, :king, :queen],
+ [:king, :queen, :jack],
+ [:queen, :jack, 10],
+ [:jack, 10, 9],
+ [10, 9, 8],
+ [9, 8, 7]
+ ]
+
+ FOUR_CONSECUTIVE = [
+ [:ace, :king, :queen, :jack],
+ [:king, :queen, :jack, 10],
+ [:queen, :jack, 10, 9],
+ [:jack, 10, 9, 8],
+ [10, 9, 8, 7]
+ ]
+
+ FIVE_CONSECUTIVE = [
+ [:ace, :king, :queen, :jack, 10],
+ [:king, :queen, :jack, 10, 9],
+ [:queen, :jack, 10, 9, 8],
+ [:jack, 10, 9, 8, 7]
+ ]
+
+
def initialize(*arguments)
super
end
def deal
BeloteDeck.new(draw)
end
def highest_of_suit(suit)
sort.select { |card| card.suit == suit }.first
end
def belote?
twenty = []
SUITS.each do |suit|
twenty << @cards.include?(Card.new(:king, suit)) &&
@cards.include?(Card.new(:queen, suit))
end
twenty.any?
end
def tierce?
+ three_consecutive = []
+ THREE_CONSECUTIVE.each do |tierce|
+ split_hand.each {|element| three_consecutive << (element == tierce) }
+ end
+ three_consecutive.any?
end
def quarte?
+ four_consecutive = []
+ FOUR_CONSECUTIVE.each do |quarte|
+ split_hand.each {|element| four_consecutive << (element == quarte) }
+ end
+ four_consecutive.any?
end
def quint?
+ five_consecutive = []
+ FIVE_CONSECUTIVE.each do |quint|
+ split_hand.each {|element| five_consecutive << (element == quint) }
+ end
+ five_consecutive.any?
+ end
+
+ def split_hand
+ splits = []
+ SUITS.each do |suit|
+ splits << sort.select{ |card| card.suit == suit }.map(&:rank)
+ end
+ splits
end
def carre_of_jacks?
@cards.select { |card| card.rank == :jack }.size == 4
end
def carre_of_nines?
@cards.select { |card| card.rank == 9 }.size == 4
end
def carre_of_aces?
@cards.select { |card| card.rank == :ace }.size == 4
end
private
def draw
cards = []
(size / 4).times{ |i| cards << draw_top_card }
cards
end
end
class SixtySixDeck < Deck
RANKS = [:ace, :king, :queen, :jack, 10, 9]
def initialize(*arguments)
super
end
def deal
SixtySixDeck.new(draw)
end
def twenty?(trump_suit)
twenty = []
(SUITS - [trump_suit]).each do |suit|
twenty << @cards.include?(Card.new(:king, suit)) &&
@cards.include?(Card.new(:queen, suit))
end
twenty.any?
end
def forty?(trump_suit)
@cards.include?(Card.new(:king, trump_suit)) &&
@cards.include?(Card.new(:queen, trump_suit))
end
private
def draw
cards = []
6.times{ |i| cards << draw_top_card }
cards
end
end

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

Отнемам ти точка заради това и заради факта, че вадиш неща на екрана (с p), вместо да ги връщаш като низ.

Прегледай добре документацията на Enumerable, на много места кодът ти може да стане по-добре, ако използваш правилно подходящите методи от там.

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