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

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

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

Резултати

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

Код

class Card
attr_accessor :rank, :suit
def initialize(rank, suit)
@rank = rank
@suit = suit
end
def to_s
card_name = @rank.to_s.capitalize + " of " + @suit.to_s.capitalize
end
def ==(other)
return true if @rank == other.rank and @suit == other.suit
false
end
end
class Deck
include Enumerable
def initialize(*cards)
if(cards.empty?)
@deck_of_cards = generate_cards
shuffle
else
@deck_of_cards = cards.first
end
end
def size
@deck_of_cards.length
end
def draw_top_card
@deck_of_cards.shift
end
def draw_bottom_card
@deck_of_cards.pop
end
def top_card
@deck_of_cards.first
end
def bottom_card
@deck_of_cards.last
end
def shuffle
@deck_of_cards.shuffle!
self
end
def sort
@deck_of_cards = @deck_of_cards.sort_by{ |card| [card.suit] }
self
end
def to_s
all_cards_names = ""
self.each { |card| all_cards_names.concat(card.to_s.concat("\n")) }
all_cards_names
end
def each
number_of_cards = self.size
counter = number_of_cards - 1
while counter >= 0
yield @deck_of_cards[counter]
counter -= 1
end
end
def deal(number_of_cards_for_dealing)
cards_for_dealing = []
while number_of_cards_for_dealing > 0
top_card = draw_top_card
cards_for_dealing.push(top_card)
number_of_cards_for_dealing -= 1
end
Hand.new(cards_for_dealing)
end
private
def generate_cards
suits = [:spades, :hearts, :diamonds, :clubs]
ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
card_array = merge_card_arrays(ranks, suits)
end
def merge_card_arrays(array_1, array_2)
merged_array = []
array_1.each { |x| array_2.each{ |y| merged_array.push(Card.new(x, y)) }}
merged_array
end
end
class WarDeck < Deck
def initialize(*cards)
super
@number_of_cards_in_hand = 26
end
def deal
super(@number_of_cards_in_hand)
end
end
class BeloteDeck < Deck
def initialize(*cards)
super
@number_of_cards_in_hand = 8
end
def deal
super(@number_of_cards_in_hand)
end
private
def generate_cards
suits = [:spades, :hearts, :diamonds, :clubs]
ranks = [7, 8, 9, :jack, :queen, :king, 10, :ace]
card_array = merge_card_arrays(ranks, suits)
end
end
class SixtySixDeck < Deck
def initialize(*cards)
super
@number_of_cards_in_hand = 6
end
def deal
super(@number_of_cards_in_hand)
end
private
def generate_cards
suits = [:spades, :hearts, :diamonds, :clubs]
ranks = [9, :jack, :queen, :king, 10, :ace]
card_array = merge_card_arrays(ranks, suits)
end
end
class Hand
def initialize(cards)
@cards_in_hand = Deck.new(cards)
end
def to_s
@cards_in_hand.to_s
end
def size
@cards_in_hand.size
end
end

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

...F....FF.FF.FFF....FF.FF.FFFFFFFFFFFFFFFFF....FF.FF.FFF

