Решение на Четвърта задача от София Петрова

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

Към профила на София Петрова

Резултати

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

Код

class Card
attr_reader :rank, :suit
def initialize(rank, suit)
@rank = rank
@suit = suit
end
def to_s
"#{ @rank.to_s.capitalize } of #{ @suit.capitalize }"
end
def ==(another_card)
self.class == another_card.class && state == another_card.state
end
protected
def state
[@rank, @suit]
end
end
class Deck
include Enumerable
attr_accessor :deck
SUITS = [:clubs, :diamonds, :hearts, :spades]
RANKS = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
def initialize(deck = nil)
@deck = if deck
deck
else
RANKS.product(SUITS).collect {|rank, suit| Card.new(rank, suit)}
end
end
def each(&block)
@deck.each(&block)
end
def size
@deck.size
end
def draw_top_card
@deck.shift
end
def draw_bottom_card
@deck.pop
end
def top_card
@deck[0]
end
def bottom_card
@deck[-1]
end
def shuffle
@deck = deck.shuffle
end
def sort
@deck = @deck.sort do |first_card, second_card|
if first_card.suit == second_card.suit
rank_index(first_card.rank) <=> rank_index(second_card.rank)
else
suit_index(first_card.suit) <=> suit_index(second_card.suit)
end
end
end
def to_s
printed_deck = @deck.each { |card| puts card }
end
private
def suit_index(suit)
Deck::SUITS.reverse.index(suit)
end
def rank_index(rank)
Deck::RANKS.reverse.index(rank)
end
end
class WarDeck < Deck
def deal
draw = Hand.new
26.times { draw.hand << @deck.pop }
draw
end
class Hand
attr_accessor :hand
def initialize(hand = [])
@hand = hand
end
def size
@hand.size
end
def allow_face_up?
@hand.size <= 3
end
def play_card
@hand.shift
end
end
end
class BeloteDeck < Deck
RANKS = [7, 8, 9, :jack, :queen, :king, 10, :ace]
SUITS = [:clubs, :diamonds, :hearts, :spades]
def initialize(deck = nil)
@deck = if deck
deck
else
RANKS.product(SUITS).collect {|rank, suit| Card.new(rank, suit)}
end
end
def deal
draw = Hand.new
8.times { draw.hand << @deck.pop }
draw
end
def size
@hand.size
end
def sort
@deck = @deck.sort do |first_card, second_card|
if first_card.suit == second_card.suit
rank_index(first_card.rank) <=> rank_index(second_card.rank)
else
suit_index(first_card.suit) <=> suit_index(second_card.suit)
end
end
end
class Hand
attr_accessor :hand
def initialize(hand = [])
@hand = hand
end
def size
@hand.size
end
def highest_of_suit(suit)
suits_allowed = @hand.find_all { |card| card.suit == suit }
suits_allowed = suits_allowed.sort do |first_card, second_card|
rank_index(first_card.rank) <=> rank_index(second_card.rank)
end
suits_allowed.first
end
def belote?
kings = @hand.find_all { |card| card.rank == :king }
kings_suits = kings.map { |card| card.suit }
queens = @hand.find_all { |card| card.rank == :queen }
queens_suits = queens.map { |card| card.suit }
kings_suits - queens_suits != kings_suits
end
def tierce?
sorted_hand = @hand.sort do |first_card, second_card|
rank_index(first_card.rank) <=> rank_index(second_card.rank)
end
sorted_hand.each_cons(3).any? do |first_card, second_card|
((rank_index(second_card.rank) == rank_index(first_card.rank) + 1) &&
first_card.suit == second_card.suit)
end
end
def quarte?
sorted_hand = @hand.sort do |first_card, second_card|
rank_index(first_card.rank) <=> rank_index(second_card.rank)
end
sorted_hand.each_cons(4).any? do |first_card, second_card|
((rank_index(second_card.rank) == rank_index(first_card.rank) + 1) &&
first_card.suit == second_card.suit)
end
end
def quint?
sorted_hand = @hand.sort do |first_card, second_card|
rank_index(first_card.rank) <=> rank_index(second_card.rank)
end
sorted_hand.each_cons(5).any? do |first_card, second_card|
((rank_index(second_card.rank) == rank_index(first_card.rank) + 1) &&
first_card.suit == second_card.suit)
end
end
def carre_of_jacks?
jacks = @hand.find_all { |card| card.rank == :jack }
jacks.length == 4
end
def carre_of_nines?
nines = @hand.find_all { |card| card.rank == 9 }
nines.length == 4
end
def carre_of_aces?
aces = @hand.find_all { |card| card.rank == :ace }
aces.length == 4
end
private
def rank_index(rank)
BeloteDeck::RANKS.reverse.index(rank)
end
end
private
def suit_index(suit)
BeloteDeck::SUITS.reverse.index(suit)
end
def rank_index(rank)
BeloteDeck::RANKS.reverse.index(rank)
end
end
class SixtySixDeck < Deck
RANKS = [9, :jack, :queen, :king, 10, :ace]
SUITS = [:clubs, :diamonds, :hearts, :spades]
def initialize(deck = nil)
@deck = if deck
deck
else
RANKS.product(SUITS).collect {|rank, suit| Card.new(rank, suit)}
end
end
def deal
draw = Hand.new
6.times { draw.hand << @deck.pop }
draw
end
def size
@hand.size
end
def sort
@deck = @deck.sort do |first_card, second_card|
if first_card.suit == second_card.suit
rank_index(first_card.rank) <=> rank_index(second_card.rank)
else
suit_index(first_card.suit) <=> suit_index(second_card.suit)
end
end
end
class Hand
attr_accessor :hand
def initialize(hand = [])
@hand = hand
end
def size
@hand.size
end
def twenty?(trump_suit)
@hand.any? {|card| card.rank == :king && card.suit != trump_suit } &&
@hand.any? {|card| card.rank == :queen && card.suit != trump_suit }
end
def forty?(trump_suit)
@hand.any? {|card| card.rank == :king && card.suit == trump_suit } &&
@hand.any? {|card| card.rank == :queen && card.suit == trump_suit }
end
end
private
def suit_index(suit)
SixtySixDeck::SUITS.reverse.index(suit)
end
def rank_index(rank)
SixtySixDeck::RANKS.reverse.index(rank)
end
end

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

