Решение на Четвърта задача от Александрина Каракехайова

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

Към профила на Александрина Каракехайова

Резултати

  • 5 точки от тестове
  • 1 отнета точка
  • 4 точки общо
  • 47 успешни тест(а)
  • 10 неуспешни тест(а)

Код

class Card
def initialize(rank, suit)
@rank = rank
@suit = suit
end
def rank
@rank
end
def suit
@suit
end
def to_s
"#{rank.to_s.capitalize} of #{suit.capitalize}"
end
def ==(another_card)
@rank == another_card.rank && @suit == another_card.suit
end
end
class Deck
include Enumerable
def standard_deck
deck = Array.new
ranks = %w{2 3 4 5 6 7 8 9 10 :jack :queen :king :ace}
suits = %w{:spades :heart :diamonds :clubs}
suits.each do |suit|
ranks.each do |rank|
deck << Card.new(rank, suit)
end
end
deck
end
def initialize(array_of_cards = standard_deck)
@deck = array_of_cards
end
def each
@deck.each { |card| yield card}
end
def size
@deck.length
end
def draw_top_card
@deck.slice!(0)
end
def draw_bottom_card
@deck.pop
end
def top_card
@deck.first
end
def bottom_card
@deck.last
end
def shuffle
@deck.shuffle!
end
def to_s
@deck.each{|card| card.to_s + "\n"}
end
def deal
@deck.slice!(0,26)
end
end
class WarDeck < Deck
def standard_deck
deck = Array.new
ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
suits = [:spades, :heart, :diamonds, :clubs]
suits.each do |suit|
ranks.each do |rank|
deck << Card.new(rank, suit)
end
end
deck
end
def initialize(array_of_cards = standard_deck)
@deck = array_of_cards
end
def deal
array = @deck.slice!(0,26)
Hand.new(array)
end
def sort
rank = [:ace, :king, :queen, :jack, 10, 9, 8, 7, 6, 5, 4, 3, 2]
suit = [:spades, :hearts, :queen, :jack]
@deck.sort_by!{|a| [rank.index(a.rank), suit.index(a.suit)].reverse}
end
end
class BeloteDeck < Deck
def standard_deck
deck = Array.new
ranks = [7, 8, 9, 10, :jack, :queen, :king, :ace]
suits = [:spades, :hearts, :diamonds, :clubs]
suits.each do |suit|
ranks.each do |rank|
deck << Card.new(rank, suit)
end
end
deck
end
def initialize(array_of_cards = standard_deck)
@deck = array_of_cards
end
def deal
array = @deck.slice!(0,8)
BeloteHand.new(array)
end
def sort
rank = [:ace, 10, :king, :queen, :jack, 9, 8, 7]
suit = [:spades, :hearts, :diamonds, :clubs]
@deck.sort_by!{|a| [rank.index(a.rank), suit.index(a.suit)].reverse}
end
end
class SixtySixDeck < Deck
def standard_deck
deck = Array.new
ranks = [9, 10, :jack, :queen, :king, :ace]
suits = [:spades, :hearts, :diamonds, :clubs]
suits.each do |suit|
ranks.each do |rank|
deck << Card.new(rank, suit)
end
end
deck
end
def initialize(array_of_cards = standard_deck)
@deck = array_of_cards
end
def deal
cards = @deck.slice!(0,6)
Hand.new(cards)
end
def sort
rank = [:ace, 10, :king, :queen, :jack, 9]
suit = [:spades, :hearts, :diamonds, :clubs]
@deck.sort_by!{|a| [rank.index(a.rank), suit.index(a.suit)].reverse}
end
end
class Hand < Deck
def initialize(array)
@hand = array
end
def play_card
@hand.pop
end
def allow_face_up?
@hand.size <= 3
end
def twenty?(trump_suit)
suits = [:spades, :hearts, :diamonds, :clubs]
suits.delete(trump_suit)
twenty_help(suits)
end
def twenty_help(array_of_suits)
count, found = 0, false
while count < 3
king = @hand.include?(Card.new(:king, array_of_suits[count]))
queen = @hand.include?(Card.new(:queen, array_of_suits[count]))
if king and queen
found = true
end
count += 1
end
found
end
def size
@hand.length
end
def forty?(trump_suit)
king = @hand.include?(Card.new(:king, trump_suit))
queen = @hand.include?(Card.new(:queen, trump_suit))
king and queen
end
end
class BeloteHand < BeloteDeck
def initialize(array)
@hand = array
end
def highest_of_suit(suit)
deck = Array.new
ranks = [:ace, 10, :king, :queen, :jack, 9, 8, 7]
@hand.sort_by! {|card| ranks.index(card.rank)}
@hand.each do |card|
if card.suit == suit
deck << card
end
end
deck.first
end
def belote?
suits = [:spades, :hearts, :diamonds, :clubs]
belote_help(suits)
end
def belote_help(array_of_suits)
count, found = 0, false
while count < 4
king = @hand.include?(Card.new(:king, array_of_suits[count]))
queen = @hand.include?(Card.new(:queen, array_of_suits[count]))
if king and queen
found = true
end
count += 1
end
found
end
def help(number)
deck = tierce_help
found, first, second = false, [], []
deck.each_cons(number) do |cards| first << cards
end
@hand.each_cons(number) do |other_cards| second << other_cards
end
if first & second != []
found = true
end
found
end
def tierce?
help(3)
end
def quarte?
help(4)
end
def quint?
help(5)
end
def tierce_help
deck = Array.new
rank = [7, 8, 9, :jack, :queen, :king, 10, :ace]
suit = [:spades, :hearts, :diamonds, :clubs]
suit.each do |suit|
rank.each do |rank|
deck << Card.new(rank, suit)
end
end
deck
end
def carre?(rank)
spade = @hand.include?(Card.new(rank, :spades))
heart = @hand.include?(Card.new(rank, :hearts))
diamond = @hand.include?(Card.new(rank, :diamonds))
club = @hand.include?(Card.new(rank, :clubs))
spade and heart and diamond and club
end
def carre_of_jacks?
carre?(:jack)
end
def carre_of_nines?
carre?(9)
end
def carre_of_aces?
carre?(:ace)
end
end

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