Failures:

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

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

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

  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"
       
       expected: "Ace of Spades\n9 of Clubs"
            got: "9 of Clubs\nAce of Spades"
       
       (compared using ==)
       
       Diff:
       
       @@ -1,3 +1,3 @@
       -Ace of Spades
        9 of Clubs
       +Ace of Spades
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1iah12b/spec.rb:140
     # /tmp/d20151112-27349-1iah12b/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) 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:0x007ffb19c22ad8 @rank=:jack, @suit=:spades>, #<Card:0x007ffb19c22a60 @rank=10, @suit=:hearts>, #<Card:0x007ffb19c22b00 @rank=:ace, @suit=:clubs>, #<Card:0x007ffb19c22a88 @rank=2, @suit=:clubs>]
            got: [#<Card:0x007ffb19c22ad8 @rank=:jack, @suit=:spades>, #<Card:0x007ffb19c22a60 @rank=10, @suit=:hearts>, #<Card:0x007ffb19c22a88 @rank=2, @suit=:clubs>, #<Card:0x007ffb19c22b00 @rank=:ace, @suit=:clubs>]
       
       (compared using ==)
       
       Diff:
       @@ -1,5 +1,5 @@
        [#<Card:0x007ffb19c22ad8 @rank=:jack, @suit=:spades>,
         #<Card:0x007ffb19c22a60 @rank=10, @suit=:hearts>,
       - #<Card:0x007ffb19c22b00 @rank=:ace, @suit=:clubs>,
       - #<Card:0x007ffb19c22a88 @rank=2, @suit=:clubs>]
       + #<Card:0x007ffb19c22a88 @rank=2, @suit=:clubs>,
       + #<Card:0x007ffb19c22b00 @rank=:ace, @suit=:clubs>]
     # /tmp/d20151112-27349-1iah12b/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)>'

  6) WarDeck hand #allow_face_up? returns false if the cards are more than 3
     Failure/Error: expect(hand.allow_face_up?).to eq false
     NoMethodError:
       undefined method `allow_face_up?' for #<Hand:0x007ffb19bab8c0>
     # /tmp/d20151112-27349-1iah12b/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: 23.times { hand.play_card }
     NoMethodError:
       undefined method `play_card' for #<Hand:0x007ffb19b89ef0>
     # /tmp/d20151112-27349-1iah12b/spec.rb:176:in `block (5 levels) in <top (required)>'
     # /tmp/d20151112-27349-1iah12b/spec.rb:176:in `times'
     # /tmp/d20151112-27349-1iah12b/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 implements Enumerable
     Failure/Error: expect(small_deck.to_a).to eq [ace_of_spades, nine_of_clubs]
       
       expected: [#<Card:0x007ffb19b70630 @rank=:ace, @suit=:spades>, #<Card:0x007ffb19b705e0 @rank=9, @suit=:clubs>]
            got: [#<Card:0x007ffb19b705e0 @rank=9, @suit=:clubs>, #<Card:0x007ffb19b70630 @rank=:ace, @suit=:spades>]
       
       (compared using ==)
       
       Diff:
       @@ -1,3 +1,3 @@
       -[#<Card:0x007ffb19b70630 @rank=:ace, @suit=:spades>,
       - #<Card:0x007ffb19b705e0 @rank=9, @suit=:clubs>]
       +[#<Card:0x007ffb19b705e0 @rank=9, @suit=:clubs>,
       + #<Card:0x007ffb19b70630 @rank=:ace, @suit=:spades>]
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1iah12b/spec.rb:191
     # /tmp/d20151112-27349-1iah12b/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)>'

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

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

  11) 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"
       
       expected: "Ace of Spades\n9 of Clubs"
            got: "9 of Clubs\nAce of Spades"
       
       (compared using ==)
       
       Diff:
       
       @@ -1,3 +1,3 @@
       -Ace of Spades
        9 of Clubs
       +Ace of Spades
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1iah12b/spec.rb:191
     # /tmp/d20151112-27349-1iah12b/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)>'

  12) 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:0x007ffb19a40ff8 @rank=:jack, @suit=:spades>, #<Card:0x007ffb19a40fa8 @rank=10, @suit=:hearts>, #<Card:0x007ffb19a41020 @rank=:ace, @suit=:clubs>, #<Card:0x007ffb19a40fd0 @rank=7, @suit=:clubs>]
            got: [#<Card:0x007ffb19a40ff8 @rank=:jack, @suit=:spades>, #<Card:0x007ffb19a40fa8 @rank=10, @suit=:hearts>, #<Card:0x007ffb19a40fd0 @rank=7, @suit=:clubs>, #<Card:0x007ffb19a41020 @rank=:ace, @suit=:clubs>]
       
       (compared using ==)
       
       Diff:
       @@ -1,5 +1,5 @@
        [#<Card:0x007ffb19a40ff8 @rank=:jack, @suit=:spades>,
         #<Card:0x007ffb19a40fa8 @rank=10, @suit=:hearts>,
       - #<Card:0x007ffb19a41020 @rank=:ace, @suit=:clubs>,
       - #<Card:0x007ffb19a40fd0 @rank=7, @suit=:clubs>]
       + #<Card:0x007ffb19a40fd0 @rank=7, @suit=:clubs>,
       + #<Card:0x007ffb19a41020 @rank=:ace, @suit=:clubs>]
     # /tmp/d20151112-27349-1iah12b/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)>'

  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 #<Hand:0x007ffb199a2b50>
     # /tmp/d20151112-27349-1iah12b/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 #<Hand:0x007ffb1998bce8>
     # /tmp/d20151112-27349-1iah12b/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 #<Hand:0x007ffb19989268>
     # /tmp/d20151112-27349-1iah12b/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 #<Hand:0x007ffb19962578>
     # /tmp/d20151112-27349-1iah12b/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 #<Hand:0x007ffb197ab838>
     # /tmp/d20151112-27349-1iah12b/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 #<Hand:0x007ffb197a8fc0>
     # /tmp/d20151112-27349-1iah12b/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 #<Hand:0x007ffb19779068>
     # /tmp/d20151112-27349-1iah12b/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 #<Hand:0x007ffb19753818>
     # /tmp/d20151112-27349-1iah12b/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 #<Hand:0x007ffb19744598>
     # /tmp/d20151112-27349-1iah12b/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 #<Hand:0x007ffb19730368>
     # /tmp/d20151112-27349-1iah12b/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 #<Hand:0x007ffb19709600>
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1iah12b/spec.rb:386
     # /tmp/d20151112-27349-1iah12b/spec.rb:86:in `public_send'
     # /tmp/d20151112-27349-1iah12b/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 #<Hand:0x007ffb196dbf48>
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1iah12b/spec.rb:386
     # /tmp/d20151112-27349-1iah12b/spec.rb:101:in `public_send'
     # /tmp/d20151112-27349-1iah12b/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 #<Hand:0x007ffb196c90f0>
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1iah12b/spec.rb:390
     # /tmp/d20151112-27349-1iah12b/spec.rb:86:in `public_send'
     # /tmp/d20151112-27349-1iah12b/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 #<Hand:0x007ffb1964af20>
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1iah12b/spec.rb:390
     # /tmp/d20151112-27349-1iah12b/spec.rb:101:in `public_send'
     # /tmp/d20151112-27349-1iah12b/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 #<Hand:0x007ffb1962c250>
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1iah12b/spec.rb:394
     # /tmp/d20151112-27349-1iah12b/spec.rb:86:in `public_send'
     # /tmp/d20151112-27349-1iah12b/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 #<Hand:0x007ffb19625798>
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1iah12b/spec.rb:394
     # /tmp/d20151112-27349-1iah12b/spec.rb:101:in `public_send'
     # /tmp/d20151112-27349-1iah12b/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 implements Enumerable
     Failure/Error: expect(small_deck.to_a).to eq [ace_of_spades, nine_of_clubs]
       
       expected: [#<Card:0x007ffb1960bbb8 @rank=:ace, @suit=:spades>, #<Card:0x007ffb1960bb68 @rank=9, @suit=:clubs>]
            got: [#<Card:0x007ffb1960bb68 @rank=9, @suit=:clubs>, #<Card:0x007ffb1960bbb8 @rank=:ace, @suit=:spades>]
       
       (compared using ==)
       
       Diff:
       @@ -1,3 +1,3 @@
       -[#<Card:0x007ffb1960bbb8 @rank=:ace, @suit=:spades>,
       - #<Card:0x007ffb1960bb68 @rank=9, @suit=:clubs>]
       +[#<Card:0x007ffb1960bb68 @rank=9, @suit=:clubs>,
       + #<Card:0x007ffb1960bbb8 @rank=:ace, @suit=:spades>]
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1iah12b/spec.rb:400
     # /tmp/d20151112-27349-1iah12b/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)>'

  30) SixtySixDeck behaves like a deck #top peeks at the top-most card
     Failure/Error: expect(small_deck.to_a).to eq [ace_of_spades, nine_of_clubs]
       
       expected: [#<Card:0x007ffb18fde218 @rank=:ace, @suit=:spades>, #<Card:0x007ffb18fde038 @rank=9, @suit=:clubs>]
            got: [#<Card:0x007ffb18fde038 @rank=9, @suit=:clubs>, #<Card:0x007ffb18fde218 @rank=:ace, @suit=:spades>]
       
       (compared using ==)
       
       Diff:
       @@ -1,3 +1,3 @@
       -[#<Card:0x007ffb18fde218 @rank=:ace, @suit=:spades>,
       - #<Card:0x007ffb18fde038 @rank=9, @suit=:clubs>]
       +[#<Card:0x007ffb18fde038 @rank=9, @suit=:clubs>,
       + #<Card:0x007ffb18fde218 @rank=:ace, @suit=:spades>]
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1iah12b/spec.rb:400
     # /tmp/d20151112-27349-1iah12b/spec.rb:44:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  31) SixtySixDeck behaves like a deck #bottom peeks at the bottom-most card
     Failure/Error: expect(small_deck.to_a).to eq [ace_of_spades, nine_of_clubs]
       
       expected: [#<Card:0x007ffb18dcbb60 @rank=:ace, @suit=:spades>, #<Card:0x007ffb18dcb7f0 @rank=9, @suit=:clubs>]
            got: [#<Card:0x007ffb18dcb7f0 @rank=9, @suit=:clubs>, #<Card:0x007ffb18dcbb60 @rank=:ace, @suit=:spades>]
       
       (compared using ==)
       
       Diff:
       @@ -1,3 +1,3 @@
       -[#<Card:0x007ffb18dcbb60 @rank=:ace, @suit=:spades>,
       - #<Card:0x007ffb18dcb7f0 @rank=9, @suit=:clubs>]
       +[#<Card:0x007ffb18dcb7f0 @rank=9, @suit=:clubs>,
       + #<Card:0x007ffb18dcbb60 @rank=:ace, @suit=:spades>]
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1iah12b/spec.rb:400
     # /tmp/d20151112-27349-1iah12b/spec.rb:51:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  32) 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"
       
       expected: "Ace of Spades\n9 of Clubs"
            got: "9 of Clubs\nAce of Spades"
       
       (compared using ==)
       
       Diff:
       
       @@ -1,3 +1,3 @@
       -Ace of Spades
        9 of Clubs
       +Ace of Spades
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1iah12b/spec.rb:400
     # /tmp/d20151112-27349-1iah12b/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)>'

  33) 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:0x007ffb18df7468 @rank=:jack, @suit=:spades>, #<Card:0x007ffb18df7418 @rank=10, @suit=:hearts>, #<Card:0x007ffb18df74e0 @rank=:ace, @suit=:clubs>, #<Card:0x007ffb18df7440 @rank=9, @suit=:clubs>]
            got: [#<Card:0x007ffb18df7468 @rank=:jack, @suit=:spades>, #<Card:0x007ffb18df7418 @rank=10, @suit=:hearts>, #<Card:0x007ffb18df7440 @rank=9, @suit=:clubs>, #<Card:0x007ffb18df74e0 @rank=:ace, @suit=:clubs>]
       
       (compared using ==)
       
       Diff:
       @@ -1,5 +1,5 @@
        [#<Card:0x007ffb18df7468 @rank=:jack, @suit=:spades>,
         #<Card:0x007ffb18df7418 @rank=10, @suit=:hearts>,
       - #<Card:0x007ffb18df74e0 @rank=:ace, @suit=:clubs>,
       - #<Card:0x007ffb18df7440 @rank=9, @suit=:clubs>]
       + #<Card:0x007ffb18df7440 @rank=9, @suit=:clubs>,
       + #<Card:0x007ffb18df74e0 @rank=:ace, @suit=:clubs>]
     # /tmp/d20151112-27349-1iah12b/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)>'

  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 #<Hand:0x007ffb190c8d18>
     # /tmp/d20151112-27349-1iah12b/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 #<Hand:0x007ffb19556330>
     # /tmp/d20151112-27349-1iah12b/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 #<Hand:0x007ffb1953d4e8>
     # /tmp/d20151112-27349-1iah12b/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.06438 seconds
57 examples, 36 failures

Failed examples:

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

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

Кристъфър обнови решението на 11.11.2015 13:05 (преди над 8 години)

+class Card
+ attr_accessor :rank, :suit
+
+ def initialize(rank, suit)
+ @rank = rank
+ @suit = suit
+ end
+
+ def to_s
+ card_name = @rank.to_s.capitalize + " of " + @suit.to_s.capitalize
+ end
+
+ def ==(other)
+ return true if @rank == other.rank and @suit == other.suit
+ false
+ end
+end
+
+class Deck
+ include Enumerable
+
+ def initialize(*cards)
+ if(cards.empty?)
+ @deck_of_cards = generate_cards
+ shuffle
+ else
+ @deck_of_cards = cards.first
+ end
+ end
+
+ def size
+ @deck_of_cards.length
+ end
+
+ def draw_top_card
+ @deck_of_cards.shift
+ end
+
+ def draw_bottom_card
+ @deck_of_cards.pop
+ end
+
+ def top_card
+ @deck_of_cards.first
+ end
+
+ def bottom_card
+ @deck_of_cards.last
+ end
+
+ def shuffle
+ @deck_of_cards.shuffle!
+ self
+ end
+
+ def sort
+ @deck_of_cards = @deck_of_cards.sort_by{ |card| [card.suit] }
+ self
+ end
+
+ def to_s
+ all_cards_names = ""
+ self.each { |card| all_cards_names.concat(card.to_s.concat("\n")) }
+ all_cards_names
+ end
+
+ def each
+ number_of_cards = self.size
+ counter = number_of_cards - 1
+ while counter >= 0
+ yield @deck_of_cards[counter]
+ counter -= 1
+ end
+ end
+
+ def deal(number_of_cards_for_dealing)
+ cards_for_dealing = []
+ while number_of_cards_for_dealing > 0
+ top_card = draw_top_card
+ cards_for_dealing.push(top_card)
+ number_of_cards_for_dealing -= 1
+ end
+ Hand.new(cards_for_dealing)
+ end
+
+ private
+
+ def generate_cards
+ suits = [:spades, :hearts, :diamonds, :clubs]
+ ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
+ card_array = merge_card_arrays(ranks, suits)
+ end
+
+ def merge_card_arrays(array_1, array_2)
+ merged_array = []
+ array_1.each { |x| array_2.each{ |y| merged_array.push(Card.new(x, y)) }}
+ merged_array
+ end
+end
+
+class WarDeck < Deck
+ def initialize(*cards)
+ super
+ @number_of_cards_in_hand = 26
+ end
+
+ def deal
+ super(@number_of_cards_in_hand)
+ end
+end
+
+class BeloteDeck < Deck
+ def initialize(*cards)
+ super
+ @number_of_cards_in_hand = 8
+ end
+
+ def deal
+ super(@number_of_cards_in_hand)
+ end
+
+ private
+
+ def generate_cards
+ suits = [:spades, :hearts, :diamonds, :clubs]
+ ranks = [7, 8, 9, :jack, :queen, :king, 10, :ace]
+ card_array = merge_card_arrays(ranks, suits)
+ end
+end
+
+class SixtySixDeck < Deck
+ def initialize(*cards)
+ super
+ @number_of_cards_in_hand = 6
+ end
+
+ def deal
+ super(@number_of_cards_in_hand)
+ end
+
+ private
+
+ def generate_cards
+ suits = [:spades, :hearts, :diamonds, :clubs]
+ ranks = [9, :jack, :queen, :king, 10, :ace]
+ card_array = merge_card_arrays(ranks, suits)
+ end
+end
+
+class Hand
+ def initialize(cards)
+ @cards_in_hand = Deck.new(cards)
+ end
+
+ def to_s
+ @cards_in_hand.to_s
+ end
+
+ def size
+ @cards_in_hand.size
+ end
+end

Кристъфър обнови решението на 11.11.2015 13:16 (преди над 8 години)

class Card
attr_accessor :rank, :suit
def initialize(rank, suit)
@rank = rank
@suit = suit
end
def to_s
- card_name = @rank.to_s.capitalize + " of " + @suit.to_s.capitalize
+ card_name = @rank.to_s.capitalize + " of " + @suit.to_s.capitalize
end
def ==(other)
return true if @rank == other.rank and @suit == other.suit
- false
+ false
end
end
class Deck
- include Enumerable
+ include Enumerable
def initialize(*cards)
if(cards.empty?)
@deck_of_cards = generate_cards
shuffle
else
@deck_of_cards = cards.first
end
end
def size
- @deck_of_cards.length
+ @deck_of_cards.length
end
def draw_top_card
@deck_of_cards.shift
end
def draw_bottom_card
@deck_of_cards.pop
end
def top_card
@deck_of_cards.first
- end
+ end
def bottom_card
- @deck_of_cards.last
+ @deck_of_cards.last
end
def shuffle
@deck_of_cards.shuffle!
self
end
def sort
@deck_of_cards = @deck_of_cards.sort_by{ |card| [card.suit] }
self
end
def to_s
all_cards_names = ""
self.each { |card| all_cards_names.concat(card.to_s.concat("\n")) }
- all_cards_names
+ all_cards_names
end
def each
- number_of_cards = self.size
+ number_of_cards = self.size
counter = number_of_cards - 1
while counter >= 0
yield @deck_of_cards[counter]
counter -= 1
end
end
def deal(number_of_cards_for_dealing)
cards_for_dealing = []
while number_of_cards_for_dealing > 0
top_card = draw_top_card
cards_for_dealing.push(top_card)
number_of_cards_for_dealing -= 1
end
Hand.new(cards_for_dealing)
end
private
def generate_cards
suits = [:spades, :hearts, :diamonds, :clubs]
ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
- card_array = merge_card_arrays(ranks, suits)
+ card_array = merge_card_arrays(ranks, suits)
end
def merge_card_arrays(array_1, array_2)
merged_array = []
array_1.each { |x| array_2.each{ |y| merged_array.push(Card.new(x, y)) }}
merged_array
end
end
class WarDeck < Deck
def initialize(*cards)
super
@number_of_cards_in_hand = 26
end
def deal
super(@number_of_cards_in_hand)
end
end
class BeloteDeck < Deck
def initialize(*cards)
super
@number_of_cards_in_hand = 8
end
def deal
super(@number_of_cards_in_hand)
end
private
def generate_cards
suits = [:spades, :hearts, :diamonds, :clubs]
ranks = [7, 8, 9, :jack, :queen, :king, 10, :ace]
card_array = merge_card_arrays(ranks, suits)
end
end
class SixtySixDeck < Deck
def initialize(*cards)
super
@number_of_cards_in_hand = 6
end
def deal
super(@number_of_cards_in_hand)
end
private
def generate_cards
suits = [:spades, :hearts, :diamonds, :clubs]
ranks = [9, :jack, :queen, :king, 10, :ace]
card_array = merge_card_arrays(ranks, suits)
end
end
class Hand
def initialize(cards)
@cards_in_hand = Deck.new(cards)
end
def to_s
@cards_in_hand.to_s
end
def size
@cards_in_hand.size
end
end

Кристъфър обнови решението на 11.11.2015 13:18 (преди над 8 години)

class Card
attr_accessor :rank, :suit
def initialize(rank, suit)
@rank = rank
@suit = suit
end
def to_s
- card_name = @rank.to_s.capitalize + " of " + @suit.to_s.capitalize
+ card_name = @rank.to_s.capitalize + " of " + @suit.to_s.capitalize
end
def ==(other)
return true if @rank == other.rank and @suit == other.suit
- false
+ false
end
end
class Deck
include Enumerable
def initialize(*cards)
if(cards.empty?)
@deck_of_cards = generate_cards
shuffle
else
@deck_of_cards = cards.first
end
end
def size
- @deck_of_cards.length
+ @deck_of_cards.length
end
def draw_top_card
@deck_of_cards.shift
end
def draw_bottom_card
@deck_of_cards.pop
end
def top_card
@deck_of_cards.first
end
def bottom_card
- @deck_of_cards.last
+ @deck_of_cards.last
end
def shuffle
@deck_of_cards.shuffle!
self
end
def sort
@deck_of_cards = @deck_of_cards.sort_by{ |card| [card.suit] }
self
end
def to_s
all_cards_names = ""
self.each { |card| all_cards_names.concat(card.to_s.concat("\n")) }
- all_cards_names
+ all_cards_names
end
def each
- number_of_cards = self.size
+ number_of_cards = self.size
counter = number_of_cards - 1
while counter >= 0
yield @deck_of_cards[counter]
counter -= 1
end
end
def deal(number_of_cards_for_dealing)
cards_for_dealing = []
while number_of_cards_for_dealing > 0
top_card = draw_top_card
cards_for_dealing.push(top_card)
number_of_cards_for_dealing -= 1
end
Hand.new(cards_for_dealing)
end
private
def generate_cards
suits = [:spades, :hearts, :diamonds, :clubs]
ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
- card_array = merge_card_arrays(ranks, suits)
+ card_array = merge_card_arrays(ranks, suits)
end
def merge_card_arrays(array_1, array_2)
merged_array = []
array_1.each { |x| array_2.each{ |y| merged_array.push(Card.new(x, y)) }}
merged_array
end
end
class WarDeck < Deck
def initialize(*cards)
super
@number_of_cards_in_hand = 26
end
def deal
super(@number_of_cards_in_hand)
end
end
class BeloteDeck < Deck
def initialize(*cards)
super
@number_of_cards_in_hand = 8
end
def deal
super(@number_of_cards_in_hand)
end
private
def generate_cards
suits = [:spades, :hearts, :diamonds, :clubs]
ranks = [7, 8, 9, :jack, :queen, :king, 10, :ace]
card_array = merge_card_arrays(ranks, suits)
end
end
class SixtySixDeck < Deck
def initialize(*cards)
super
@number_of_cards_in_hand = 6
end
def deal
super(@number_of_cards_in_hand)
end
private
def generate_cards
suits = [:spades, :hearts, :diamonds, :clubs]
ranks = [9, :jack, :queen, :king, 10, :ace]
card_array = merge_card_arrays(ranks, suits)
end
end
class Hand
def initialize(cards)
@cards_in_hand = Deck.new(cards)
end
def to_s
@cards_in_hand.to_s
end
def size
@cards_in_hand.size
end
end

Кристъфър обнови решението на 11.11.2015 13:18 (преди над 8 години)

class Card
attr_accessor :rank, :suit
def initialize(rank, suit)
@rank = rank
@suit = suit
end
def to_s
- card_name = @rank.to_s.capitalize + " of " + @suit.to_s.capitalize
+ card_name = @rank.to_s.capitalize + " of " + @suit.to_s.capitalize
end
def ==(other)
return true if @rank == other.rank and @suit == other.suit
false
end
end
class Deck
include Enumerable
def initialize(*cards)
if(cards.empty?)
@deck_of_cards = generate_cards
shuffle
else
@deck_of_cards = cards.first
end
end
def size
@deck_of_cards.length
end
def draw_top_card
@deck_of_cards.shift
end
def draw_bottom_card
@deck_of_cards.pop
end
def top_card
@deck_of_cards.first
end
def bottom_card
@deck_of_cards.last
end
def shuffle
@deck_of_cards.shuffle!
self
end
def sort
@deck_of_cards = @deck_of_cards.sort_by{ |card| [card.suit] }
self
end
def to_s
all_cards_names = ""
self.each { |card| all_cards_names.concat(card.to_s.concat("\n")) }
all_cards_names
end
def each
number_of_cards = self.size
counter = number_of_cards - 1
while counter >= 0
yield @deck_of_cards[counter]
counter -= 1
end
end
def deal(number_of_cards_for_dealing)
cards_for_dealing = []
while number_of_cards_for_dealing > 0
top_card = draw_top_card
cards_for_dealing.push(top_card)
number_of_cards_for_dealing -= 1
end
Hand.new(cards_for_dealing)
end
private
def generate_cards
suits = [:spades, :hearts, :diamonds, :clubs]
ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
card_array = merge_card_arrays(ranks, suits)
end
def merge_card_arrays(array_1, array_2)
merged_array = []
array_1.each { |x| array_2.each{ |y| merged_array.push(Card.new(x, y)) }}
merged_array
end
end
class WarDeck < Deck
def initialize(*cards)
super
@number_of_cards_in_hand = 26
end
def deal
super(@number_of_cards_in_hand)
end
end
class BeloteDeck < Deck
def initialize(*cards)
super
@number_of_cards_in_hand = 8
end
def deal
super(@number_of_cards_in_hand)
end
private
def generate_cards
suits = [:spades, :hearts, :diamonds, :clubs]
ranks = [7, 8, 9, :jack, :queen, :king, 10, :ace]
card_array = merge_card_arrays(ranks, suits)
end
end
class SixtySixDeck < Deck
def initialize(*cards)
super
@number_of_cards_in_hand = 6
end
def deal
super(@number_of_cards_in_hand)
end
private
def generate_cards
suits = [:spades, :hearts, :diamonds, :clubs]
ranks = [9, :jack, :queen, :king, 10, :ace]
card_array = merge_card_arrays(ranks, suits)
end
end
class Hand
def initialize(cards)
@cards_in_hand = Deck.new(cards)
end
def to_s
@cards_in_hand.to_s
end
def size
@cards_in_hand.size
end
end