...........Ace of Spades
9 of Clubs
F......F....FAce of Spades
9 of Clubs
F.........F.F........F....FAce of Spades
9 of Clubs
F....F

Failures:

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

  2) BeloteDeck behaves like a deck #size returns the size of the deck
     Failure/Error: expect(small_deck.size).to eq 2
     NoMethodError:
       undefined method `size' for nil:NilClass
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-55it6j/spec.rb:191
     # /tmp/d20151112-27349-55it6j/solution.rb:142:in `size'
     # /tmp/d20151112-27349-55it6j/spec.rb:23: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) BeloteDeck behaves like a deck #shuffle does not remove cards from the deck
     Failure/Error: initial_size = deck.size
     NoMethodError:
       undefined method `size' for nil:NilClass
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-55it6j/spec.rb:191
     # /tmp/d20151112-27349-55it6j/solution.rb:142:in `size'
     # /tmp/d20151112-27349-55it6j/spec.rb:59: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:0x007ff814f4f380>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-55it6j/spec.rb:191
     # /tmp/d20151112-27349-55it6j/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 #quarte? does not return true if there is no quarte
     Failure/Error: expect(hand.quarte?).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-55it6j/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)>'

  6) BeloteDeck hand #quint? does not return true if there is no quint
     Failure/Error: expect(hand.quint?).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-55it6j/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)>'

  7) SixtySixDeck behaves like a deck #size returns the size of the deck
     Failure/Error: expect(small_deck.size).to eq 2
     NoMethodError:
       undefined method `size' for nil:NilClass
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-55it6j/spec.rb:400
     # /tmp/d20151112-27349-55it6j/solution.rb:273:in `size'
     # /tmp/d20151112-27349-55it6j/spec.rb:23: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)>'

  8) SixtySixDeck behaves like a deck #shuffle does not remove cards from the deck
     Failure/Error: initial_size = deck.size
     NoMethodError:
       undefined method `size' for nil:NilClass
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-55it6j/spec.rb:400
     # /tmp/d20151112-27349-55it6j/solution.rb:273:in `size'
     # /tmp/d20151112-27349-55it6j/spec.rb:59: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)>'

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

  10) 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-55it6j/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.04059 seconds
57 examples, 10 failures

Failed examples:

