Решение на Четвърта задача от Николай Станев

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

Към профила на Николай Станев

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 34 успешни тест(а)
  • 23 неуспешни тест(а)

Код

class Card
def initialize(rank, suit)
@rank = rank
@suit = suit
end
def rank
@rank
end
def suit
@suit
end
def to_s
string = "#{@rank.to_s.capitalize} of #{@suit.to_s.capitalize}"
string
end
def ==(card)
if @rank == card.rank and @suit == card.suit
true
else
false
end
end
end
class Ranks
WAR_RANKS = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace].reverse
BELOTE_RANKS = [7, 8, 9, :jack, :queen, :king, 10, :ace].reverse
SIXTY_SIX_RANKS = [9, :jack, :queen, :king, 10, :ace].reverse
SUITS = [:spades, :hearts, :diamonds, :clubs]
def self.rank(deck_type, card)
if deck_type.is_a? WarDeck
WAR_RANKS.index(card.rank)
elsif deck_type.is_a? BeloteDeck
BELOTE_RANKS.index(card.rank)
elsif deck_type.is_a? SixtySixDeck
SIXTY_SIX_RANKS.index(card.rank)
else
WAR_RANKS.index(card.rank)
end
end
def self.suit(card)
SUITS.index(card.suit)
end
end
class DeckFactory
ALL_RANKS = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
SUITS = [:spades, :hearts, :diamonds, :clubs]
@ranks = Array.new
def self.choose_game(deck_type)
if deck_type.is_a? WarDeck
@ranks = ALL_RANKS
elsif deck_type.is_a? BeloteDeck
@ranks = ALL_RANKS - [2, 3, 4, 5, 6]
elsif deck_type.is_a? SixtySixDeck
@ranks = ALL_RANKS - [2, 3, 4, 5, 6, 7, 8]
else
@ranks = ALL_RANKS
end
end
def self.create_deck(deck_type)
self.choose_game(deck_type)
deck = Array.new
@ranks.product(SUITS).each {|card| deck.push(Card.new(card[0], card[1]))}
deck
end
end
class Hand
def initialize(hand, cards)
@hand = hand.take(cards)
@size = @hand.size
end
def size
@size
end
end
class WarHand < Hand
def play_card
if size > 0
@hand.pop
end
end
def allow_face_up?
if size <= 3
true
else
false
end
end
end
class BeloteHand < Hand
BELOTE_RANKS = [7, 8, 9, 10, :jack, :queen, :king, :ace]
def highest_of_suit(suit)
array = Array.new
@hand.each do |card|
array.push(card) if card.suit == suit
end
array.sort_by!{|card| Ranks.rank(BeloteDeck, card)}
array.first
end
def belote?
array = [:spades, :hearts, :diamonds, :clubs]
array.each {|suits| return true if belote_of_suit?(suits)}
false
end
def belote_of_suit?(suit)
queen = @hand.any?{|card| card.rank == :queen and card.suit == suit}
king = @hand.any?{|card| card.rank == :king and card.suit == suit}
queen and king
end
def tierce?
sort_hand
@hand.reverse.each_cons(3) do |cards|
return true if straight_all_suits?(cards)
end
end
def quarte?
sort_hand
@hand.reverse.each_cons(4) do |cards|
return true if straight_all_suits?(cards)
end
false
end
def quint?
sort_hand
@hand.reverse.each_cons(5) do |cards|
return true if straight_all_suits?(cards)
end
false
end
def straight_all_suits?(cards)
return true if straight?(cards, :spades)
return true if straight?(cards, :hearts)
return true if straight?(cards, :diamonds)
return true if straight?(cards, :clubs)
false
end
def straight?(cards, suit)
ranks = Array.new
cards.each do |card|
return false if card.suit != suit
ranks.push(card.rank)
end
return true if (BELOTE_RANKS & ranks) == ranks
false
end
def carre_of_jacks?
carre?(:jack)
end
def carre_of_nines?
carre?(9)
end
def carre_of_aces?
carre?(:ace)
end
def sort_hand
@hand.sort_by!{|x| [Ranks.suit(x), Ranks.rank(BeloteDeck.new, x)]}
end
def carre?(rank)
count = 0
@hand.each{|card| count += 1 if card.rank == rank}
return true if count == 4
false
end
end
class SixtySixHand < Hand
def twenty?(trump_suit)
return false if forty?(trump_suit)
array = [:spades, :hearts, :diamonds, :clubs]
array.delete(trump_suit)
array.each {|suits| return true if forty?(suits)}
false
end
def forty?(trump_suit)
queen = @hand.any?{|card| card.rank == :queen and card.suit == trump_suit}
king = @hand.any?{|card| card.rank == :king and card.suit == trump_suit}
queen and king
end
end
class Deck
include Enumerable
def initialize(*deck)
if deck.size == 0
@deck = DeckFactory.create_deck(self)
else
@deck = deck[0]
end
end
def each
@deck.each{ |x| yield x}
end
def size
@deck.size
end
def draw_top_card
@deck.shift
end
def draw_bottom_card
@deck.pop
end
def top_card
@deck.first
end
def bottom_card
@deck.last
end
def sort
@deck.sort_by!{|x| [Ranks.suit(x), Ranks.rank(self, x)]}
end
def shuffle
@deck.shuffle!
end
def to_s
@deck.each {|card| puts card.to_s}
end
def deal
new_hand(26)
end
def new_hand(size)
if self.size > size
hand = right_hand(@deck, size)
@deck = @deck.drop(size)
hand
end
end
def right_hand(deck, size)
if self.is_a? BeloteDeck
BeloteHand.new(deck, size)
elsif self.is_a? SixtySixDeck
SixtySixHand.new(deck, size)
else
WarHand.new(deck, size)
end
end
end
class WarDeck < Deck
def deal
new_hand(26)
end
end
class BeloteDeck < Deck
def deal
new_hand(8)
end
end
class SixtySixDeck < Deck
def initialize(*deck)
super(*deck)
@trump_suit = [:spades, :hearts, :diamonds, :clubs].sample
end
def deal
hand = new_hand(6)
end
end

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