....F......FF...........F.F...FF.F.F...............F.....

Failures:

  1) WarDeck behaves like a deck fills the deck if no initialize parameters are given
     Failure/Error: expect(deck.to_a).to match_array all_available_cards
       expected collection contained:  [#<Card:0x007ff90a242ce0 @rank=2, @suit=:clubs>, #<Card:0x007ff90a242cb8 @rank=3, @suit=:clubs>, #<Card:0x007ff90a242c90 @rank=4, @suit=:clubs>, #<Card:0x007ff90a242c68 @rank=5, @suit=:clubs>, #<Card:0x007ff90a242c40 @rank=6, @suit=:clubs>, #<Card:0x007ff90a242c18 @rank=7, @suit=:clubs>, #<Card:0x007ff90a242bf0 @rank=8, @suit=:clubs>, #<Card:0x007ff90a242bc8 @rank=9, @suit=:clubs>, #<Card:0x007ff90a242ba0 @rank=10, @suit=:clubs>, #<Card:0x007ff90a242b78 @rank=:jack, @suit=:clubs>, #<Card:0x007ff90a242b50 @rank=:queen, @suit=:clubs>, #<Card:0x007ff90a242b28 @rank=:king, @suit=:clubs>, #<Card:0x007ff90a242b00 @rank=:ace, @suit=:clubs>, #<Card:0x007ff90a242ad8 @rank=2, @suit=:diamonds>, #<Card:0x007ff90a242ab0 @rank=3, @suit=:diamonds>, #<Card:0x007ff90a242a88 @rank=4, @suit=:diamonds>, #<Card:0x007ff90a242a10 @rank=5, @suit=:diamonds>, #<Card:0x007ff90a2429e8 @rank=6, @suit=:diamonds>, #<Card:0x007ff90a242998 @rank=7, @suit=:diamonds>, #<Card:0x007ff90a242970 @rank=8, @suit=:diamonds>, #<Card:0x007ff90a242948 @rank=9, @suit=:diamonds>, #<Card:0x007ff90a242920 @rank=10, @suit=:diamonds>, #<Card:0x007ff90a2428f8 @rank=:jack, @suit=:diamonds>, #<Card:0x007ff90a2428d0 @rank=:queen, @suit=:diamonds>, #<Card:0x007ff90a2428a8 @rank=:king, @suit=:diamonds>, #<Card:0x007ff90a242880 @rank=:ace, @suit=:diamonds>, #<Card:0x007ff90a242858 @rank=2, @suit=:hearts>, #<Card:0x007ff90a242830 @rank=3, @suit=:hearts>, #<Card:0x007ff90a242808 @rank=4, @suit=:hearts>, #<Card:0x007ff90a2427e0 @rank=5, @suit=:hearts>, #<Card:0x007ff90a2427b8 @rank=6, @suit=:hearts>, #<Card:0x007ff90a242790 @rank=7, @suit=:hearts>, #<Card:0x007ff90a242768 @rank=8, @suit=:hearts>, #<Card:0x007ff90a242740 @rank=9, @suit=:hearts>, #<Card:0x007ff90a242718 @rank=10, @suit=:hearts>, #<Card:0x007ff90a2426f0 @rank=:jack, @suit=:hearts>, #<Card:0x007ff90a2426c8 @rank=:queen, @suit=:hearts>, #<Card:0x007ff90a2426a0 @rank=:king, @suit=:hearts>, #<Card:0x007ff90a242678 @rank=:ace, @suit=:hearts>, #<Card:0x007ff90a242650 @rank=2, @suit=:spades>, #<Card:0x007ff90a242600 @rank=3, @suit=:spades>, #<Card:0x007ff90a2425b0 @rank=4, @suit=:spades>, #<Card:0x007ff90a242560 @rank=5, @suit=:spades>, #<Card:0x007ff90a242538 @rank=6, @suit=:spades>, #<Card:0x007ff90a2424e8 @rank=7, @suit=:spades>, #<Card:0x007ff90a242498 @rank=8, @suit=:spades>, #<Card:0x007ff90a242470 @rank=9, @suit=:spades>, #<Card:0x007ff90a242448 @rank=10, @suit=:spades>, #<Card:0x007ff90a242420 @rank=:jack, @suit=:spades>, #<Card:0x007ff90a2423f8 @rank=:queen, @suit=:spades>, #<Card:0x007ff90a2423d0 @rank=:king, @suit=:spades>, #<Card:0x007ff90a2423a8 @rank=:ace, @suit=:spades>]
       actual collection contained:    [#<Card:0x007ff90a1cc1a8 @rank=2, @suit=:spades>, #<Card:0x007ff90a1cc158 @rank=3, @suit=:spades>, #<Card:0x007ff90a1cc130 @rank=4, @suit=:spades>, #<Card:0x007ff90a1cc108 @rank=5, @suit=:spades>, #<Card:0x007ff90a1cc0e0 @rank=6, @suit=:spades>, #<Card:0x007ff90a1cc0b8 @rank=7, @suit=:spades>, #<Card:0x007ff90a1cc090 @rank=8, @suit=:spades>, #<Card:0x007ff90a1cc040 @rank=9, @suit=:spades>, #<Card:0x007ff90a1cc018 @rank=10, @suit=:spades>, #<Card:0x007ff90a1cde40 @rank=:jack, @suit=:spades>, #<Card:0x007ff90a243fa0 @rank=:queen, @suit=:spades>, #<Card:0x007ff90a243f78 @rank=:king, @suit=:spades>, #<Card:0x007ff90a243f50 @rank=:ace, @suit=:spades>, #<Card:0x007ff90a243f28 @rank=2, @suit=:heart>, #<Card:0x007ff90a243f00 @rank=3, @suit=:heart>, #<Card:0x007ff90a243ed8 @rank=4, @suit=:heart>, #<Card:0x007ff90a243eb0 @rank=5, @suit=:heart>, #<Card:0x007ff90a243e88 @rank=6, @suit=:heart>, #<Card:0x007ff90a243e60 @rank=7, @suit=:heart>, #<Card:0x007ff90a243e38 @rank=8, @suit=:heart>, #<Card:0x007ff90a243e10 @rank=9, @suit=:heart>, #<Card:0x007ff90a243de8 @rank=10, @suit=:heart>, #<Card:0x007ff90a243dc0 @rank=:jack, @suit=:heart>, #<Card:0x007ff90a243d98 @rank=:queen, @suit=:heart>, #<Card:0x007ff90a243d70 @rank=:king, @suit=:heart>, #<Card:0x007ff90a243d48 @rank=:ace, @suit=:heart>, #<Card:0x007ff90a243d20 @rank=2, @suit=:diamonds>, #<Card:0x007ff90a243cf8 @rank=3, @suit=:diamonds>, #<Card:0x007ff90a243cd0 @rank=4, @suit=:diamonds>, #<Card:0x007ff90a243ca8 @rank=5, @suit=:diamonds>, #<Card:0x007ff90a243c80 @rank=6, @suit=:diamonds>, #<Card:0x007ff90a243c58 @rank=7, @suit=:diamonds>, #<Card:0x007ff90a243c30 @rank=8, @suit=:diamonds>, #<Card:0x007ff90a243c08 @rank=9, @suit=:diamonds>, #<Card:0x007ff90a243be0 @rank=10, @suit=:diamonds>, #<Card:0x007ff90a243bb8 @rank=:jack, @suit=:diamonds>, #<Card:0x007ff90a243b90 @rank=:queen, @suit=:diamonds>, #<Card:0x007ff90a243b68 @rank=:king, @suit=:diamonds>, #<Card:0x007ff90a243b40 @rank=:ace, @suit=:diamonds>, #<Card:0x007ff90a243b18 @rank=2, @suit=:clubs>, #<Card:0x007ff90a243af0 @rank=3, @suit=:clubs>, #<Card:0x007ff90a243ac8 @rank=4, @suit=:clubs>, #<Card:0x007ff90a243aa0 @rank=5, @suit=:clubs>, #<Card:0x007ff90a243a78 @rank=6, @suit=:clubs>, #<Card:0x007ff90a243a50 @rank=7, @suit=:clubs>, #<Card:0x007ff90a2439d8 @rank=8, @suit=:clubs>, #<Card:0x007ff90a2439b0 @rank=9, @suit=:clubs>, #<Card:0x007ff90a243960 @rank=10, @suit=:clubs>, #<Card:0x007ff90a243910 @rank=:jack, @suit=:clubs>, #<Card:0x007ff90a243898 @rank=:queen, @suit=:clubs>, #<Card:0x007ff90a243870 @rank=:king, @suit=:clubs>, #<Card:0x007ff90a243848 @rank=:ace, @suit=:clubs>]
       the missing elements were:      [#<Card:0x007ff90a242858 @rank=2, @suit=:hearts>, #<Card:0x007ff90a242830 @rank=3, @suit=:hearts>, #<Card:0x007ff90a242808 @rank=4, @suit=:hearts>, #<Card:0x007ff90a2427e0 @rank=5, @suit=:hearts>, #<Card:0x007ff90a2427b8 @rank=6, @suit=:hearts>, #<Card:0x007ff90a242790 @rank=7, @suit=:hearts>, #<Card:0x007ff90a242768 @rank=8, @suit=:hearts>, #<Card:0x007ff90a242740 @rank=9, @suit=:hearts>, #<Card:0x007ff90a242718 @rank=10, @suit=:hearts>, #<Card:0x007ff90a2426f0 @rank=:jack, @suit=:hearts>, #<Card:0x007ff90a2426c8 @rank=:queen, @suit=:hearts>, #<Card:0x007ff90a2426a0 @rank=:king, @suit=:hearts>, #<Card:0x007ff90a242678 @rank=:ace, @suit=:hearts>]
       the extra elements were:        [#<Card:0x007ff90a243f28 @rank=2, @suit=:heart>, #<Card:0x007ff90a243f00 @rank=3, @suit=:heart>, #<Card:0x007ff90a243ed8 @rank=4, @suit=:heart>, #<Card:0x007ff90a243eb0 @rank=5, @suit=:heart>, #<Card:0x007ff90a243e88 @rank=6, @suit=:heart>, #<Card:0x007ff90a243e60 @rank=7, @suit=:heart>, #<Card:0x007ff90a243e38 @rank=8, @suit=:heart>, #<Card:0x007ff90a243e10 @rank=9, @suit=:heart>, #<Card:0x007ff90a243de8 @rank=10, @suit=:heart>, #<Card:0x007ff90a243dc0 @rank=:jack, @suit=:heart>, #<Card:0x007ff90a243d98 @rank=:queen, @suit=:heart>, #<Card:0x007ff90a243d70 @rank=:king, @suit=:heart>, #<Card:0x007ff90a243d48 @rank=:ace, @suit=:heart>]
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-mydqzs/spec.rb:140
     # /tmp/d20151112-27349-mydqzs/spec.rb:18:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

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

  3) WarDeck #sort sorts the cards in the defined order
     Failure/Error: expect(deck.sort.to_a).to eq [jack_of_spades, ten_of_hearts, ace_of_clubs, two_of_clubs]
     ArgumentError:
       comparison of Array with Array failed
     # /tmp/d20151112-27349-mydqzs/solution.rb:105:in `sort_by'
     # /tmp/d20151112-27349-mydqzs/solution.rb:105:in `sort_by!'
     # /tmp/d20151112-27349-mydqzs/solution.rb:105:in `sort'
     # /tmp/d20151112-27349-mydqzs/spec.rb:155:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) BeloteDeck behaves like a deck #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:0x007ff90a1ef018>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-mydqzs/spec.rb:191
     # /tmp/d20151112-27349-mydqzs/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 hand #deal deals 8 cards
     Failure/Error: expect(hand.size).to eq 8
     NoMethodError:
       undefined method `length' for nil:NilClass
     # /tmp/d20151112-27349-mydqzs/solution.rb:48:in `size'
     # /tmp/d20151112-27349-mydqzs/spec.rb:215:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  6) 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-mydqzs/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)>'

  7) 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-mydqzs/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)>'

  8) 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-mydqzs/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)>'

  9) 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-mydqzs/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)>'

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

