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

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

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

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 50 успешни тест(а)
  • 7 неуспешни тест(а)

Код

class Card
attr_reader :rank
attr_reader :suit
def initialize(rank, suit)
@rank = rank
@suit = suit
end
def to_s
rank.to_s.capitalize + " of " + suit.to_s.capitalize
end
def ==(other)
rank == other.rank and suit == other.suit
end
end
class Deck
include Enumerable
SUITS = [:spades, :hearts, :diamonds, :clubs]
def generate_all_cards(ranks)
ranks.product(SUITS).collect { |x, y| Card.new(x, y) }
end
def initialize(cards = [])
@ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
@ranks_ascending = Hash[(0...@ranks.size).zip @ranks]
cards.empty? ? @cards = generate_all_cards(@ranks) : @cards = cards
end
def each
@cards.each { |card| yield card }
end
def size
@cards.size
end
def draw_top_card
@cards.delete_at(0)
end
def draw_bottom_card
@cards.delete_at(-1)
end
def top_card
@cards.first
end
def bottom_card
@cards.last
end
def shuffle
@cards.shuffle!
end
def sort
@cards.sort_by! { |x| [x.suit, @ranks_ascending.key(x.rank)] }.reverse!
end
def to_s
@cards.map { |card| card.to_s + "\n" }.reduce(&:+)
end
def deal
Hand.new(@cards, @ranks_ascending)
end
end
class Hand
def initialize(cards, ranks_ascending)
@cards_hand = cards
@ranks_ascending = ranks_ascending
end
def size
@cards_hand.size
end
def to_s
@cards_hand.map { |card| card.to_s + "\n" }.reduce(&:+)
end
end
class WarDeck < Deck
DECK_SIZE = 52
CARDS_IN_HAND = 26
def deal
cards_hand = @cards[0..CARDS_IN_HAND - 1]
@cards = @cards - cards_hand
WarDeckHand.new(cards_hand, @ranks_ascending)
end
end
class WarDeckHand < Hand
def play_card
@cards_hand.delete(@cards_hand.sample)
end
def allow_face_up?
size <= 3 ? true : false
end
end
class BeloteDeck < Deck
DECK_SIZE = 32
CARDS_IN_HAND = 8
def initialize(cards = [])
@ranks = [7, 8, 9, :jack, :queen, :king, 10, :ace]
@ranks_ascending = Hash[(0...@ranks.size).zip @ranks]
cards.empty? ? @cards = generate_all_cards(@ranks) : @cards = cards
end
def deal
cards_hand = @cards[0..CARDS_IN_HAND - 1]
@cards = @cards - cards_hand
BeloteDeckHand.new(cards_hand, @ranks_ascending)
end
end
class BeloteDeckHand < Hand
def get_rank_key(card)
@ranks_ascending.key(card.rank)
end
def highest_of_suit(suit)
@cards_hand.select { |x| x.suit == suit }.
max_by { |x| get_rank_key(x) }
end
def belote?
pairs = [:spades, :hearts, :diamonds, :clubs].product([:king, :queen]).
map { |s, r| Card.new(r, s) }
cards_hand_combinations = @cards_hand.combination(2).to_a
cards_hand_combinations.include?([pairs[0], pairs[1]]) or
cards_hand_combinations.include?([pairs[2], pairs[3]]) or
cards_hand_combinations.include?([pairs[4], pairs[5]]) or
cards_hand_combinations.include?([pairs[6], pairs[7]])
end
def sort
@cards_hand.sort_by { |x| [x.suit, get_rank_key(x)] }.reverse
end
def are_n_numbers_consecutive(n, x)
if x.all? { |a| a.suit == x[0].suit }
are_consecutive = true
return x.each_cons(2).all? { |x,y| ((get_rank_key(x) -
get_rank_key(y)) == 1) }
end
false
end
def tierce?
sort.each_cons(3).each do |x|
result = are_n_numbers_consecutive(3, x)
if result then return true end
end
false
end
def quarte?
sort.each_cons(4).each do |x|
result = are_n_numbers_consecutive(4, x)
if result then return true end
end
false
end
def quint?
sort.each_cons(5).each do |x|
result = are_n_numbers_consecutive(5, x)
if result then return true end
end
false
end
def carre_of_jacks?
jacks = [:spades, :hearts, :diamonds, :clubs].product([:jack])
@cards_hand.include?(jacks[0]) and @cards_hand.include?(jacks[1]) and
@cards_hand.include?(jacks[2]) and @cards_hand.include?(jacks[3])
end
def carre_of_nines?
nines = [:spades, :hearts, :diamonds, :clubs].product([9])
@cards_hand.include?(nines[0]) and @cards_hand.include?(nines[1]) and
@cards_hand.include?(nines[2]) and @cards_hand.include?(nines[3])
end
def carre_of_aces?
aces = [:spades, :hearts, :diamonds, :clubs].product([:aces])
@cards_hand.include?(aces[0]) and @cards_hand.include?(aces[1]) and
@cards_hand.include?(aces[2]) and @cards_hand.include?(aces[3])
end
end
class SixtySixDeck < Deck
DECK_SIZE = 24
CARDS_IN_HAND = 6
def initialize(cards = [])
@ranks = [9, :jack, :queen, :king, 10, :ace]
@ranks_ascending = Hash[(0...@ranks.size).zip @ranks]
cards.empty? ? @cards = generate_all_cards(@ranks) : @cards = cards
end
def deal
cards_hand = @cards[0..CARDS_IN_HAND - 1]
@cards = @cards - cards_hand
SixtySixDeckHand.new(cards_hand, @ranks_ascending)
end
end
class SixtySixDeckHand < Hand
def twenty?(trump_suit)
temp = [:spades, :hearts, :diamonds, :clubs] - [trump_suit]
pairs = temp.product([:queen, :king]).map { |s, r| Card.new(r, s) }
cards_hand_combinations = @cards_hand.combination(2).to_a
cards_hand_combinations.include?([pairs[0], pairs[1]]) or
cards_hand_combinations.include?([pairs[2], pairs[3]]) or
cards_hand_combinations.include?([pairs[4], pairs[5]])
end
def forty?(trump_suit)
@cards_hand.combination(2).to_a.include?(
[Card.new(:queen, trump_suit), Card.new(:king, trump_suit)])
end
end

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