...........Ace of Spades
9 of Clubs
F...F........Ace of Spades
9 of Clubs
F..FFFFFFFFFFFFFFFF........Ace of Spades
9 of Clubs
F..FFF

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:0x007f0f5b0d0018>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1o3ajcp/spec.rb:140
     # /tmp/d20151112-27349-1o3ajcp/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) WarDeck hand #allow_face_up? returns true if the cards are less than or equal to 3
     Failure/Error: expect(hand.allow_face_up?).to eq true
       
       expected: true
            got: false
       
       (compared using ==)
     # /tmp/d20151112-27349-1o3ajcp/spec.rb:178: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)>'

  3) 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:0x007f0f5b017c98>
     Shared Example Group: "a deck" called from /tmp/d20151112-27349-1o3ajcp/spec.rb:191
     # /tmp/d20151112-27349-1o3ajcp/spec.rb:68:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) 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 nil:NilClass
     # /tmp/d20151112-27349-1o3ajcp/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)>'

  5) 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 nil:NilClass
     # /tmp/d20151112-27349-1o3ajcp/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)>'

  6) 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 nil:NilClass
     # /tmp/d20151112-27349-1o3ajcp/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)>'

  7) BeloteDeck hand #tierce? with tierce returns true for cards with names
     Failure/Error: expect(hand.tierce?).to be true
     NoMethodError:
       undefined method `tierce?' for nil:NilClass
     # /tmp/d20151112-27349-1o3ajcp/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)>'

  8) BeloteDeck hand #tierce? with tierce returns true for cards with numbers
     Failure/Error: expect(hand.tierce?).to be true
     NoMethodError:
       undefined method `tierce?' for nil:NilClass
     # /tmp/d20151112-27349-1o3ajcp/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)>'

  9) 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 nil:NilClass
     # /tmp/d20151112-27349-1o3ajcp/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)>'

  10) BeloteDeck hand #quarte? detects four cards with increasing ranks
     Failure/Error: expect(hand.quarte?).to be true
     NoMethodError:
       undefined method `quarte?' for nil:NilClass
     # /tmp/d20151112-27349-1o3ajcp/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)>'

  11) 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 nil:NilClass
     # /tmp/d20151112-27349-1o3ajcp/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)>'

  12) BeloteDeck hand #quint? detects five cards with increasing ranks
     Failure/Error: expect(hand.quint?).to be true
     NoMethodError:
       undefined method `quint?' for nil:NilClass
     # /tmp/d20151112-27349-1o3ajcp/spec.rb:366:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  13) BeloteDeck hand #quint? does not return true if there is no quint
     Failure/Error: expect(hand.quint?).to be false
     NoMethodError:
       undefined method `quint?' for nil:NilClass
     # /tmp/d20151112-27349-1o3ajcp/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)>'

  14) 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 nil:NilClass
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1o3ajcp/spec.rb:386
     # /tmp/d20151112-27349-1o3ajcp/spec.rb:86:in `public_send'
     # /tmp/d20151112-27349-1o3ajcp/spec.rb:86:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  15) BeloteDeck hand #carre_of_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 nil:NilClass
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1o3ajcp/spec.rb:386
     # /tmp/d20151112-27349-1o3ajcp/spec.rb:101:in `public_send'
     # /tmp/d20151112-27349-1o3ajcp/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)>'

  16) 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 nil:NilClass
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1o3ajcp/spec.rb:390
     # /tmp/d20151112-27349-1o3ajcp/spec.rb:86:in `public_send'
     # /tmp/d20151112-27349-1o3ajcp/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)>'

  17) 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 nil:NilClass
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1o3ajcp/spec.rb:390
     # /tmp/d20151112-27349-1o3ajcp/spec.rb:101:in `public_send'
     # /tmp/d20151112-27349-1o3ajcp/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)>'

  18) 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 nil:NilClass
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1o3ajcp/spec.rb:394
     # /tmp/d20151112-27349-1o3ajcp/spec.rb:86:in `public_send'
     # /tmp/d20151112-27349-1o3ajcp/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)>'

  19) 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 nil:NilClass
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1o3ajcp/spec.rb:394
     # /tmp/d20151112-27349-1o3ajcp/spec.rb:101:in `public_send'
     # /tmp/d20151112-27349-1o3ajcp/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)>'

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

  21) 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 nil:NilClass
     # /tmp/d20151112-27349-1o3ajcp/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)>'

  22) 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 nil:NilClass
     # /tmp/d20151112-27349-1o3ajcp/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)>'

  23) 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 nil:NilClass
     # /tmp/d20151112-27349-1o3ajcp/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.03688 seconds
57 examples, 23 failures

Failed examples:

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

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

Николай обнови решението на 11.11.2015 09:08 (преди над 8 години)

