Решение на Четвърта задача от Виктор Радивчев

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

Към профила на Виктор Радивчев

Резултати

  • 4 точки от тестове
  • 2 отнети точки
  • 2 точки общо
  • 42 успешни тест(а)
  • 15 неуспешни тест(а)

Код

class Card
def initialize(rank, suit)
@rank = rank
@suit = suit
end
attr_reader :rank
attr_reader :suit
def rank_to_number
case @rank
when :ace then 14
when :king then 13
when :queen then 12
when :jack then 11
else @rank
end
end
def to_s
if rank_to_number > 10
@rank.capitalize.to_s + " of " + @suit.capitalize.to_s
else @rank.to_s + " of " + @suit.capitalize.to_s
end
end
def ==(card)
@rank == card.rank and @suit == card.suit
end
def <=>(card)
if @suit.to_s[0].ord != card.suit.to_s[0].ord
@suit.to_s[0].ord <=> card.suit.to_s[0].ord
else rank_to_number <=> card.rank_to_number
end
end
def to_belote
BeloteCard.new(@rank, @suit)
end
end
class BeloteCard < Card
def rank_to_number
case @rank
when :ace then 14
when :king then 12
when :queen then 11
when :jack then 10
when 10 then 13
else @rank
end
end
end
class Deck
include Enumerable
def basic_deck
array_of_cards = []
count = 1
while count <= 52
card = Card.new(number_to_rank(count % 13 + 2),number_to_suit(count % 4))
array_of_cards << card
count += 1
end
array_of_cards
end
def initialize(array_of_cards = basic_deck)
@cards = array_of_cards.dup
end
def each
counter = 0
while counter < @cards.size
yield @cards[counter]
counter += 1
end
end
def size
@cards.size
end
def draw_top_card
@cards.pop
end
def draw_bottom_card
@cards.shift
end
def top_card
@cards[-1]
end
def bottom_card
@cards[0]
end
def to_s
counter = 0
string = ""
while counter < @cards.size
string = string + @cards[counter].to_s + "\n"
counter += 1
end
string
end
def sort
@cards.sort!.reverse!
end
def shuffle
@cards.shuffle!
end
def number_to_rank(number)
case number
when 14 then :ace
when 13 then :king
when 12 then :queen
when 11 then :jack
else number
end
end
def number_to_suit(number)
case number
when 3 then :spades
when 2 then :hearts
when 1 then :diamonds
else :clubs
end
end
end
class WarDeck < Deck
def deal
dealt_cards, counter = [], 0
while counter < 26
dealt_cards << draw_top_card
counter += 1
end
PlayerWarDeck.new(dealt_cards)
end
end
class PlayerWarDeck < Deck
def play_card
draw_bottom_card
end
def allow_face_up?
size <= 3
end
end
class BeloteDeck < Deck
def basic_deck
array_of_cards, count = [], -1
while (count += 1) < 8
array_of_cards << BeloteCard.new(number_to_rank(count % 8 + 7),:spades)
array_of_cards << BeloteCard.new(number_to_rank(count % 8 + 7),:diamonds)
array_of_cards << BeloteCard.new(number_to_rank(count % 8 + 7),:hearts)
array_of_cards << BeloteCard.new(number_to_rank(count % 8 + 7),:clubs)
end
array_of_cards
end
def initialize(array_of_cards = basic_deck)
counter = 0
while counter < array_of_cards.size
array_of_cards[counter] = array_of_cards[counter].to_belote
counter += 1
end
@cards = array_of_cards.dup
end
def deal
dealt_cards, counter = [], 0
while counter < 8
dealt_cards << draw_top_card
counter += 1
end
PlayerBeloteDeck.new(dealt_cards)
end
def sort
@cards.sort!.reverse!
end
end
class PlayerBeloteDeck < BeloteDeck
def carre_of_jacks?
@cards.select{|card| card.rank == :jack}.size == 4
end
def carre_of_nines?
@cards.select{|card| card.rank == 9}.size == 4
end
def carre_of_aces?
@cards.select{|card| card.rank == :ace}.size == 4
end
def highest_of_suit(suit)
@cards.select{|card| card.suit == suit}.max
end
def belote_from_suit(suit)
has_queen = @cards.any? {|card| card == Card.new(:queen,suit)}
has_king = @cards.any? {|card| card == Card.new(:king,suit)}
has_king and has_queen
end
def belote?
minor_belote = (belote_from_suit(:clubs) or belote_from_suit(:diamonds))
major_belote = (belote_from_suit(:hearts) or belote_from_suit(:spades))
minor_belote or major_belote
end
def max_sequence(suit)
cards = @cards.select{|card| card.suit == suit}
max_count, count, counter = 0, 1, 0
while counter < cards.size - 1
if (cards[counter].rank_to_number - 1) == cards[counter + 1].rank_to_number
count, counter = count + 1, counter + 1
else max_count, count, counter = [max_count, count].max, 1, counter + 1
end
end
max_count
end
def tierce?
self.sort
if (max_sequence(:spades) >= 3) or (max_sequence(:diamonds) >= 3)
true
elsif (max_sequence(:clubs) >= 3) or (max_sequence(:hearts) >= 3)
true
else false
end
end
def quarte?
self.sort
if (max_sequence(:spades) >= 4) or (max_sequence(:diamonds) >= 4)
true
elsif (max_sequence(:clubs) >= 4) or (max_sequence(:hearts) >= 4)
true
else false
end
end
def quint?
self.sort
if (max_sequence(:spades) >= 5) or (max_sequence(:diamonds) >= 5)
true
elsif (max_sequence(:clubs) >= 5) or (max_sequence(:hearts) >= 5)
true
else false
end
end
end
class SixtySixDeck < Deck
def deal
dealt_cards, counter = [], 0
while counter < 6
dealt_cards << draw_top_card
counter += 1
end
PlayerSixtySixDeck.new(dealt_cards)
end
def basic_deck
array_of_cards, count = [], -1
while (count += 1) < 6
array_of_cards << BeloteCard.new(number_to_rank(count % 8 + 9),:spades)
array_of_cards << BeloteCard.new(number_to_rank(count % 8 + 9),:diamonds)
array_of_cards << BeloteCard.new(number_to_rank(count % 8 + 9),:hearts)
array_of_cards << BeloteCard.new(number_to_rank(count % 8 + 9),:clubs)
end
array_of_cards
end
def initialize(array_of_cards = basic_deck)
counter = 0
while counter < array_of_cards.size
array_of_cards[counter] = array_of_cards[counter].to_belote
counter += 1
end
@cards = array_of_cards.dup
end
end
class PlayerSixtySixDeck < SixtySixDeck
def forty?(trump_suit)
has_queen = @cards.any? {|card| card == Card.new(:queen,trump_suit)}
has_king = @cards.any? {|card| card == Card.new(:king,trump_suit)}
has_king and has_queen
end
def twenty?(trump_suit)
first_suit, second_suit = false, false
third_suit, forth_suit = false, false
first_suit = (forty?(:spades) and :spades != trump_suit)
second_suit = (forty?(:diamonds) and :diamonds != trump_suit)
third_suit = (forty?(:hearts) and :hearts != trump_suit)
forth_suit = (forty?(:clubs) and :clubs != trump_suit)
first_suit or second_suit or third_suit or forth_suit
end
end

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