rspec /tmp/d20151112-27349-55it6j/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-55it6j/spec.rb:22 # BeloteDeck behaves like a deck #size returns the size of the deck
rspec /tmp/d20151112-27349-55it6j/spec.rb:56 # BeloteDeck behaves like a deck #shuffle does not remove cards from the deck
rspec /tmp/d20151112-27349-55it6j/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-55it6j/spec.rb:337 # BeloteDeck hand #quarte? does not return true if there is no quarte
rspec /tmp/d20151112-27349-55it6j/spec.rb:369 # BeloteDeck hand #quint? does not return true if there is no quint
rspec /tmp/d20151112-27349-55it6j/spec.rb:22 # SixtySixDeck behaves like a deck #size returns the size of the deck
rspec /tmp/d20151112-27349-55it6j/spec.rb:56 # SixtySixDeck behaves like a deck #shuffle does not remove cards from the deck
rspec /tmp/d20151112-27349-55it6j/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-55it6j/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:37 (преди около 9 години)

+class Card
+ attr_reader :rank, :suit
+
+ def initialize(rank, suit)
+ @rank = rank
+ @suit = suit
+ end
+
+ def to_s
+ "#{@rank.to_s.capitalize} of #{@suit.capitalize}"
+ end
+
+ def ==(another_card)
+ self.class == another_card.class && state == another_card.state
+ end
+
+ protected
+ def state
+ [@rank, @suit]
+ end
+end
+
+class Deck
+SUITS = [:clubs, :diamonds, :hearts, :spades]
+RANKS = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
+
+ include Enumerable
+
+ attr_accessor :deck
+
+ def initialize(deck = nil)
+ @deck = if deck
+ deck
+ else
+ RANKS.product(SUITS).collect {|rank, suit| Card.new(rank, suit)}
+ end
+ end
+
+ def each(&block)
+ @deck.each(&block)
+ end
+
+ def size
+ @deck.size
+ end
+
+ def draw_top_card
+ @deck.shift
+ end
+
+ def draw_bottom_card
+ @deck.pop
+ end
+
+ def top_card
+ @deck[0]
+ end
+
+ def bottom_card
+ @deck[-1]
+ end
+
+ def shuffle
+ @deck = deck.shuffle
+ end
+
+ def sort
+ @deck = @deck.sort do |first_card, second_card|
+ if first_card.suit == second_card.suit
+ rank_index(first_card.rank) <=> rank_index(second_card.rank)
+ else
+ suit_index(first_card.suit) <=> suit_index(second_card.suit)
+ end
+ end
+ end
+
+ def to_s
+ printed_deck = @deck.each {|card| puts card}
+ end
+
+ private
+
+ def suit_index(suit)
+ Deck::SUITS.reverse.index(suit)
+ end
+
+ def rank_index(rank)
+ Deck::RANKS.reverse.index(rank)
+ end
+end
+
+class WarDeck < Deck
+
+ def deal
+ draw = Hand.new
+ 26.times { draw.hand << @deck.pop }
+ draw
+ end
+
+ class Hand
+ attr_accessor :hand
+
+ def initialize(hand = [])
+ @hand = hand
+ end
+
+ def size
+ @hand.size
+ end
+
+ def allow_face_up?
+ @hand.size <= 3
+ end
+
+ def play_card
+ @hand.shift
+ end
+ end
+
+end
+
+class BeloteDeck < Deck
+ RANKS = [7, 8, 9, :jack, :queen, :king, 10, :ace]
+ SUITS = [:clubs, :diamonds, :hearts, :spades]
+
+ def initialize(deck = nil)
+ @deck = if deck
+ deck
+ else
+ RANKS.product(SUITS).collect {|rank, suit| Card.new(rank, suit)}
+ end
+ end
+
+ def deal
+ draw = Hand.new
+ 8.times { draw.hand << @deck.pop }
+ draw
+ end
+
+ def size
+ @hand.size
+ end
+
+ def sort
+ @deck = @deck.sort do |first_card, second_card|
+ if first_card.suit == second_card.suit
+ rank_index(first_card.rank) <=> rank_index(second_card.rank)
+ else
+ suit_index(first_card.suit) <=> suit_index(second_card.suit)
+ end
+ end
+ end
+
+ class Hand
+ attr_accessor :hand
+ def initialize(hand = [])
+ @hand = hand
+ end
+
+ def size
+ @hand.size
+ end
+
+ def highest_of_suit(suit)
+ suits_allowed = @hand.find_all {|card| card.suit == suit}
+ suits_allowed = suits_allowed.sort do |first_card, second_card|
+ rank_index(first_card.rank) <=> rank_index(second_card.rank)
+ end
+ suits_allowed.first
+ end
+
+ def belote?
+ kings = @hand.find_all {|card| card.rank == :king }
+ kings_suits = kings.map { |card| card.suit }
+
+ queens = @hand.find_all { |card| card.rank == :queen }
+ queens_suits = queens.map { |card| card.suit }
+
+ kings_suits - queens_suits != kings_suits
+ end
+
+ def tierce?
+ sorted_hand = @hand.sort do |first_card, second_card|
+ rank_index(first_card.rank) <=> rank_index(second_card.rank)
+ end
+ sorted_hand.each_cons(3).any? do |first_card, second_card|
+ ((rank_index(second_card.rank) == rank_index(first_card.rank) + 1) &&
+ first_card.suit == second_card.suit)
+ end
+ end
+
+ def quarte?
+ sorted_hand = @hand.sort do |first_card, second_card|
+ rank_index(first_card.rank) <=> rank_index(second_card.rank)
+ end
+ sorted_hand.each_cons(4).any? do |first_card, second_card|
+ ((rank_index(second_card.rank) == rank_index(first_card.rank) + 1) &&
+ first_card.suit == second_card.suit) end
+ end
+
+ def quint?
+ sorted_hand = @hand.sort do |first_card, second_card|
+ rank_index(first_card.rank) <=> rank_index(second_card.rank)
+ end
+ sorted_hand.each_cons(5).any? do |first_card, second_card|
+ ((rank_index(second_card.rank) == rank_index(first_card.rank) + 1) &&
+ first_card.suit == second_card.suit)
+ end
+ end
+
+ def carre_of_jacks?
+ jacks = @hand.find_all {|card| card.rank == :jack}
+ jacks.length == 4
+ end
+
+ def carre_of_nines?
+ nines = @hand.find_all {|card| card.rank == 9}
+ nines.length == 4
+ end
+
+ def carre_of_aces?
+ aces = @hand.find_all {|card| card.rank == :ace}
+ aces.length == 4
+ end
+
+ private
+
+ def rank_index(rank)
+ BeloteDeck::RANKS.reverse.index(rank)
+ end
+ end
+
+ private
+
+ def suit_index(suit)
+ BeloteDeck::SUITS.reverse.index(suit)
+ end
+
+ def rank_index(rank)
+ BeloteDeck::RANKS.reverse.index(rank)
+ end
+end
+
+
+class SixtySixDeck < Deck
+RANKS = [9, :jack, :queen, :king, 10, :ace]
+SUITS = [:clubs, :diamonds, :hearts, :spades]
+
+ def initialize(deck = nil)
+ @deck = if deck
+ deck
+ else
+ RANKS.product(SUITS).collect {|rank, suit| Card.new(rank, suit)}
+ end
+ end
+
+ def deal
+ draw = Hand.new
+ 6.times do draw.hand << @deck.pop end
+ draw
+ end
+
+ def size
+ @hand.size
+ end
+
+ def sort
+ @deck = @deck.sort do |first_card, second_card|
+ if first_card.suit == second_card.suit
+ rank_index(first_card.rank) <=> rank_index(second_card.rank)
+ else
+ suit_index(first_card.suit) <=> suit_index(second_card.suit)
+ end
+ end
+ end
+
+ class Hand
+ attr_accessor :hand
+ def initialize(hand = [])
+ @hand = hand
+ end
+
+ def size
+ @hand.size
+ end
+
+ def twenty?(trump_suit)
+ @hand.any? {|card| card.rank == :king && card.suit != trump_suit } &&
+ @hand.any? {|card| card.rank == :queen && card.suit != trump_suit }
+ end
+
+ def forty?(trump_suit)
+ @hand.any? {|card| card.rank == :king && card.suit == trump_suit } &&
+ @hand.any? {|card| card.rank == :queen && card.suit == trump_suit }
+ end
+ end
+
+ private
+
+ def suit_index(suit)
+ SixtySixDeck::SUITS.reverse.index(suit)
+ end
+
+ def rank_index(rank)
+ SixtySixDeck::RANKS.reverse.index(rank)
+ end
+end

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