+class Card
+ def initialize(rank, suit)
+ @rank = rank
+ @suit = suit
+ end
+
+ def convert_name(name)
+ temp_name = name.to_s
+ temp_name[0] = temp_name[0].upcase
+ temp_name
+ end
+
+ def rank
+ @rank
+ end
+
+ def suit
+ @suit
+ end
+
+ def to_s
+ string = "#{convert_name(@rank)} of #{convert_name(@suit)}"
+ string
+ end
+
+ def ==(card)
+ if @rank == card.rank and @suit == card.suit
+ return true
+ else
+ return false
+ end
+ end
+end
+
+class Ranks
+ WAR_RANKS = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace].reverse
+ BELOTE_RANKS = [7, 8, 9, :jack, :queen, :king, 10, :ace].reverse
+ SIXTY_SIX_RANKS = [9, :jack, :queen, :king, 10, :ace].reverse
+ SUITS = [:spades, :hearts, :diamonds, :clubs]
+
+ def self.rank(deck_type, card)
+ if deck_type.is_a? WarDeck
+ WAR_RANKS.index(card.rank)
+ elsif deck_type.is_a? BeloteDeck
+ BELOTE_RANKS.index(card.rank)
+ elsif deck_type.is_a? SixtySixDeck
+ SIXTY_SIX_RANKS.index(card.rank)
+ else
+ WAR_RANKS.index(card.rank)
+ end
+ end
+
+ def self.suit(card)
+ SUITS.index(card.suit)
+ end
+end
+
+class DeckFactory
+ @all_ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
+ @suits = [:spades, :hearts, :diamonds, :clubs]
+ @ranks = Array.new
+
+ def self.choose_game(deck_type)
+ if deck_type.is_a? WarDeck
+ @ranks = @all_ranks
+ elsif deck_type.is_a? BeloteDeck
+ @ranks = @all_ranks - [2, 3, 4, 5, 6]
+ elsif deck_type.is_a? SixtySixDeck
+ @ranks = @all_ranks - [2, 3, 4, 5, 6, 7, 8]
+ else
+ @ranks = @all_ranks
+ end
+ end
+
+ def self.create_deck(deck_type)
+ self.choose_game(deck_type)
+ deck = Array.new
+ @ranks.product(@suits).each {|card| deck.push(Card.new(card[0], card[1]))}
+ deck
+ end
+end
+
+class Hand
+ def initialize(hand, cards)
+ @hand = hand.take(cards)
+ @size = @hand.size
+ end
+
+ def size
+ @size
+ end
+end
+
+class WarHand < Hand
+ def play_card
+ if size > 0
+ @hand.pop
+ else
+ puts "no more cards"
+ end
+ end
+
+ def allow_face_up
+ if size <= 3
+ true
+ else
+ false
+ end
+ end
+end
+
+class BeloteHand < Hand
+ BELOTE_RANKS = [7, 8, 9, 10, :jack, :queen, :king, :ace]
+
+ def highest_of_suit(suit)
+ array = Array.new
+ @hand.each do |card|
+ if card.suit == suit then array.push(card) end
+ end
+ array.sort_by!{|card| Ranks.rank(BeloteDeck, card)}
+ array.first
+ end
+
+ def belote?
+ array = [:spades, :hearts, :diamonds, :clubs]
+ array.each {|suits| return true if belote_of_suit?(suits)}
+ return false
+ end
+
+ def belote_of_suit?(suit)
+ queen = @hand.any?{|card| card.rank == :queen and card.suit == suit}
+ king = @hand.any?{|card| card.rank == :king and card.suit == suit}
+ queen and king
+ end
+
+ def tierce?
+ sort_hand
+ @hand.reverse.each_cons(3) do |cards|
+ if straight_all_suits?(cards) then return true end
+ end
+ end
+
+ def quarte?
+ sort_hand
+ @hand.reverse.each_cons(4) do |cards|
+ if straight_all_suits?(cards) then return true end
+ end
+ return false
+ end
+
+ def quint?
+ sort_hand
+ puts @hand
+ @hand.reverse.each_cons(5) do |cards|
+ if straight_all_suits?(cards) then return true end
+ end
+ return false
+ end
+
+ def straight_all_suits?(cards)
+ if straight?(cards, :spades) then return true end
+ if straight?(cards, :hearts) then return true end
+ if straight?(cards, :diamonds) then return true end
+ if straight?(cards, :clubs) then return true end
+ return false
+ end
+ def straight?(cards, suit)
+ ranks = Array.new
+ cards.each do |card|
+ if card.suit != suit then return false end
+ ranks.push(card.rank)
+ end
+ if (BELOTE_RANKS & ranks) == ranks then return true end
+ return false
+ end
+
+ def carre_of_jacks?
+ carre?(:jack)
+ end
+
+ def carre_of_nines?
+ carre?(9)
+ end
+
+ def carre_of_aces?
+ carre?(:ace)
+ end
+
+ def sort_hand
+ @hand.sort_by!{|x| [Ranks.suit(x), Ranks.rank(BeloteDeck.new, x)]}
+ end
+
+ def carre?(rank)
+ count = 0
+ @hand.each{|card| if card.rank == rank then count += 1 end}
+ if count == 4 then return true end
+ return false
+ end
+end
+
+class SixtySixHand < Hand
+ def twenty?(trump_suit)
+ if forty?(trump_suit) == true then return false end
+ array = [:spades, :hearts, :diamonds, :clubs]
+ array.delete(trump_suit)
+ array.each {|suits| return true if forty?(suits)}
+ return false
+ end
+
+ def forty?(trump_suit)
+ queen = @hand.any?{|card| card.rank == :queen and card.suit == trump_suit}
+ king = @hand.any?{|card| card.rank == :king and card.suit == trump_suit}
+ queen and king
+ end
+end
+
+class Deck
+ include Enumerable
+
+ def initialize(*deck)
+ if deck.size == 0
+ @deck = DeckFactory.create_deck(self)
+ else
+ @deck = deck[0]
+ end
+ end
+
+ def each
+ @deck.each{ |x| yield x}
+ end
+
+ def size
+ @deck.size
+ end
+
+ def draw_top_card
+ @deck.shift
+ end
+
+ def draw_bottom_card
+ @deck.pop
+ end
+
+ def top_card
+ @deck.first
+ end
+
+ def bottom_card
+ @deck.last
+ end
+
+ def sort
+ @deck.sort_by!{|x| [Ranks.suit(x), Ranks.rank(self, x)]}
+ end
+
+ def shuffle
+ @deck.shuffle!
+ end
+
+ def to_s
+ @deck.each {|card| puts card.to_s}
+ end
+
+ def deal
+ new_hand(26)
+ end
+
+ def new_hand(size)
+ if self.size > size
+ hand = right_hand(@deck, size)
+ @deck = @deck.drop(size)
+ hand
+ else
+ puts "not eneough cards in the deck to deal"
+ end
+ end
+
+ def right_hand(deck, size)
+ if self.is_a? BeloteDeck
+ BeloteHand.new(deck, size)
+ elsif self.is_a? SixtySixDeck
+ SixtySixHand.new(deck, size)
+ else
+ WarHand.new(deck, size)
+ end
+ end
+end
+
+class WarDeck < Deck
+def deal
+ new_hand(26)
+ end
+end
+
+class BeloteDeck < Deck
+ def deal
+ new_hand(8)
+ end
+end
+
+class SixtySixDeck < Deck
+ def initialize(*deck)
+ super(*deck)
+ @trump_suit = [:spades, :hearts, :diamonds, :clubs].sample
+ end
+
+ def deal
+ hand = new_hand(6)
+ end
+end

Николай обнови решението на 11.11.2015 09:10 (преди над 8 години)