Finished in 0.04101 seconds
57 examples, 10 failures

Failed examples:

rspec /tmp/d20151112-27349-mydqzs/spec.rb:14 # WarDeck behaves like a deck fills the deck if no initialize parameters are given
rspec /tmp/d20151112-27349-mydqzs/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-mydqzs/spec.rb:145 # WarDeck #sort sorts the cards in the defined order
rspec /tmp/d20151112-27349-mydqzs/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-mydqzs/spec.rb:212 # BeloteDeck hand #deal deals 8 cards
rspec /tmp/d20151112-27349-mydqzs/spec.rb:272 # BeloteDeck hand #tierce? with tierce returns true for cards with names
rspec /tmp/d20151112-27349-mydqzs/spec.rb:287 # BeloteDeck hand #tierce? with tierce returns true for cards with numbers
rspec /tmp/d20151112-27349-mydqzs/spec.rb:322 # BeloteDeck hand #quarte? detects four cards with increasing ranks
rspec /tmp/d20151112-27349-mydqzs/spec.rb:354 # BeloteDeck hand #quint? detects five cards with increasing ranks
rspec /tmp/d20151112-27349-mydqzs/spec.rb:67 # SixtySixDeck behaves like a deck #to_s returns the names of the cards, each on its own line

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

Александрина обнови решението на 11.11.2015 15:41 (преди над 8 години)