class Card
attr_reader :rank, :suit
def initialize(rank, suit)
@rank = rank
@suit = suit
end
def to_s
- "#{@rank.to_s.capitalize} of #{@suit.capitalize}"
+ "#{ @rank.to_s.capitalize } of #{ @suit.capitalize }"
end
def ==(another_card)
self.class == another_card.class && state == another_card.state
end
protected
+
def state
[@rank, @suit]
end
end
class Deck
-SUITS = [:clubs, :diamonds, :hearts, :spades]
-RANKS = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
-
include Enumerable
-
attr_accessor :deck
+SUITS = [:clubs, :diamonds, :hearts, :spades]
+RANKS = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
+
def initialize(deck = nil)
@deck = if deck
deck
else
RANKS.product(SUITS).collect {|rank, suit| Card.new(rank, suit)}
end
end
def each(&block)
@deck.each(&block)
end
def size
@deck.size
end
def draw_top_card
@deck.shift
end
def draw_bottom_card
@deck.pop
end
def top_card
@deck[0]
end
def bottom_card
@deck[-1]
end
def shuffle
@deck = deck.shuffle
end
def sort
@deck = @deck.sort do |first_card, second_card|
if first_card.suit == second_card.suit
rank_index(first_card.rank) <=> rank_index(second_card.rank)
else
suit_index(first_card.suit) <=> suit_index(second_card.suit)
end
end
end
def to_s
- printed_deck = @deck.each {|card| puts card}
+ printed_deck = @deck.each { |card| puts card }
end
private
def suit_index(suit)
Deck::SUITS.reverse.index(suit)
end
def rank_index(rank)
Deck::RANKS.reverse.index(rank)
end
end
class WarDeck < Deck
def deal
draw = Hand.new
26.times { draw.hand << @deck.pop }
draw
end
class Hand
attr_accessor :hand
def initialize(hand = [])
@hand = hand
end
def size
@hand.size
end
def allow_face_up?
@hand.size <= 3
end
def play_card
@hand.shift
end
end
end
class BeloteDeck < Deck
+
RANKS = [7, 8, 9, :jack, :queen, :king, 10, :ace]
SUITS = [:clubs, :diamonds, :hearts, :spades]
def initialize(deck = nil)
@deck = if deck
deck
else
RANKS.product(SUITS).collect {|rank, suit| Card.new(rank, suit)}
end
end
def deal
draw = Hand.new
8.times { draw.hand << @deck.pop }
draw
end
def size
@hand.size
end
def sort
@deck = @deck.sort do |first_card, second_card|
if first_card.suit == second_card.suit
rank_index(first_card.rank) <=> rank_index(second_card.rank)
else
suit_index(first_card.suit) <=> suit_index(second_card.suit)
end
end
end
class Hand
attr_accessor :hand
+
def initialize(hand = [])
@hand = hand
end
def size
@hand.size
end
def highest_of_suit(suit)
suits_allowed = @hand.find_all {|card| card.suit == suit}
+
suits_allowed = suits_allowed.sort do |first_card, second_card|
rank_index(first_card.rank) <=> rank_index(second_card.rank)
end
+
suits_allowed.first
end
def belote?
- kings = @hand.find_all {|card| card.rank == :king }
+ kings = @hand.find_all { |card| card.rank == :king }
kings_suits = kings.map { |card| card.suit }
queens = @hand.find_all { |card| card.rank == :queen }
queens_suits = queens.map { |card| card.suit }
kings_suits - queens_suits != kings_suits
end
def tierce?
sorted_hand = @hand.sort do |first_card, second_card|
rank_index(first_card.rank) <=> rank_index(second_card.rank)
end
+
sorted_hand.each_cons(3).any? do |first_card, second_card|
((rank_index(second_card.rank) == rank_index(first_card.rank) + 1) &&
first_card.suit == second_card.suit)
end
end
def quarte?
sorted_hand = @hand.sort do |first_card, second_card|
rank_index(first_card.rank) <=> rank_index(second_card.rank)
end
+
sorted_hand.each_cons(4).any? do |first_card, second_card|
((rank_index(second_card.rank) == rank_index(first_card.rank) + 1) &&
first_card.suit == second_card.suit) end
end
def quint?
sorted_hand = @hand.sort do |first_card, second_card|
rank_index(first_card.rank) <=> rank_index(second_card.rank)
end
+
sorted_hand.each_cons(5).any? do |first_card, second_card|
((rank_index(second_card.rank) == rank_index(first_card.rank) + 1) &&
first_card.suit == second_card.suit)
end
end
def carre_of_jacks?
jacks = @hand.find_all {|card| card.rank == :jack}
jacks.length == 4
end
def carre_of_nines?
nines = @hand.find_all {|card| card.rank == 9}
nines.length == 4
end
def carre_of_aces?
aces = @hand.find_all {|card| card.rank == :ace}
aces.length == 4
end
private
def rank_index(rank)
BeloteDeck::RANKS.reverse.index(rank)
end
end
private
def suit_index(suit)
BeloteDeck::SUITS.reverse.index(suit)
end
def rank_index(rank)
BeloteDeck::RANKS.reverse.index(rank)
end
end
class SixtySixDeck < Deck
+
RANKS = [9, :jack, :queen, :king, 10, :ace]
SUITS = [:clubs, :diamonds, :hearts, :spades]
def initialize(deck = nil)
@deck = if deck
deck
else
RANKS.product(SUITS).collect {|rank, suit| Card.new(rank, suit)}
end
end
def deal
draw = Hand.new
6.times do draw.hand << @deck.pop end
draw
end
def size
@hand.size
end
def sort
@deck = @deck.sort do |first_card, second_card|
if first_card.suit == second_card.suit
rank_index(first_card.rank) <=> rank_index(second_card.rank)
else
suit_index(first_card.suit) <=> suit_index(second_card.suit)
end
end
end
class Hand
attr_accessor :hand
+
def initialize(hand = [])
@hand = hand
end
def size
@hand.size
end
def twenty?(trump_suit)
@hand.any? {|card| card.rank == :king && card.suit != trump_suit } &&
@hand.any? {|card| card.rank == :queen && card.suit != trump_suit }
end
def forty?(trump_suit)
@hand.any? {|card| card.rank == :king && card.suit == trump_suit } &&
@hand.any? {|card| card.rank == :queen && card.suit == trump_suit }
end
end
private
def suit_index(suit)
SixtySixDeck::SUITS.reverse.index(suit)
end
def rank_index(rank)
SixtySixDeck::RANKS.reverse.index(rank)
end
end

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