class Card
def initialize(rank, suit)
@rank = rank
@suit = suit
end
def convert_name(name)
temp_name = name.to_s
temp_name[0] = temp_name[0].upcase
temp_name
end
def rank
@rank
end
def suit
@suit
end
def to_s
string = "#{convert_name(@rank)} of #{convert_name(@suit)}"
string
end
def ==(card)
if @rank == card.rank and @suit == card.suit
return true
else
return false
end
end
end
class Ranks
WAR_RANKS = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace].reverse
BELOTE_RANKS = [7, 8, 9, :jack, :queen, :king, 10, :ace].reverse
SIXTY_SIX_RANKS = [9, :jack, :queen, :king, 10, :ace].reverse
SUITS = [:spades, :hearts, :diamonds, :clubs]
def self.rank(deck_type, card)
if deck_type.is_a? WarDeck
WAR_RANKS.index(card.rank)
elsif deck_type.is_a? BeloteDeck
BELOTE_RANKS.index(card.rank)
elsif deck_type.is_a? SixtySixDeck
SIXTY_SIX_RANKS.index(card.rank)
else
WAR_RANKS.index(card.rank)
end
end
def self.suit(card)
SUITS.index(card.suit)
end
end
class DeckFactory
@all_ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
@suits = [:spades, :hearts, :diamonds, :clubs]
@ranks = Array.new
def self.choose_game(deck_type)
if deck_type.is_a? WarDeck
@ranks = @all_ranks
elsif deck_type.is_a? BeloteDeck
@ranks = @all_ranks - [2, 3, 4, 5, 6]
elsif deck_type.is_a? SixtySixDeck
@ranks = @all_ranks - [2, 3, 4, 5, 6, 7, 8]
else
@ranks = @all_ranks
end
end
def self.create_deck(deck_type)
self.choose_game(deck_type)
deck = Array.new
@ranks.product(@suits).each {|card| deck.push(Card.new(card[0], card[1]))}
deck
end
end
class Hand
def initialize(hand, cards)
@hand = hand.take(cards)
@size = @hand.size
end
def size
@size
end
end
class WarHand < Hand
def play_card
if size > 0
@hand.pop
else
puts "no more cards"
end
end
def allow_face_up
if size <= 3
true
else
false
end
end
end
class BeloteHand < Hand
BELOTE_RANKS = [7, 8, 9, 10, :jack, :queen, :king, :ace]
def highest_of_suit(suit)
array = Array.new
@hand.each do |card|
if card.suit == suit then array.push(card) end
end
array.sort_by!{|card| Ranks.rank(BeloteDeck, card)}
array.first
end
def belote?
array = [:spades, :hearts, :diamonds, :clubs]
array.each {|suits| return true if belote_of_suit?(suits)}
return false
end
def belote_of_suit?(suit)
queen = @hand.any?{|card| card.rank == :queen and card.suit == suit}
king = @hand.any?{|card| card.rank == :king and card.suit == suit}
queen and king
end
def tierce?
sort_hand
@hand.reverse.each_cons(3) do |cards|
if straight_all_suits?(cards) then return true end
end
end
def quarte?
sort_hand
@hand.reverse.each_cons(4) do |cards|
if straight_all_suits?(cards) then return true end
end
return false
end
def quint?
sort_hand
puts @hand
@hand.reverse.each_cons(5) do |cards|
if straight_all_suits?(cards) then return true end
end
return false
end
def straight_all_suits?(cards)
if straight?(cards, :spades) then return true end
if straight?(cards, :hearts) then return true end
if straight?(cards, :diamonds) then return true end
if straight?(cards, :clubs) then return true end
return false
end
def straight?(cards, suit)
ranks = Array.new
cards.each do |card|
if card.suit != suit then return false end
ranks.push(card.rank)
end
if (BELOTE_RANKS & ranks) == ranks then return true end
return false
end
def carre_of_jacks?
carre?(:jack)
end
def carre_of_nines?
carre?(9)
end
def carre_of_aces?
carre?(:ace)
end
def sort_hand
@hand.sort_by!{|x| [Ranks.suit(x), Ranks.rank(BeloteDeck.new, x)]}
end
def carre?(rank)
count = 0
@hand.each{|card| if card.rank == rank then count += 1 end}
if count == 4 then return true end
return false
end
end
class SixtySixHand < Hand
def twenty?(trump_suit)
if forty?(trump_suit) == true then return false end
array = [:spades, :hearts, :diamonds, :clubs]
array.delete(trump_suit)
array.each {|suits| return true if forty?(suits)}
return false
end
def forty?(trump_suit)
queen = @hand.any?{|card| card.rank == :queen and card.suit == trump_suit}
king = @hand.any?{|card| card.rank == :king and card.suit == trump_suit}
queen and king
end
end
class Deck
include Enumerable
def initialize(*deck)
if deck.size == 0
@deck = DeckFactory.create_deck(self)
else
@deck = deck[0]
end
end
def each
@deck.each{ |x| yield x}
end
def size
@deck.size
end
def draw_top_card
@deck.shift
end
def draw_bottom_card
@deck.pop
end
def top_card
@deck.first
end
def bottom_card
@deck.last
end
def sort
@deck.sort_by!{|x| [Ranks.suit(x), Ranks.rank(self, x)]}
end
def shuffle
@deck.shuffle!
end
def to_s
@deck.each {|card| puts card.to_s}
end
def deal
new_hand(26)
end
def new_hand(size)
if self.size > size
hand = right_hand(@deck, size)
@deck = @deck.drop(size)
hand
else
puts "not eneough cards in the deck to deal"
end
end
def right_hand(deck, size)
if self.is_a? BeloteDeck
BeloteHand.new(deck, size)
elsif self.is_a? SixtySixDeck
SixtySixHand.new(deck, size)
else
WarHand.new(deck, size)
end
end
end
class WarDeck < Deck
-def deal
+ def deal
new_hand(26)
end
end
class BeloteDeck < Deck
def deal
new_hand(8)
end
end
class SixtySixDeck < Deck
def initialize(*deck)
super(*deck)
@trump_suit = [:spades, :hearts, :diamonds, :clubs].sample
end
def deal
hand = new_hand(6)
end
end

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

Не оставяй puts в кода си. Ако има грешки и ако държиш да ги обработиш, въпреки, че това не се иска по условие, хвърляй изключение.

На места имаш сериозни стилови проблеми. Опитай да намериш начин да ги идентифицираш и премахнеш. За груби нарушения в стила е възможно да отнемем точки.

Николай обнови решението на 11.11.2015 11:35 (преди над 8 години)