+class Card
+ def initialize(rank, suit)
+ @rank = rank
+ @suit = suit
+ end
+
+ def rank
+ @rank
+ end
+
+ def suit
+ @suit
+ end
+
+ def to_s
+ "#{rank.to_s.capitalize} of #{suit.capitalize}"
+ end
+
+ def ==(another_card)
+ @rank == another_card.rank && @suit == another_card.suit
+ end
+end
+
+class Deck
+ include Enumerable
+ def standard_deck
+ deck = Array.new
+ ranks = %w{2 3 4 5 6 7 8 9 10 :jack :queen :king :ace}
+ suits = %w{:spades :heart :diamonds :clubs}
+ suits.each do |suit|
+ ranks.each do |rank|
+ deck << Card.new(rank, suit)
+ end
+ end
+ deck
+ end
+
+
+ def initialize(array_of_cards = standard_deck)
+ @deck = array_of_cards
+ end
+
+ def each
+ @deck.each { |card| yield card}
+ end
+
+ def size
+ @deck.length
+ end
+
+ def draw_top_card
+ @deck.slice!(0)
+ end
+
+ def draw_bottom_card
+ @deck.pop
+ end
+
+ def top_card
+ @deck.first
+ end
+
+ def bottom_card
+ @deck.last
+ end
+
+ def shuffle
+ @deck.shuffle!
+ end
+
+ def to_s
+ @deck.each{|card| card.to_s + "\n"}
+ end
+
+ def deal
+ @deck.slice!(0,26)
+ end
+end
+
+class WarDeck < Deck
+ def standard_deck
+ deck = Array.new
+ ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
+ suits = [:spades, :heart, :diamonds, :clubs]
+ suits.each do |suit|
+ ranks.each do |rank|
+ deck << Card.new(rank, suit)
+ end
+ end
+ deck
+ end
+
+ def initialize(array_of_cards = standard_deck)
+ @deck = array_of_cards
+ end
+
+ def deal
+ array = @deck.slice!(0,26)
+ Hand.new(array)
+ end
+
+ def sort
+ rank = [:ace, :king, :queen, :jack, 10, 9, 8, 7, 6, 5, 4, 3, 2]
+ suit = [:spades, :hearts, :queen, :jack]
+ @deck.sort_by!{|a| [rank.index(a.rank), suit.index(a.suit)].reverse}
+ end
+
+end
+
+class BeloteDeck < Deck
+ def standard_deck
+ deck = Array.new
+ ranks = [7, 8, 9, 10, :jack, :queen, :king, :ace]
+ suits = [:spades, :hearts, :diamonds, :clubs]
+ suits.each do |suit|
+ ranks.each do |rank|
+ deck << Card.new(rank, suit)
+ end
+ end
+ deck
+ end
+
+ def initialize(array_of_cards = standard_deck)
+ @deck = array_of_cards
+ end
+
+ def deal
+ array = @deck.slice!(0,8)
+ BeloteHand.new(array)
+ end
+
+ def sort
+ rank = [:ace, 10, :king, :queen, :jack, 9, 8, 7]
+ suit = [:spades, :hearts, :diamonds, :clubs]
+ @deck.sort_by!{|a| [rank.index(a.rank), suit.index(a.suit)].reverse}
+ end
+end
+
+class SixtySixDeck < Deck
+ def standard_deck
+ deck = Array.new
+ ranks = [9, 10, :jack, :queen, :king, :ace]
+ suits = [:spades, :hearts, :diamonds, :clubs]
+ suits.each do |suit|
+ ranks.each do |rank|
+ deck << Card.new(rank, suit)
+ end
+ end
+ deck
+ end
+
+ def initialize(array_of_cards = standard_deck)
+ @deck = array_of_cards
+ end
+
+ def deal
+ cards = @deck.slice!(0,6)
+ Hand.new(cards)
+ end
+
+ def sort
+ rank = [:ace, 10, :king, :queen, :jack, 9]
+ suit = [:spades, :hearts, :diamonds, :clubs]
+ @deck.sort_by!{|a| [rank.index(a.rank), suit.index(a.suit)].reverse}
+ end
+end
+
+class Hand < Deck
+ def initialize(array)
+ @hand = array
+ end
+
+
+ def play_card
+ @hand.pop
+ end
+
+ def allow_face_up?
+ @hand.size <= 3
+ end
+
+
+ def twenty?(trump_suit)
+ suits = [:spades, :hearts, :diamonds, :clubs]
+ suits.delete(trump_suit)
+ twenty_help(suits)
+ end
+
+ def twenty_help(array_of_suits)
+ count, found = 0, false
+ while count < number
+ king = @hand.include?(Card.new(:king, suit[count]))
+ queen = @hand.include?(Card.new(:queen, suit[count]))
+ if king and queen
+ found = true
+ end
+ count += 1
+ end
+ found
+ end
+
+
+ def size
+ @hand.length
+ end
+
+ def forty?(trump_suit)
+ king = @hand.include?(Card.new(:king, trump_suit))
+ queen = @hand.include?(Card.new(:queen, trump_suit))
+ king and queen
+ end
+end
+
+class BeloteHand < BeloteDeck
+ def initialize(array)
+ @hand = array
+ end
+
+ def highest_of_suit(suit)
+ deck = Array.new
+ ranks = [:ace, 10, :king, :queen, :jack, 9, 8, 7]
+ @hand.sort_by! {|card| ranks.index(card.rank)}
+ @hand.each do |card|
+ if card.suit == suit
+ deck << card
+ end
+ end
+ deck.first
+ end
+
+ def belote?
+ suits = [:spades, :hearts, :diamonds, :clubs]
+ belote_help(suits)
+ end
+
+ def belote_help(array_of_suits)
+ count, found = 0, false
+ while count < number
+ king = @hand.include?(Card.new(:king, suit[count]))
+ queen = @hand.include?(Card.new(:queen, suit[count]))
+ if king and queen
+ found = true
+ end
+ count += 1
+ end
+ found
+ end
+
+ def help(number)
+ deck = tierce_help
+ found, first, second = false, [], []
+ deck.each_cons(number) do |cards| first << cards
+ end
+ @hand.each_cons(number) do |other_cards| second << other_cards
+ end
+ if first & second == []
+ found = true
+ end
+ found
+ end
+
+ def tierce?
+ help(3)
+ end
+
+ def quarte?
+ help(4)
+ end
+
+ def quint?
+ help(5)
+ end
+
+ def tierce_help
+ deck = Array.new
+ rank = [7, 8, 9, :jack, :queen, :king, 10, :ace]
+ suit = [:spades, :hearts, :diamonds, :clubs]
+ suit.each do |suit|
+ rank.each do |rank|
+ deck << Card.new(rank, suit)
+ end
+ end
+ deck
+ end
+
+ def carre?(rank)
+ spade = @hand.include?(Card.new(rank, :spades))
+ heart = @hand.include?(Card.new(rank, :hearts))
+ diamond = @hand.include?(Card.new(rank, :diamonds))
+ club = @hand.include?(Card.new(rank, :clubs))
+ spade and heart and diamond and club
+ end
+
+ def carre_of_jacks?
+ carre?(:jack)
+ end
+
+ def carre_of_nines?
+ carre?(9)
+ end
+
+ def carre_of_aces?
+ carre?(:ace)
+ end
+end