......FFFF.........FFFF........F.F.F..........FFFF.......

Failures:

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

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

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

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

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

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

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

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

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

  10) 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-1l2y14d/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 #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-1l2y14d/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)>'

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

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

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

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

Finished in 0.04918 seconds
57 examples, 15 failures

Failed examples:

rspec /tmp/d20151112-27349-1l2y14d/spec.rb:28 # WarDeck behaves like a deck #draw_top_card pops the top-most card
rspec /tmp/d20151112-27349-1l2y14d/spec.rb:35 # WarDeck behaves like a deck #draw_bottom_card pops the bottom-most card
rspec /tmp/d20151112-27349-1l2y14d/spec.rb:42 # WarDeck behaves like a deck #top peeks at the top-most card
rspec /tmp/d20151112-27349-1l2y14d/spec.rb:49 # WarDeck behaves like a deck #bottom peeks at the bottom-most card
rspec /tmp/d20151112-27349-1l2y14d/spec.rb:28 # BeloteDeck behaves like a deck #draw_top_card pops the top-most card
rspec /tmp/d20151112-27349-1l2y14d/spec.rb:35 # BeloteDeck behaves like a deck #draw_bottom_card pops the bottom-most card
rspec /tmp/d20151112-27349-1l2y14d/spec.rb:42 # BeloteDeck behaves like a deck #top peeks at the top-most card
rspec /tmp/d20151112-27349-1l2y14d/spec.rb:49 # BeloteDeck behaves like a deck #bottom peeks at the bottom-most card
rspec /tmp/d20151112-27349-1l2y14d/spec.rb:287 # BeloteDeck hand #tierce? with tierce returns true for cards with numbers
rspec /tmp/d20151112-27349-1l2y14d/spec.rb:322 # BeloteDeck hand #quarte? detects four cards with increasing ranks
rspec /tmp/d20151112-27349-1l2y14d/spec.rb:354 # BeloteDeck hand #quint? detects five cards with increasing ranks
rspec /tmp/d20151112-27349-1l2y14d/spec.rb:28 # SixtySixDeck behaves like a deck #draw_top_card pops the top-most card
rspec /tmp/d20151112-27349-1l2y14d/spec.rb:35 # SixtySixDeck behaves like a deck #draw_bottom_card pops the bottom-most card
rspec /tmp/d20151112-27349-1l2y14d/spec.rb:42 # SixtySixDeck behaves like a deck #top peeks at the top-most card
rspec /tmp/d20151112-27349-1l2y14d/spec.rb:49 # SixtySixDeck behaves like a deck #bottom peeks at the bottom-most card

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