class Card
def initialize(rank, suit)
@rank = rank
@suit = suit
end
def convert_name(name)
temp_name = name.to_s
temp_name[0] = temp_name[0].upcase
temp_name
end
def rank
@rank
end
def suit
@suit
end
def to_s
string = "#{convert_name(@rank)} of #{convert_name(@suit)}"
string
end
def ==(card)
if @rank == card.rank and @suit == card.suit
- return true
+ true
else
- return false
+ false
end
end
end
class Ranks
WAR_RANKS = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace].reverse
BELOTE_RANKS = [7, 8, 9, :jack, :queen, :king, 10, :ace].reverse
SIXTY_SIX_RANKS = [9, :jack, :queen, :king, 10, :ace].reverse
SUITS = [:spades, :hearts, :diamonds, :clubs]
def self.rank(deck_type, card)
if deck_type.is_a? WarDeck
WAR_RANKS.index(card.rank)
elsif deck_type.is_a? BeloteDeck
BELOTE_RANKS.index(card.rank)
elsif deck_type.is_a? SixtySixDeck
SIXTY_SIX_RANKS.index(card.rank)
else
WAR_RANKS.index(card.rank)
end
end
def self.suit(card)
SUITS.index(card.suit)
end
end
class DeckFactory
- @all_ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
- @suits = [:spades, :hearts, :diamonds, :clubs]
+ ALL_RANKS = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
+ SUITS = [:spades, :hearts, :diamonds, :clubs]
@ranks = Array.new
def self.choose_game(deck_type)
if deck_type.is_a? WarDeck
- @ranks = @all_ranks
+ @ranks = ALL_RANKS
elsif deck_type.is_a? BeloteDeck
- @ranks = @all_ranks - [2, 3, 4, 5, 6]
+ @ranks = ALL_RANKS - [2, 3, 4, 5, 6]
elsif deck_type.is_a? SixtySixDeck
- @ranks = @all_ranks - [2, 3, 4, 5, 6, 7, 8]
+ @ranks = ALL_RANKS - [2, 3, 4, 5, 6, 7, 8]
else
- @ranks = @all_ranks
+ @ranks = ALL_RANKS
end
end
def self.create_deck(deck_type)
self.choose_game(deck_type)
deck = Array.new
- @ranks.product(@suits).each {|card| deck.push(Card.new(card[0], card[1]))}
+ @ranks.product(SUITS).each {|card| deck.push(Card.new(card[0], card[1]))}
deck
end
end
class Hand
def initialize(hand, cards)
@hand = hand.take(cards)
@size = @hand.size
end
def size
@size
end
end
class WarHand < Hand
def play_card
if size > 0
@hand.pop
- else
- puts "no more cards"
end
end
def allow_face_up
if size <= 3
true
else
false
end
end
end
class BeloteHand < Hand
BELOTE_RANKS = [7, 8, 9, 10, :jack, :queen, :king, :ace]
def highest_of_suit(suit)
array = Array.new
@hand.each do |card|
- if card.suit == suit then array.push(card) end
+ array.push(card) if card.suit == suit
end
array.sort_by!{|card| Ranks.rank(BeloteDeck, card)}
array.first
end
def belote?
array = [:spades, :hearts, :diamonds, :clubs]
array.each {|suits| return true if belote_of_suit?(suits)}
return false
end
def belote_of_suit?(suit)
queen = @hand.any?{|card| card.rank == :queen and card.suit == suit}
king = @hand.any?{|card| card.rank == :king and card.suit == suit}
queen and king
end
def tierce?
sort_hand
@hand.reverse.each_cons(3) do |cards|
- if straight_all_suits?(cards) then return true end
+ return true if straight_all_suits?(cards)
end
end
def quarte?
sort_hand
@hand.reverse.each_cons(4) do |cards|
- if straight_all_suits?(cards) then return true end
+ return true if straight_all_suits?(cards)
end
return false
end
def quint?
sort_hand
- puts @hand
@hand.reverse.each_cons(5) do |cards|
- if straight_all_suits?(cards) then return true end
+ return true if straight_all_suits?(cards)
end
return false
end
def straight_all_suits?(cards)
- if straight?(cards, :spades) then return true end
- if straight?(cards, :hearts) then return true end
- if straight?(cards, :diamonds) then return true end
- if straight?(cards, :clubs) then return true end
+ return true if straight?(cards, :spades)
+ return true if straight?(cards, :hearts)
+ return true if straight?(cards, :diamonds)
+ return true if straight?(cards, :clubs)
return false
end
+
def straight?(cards, suit)
ranks = Array.new
cards.each do |card|
- if card.suit != suit then return false end
+ return false if card.suit != suit
ranks.push(card.rank)
end
- if (BELOTE_RANKS & ranks) == ranks then return true end
- return false
+ return true if (BELOTE_RANKS & ranks) == ranks
+ false
end
def carre_of_jacks?
carre?(:jack)
end
def carre_of_nines?
carre?(9)
end
def carre_of_aces?
carre?(:ace)
end
def sort_hand
@hand.sort_by!{|x| [Ranks.suit(x), Ranks.rank(BeloteDeck.new, x)]}
end
def carre?(rank)
count = 0
- @hand.each{|card| if card.rank == rank then count += 1 end}
- if count == 4 then return true end
- return false
+ @hand.each{|card| count += 1 if card.rank == rank}
+ return true if count == 4
+ false
end
end
class SixtySixHand < Hand
def twenty?(trump_suit)
- if forty?(trump_suit) == true then return false end
+ return false if forty?(trump_suit)
array = [:spades, :hearts, :diamonds, :clubs]
array.delete(trump_suit)
array.each {|suits| return true if forty?(suits)}
- return false
+ false
end
def forty?(trump_suit)
queen = @hand.any?{|card| card.rank == :queen and card.suit == trump_suit}
king = @hand.any?{|card| card.rank == :king and card.suit == trump_suit}
queen and king
end
end
class Deck
include Enumerable
def initialize(*deck)
if deck.size == 0
@deck = DeckFactory.create_deck(self)
else
@deck = deck[0]
end
end
def each
@deck.each{ |x| yield x}
end
def size
@deck.size
end
def draw_top_card
@deck.shift
end
def draw_bottom_card
@deck.pop
end
def top_card
@deck.first
end
def bottom_card
@deck.last
end
def sort
@deck.sort_by!{|x| [Ranks.suit(x), Ranks.rank(self, x)]}
end
def shuffle
@deck.shuffle!
end
def to_s
@deck.each {|card| puts card.to_s}
end
def deal
new_hand(26)
end
def new_hand(size)
if self.size > size
hand = right_hand(@deck, size)
@deck = @deck.drop(size)
hand
- else
- puts "not eneough cards in the deck to deal"
end
end
def right_hand(deck, size)
if self.is_a? BeloteDeck
BeloteHand.new(deck, size)
elsif self.is_a? SixtySixDeck
SixtySixHand.new(deck, size)
else
WarHand.new(deck, size)
end
end
end
class WarDeck < Deck
def deal
new_hand(26)
end
end
class BeloteDeck < Deck
def deal
new_hand(8)
end
end
class SixtySixDeck < Deck
def initialize(*deck)
super(*deck)
@trump_suit = [:spades, :hearts, :diamonds, :clubs].sample
end
def deal
hand = new_hand(6)
end
end