............................F........FFFFFF..............

Failures:

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

  2) 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 `rank' for [:spades, :jack]:Array
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1nfpn6z/spec.rb:386
     # /tmp/d20151112-27349-1nfpn6z/solution.rb:15:in `=='
     # /tmp/d20151112-27349-1nfpn6z/solution.rb:188:in `include?'
     # /tmp/d20151112-27349-1nfpn6z/solution.rb:188:in `carre_of_jacks?'
     # /tmp/d20151112-27349-1nfpn6z/spec.rb:86:in `public_send'
     # /tmp/d20151112-27349-1nfpn6z/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)>'

  3) 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 `rank' for [:spades, :jack]:Array
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1nfpn6z/spec.rb:386
     # /tmp/d20151112-27349-1nfpn6z/solution.rb:15:in `=='
     # /tmp/d20151112-27349-1nfpn6z/solution.rb:188:in `include?'
     # /tmp/d20151112-27349-1nfpn6z/solution.rb:188:in `carre_of_jacks?'
     # /tmp/d20151112-27349-1nfpn6z/spec.rb:101:in `public_send'
     # /tmp/d20151112-27349-1nfpn6z/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)>'

  4) 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 `rank' for [:spades, 9]:Array
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1nfpn6z/spec.rb:390
     # /tmp/d20151112-27349-1nfpn6z/solution.rb:15:in `=='
     # /tmp/d20151112-27349-1nfpn6z/solution.rb:194:in `include?'
     # /tmp/d20151112-27349-1nfpn6z/solution.rb:194:in `carre_of_nines?'
     # /tmp/d20151112-27349-1nfpn6z/spec.rb:86:in `public_send'
     # /tmp/d20151112-27349-1nfpn6z/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)>'

  5) 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 `rank' for [:spades, 9]:Array
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1nfpn6z/spec.rb:390
     # /tmp/d20151112-27349-1nfpn6z/solution.rb:15:in `=='
     # /tmp/d20151112-27349-1nfpn6z/solution.rb:194:in `include?'
     # /tmp/d20151112-27349-1nfpn6z/solution.rb:194:in `carre_of_nines?'
     # /tmp/d20151112-27349-1nfpn6z/spec.rb:101:in `public_send'
     # /tmp/d20151112-27349-1nfpn6z/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)>'

  6) 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 `rank' for [:spades, :aces]:Array
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1nfpn6z/spec.rb:394
     # /tmp/d20151112-27349-1nfpn6z/solution.rb:15:in `=='
     # /tmp/d20151112-27349-1nfpn6z/solution.rb:200:in `include?'
     # /tmp/d20151112-27349-1nfpn6z/solution.rb:200:in `carre_of_aces?'
     # /tmp/d20151112-27349-1nfpn6z/spec.rb:86:in `public_send'
     # /tmp/d20151112-27349-1nfpn6z/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)>'

  7) 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 `rank' for [:spades, :aces]:Array
     Shared Example Group: "carre-checking method" called from /tmp/d20151112-27349-1nfpn6z/spec.rb:394
     # /tmp/d20151112-27349-1nfpn6z/solution.rb:15:in `=='
     # /tmp/d20151112-27349-1nfpn6z/solution.rb:200:in `include?'
     # /tmp/d20151112-27349-1nfpn6z/solution.rb:200:in `carre_of_aces?'
     # /tmp/d20151112-27349-1nfpn6z/spec.rb:101:in `public_send'
     # /tmp/d20151112-27349-1nfpn6z/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)>'