Виктор обнови решението на 10.11.2015 19:47 (преди над 8 години)

+class Card
+ def initialize(rank, suit)
+ @rank = rank
+ @suit = suit
+ end
+ attr_reader :rank
+ attr_reader :suit
+ def rank_to_number
+ case @rank
+ when :ace then 14
+ when :king then 13
+ when :queen then 12
+ when :jack then 11
+ else @rank
+ end
+ end
+ def to_s
+ if rank_to_number > 10
+ @rank.capitalize.to_s + " of " + @suit.capitalize.to_s
+ else @rank.to_s + " of " + @suit.capitalize.to_s
+ end
+ end
+ def ==(card)
+ @rank == card.rank and @suit == card.suit
+ end
+ def <=>(card)
+ if @suit.to_s[0].ord != card.suit.to_s[0].ord
+ @suit.to_s[0].ord <=> card.suit.to_s[0].ord
+ else rank_to_number <=> card.rank_to_number
+ end
+ end
+ def to_belote
+ BeloteCard.new(@rank, @suit)
+ end
+end
+
+class BeloteCard < Card
+ def rank_to_number
+ case @rank
+ when :ace then 14
+ when :king then 12
+ when :queen then 11
+ when :jack then 10
+ when 10 then 13
+ else @rank
+ end
+ end
+end
+
+class Deck
+ include Enumerable
+ def basic_deck
+ array_of_cards = []
+ count = 1
+ while count <= 52
+ card = Card.new(number_to_rank(count % 13 + 2),number_to_suit(count % 4))
+ array_of_cards << card
+ count += 1
+ end
+ array_of_cards
+ end
+ def initialize(array_of_cards = basic_deck)
+ @cards = array_of_cards.dup
+ end
+ def each
+ counter = 0
+ while counter < @cards.size
+ yield @cards[counter]
+ counter += 1
+ end
+ end
+ def size
+ @cards.size
+ end
+ def draw_top_card
+ @cards.pop
+ end
+ def draw_bottom_card
+ @cards.shift
+ end
+ def top_card
+ @cards[-1]
+ end
+ def bottom_card
+ @cards[0]
+ end
+ def to_s
+ counter = 0
+ string = ""
+ while counter < @cards.size
+ string = string + @cards[counter].to_s + "\n"
+ counter += 1
+ end
+ string
+ end
+ def sort
+ @cards.sort!.reverse!
+ end
+ def shuffle
+ @cards.shuffle!
+ end
+ def number_to_rank(number)
+ case number
+ when 14 then :ace
+ when 13 then :king
+ when 12 then :queen
+ when 11 then :jack
+ else number
+ end
+ end
+ def number_to_suit(number)
+ case number
+ when 3 then :spades
+ when 2 then :hearts
+ when 1 then :diamonds
+ else :clubs
+ end
+end
+end
+
+class WarDeck < Deck
+ def deal
+ dealt_cards, counter = [], 0
+ while counter < 26
+ dealt_cards << draw_top_card
+ counter += 1
+ end
+ PlayerWarDeck.new(dealt_cards)
+ end
+end
+
+class PlayerWarDeck < Deck
+ def play_card
+ draw_bottom_card
+ end
+ def allow_face_up?
+ size <= 3
+ end
+end
+
+class BeloteDeck < Deck
+ def basic_deck
+ array_of_cards, count = [], -1
+ while (count += 1) < 8
+ array_of_cards << BeloteCard.new(number_to_rank(count % 8 + 7),:spades)
+ array_of_cards << BeloteCard.new(number_to_rank(count % 8 + 7),:diamonds)
+ array_of_cards << BeloteCard.new(number_to_rank(count % 8 + 7),:hearts)
+ array_of_cards << BeloteCard.new(number_to_rank(count % 8 + 7),:clubs)
+ end
+ array_of_cards
+ end
+ def initialize(array_of_cards = basic_deck)
+ counter = 0
+ while counter < array_of_cards.size
+ array_of_cards[counter] = array_of_cards[counter].to_belote
+ counter += 1
+ end
+ @cards = array_of_cards.dup
+ end
+ def deal
+ dealt_cards, counter = [], 0
+ while counter < 8
+ dealt_cards << draw_top_card
+ counter += 1
+ end
+ PlayerBeloteDeck.new(dealt_cards)
+ end
+ def sort
+ @cards.sort!.reverse!
+ end
+end
+
+class PlayerBeloteDeck < Deck
+ def carre_of_jacks?
+ @cards.select{|card| card.rank == :jack}.size == 4
+ end
+ def carre_of_nines?
+ @cards.select{|card| card.rank == 9}.size == 4
+ end
+ def carre_of_aces?
+ @cards.select{|card| card.rank == :ace}.size == 4
+ end
+ def highest_of_suit(suit)
+ @cards.select{|card| card.suit == suit}.max
+ end
+ def belote_from_suit(suit)
+ has_queen = @cards.any? {|card| card == Card.new(:queen,suit)}
+ has_king = @cards.any? {|card| card == Card.new(:king,suit)}
+ has_king and has_queen
+ end
+ def belote?
+ minor_belote = (belote_from_suit(:clubs) or belote_from_suit(:diamonds))
+ major_belote = (belote_from_suit(:hearts) or belote_from_suit(:spades))
+ minor_belote or major_belote
+ end
+ def max_sequence(suit)
+ cards = @cards.select{|card| card.suit == suit}
+ max_count, count, counter = 0, 1, 0
+ while counter < cards.size - 1
+ if (cards[counter].rank_to_number - 1) == cards[counter + 1].rank_to_number
+ count, counter = count + 1, counter + 1
+ else max_count, count, counter = [max_count, count].max, 1, counter + 1
+ end
+ end
+ max_count
+ end
+ def tierce?
+ self.sort
+ if (max_sequence(:spades) >= 3) or (max_sequence(:diamonds) >= 3)
+ true
+ elsif (max_sequence(:clubs) >= 3) or (max_sequence(:hearts) >= 3)
+ true
+ else false
+ end
+ end
+ def quarte?
+ self.sort
+ if (max_sequence(:spades) >= 4) or (max_sequence(:diamonds) >= 4)
+ true
+ elsif (max_sequence(:clubs) >= 4) or (max_sequence(:hearts) >= 4)
+ true
+ else false
+ end
+ end
+ def quint?
+ self.sort
+ if (max_sequence(:spades) >= 5) or (max_sequence(:diamonds) >= 5)
+ true
+ elsif (max_sequence(:clubs) >= 5) or (max_sequence(:hearts) >= 5)
+ true
+ else false
+ end
+ end
+end
+
+class SixtySixDeck < Deck
+ def deal
+ dealt_cards, counter = [], 0
+ while counter < 6
+ dealt_cards << draw_top_card
+ counter += 1
+ end
+ PlayerSixtySixDeck.new(dealt_cards)
+ end
+ def basic_deck
+ array_of_cards, count = [], -1
+ while (count += 1) < 6
+ array_of_cards << BeloteCard.new(number_to_rank(count % 8 + 9),:spades)
+ array_of_cards << BeloteCard.new(number_to_rank(count % 8 + 9),:diamonds)
+ array_of_cards << BeloteCard.new(number_to_rank(count % 8 + 9),:hearts)
+ array_of_cards << BeloteCard.new(number_to_rank(count % 8 + 9),:clubs)
+ end
+ array_of_cards
+ end
+ def initialize(array_of_cards = basic_deck)
+ counter = 0
+ while counter < array_of_cards.size
+ array_of_cards[counter] = array_of_cards[counter].to_belote
+ counter += 1
+ end
+ @cards = array_of_cards.dup
+ end
+end
+
+class PlayerSixtySixDeck < Deck
+ def forty?(trump_suit)
+ has_queen = @cards.any? {|card| card == Card.new(:queen,trump_suit)}
+ has_king = @cards.any? {|card| card == Card.new(:king,trump_suit)}
+ has_king and has_queen
+ end
+ def twenty?(trump_suit)
+ first_suit, second_suit = false, false
+ third_suit, forth_suit = false, false
+ first_suit = (forty?(:spades) and :spades != trump_suit)
+ second_suit = (forty?(:diamonds) and :diamonds != trump_suit)
+ third_suit = (forty?(:hearts) and :hearts != trump_suit)
+ forth_suit = (forty?(:clubs) and :clubs != trump_suit)
+ first_suit or second_suit or third_suit or forth_suit
+ end
+end