Николай обнови решението на 11.11.2015 11:54 (преди над 8 години)

class Card
def initialize(rank, suit)
@rank = rank
@suit = suit
end
- def convert_name(name)
- temp_name = name.to_s
- temp_name[0] = temp_name[0].upcase
- temp_name
- end
-
def rank
@rank
end
def suit
@suit
end
def to_s
- string = "#{convert_name(@rank)} of #{convert_name(@suit)}"
+ string = "#{@rank.to_s.capitalize} of #{@suit.to_s.capitalize}"
string
end
def ==(card)
if @rank == card.rank and @suit == card.suit
true
else
false
end
end
end
class Ranks
WAR_RANKS = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace].reverse
BELOTE_RANKS = [7, 8, 9, :jack, :queen, :king, 10, :ace].reverse
SIXTY_SIX_RANKS = [9, :jack, :queen, :king, 10, :ace].reverse
SUITS = [:spades, :hearts, :diamonds, :clubs]
def self.rank(deck_type, card)
if deck_type.is_a? WarDeck
WAR_RANKS.index(card.rank)
elsif deck_type.is_a? BeloteDeck
BELOTE_RANKS.index(card.rank)
elsif deck_type.is_a? SixtySixDeck
SIXTY_SIX_RANKS.index(card.rank)
else
WAR_RANKS.index(card.rank)
end
end
def self.suit(card)
SUITS.index(card.suit)
end
end
class DeckFactory
ALL_RANKS = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
SUITS = [:spades, :hearts, :diamonds, :clubs]
@ranks = Array.new
def self.choose_game(deck_type)
if deck_type.is_a? WarDeck
@ranks = ALL_RANKS
elsif deck_type.is_a? BeloteDeck
@ranks = ALL_RANKS - [2, 3, 4, 5, 6]
elsif deck_type.is_a? SixtySixDeck
@ranks = ALL_RANKS - [2, 3, 4, 5, 6, 7, 8]
else
@ranks = ALL_RANKS
end
end
def self.create_deck(deck_type)
self.choose_game(deck_type)
deck = Array.new
@ranks.product(SUITS).each {|card| deck.push(Card.new(card[0], card[1]))}
deck
end
end
class Hand
def initialize(hand, cards)
@hand = hand.take(cards)
@size = @hand.size
end
def size
@size
end
end
class WarHand < Hand
def play_card
if size > 0
@hand.pop
end
end
def allow_face_up
if size <= 3
true
else
false
end
end
end
class BeloteHand < Hand
BELOTE_RANKS = [7, 8, 9, 10, :jack, :queen, :king, :ace]
def highest_of_suit(suit)
array = Array.new
@hand.each do |card|
array.push(card) if card.suit == suit
end
array.sort_by!{|card| Ranks.rank(BeloteDeck, card)}
array.first
end
def belote?
array = [:spades, :hearts, :diamonds, :clubs]
array.each {|suits| return true if belote_of_suit?(suits)}
return false
end
def belote_of_suit?(suit)
queen = @hand.any?{|card| card.rank == :queen and card.suit == suit}
king = @hand.any?{|card| card.rank == :king and card.suit == suit}
queen and king
end
def tierce?
sort_hand
@hand.reverse.each_cons(3) do |cards|
return true if straight_all_suits?(cards)
end
end
def quarte?
sort_hand
@hand.reverse.each_cons(4) do |cards|
return true if straight_all_suits?(cards)
end
return false
end
def quint?
sort_hand
@hand.reverse.each_cons(5) do |cards|
return true if straight_all_suits?(cards)
end
return false
end
def straight_all_suits?(cards)
return true if straight?(cards, :spades)
return true if straight?(cards, :hearts)
return true if straight?(cards, :diamonds)
return true if straight?(cards, :clubs)
return false
end
def straight?(cards, suit)
ranks = Array.new
cards.each do |card|
return false if card.suit != suit
ranks.push(card.rank)
end
return true if (BELOTE_RANKS & ranks) == ranks
false
end
def carre_of_jacks?
carre?(:jack)
end
def carre_of_nines?
carre?(9)
end
def carre_of_aces?
carre?(:ace)
end
def sort_hand
@hand.sort_by!{|x| [Ranks.suit(x), Ranks.rank(BeloteDeck.new, x)]}
end
def carre?(rank)
count = 0
@hand.each{|card| count += 1 if card.rank == rank}
return true if count == 4
false
end
end
class SixtySixHand < Hand
def twenty?(trump_suit)
return false if forty?(trump_suit)
array = [:spades, :hearts, :diamonds, :clubs]
array.delete(trump_suit)
array.each {|suits| return true if forty?(suits)}
false
end
def forty?(trump_suit)
queen = @hand.any?{|card| card.rank == :queen and card.suit == trump_suit}
king = @hand.any?{|card| card.rank == :king and card.suit == trump_suit}
queen and king
end
end
class Deck
include Enumerable
def initialize(*deck)
if deck.size == 0
@deck = DeckFactory.create_deck(self)
else
@deck = deck[0]
end
end
def each
@deck.each{ |x| yield x}
end
def size
@deck.size
end
def draw_top_card
@deck.shift
end
def draw_bottom_card
@deck.pop
end
def top_card
@deck.first
end
def bottom_card
@deck.last
end
def sort
@deck.sort_by!{|x| [Ranks.suit(x), Ranks.rank(self, x)]}
end
def shuffle
@deck.shuffle!
end
def to_s
@deck.each {|card| puts card.to_s}
end
def deal
new_hand(26)
end
def new_hand(size)
if self.size > size
hand = right_hand(@deck, size)
@deck = @deck.drop(size)
hand
end
end
def right_hand(deck, size)
if self.is_a? BeloteDeck
BeloteHand.new(deck, size)
elsif self.is_a? SixtySixDeck
SixtySixHand.new(deck, size)
else
WarHand.new(deck, size)
end
end
end
class WarDeck < Deck
def deal
new_hand(26)
end
end
class BeloteDeck < Deck
def deal
new_hand(8)
end
end
class SixtySixDeck < Deck
def initialize(*deck)
super(*deck)
@trump_suit = [:spades, :hearts, :diamonds, :clubs].sample
end
def deal
hand = new_hand(6)
end
end