Finished in 0.0382 seconds
57 examples, 7 failures

Failed examples:

rspec /tmp/d20151112-27349-1nfpn6z/spec.rb:239 # BeloteDeck hand #belote? returns true if there is a king and a queen of the same suit
rspec /tmp/d20151112-27349-1nfpn6z/spec.rb:74 # BeloteDeck hand #carre_of_jacks? behaves like carre-checking method returns true when there is a carre
rspec /tmp/d20151112-27349-1nfpn6z/spec.rb:89 # BeloteDeck hand #carre_of_jacks? behaves like carre-checking method returns false when there is no carre
rspec /tmp/d20151112-27349-1nfpn6z/spec.rb:74 # BeloteDeck hand #carre_of_nines? behaves like carre-checking method returns true when there is a carre
rspec /tmp/d20151112-27349-1nfpn6z/spec.rb:89 # BeloteDeck hand #carre_of_nines? behaves like carre-checking method returns false when there is no carre
rspec /tmp/d20151112-27349-1nfpn6z/spec.rb:74 # BeloteDeck hand #carre_of_aces? behaves like carre-checking method returns true when there is a carre
rspec /tmp/d20151112-27349-1nfpn6z/spec.rb:89 # BeloteDeck hand #carre_of_aces? behaves like carre-checking method returns false when there is no carre

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

Петър обнови решението на 11.11.2015 08:46 (преди около 9 години)