Виктор обнови решението на 10.11.2015 20:53 (преди над 8 години)

class Card
def initialize(rank, suit)
@rank = rank
@suit = suit
end
attr_reader :rank
attr_reader :suit
def rank_to_number
case @rank
when :ace then 14
when :king then 13
when :queen then 12
when :jack then 11
else @rank
end
end
def to_s
if rank_to_number > 10
@rank.capitalize.to_s + " of " + @suit.capitalize.to_s
else @rank.to_s + " of " + @suit.capitalize.to_s
end
end
def ==(card)
@rank == card.rank and @suit == card.suit
end
def <=>(card)
if @suit.to_s[0].ord != card.suit.to_s[0].ord
@suit.to_s[0].ord <=> card.suit.to_s[0].ord
else rank_to_number <=> card.rank_to_number
end
end
def to_belote
BeloteCard.new(@rank, @suit)
end
end
class BeloteCard < Card
def rank_to_number
case @rank
when :ace then 14
when :king then 12
when :queen then 11
when :jack then 10
when 10 then 13
else @rank
end
end
end
class Deck
include Enumerable
def basic_deck
array_of_cards = []
count = 1
while count <= 52
card = Card.new(number_to_rank(count % 13 + 2),number_to_suit(count % 4))
array_of_cards << card
count += 1
end
array_of_cards
end
def initialize(array_of_cards = basic_deck)
@cards = array_of_cards.dup
end
def each
counter = 0
while counter < @cards.size
yield @cards[counter]
counter += 1
end
end
def size
@cards.size
end
def draw_top_card
@cards.pop
end
def draw_bottom_card
@cards.shift
end
def top_card
@cards[-1]
end
def bottom_card
@cards[0]
end
def to_s
counter = 0
string = ""
while counter < @cards.size
string = string + @cards[counter].to_s + "\n"
counter += 1
end
string
end
def sort
@cards.sort!.reverse!
end
def shuffle
@cards.shuffle!
end
def number_to_rank(number)
case number
when 14 then :ace
when 13 then :king
when 12 then :queen
when 11 then :jack
else number
end
end
def number_to_suit(number)
case number
when 3 then :spades
when 2 then :hearts
when 1 then :diamonds
else :clubs
end
end
end
class WarDeck < Deck
def deal
dealt_cards, counter = [], 0
while counter < 26
dealt_cards << draw_top_card
counter += 1
end
PlayerWarDeck.new(dealt_cards)
end
end
class PlayerWarDeck < Deck
def play_card
draw_bottom_card
end
def allow_face_up?
size <= 3
end
end
class BeloteDeck < Deck
def basic_deck
array_of_cards, count = [], -1
while (count += 1) < 8
array_of_cards << BeloteCard.new(number_to_rank(count % 8 + 7),:spades)
array_of_cards << BeloteCard.new(number_to_rank(count % 8 + 7),:diamonds)
array_of_cards << BeloteCard.new(number_to_rank(count % 8 + 7),:hearts)
array_of_cards << BeloteCard.new(number_to_rank(count % 8 + 7),:clubs)
end
array_of_cards
end
def initialize(array_of_cards = basic_deck)
counter = 0
while counter < array_of_cards.size
array_of_cards[counter] = array_of_cards[counter].to_belote
counter += 1
end
@cards = array_of_cards.dup
end
def deal
dealt_cards, counter = [], 0
while counter < 8
dealt_cards << draw_top_card
counter += 1
end
PlayerBeloteDeck.new(dealt_cards)
end
def sort
@cards.sort!.reverse!
end
end
-class PlayerBeloteDeck < Deck
+class PlayerBeloteDeck < BeloteDeck
def carre_of_jacks?
@cards.select{|card| card.rank == :jack}.size == 4
end
def carre_of_nines?
@cards.select{|card| card.rank == 9}.size == 4
end
def carre_of_aces?
@cards.select{|card| card.rank == :ace}.size == 4
end
def highest_of_suit(suit)
@cards.select{|card| card.suit == suit}.max
end
def belote_from_suit(suit)
has_queen = @cards.any? {|card| card == Card.new(:queen,suit)}
has_king = @cards.any? {|card| card == Card.new(:king,suit)}
has_king and has_queen
end
def belote?
minor_belote = (belote_from_suit(:clubs) or belote_from_suit(:diamonds))
major_belote = (belote_from_suit(:hearts) or belote_from_suit(:spades))
minor_belote or major_belote
end
def max_sequence(suit)
cards = @cards.select{|card| card.suit == suit}
max_count, count, counter = 0, 1, 0
while counter < cards.size - 1
if (cards[counter].rank_to_number - 1) == cards[counter + 1].rank_to_number
count, counter = count + 1, counter + 1
else max_count, count, counter = [max_count, count].max, 1, counter + 1
end
end
max_count
end
def tierce?
self.sort
if (max_sequence(:spades) >= 3) or (max_sequence(:diamonds) >= 3)
true
elsif (max_sequence(:clubs) >= 3) or (max_sequence(:hearts) >= 3)
true
else false
end
end
def quarte?
self.sort
if (max_sequence(:spades) >= 4) or (max_sequence(:diamonds) >= 4)
true
elsif (max_sequence(:clubs) >= 4) or (max_sequence(:hearts) >= 4)
true
else false
end
end
def quint?
self.sort
if (max_sequence(:spades) >= 5) or (max_sequence(:diamonds) >= 5)
true
elsif (max_sequence(:clubs) >= 5) or (max_sequence(:hearts) >= 5)
true
else false
end
end
end
class SixtySixDeck < Deck
def deal
dealt_cards, counter = [], 0
while counter < 6
dealt_cards << draw_top_card
counter += 1
end
PlayerSixtySixDeck.new(dealt_cards)
end
def basic_deck
array_of_cards, count = [], -1
while (count += 1) < 6
array_of_cards << BeloteCard.new(number_to_rank(count % 8 + 9),:spades)
array_of_cards << BeloteCard.new(number_to_rank(count % 8 + 9),:diamonds)
array_of_cards << BeloteCard.new(number_to_rank(count % 8 + 9),:hearts)
array_of_cards << BeloteCard.new(number_to_rank(count % 8 + 9),:clubs)
end
array_of_cards
end
def initialize(array_of_cards = basic_deck)
counter = 0
while counter < array_of_cards.size
array_of_cards[counter] = array_of_cards[counter].to_belote
counter += 1
end
@cards = array_of_cards.dup
end
end
-class PlayerSixtySixDeck < Deck
+class PlayerSixtySixDeck < SixtySixDeck
def forty?(trump_suit)
has_queen = @cards.any? {|card| card == Card.new(:queen,trump_suit)}
has_king = @cards.any? {|card| card == Card.new(:king,trump_suit)}
has_king and has_queen
end
def twenty?(trump_suit)
first_suit, second_suit = false, false
third_suit, forth_suit = false, false
first_suit = (forty?(:spades) and :spades != trump_suit)
second_suit = (forty?(:diamonds) and :diamonds != trump_suit)
third_suit = (forty?(:hearts) and :hearts != trump_suit)
forth_suit = (forty?(:clubs) and :clubs != trump_suit)
first_suit or second_suit or third_suit or forth_suit
end
end