Николай обнови решението на 11.11.2015 11:56 (преди над 8 години)

class Card
def initialize(rank, suit)
@rank = rank
@suit = suit
end
def rank
@rank
end
def suit
@suit
end
def to_s
string = "#{@rank.to_s.capitalize} of #{@suit.to_s.capitalize}"
string
end
def ==(card)
if @rank == card.rank and @suit == card.suit
true
else
false
end
end
end
class Ranks
WAR_RANKS = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace].reverse
BELOTE_RANKS = [7, 8, 9, :jack, :queen, :king, 10, :ace].reverse
SIXTY_SIX_RANKS = [9, :jack, :queen, :king, 10, :ace].reverse
SUITS = [:spades, :hearts, :diamonds, :clubs]
def self.rank(deck_type, card)
if deck_type.is_a? WarDeck
WAR_RANKS.index(card.rank)
elsif deck_type.is_a? BeloteDeck
BELOTE_RANKS.index(card.rank)
elsif deck_type.is_a? SixtySixDeck
SIXTY_SIX_RANKS.index(card.rank)
else
WAR_RANKS.index(card.rank)
end
end
def self.suit(card)
SUITS.index(card.suit)
end
end
class DeckFactory
ALL_RANKS = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
SUITS = [:spades, :hearts, :diamonds, :clubs]
@ranks = Array.new
def self.choose_game(deck_type)
if deck_type.is_a? WarDeck
@ranks = ALL_RANKS
elsif deck_type.is_a? BeloteDeck
@ranks = ALL_RANKS - [2, 3, 4, 5, 6]
elsif deck_type.is_a? SixtySixDeck
@ranks = ALL_RANKS - [2, 3, 4, 5, 6, 7, 8]
else
@ranks = ALL_RANKS
end
end
def self.create_deck(deck_type)
self.choose_game(deck_type)
deck = Array.new
@ranks.product(SUITS).each {|card| deck.push(Card.new(card[0], card[1]))}
deck
end
end
class Hand
def initialize(hand, cards)
@hand = hand.take(cards)
@size = @hand.size
end
def size
@size
end
end
class WarHand < Hand
def play_card
if size > 0
@hand.pop
end
end
def allow_face_up
if size <= 3
true
else
false
end
end
end
class BeloteHand < Hand
BELOTE_RANKS = [7, 8, 9, 10, :jack, :queen, :king, :ace]
def highest_of_suit(suit)
array = Array.new
@hand.each do |card|
array.push(card) if card.suit == suit
end
array.sort_by!{|card| Ranks.rank(BeloteDeck, card)}
array.first
end
def belote?
array = [:spades, :hearts, :diamonds, :clubs]
array.each {|suits| return true if belote_of_suit?(suits)}
- return false
+ false
end
def belote_of_suit?(suit)
queen = @hand.any?{|card| card.rank == :queen and card.suit == suit}
king = @hand.any?{|card| card.rank == :king and card.suit == suit}
queen and king
end
def tierce?
sort_hand
@hand.reverse.each_cons(3) do |cards|
return true if straight_all_suits?(cards)
end
end
def quarte?
sort_hand
@hand.reverse.each_cons(4) do |cards|
return true if straight_all_suits?(cards)
end
- return false
+ false
end
def quint?
sort_hand
@hand.reverse.each_cons(5) do |cards|
return true if straight_all_suits?(cards)
end
- return false
+ false
end
def straight_all_suits?(cards)
return true if straight?(cards, :spades)
return true if straight?(cards, :hearts)
return true if straight?(cards, :diamonds)
return true if straight?(cards, :clubs)
- return false
+ false
end
def straight?(cards, suit)
ranks = Array.new
cards.each do |card|
return false if card.suit != suit
ranks.push(card.rank)
end
return true if (BELOTE_RANKS & ranks) == ranks
false
end
def carre_of_jacks?
carre?(:jack)
end
def carre_of_nines?
carre?(9)
end
def carre_of_aces?
carre?(:ace)
end
def sort_hand
@hand.sort_by!{|x| [Ranks.suit(x), Ranks.rank(BeloteDeck.new, x)]}
end
def carre?(rank)
count = 0
@hand.each{|card| count += 1 if card.rank == rank}
return true if count == 4
false
end
end
class SixtySixHand < Hand
def twenty?(trump_suit)
return false if forty?(trump_suit)
array = [:spades, :hearts, :diamonds, :clubs]
array.delete(trump_suit)
array.each {|suits| return true if forty?(suits)}
false
end
def forty?(trump_suit)
queen = @hand.any?{|card| card.rank == :queen and card.suit == trump_suit}
king = @hand.any?{|card| card.rank == :king and card.suit == trump_suit}
queen and king
end
end
class Deck
include Enumerable
def initialize(*deck)
if deck.size == 0
@deck = DeckFactory.create_deck(self)
else
@deck = deck[0]
end
end
def each
@deck.each{ |x| yield x}
end
def size
@deck.size
end
def draw_top_card
@deck.shift
end
def draw_bottom_card
@deck.pop
end
def top_card
@deck.first
end
def bottom_card
@deck.last
end
def sort
@deck.sort_by!{|x| [Ranks.suit(x), Ranks.rank(self, x)]}
end
def shuffle
@deck.shuffle!
end
def to_s
@deck.each {|card| puts card.to_s}
end
def deal
new_hand(26)
end
def new_hand(size)
if self.size > size
hand = right_hand(@deck, size)
@deck = @deck.drop(size)
hand
end
end
def right_hand(deck, size)
if self.is_a? BeloteDeck
BeloteHand.new(deck, size)
elsif self.is_a? SixtySixDeck
SixtySixHand.new(deck, size)
else
WarHand.new(deck, size)
end
end
end
class WarDeck < Deck
def deal
new_hand(26)
end
end
class BeloteDeck < Deck
def deal
new_hand(8)
end
end
class SixtySixDeck < Deck
def initialize(*deck)
super(*deck)
@trump_suit = [:spades, :hearts, :diamonds, :clubs].sample
end
def deal
hand = new_hand(6)
end
end