+class Card
+ attr_reader :rank
+ attr_reader :suit
+
+ def initialize(rank, suit)
+ @rank = rank
+ @suit = suit
+ end
+
+ def to_s
+ rank.to_s.capitalize + " of " + suit.to_s.capitalize
+ end
+
+ def ==(other)
+ rank == other.rank and suit == other.suit
+ end
+end
+
+class Deck
+ include Enumerable
+
+ SUITS = [:spades, :hearts, :diamonds, :clubs]
+
+ def generate_all_cards(ranks)
+ ranks.product(SUITS).collect { |x, y| Card.new(x, y) }
+ end
+
+ def initialize(cards = [])
+ @ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
+ @ranks_ascending = Hash[(0...@ranks.size).zip @ranks]
+ cards.empty? ? @cards = generate_all_cards(@ranks) : @cards = cards
+ end
+
+ def each
+ @cards.each { |card| yield card }
+ end
+
+ def size
+ @cards.size
+ end
+
+ def draw_top_card
+ @cards.delete_at(0)
+ end
+
+ def draw_bottom_card
+ @cards.delete_at(-1)
+ end
+
+ def top_card
+ @cards.first
+ end
+
+ def bottom_card
+ @cards.last
+ end
+
+ def shuffle
+ @cards.shuffle!
+ end
+
+ def sort
+ @cards.sort_by! { |x| [x.suit, @ranks_ascending.key(x.rank)] }.reverse!
+ end
+
+ def to_s
+ @cards.map { |card| card.to_s + "\n" }.reduce(&:+)
+ end
+
+ def deal
+ Hand.new(@cards, @ranks_ascending)
+ end
+end
+
+class Hand
+ def initialize(cards, ranks_ascending)
+ @cards_hand = cards
+ @ranks_ascending = ranks_ascending
+ end
+
+ def size
+ @cards_hand.size
+ end
+
+ def to_s
+ @cards_hand.map { |card| card.to_s + "\n" }.reduce(&:+)
+ end
+end
+
+class WarDeck < Deck
+ DECK_SIZE = 52
+ CARDS_IN_HAND = 26
+
+ def deal
+ cards_hand = @cards[0..CARDS_IN_HAND - 1]
+ @cards = @cards - cards_hand
+ WarDeckHand.new(cards_hand, @ranks_ascending)
+ end
+end
+
+class WarDeckHand < Hand
+ def play_card
+ @cards_hand.delete(@cards_hand.sample)
+ end
+
+ def allow_face_up?
+ size <= 3 ? true : false
+ end
+end
+
+class BeloteDeck < Deck
+ DECK_SIZE = 32
+ CARDS_IN_HAND = 8
+
+ def initialize(cards = [])
+ @ranks = [7, 8, 9, :jack, :queen, :king, 10, :ace]
+ @ranks_ascending = Hash[(0...@ranks.size).zip @ranks]
+ cards.empty? ? @cards = generate_all_cards(@ranks) : @cards = cards
+ end
+
+ def deal
+ cards_hand = @cards[0..CARDS_IN_HAND - 1]
+ @cards = @cards - cards_hand
+ BeloteDeckHand.new(cards_hand, @ranks_ascending)
+ end
+end
+
+class BeloteDeckHand < Hand
+ def get_rank_key(card)
+ @ranks_ascending.key(card.rank)
+ end
+
+ def highest_of_suit(suit)
+ @cards_hand.select { |x| x.suit == suit }.
+ max_by { |x| get_rank_key(x) }
+ end
+
+ def belote?
+ pairs = [:spades, :hearts, :diamonds, :clubs].product([:king, :queen]).
+ map { |s, r| Card.new(r, s) }
+ cards_hand_combinations = @cards_hand.combination(2).to_a
+
+ cards_hand_combinations.include?([pairs[0], pairs[1]]) or
+ cards_hand_combinations.include?([pairs[2], pairs[3]]) or
+ cards_hand_combinations.include?([pairs[4], pairs[5]]) or
+ cards_hand_combinations.include?([pairs[6], pairs[7]])
+ end
+
+ def sort
+ @cards_hand.sort_by { |x| [x.suit, get_rank_key(x)] }.reverse
+ end
+
+ def are_n_numbers_consecutive(n, x)
+ if x.all? { |a| a.suit == x[0].suit }
+ are_consecutive = true
+ return x.each_cons(2).all? { |x,y| ((get_rank_key(x) -
+ get_rank_key(y)) == 1) }
+ end
+ false
+ end
+
+ def tierce?
+ sort.each_cons(3).each do |x|
+ result = are_n_numbers_consecutive(3, x)
+ if result then return true end
+ end
+ false
+ end
+
+ def quarte?
+ sort.each_cons(4).each do |x|
+ result = are_n_numbers_consecutive(4, x)
+ if result then return true end
+ end
+ false
+ end
+
+ def quint?
+ sort.each_cons(5).each do |x|
+ result = are_n_numbers_consecutive(5, x)
+ if result then return true end
+ end
+ false
+ end
+
+ def carre_of_jacks?
+ jacks = [:spades, :hearts, :diamonds, :clubs].product([:jack])
+ @cards_hand.include?(jacks[0]) and @cards_hand.include?(jacks[1]) and
+ @cards_hand.include?(jacks[2]) and @cards_hand.include?(jacks[3])
+ end
+
+ def carre_of_nines?
+ nines = [:spades, :hearts, :diamonds, :clubs].product([9])
+ @cards_hand.include?(nines[0]) and @cards_hand.include?(nines[1]) and
+ @cards_hand.include?(nines[2]) and @cards_hand.include?(nines[3])
+ end
+
+ def carre_of_aces?
+ aces = [:spades, :hearts, :diamonds, :clubs].product([:aces])
+ @cards_hand.include?(aces[0]) and @cards_hand.include?(aces[1]) and
+ @cards_hand.include?(aces[2]) and @cards_hand.include?(aces[3])
+ end
+end
+
+class SixtySixDeck < Deck
+ DECK_SIZE = 24
+ CARDS_IN_HAND = 6
+
+ def initialize(cards = [])
+ @ranks = [9, :jack, :queen, :king, 10, :ace]
+ @ranks_ascending = Hash[(0...@ranks.size).zip @ranks]
+ cards.empty? ? @cards = generate_all_cards(@ranks) : @cards = cards
+ end
+
+ def deal
+ cards_hand = @cards[0..CARDS_IN_HAND - 1]
+ @cards = @cards - cards_hand
+ SixtySixDeckHand.new(cards_hand, @ranks_ascending)
+ end
+end
+
+class SixtySixDeckHand < Hand
+ def twenty?(trump_suit)
+ temp = [:spades, :hearts, :diamonds, :clubs] - [trump_suit]
+ pairs = temp.product([:king, :queen]).map { |s, r| Card.new(r, s) }
+ cards_hand_combinations = @cards_hand.combination(2).to_a
+
+ cards_hand_combinations.include?([pairs[0], pairs[1]]) or
+ cards_hand_combinations.include?([pairs[2], pairs[3]]) or
+ cards_hand_combinations.include?([pairs[4], pairs[5]])
+ end
+
+ def forty?(trump_suit)
+ @cards_hand.combination(2).to_a.include?(
+ [Card.new(:queen, trump_suit), Card.new(:king, trump_suit)])
+ end
+end