class Card
attr_reader :rank, :suit
def initialize(rank, suit)
@rank = rank
@suit = suit
end
def to_s
"#{ @rank.to_s.capitalize } of #{ @suit.capitalize }"
end
def ==(another_card)
self.class == another_card.class && state == another_card.state
end
protected
def state
[@rank, @suit]
end
end
class Deck
include Enumerable
attr_accessor :deck
SUITS = [:clubs, :diamonds, :hearts, :spades]
RANKS = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
def initialize(deck = nil)
@deck = if deck
deck
else
RANKS.product(SUITS).collect {|rank, suit| Card.new(rank, suit)}
end
end
def each(&block)
@deck.each(&block)
end
def size
@deck.size
end
def draw_top_card
@deck.shift
end
def draw_bottom_card
@deck.pop
end
def top_card
@deck[0]
end
def bottom_card
@deck[-1]
end
def shuffle
@deck = deck.shuffle
end
def sort
@deck = @deck.sort do |first_card, second_card|
if first_card.suit == second_card.suit
rank_index(first_card.rank) <=> rank_index(second_card.rank)
else
suit_index(first_card.suit) <=> suit_index(second_card.suit)
end
end
end
def to_s
printed_deck = @deck.each { |card| puts card }
end
private
def suit_index(suit)
Deck::SUITS.reverse.index(suit)
end
def rank_index(rank)
Deck::RANKS.reverse.index(rank)
end
end
class WarDeck < Deck
def deal
draw = Hand.new
26.times { draw.hand << @deck.pop }
draw
end
class Hand
attr_accessor :hand
def initialize(hand = [])
@hand = hand
end
def size
@hand.size
end
def allow_face_up?
@hand.size <= 3
end
def play_card
@hand.shift
end
end
end
class BeloteDeck < Deck
RANKS = [7, 8, 9, :jack, :queen, :king, 10, :ace]
SUITS = [:clubs, :diamonds, :hearts, :spades]
def initialize(deck = nil)
@deck = if deck
deck
else
RANKS.product(SUITS).collect {|rank, suit| Card.new(rank, suit)}
end
end
def deal
draw = Hand.new
8.times { draw.hand << @deck.pop }
draw
end
def size
@hand.size
end
def sort
@deck = @deck.sort do |first_card, second_card|
if first_card.suit == second_card.suit
rank_index(first_card.rank) <=> rank_index(second_card.rank)
else
suit_index(first_card.suit) <=> suit_index(second_card.suit)
end
end
end
class Hand
attr_accessor :hand
def initialize(hand = [])
@hand = hand
end
def size
@hand.size
end
def highest_of_suit(suit)
- suits_allowed = @hand.find_all {|card| card.suit == suit}
+ suits_allowed = @hand.find_all { |card| card.suit == suit }
suits_allowed = suits_allowed.sort do |first_card, second_card|
rank_index(first_card.rank) <=> rank_index(second_card.rank)
end
suits_allowed.first
end
def belote?
kings = @hand.find_all { |card| card.rank == :king }
kings_suits = kings.map { |card| card.suit }
queens = @hand.find_all { |card| card.rank == :queen }
queens_suits = queens.map { |card| card.suit }
kings_suits - queens_suits != kings_suits
end
def tierce?
sorted_hand = @hand.sort do |first_card, second_card|
rank_index(first_card.rank) <=> rank_index(second_card.rank)
end
sorted_hand.each_cons(3).any? do |first_card, second_card|
((rank_index(second_card.rank) == rank_index(first_card.rank) + 1) &&
first_card.suit == second_card.suit)
end
end
def quarte?
sorted_hand = @hand.sort do |first_card, second_card|
rank_index(first_card.rank) <=> rank_index(second_card.rank)
end
sorted_hand.each_cons(4).any? do |first_card, second_card|
((rank_index(second_card.rank) == rank_index(first_card.rank) + 1) &&
- first_card.suit == second_card.suit) end
+ first_card.suit == second_card.suit)
+ end
end
def quint?
sorted_hand = @hand.sort do |first_card, second_card|
rank_index(first_card.rank) <=> rank_index(second_card.rank)
end
sorted_hand.each_cons(5).any? do |first_card, second_card|
((rank_index(second_card.rank) == rank_index(first_card.rank) + 1) &&
first_card.suit == second_card.suit)
end
end
def carre_of_jacks?
- jacks = @hand.find_all {|card| card.rank == :jack}
+ jacks = @hand.find_all { |card| card.rank == :jack }
jacks.length == 4
end
def carre_of_nines?
- nines = @hand.find_all {|card| card.rank == 9}
+ nines = @hand.find_all { |card| card.rank == 9 }
nines.length == 4
end
def carre_of_aces?
- aces = @hand.find_all {|card| card.rank == :ace}
+ aces = @hand.find_all { |card| card.rank == :ace }
aces.length == 4
end
private
def rank_index(rank)
BeloteDeck::RANKS.reverse.index(rank)
end
end
private
def suit_index(suit)
BeloteDeck::SUITS.reverse.index(suit)
end
def rank_index(rank)
BeloteDeck::RANKS.reverse.index(rank)
end
end
class SixtySixDeck < Deck
RANKS = [9, :jack, :queen, :king, 10, :ace]
SUITS = [:clubs, :diamonds, :hearts, :spades]
def initialize(deck = nil)
@deck = if deck
deck
else
RANKS.product(SUITS).collect {|rank, suit| Card.new(rank, suit)}
end
end
def deal
draw = Hand.new
- 6.times do draw.hand << @deck.pop end
+ 6.times { draw.hand << @deck.pop }
draw
end
def size
@hand.size
end
def sort
@deck = @deck.sort do |first_card, second_card|
if first_card.suit == second_card.suit
rank_index(first_card.rank) <=> rank_index(second_card.rank)
else
suit_index(first_card.suit) <=> suit_index(second_card.suit)
end
end
end
class Hand
attr_accessor :hand
def initialize(hand = [])
@hand = hand
end
def size
@hand.size
end
def twenty?(trump_suit)
@hand.any? {|card| card.rank == :king && card.suit != trump_suit } &&
@hand.any? {|card| card.rank == :queen && card.suit != trump_suit }
end
def forty?(trump_suit)
@hand.any? {|card| card.rank == :king && card.suit == trump_suit } &&
@hand.any? {|card| card.rank == :queen && card.suit == trump_suit }
end
end
private
def suit_index(suit)
SixtySixDeck::SUITS.reverse.index(suit)
end
def rank_index(rank)
SixtySixDeck::RANKS.reverse.index(rank)
end
end

София, имаш проблеми с идентацията и whitespace-а на места. Бъди по-прецизна занапред.

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