Николай обнови решението на 11.11.2015 15:17 (преди над 8 години)

class Card
def initialize(rank, suit)
@rank = rank
@suit = suit
end
def rank
@rank
end
def suit
@suit
end
def to_s
string = "#{@rank.to_s.capitalize} of #{@suit.to_s.capitalize}"
string
end
def ==(card)
if @rank == card.rank and @suit == card.suit
true
else
false
end
end
end
class Ranks
WAR_RANKS = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace].reverse
BELOTE_RANKS = [7, 8, 9, :jack, :queen, :king, 10, :ace].reverse
SIXTY_SIX_RANKS = [9, :jack, :queen, :king, 10, :ace].reverse
SUITS = [:spades, :hearts, :diamonds, :clubs]
def self.rank(deck_type, card)
if deck_type.is_a? WarDeck
WAR_RANKS.index(card.rank)
elsif deck_type.is_a? BeloteDeck
BELOTE_RANKS.index(card.rank)
elsif deck_type.is_a? SixtySixDeck
SIXTY_SIX_RANKS.index(card.rank)
else
WAR_RANKS.index(card.rank)
end
end
def self.suit(card)
SUITS.index(card.suit)
end
end
class DeckFactory
ALL_RANKS = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
SUITS = [:spades, :hearts, :diamonds, :clubs]
@ranks = Array.new
def self.choose_game(deck_type)
if deck_type.is_a? WarDeck
@ranks = ALL_RANKS
elsif deck_type.is_a? BeloteDeck
@ranks = ALL_RANKS - [2, 3, 4, 5, 6]
elsif deck_type.is_a? SixtySixDeck
@ranks = ALL_RANKS - [2, 3, 4, 5, 6, 7, 8]
else
@ranks = ALL_RANKS
end
end
def self.create_deck(deck_type)
self.choose_game(deck_type)
deck = Array.new
@ranks.product(SUITS).each {|card| deck.push(Card.new(card[0], card[1]))}
deck
end
end
class Hand
def initialize(hand, cards)
@hand = hand.take(cards)
@size = @hand.size
end
def size
@size
end
end
class WarHand < Hand
def play_card
if size > 0
@hand.pop
end
end
- def allow_face_up
+ def allow_face_up?
if size <= 3
true
else
false
end
end
end
class BeloteHand < Hand
BELOTE_RANKS = [7, 8, 9, 10, :jack, :queen, :king, :ace]
def highest_of_suit(suit)
array = Array.new
@hand.each do |card|
array.push(card) if card.suit == suit
end
array.sort_by!{|card| Ranks.rank(BeloteDeck, card)}
array.first
end
def belote?
array = [:spades, :hearts, :diamonds, :clubs]
array.each {|suits| return true if belote_of_suit?(suits)}
false
end
def belote_of_suit?(suit)
queen = @hand.any?{|card| card.rank == :queen and card.suit == suit}
king = @hand.any?{|card| card.rank == :king and card.suit == suit}
queen and king
end
def tierce?
sort_hand
@hand.reverse.each_cons(3) do |cards|
return true if straight_all_suits?(cards)
end
end
def quarte?
sort_hand
@hand.reverse.each_cons(4) do |cards|
return true if straight_all_suits?(cards)
end
false
end
def quint?
sort_hand
@hand.reverse.each_cons(5) do |cards|
return true if straight_all_suits?(cards)
end
false
end
def straight_all_suits?(cards)
return true if straight?(cards, :spades)
return true if straight?(cards, :hearts)
return true if straight?(cards, :diamonds)
return true if straight?(cards, :clubs)
false
end
def straight?(cards, suit)
ranks = Array.new
cards.each do |card|
return false if card.suit != suit
ranks.push(card.rank)
end
return true if (BELOTE_RANKS & ranks) == ranks
false
end
def carre_of_jacks?
carre?(:jack)
end
def carre_of_nines?
carre?(9)
end
def carre_of_aces?
carre?(:ace)
end
def sort_hand
@hand.sort_by!{|x| [Ranks.suit(x), Ranks.rank(BeloteDeck.new, x)]}
end
def carre?(rank)
count = 0
@hand.each{|card| count += 1 if card.rank == rank}
return true if count == 4
false
end
end
class SixtySixHand < Hand
def twenty?(trump_suit)
return false if forty?(trump_suit)
array = [:spades, :hearts, :diamonds, :clubs]
array.delete(trump_suit)
array.each {|suits| return true if forty?(suits)}
false
end
def forty?(trump_suit)
queen = @hand.any?{|card| card.rank == :queen and card.suit == trump_suit}
king = @hand.any?{|card| card.rank == :king and card.suit == trump_suit}
queen and king
end
end
class Deck
include Enumerable
def initialize(*deck)
if deck.size == 0
@deck = DeckFactory.create_deck(self)
else
@deck = deck[0]
end
end
def each
@deck.each{ |x| yield x}
end
def size
@deck.size
end
def draw_top_card
@deck.shift
end
def draw_bottom_card
@deck.pop
end
def top_card
@deck.first
end
def bottom_card
@deck.last
end
def sort
@deck.sort_by!{|x| [Ranks.suit(x), Ranks.rank(self, x)]}
end
def shuffle
@deck.shuffle!
end
def to_s
@deck.each {|card| puts card.to_s}
end
def deal
new_hand(26)
end
def new_hand(size)
if self.size > size
hand = right_hand(@deck, size)
@deck = @deck.drop(size)
hand
end
end
def right_hand(deck, size)
if self.is_a? BeloteDeck
BeloteHand.new(deck, size)
elsif self.is_a? SixtySixDeck
SixtySixHand.new(deck, size)
else
WarHand.new(deck, size)
end
end
end
class WarDeck < Deck
def deal
new_hand(26)
end
end
class BeloteDeck < Deck
def deal
new_hand(8)
end
end
class SixtySixDeck < Deck
def initialize(*deck)
super(*deck)
@trump_suit = [:spades, :hearts, :diamonds, :clubs].sample
end
def deal
hand = new_hand(6)
end
end

Имаш излишни присвоявания и ненужни if-ове на места (вж. класа Card).

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

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