Петър обнови решението на 11.11.2015 08:57 (преди около 9 години)

class Card
attr_reader :rank
attr_reader :suit
def initialize(rank, suit)
@rank = rank
@suit = suit
end
def to_s
rank.to_s.capitalize + " of " + suit.to_s.capitalize
end
def ==(other)
rank == other.rank and suit == other.suit
end
end
class Deck
include Enumerable
SUITS = [:spades, :hearts, :diamonds, :clubs]
def generate_all_cards(ranks)
ranks.product(SUITS).collect { |x, y| Card.new(x, y) }
end
def initialize(cards = [])
@ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
@ranks_ascending = Hash[(0...@ranks.size).zip @ranks]
cards.empty? ? @cards = generate_all_cards(@ranks) : @cards = cards
end
def each
@cards.each { |card| yield card }
end
def size
@cards.size
end
def draw_top_card
@cards.delete_at(0)
end
def draw_bottom_card
@cards.delete_at(-1)
end
def top_card
@cards.first
end
def bottom_card
@cards.last
end
def shuffle
@cards.shuffle!
end
def sort
@cards.sort_by! { |x| [x.suit, @ranks_ascending.key(x.rank)] }.reverse!
end
def to_s
@cards.map { |card| card.to_s + "\n" }.reduce(&:+)
end
def deal
Hand.new(@cards, @ranks_ascending)
end
end
class Hand
def initialize(cards, ranks_ascending)
@cards_hand = cards
@ranks_ascending = ranks_ascending
end
def size
@cards_hand.size
end
def to_s
@cards_hand.map { |card| card.to_s + "\n" }.reduce(&:+)
end
end
class WarDeck < Deck
DECK_SIZE = 52
CARDS_IN_HAND = 26
def deal
cards_hand = @cards[0..CARDS_IN_HAND - 1]
@cards = @cards - cards_hand
WarDeckHand.new(cards_hand, @ranks_ascending)
end
end
class WarDeckHand < Hand
def play_card
@cards_hand.delete(@cards_hand.sample)
end
def allow_face_up?
size <= 3 ? true : false
end
end
class BeloteDeck < Deck
DECK_SIZE = 32
CARDS_IN_HAND = 8
def initialize(cards = [])
@ranks = [7, 8, 9, :jack, :queen, :king, 10, :ace]
@ranks_ascending = Hash[(0...@ranks.size).zip @ranks]
cards.empty? ? @cards = generate_all_cards(@ranks) : @cards = cards
end
def deal
cards_hand = @cards[0..CARDS_IN_HAND - 1]
@cards = @cards - cards_hand
BeloteDeckHand.new(cards_hand, @ranks_ascending)
end
end
class BeloteDeckHand < Hand
def get_rank_key(card)
@ranks_ascending.key(card.rank)
end
def highest_of_suit(suit)
@cards_hand.select { |x| x.suit == suit }.
max_by { |x| get_rank_key(x) }
end
def belote?
pairs = [:spades, :hearts, :diamonds, :clubs].product([:king, :queen]).
map { |s, r| Card.new(r, s) }
cards_hand_combinations = @cards_hand.combination(2).to_a
cards_hand_combinations.include?([pairs[0], pairs[1]]) or
cards_hand_combinations.include?([pairs[2], pairs[3]]) or
cards_hand_combinations.include?([pairs[4], pairs[5]]) or
cards_hand_combinations.include?([pairs[6], pairs[7]])
end
def sort
@cards_hand.sort_by { |x| [x.suit, get_rank_key(x)] }.reverse
end
def are_n_numbers_consecutive(n, x)
if x.all? { |a| a.suit == x[0].suit }
are_consecutive = true
return x.each_cons(2).all? { |x,y| ((get_rank_key(x) -
get_rank_key(y)) == 1) }
end
false
end
def tierce?
sort.each_cons(3).each do |x|
result = are_n_numbers_consecutive(3, x)
if result then return true end
end
false
end
def quarte?
sort.each_cons(4).each do |x|
result = are_n_numbers_consecutive(4, x)
if result then return true end
end
false
end
def quint?
sort.each_cons(5).each do |x|
result = are_n_numbers_consecutive(5, x)
if result then return true end
end
false
end
def carre_of_jacks?
jacks = [:spades, :hearts, :diamonds, :clubs].product([:jack])
@cards_hand.include?(jacks[0]) and @cards_hand.include?(jacks[1]) and
@cards_hand.include?(jacks[2]) and @cards_hand.include?(jacks[3])
end
def carre_of_nines?
nines = [:spades, :hearts, :diamonds, :clubs].product([9])
@cards_hand.include?(nines[0]) and @cards_hand.include?(nines[1]) and
@cards_hand.include?(nines[2]) and @cards_hand.include?(nines[3])
end
def carre_of_aces?
aces = [:spades, :hearts, :diamonds, :clubs].product([:aces])
@cards_hand.include?(aces[0]) and @cards_hand.include?(aces[1]) and
@cards_hand.include?(aces[2]) and @cards_hand.include?(aces[3])
end
end
class SixtySixDeck < Deck
DECK_SIZE = 24
CARDS_IN_HAND = 6
def initialize(cards = [])
@ranks = [9, :jack, :queen, :king, 10, :ace]
@ranks_ascending = Hash[(0...@ranks.size).zip @ranks]
cards.empty? ? @cards = generate_all_cards(@ranks) : @cards = cards
end
def deal
cards_hand = @cards[0..CARDS_IN_HAND - 1]
@cards = @cards - cards_hand
SixtySixDeckHand.new(cards_hand, @ranks_ascending)
end
end
class SixtySixDeckHand < Hand
def twenty?(trump_suit)
temp = [:spades, :hearts, :diamonds, :clubs] - [trump_suit]
- pairs = temp.product([:king, :queen]).map { |s, r| Card.new(r, s) }
+ pairs = temp.product([:queen, :king]).map { |s, r| Card.new(r, s) }
cards_hand_combinations = @cards_hand.combination(2).to_a
cards_hand_combinations.include?([pairs[0], pairs[1]]) or
cards_hand_combinations.include?([pairs[2], pairs[3]]) or
cards_hand_combinations.include?([pairs[4], pairs[5]])
end
def forty?(trump_suit)
@cards_hand.combination(2).to_a.include?(
[Card.new(:queen, trump_suit), Card.new(:king, trump_suit)])
end
end