Александрина обнови решението на 11.11.2015 16:39 (преди над 8 години)

class Card
def initialize(rank, suit)
@rank = rank
@suit = suit
end
def rank
@rank
end
def suit
@suit
end
def to_s
"#{rank.to_s.capitalize} of #{suit.capitalize}"
end
def ==(another_card)
@rank == another_card.rank && @suit == another_card.suit
end
end
class Deck
include Enumerable
def standard_deck
deck = Array.new
ranks = %w{2 3 4 5 6 7 8 9 10 :jack :queen :king :ace}
suits = %w{:spades :heart :diamonds :clubs}
suits.each do |suit|
ranks.each do |rank|
deck << Card.new(rank, suit)
end
end
deck
end
def initialize(array_of_cards = standard_deck)
@deck = array_of_cards
end
def each
@deck.each { |card| yield card}
end
def size
@deck.length
end
def draw_top_card
@deck.slice!(0)
end
def draw_bottom_card
@deck.pop
end
def top_card
@deck.first
end
def bottom_card
@deck.last
end
def shuffle
@deck.shuffle!
end
def to_s
@deck.each{|card| card.to_s + "\n"}
end
def deal
@deck.slice!(0,26)
end
end
class WarDeck < Deck
def standard_deck
deck = Array.new
ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
suits = [:spades, :heart, :diamonds, :clubs]
suits.each do |suit|
ranks.each do |rank|
deck << Card.new(rank, suit)
end
end
deck
end
def initialize(array_of_cards = standard_deck)
@deck = array_of_cards
end
def deal
array = @deck.slice!(0,26)
Hand.new(array)
end
def sort
rank = [:ace, :king, :queen, :jack, 10, 9, 8, 7, 6, 5, 4, 3, 2]
suit = [:spades, :hearts, :queen, :jack]
@deck.sort_by!{|a| [rank.index(a.rank), suit.index(a.suit)].reverse}
end
end
class BeloteDeck < Deck
def standard_deck
deck = Array.new
ranks = [7, 8, 9, 10, :jack, :queen, :king, :ace]
suits = [:spades, :hearts, :diamonds, :clubs]
suits.each do |suit|
ranks.each do |rank|
deck << Card.new(rank, suit)
end
end
deck
end
def initialize(array_of_cards = standard_deck)
@deck = array_of_cards
end
def deal
array = @deck.slice!(0,8)
BeloteHand.new(array)
end
def sort
rank = [:ace, 10, :king, :queen, :jack, 9, 8, 7]
suit = [:spades, :hearts, :diamonds, :clubs]
@deck.sort_by!{|a| [rank.index(a.rank), suit.index(a.suit)].reverse}
end
end
class SixtySixDeck < Deck
def standard_deck
deck = Array.new
ranks = [9, 10, :jack, :queen, :king, :ace]
suits = [:spades, :hearts, :diamonds, :clubs]
suits.each do |suit|
ranks.each do |rank|
deck << Card.new(rank, suit)
end
end
deck
end
def initialize(array_of_cards = standard_deck)
@deck = array_of_cards
end
def deal
cards = @deck.slice!(0,6)
Hand.new(cards)
end
def sort
rank = [:ace, 10, :king, :queen, :jack, 9]
suit = [:spades, :hearts, :diamonds, :clubs]
@deck.sort_by!{|a| [rank.index(a.rank), suit.index(a.suit)].reverse}
end
end
class Hand < Deck
def initialize(array)
@hand = array
end
def play_card
@hand.pop
end
def allow_face_up?
@hand.size <= 3
end
def twenty?(trump_suit)
suits = [:spades, :hearts, :diamonds, :clubs]
suits.delete(trump_suit)
twenty_help(suits)
end
def twenty_help(array_of_suits)
count, found = 0, false
- while count < number
- king = @hand.include?(Card.new(:king, suit[count]))
- queen = @hand.include?(Card.new(:queen, suit[count]))
+ while count < 3
+ king = @hand.include?(Card.new(:king, array_of_suits[count]))
+ queen = @hand.include?(Card.new(:queen, array_of_suits[count]))
if king and queen
found = true
end
count += 1
end
found
end
def size
@hand.length
end
def forty?(trump_suit)
king = @hand.include?(Card.new(:king, trump_suit))
queen = @hand.include?(Card.new(:queen, trump_suit))
king and queen
end
end
class BeloteHand < BeloteDeck
def initialize(array)
@hand = array
end
def highest_of_suit(suit)
deck = Array.new
ranks = [:ace, 10, :king, :queen, :jack, 9, 8, 7]
@hand.sort_by! {|card| ranks.index(card.rank)}
@hand.each do |card|
if card.suit == suit
deck << card
end
end
deck.first
end
def belote?
suits = [:spades, :hearts, :diamonds, :clubs]
belote_help(suits)
end
def belote_help(array_of_suits)
count, found = 0, false
- while count < number
- king = @hand.include?(Card.new(:king, suit[count]))
- queen = @hand.include?(Card.new(:queen, suit[count]))
+ while count < 4
+ king = @hand.include?(Card.new(:king, array_of_suits[count]))
+ queen = @hand.include?(Card.new(:queen, array_of_suits[count]))
if king and queen
found = true
end
count += 1
end
found
end
def help(number)
deck = tierce_help
found, first, second = false, [], []
deck.each_cons(number) do |cards| first << cards
end
@hand.each_cons(number) do |other_cards| second << other_cards
end
- if first & second == []
+ if first & second != []
found = true
end
found
end
def tierce?
help(3)
end
def quarte?
help(4)
end
def quint?
help(5)
end
def tierce_help
deck = Array.new
rank = [7, 8, 9, :jack, :queen, :king, 10, :ace]
suit = [:spades, :hearts, :diamonds, :clubs]
suit.each do |suit|
rank.each do |rank|
deck << Card.new(rank, suit)
end
end
deck
end
def carre?(rank)
spade = @hand.include?(Card.new(rank, :spades))
heart = @hand.include?(Card.new(rank, :hearts))
diamond = @hand.include?(Card.new(rank, :diamonds))
club = @hand.include?(Card.new(rank, :clubs))
spade and heart and diamond and club
end
def carre_of_jacks?
carre?(:jack)
end
def carre_of_nines?
carre?(9)
end
def carre_of_aces?
carre?(:ace)
end
end

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

  • %w{:spades :heart :diamonds :clubs} == [":spades", ":heart", ":diamonds", ":clubs"], което пък е различно от [:spades, :heart, :diamonds, :clubs]
  • методите help и before_help са лошо именувани
  • array е лошо име на променлива
  • праен списък се създава с [], а не с Array.new

и т.н.

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