Даниела обнови решението на 11.11.2015 12:59 (преди около 9 години)
+class Card
+ RANKS = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
+ SUITS = [:clubs, :diamonds, :hearts, :spades]
+
+ attr_reader :rank
+ attr_reader :suit
+
+ def initialize(rank, suit)
+ @suit = suit
+ @rank = rank
+ end
+
+ def next
+ rank_index = RANKS.index(@rank)
+ suit_index = SUITS.index(@suit)
+ if @rank == RANKS[-1]
+ Card.new(RANKS[0], SUITS[suit_index + 1] || SUITS[0])
+ else
+ Card.new(RANKS[rank_index + 1], SUITS[suit_index])
+ end
+ end
+
+ def to_s
+ "#{@rank.to_s.capitalize} of #{@suit.to_s.capitalize}"
+ end
+
+ def ==(card)
+ @suit == card.suit && @rank == card.rank
+ end
+
+ def compare_to(card, ranks = RANKS)
+ first_card_suit = SUITS.index(@suit)
+ second_card_suit = SUITS.index(card.suit)
+
+ if first_card_suit < second_card_suit
+ -1
+ elsif first_card_suit > second_card_suit
+ 1
+ else
+ ranks.index(@rank) <=> ranks.index(card.rank)
+ end
+ end
+end
+
+class Deck
+ include Enumerable
+
+ RANKS = Card::RANKS
+ SUITS = Card::SUITS
+ NUMBER_OF_CARDS_IN_HAND = 52
+
+ def initialize(cards = generate_game_deck)
+ @cards = cards.dup
+ end
+
+ def each
+ @cards.each { |card| yield card }
+ end
+
+ def generate_game_deck
+ suits = self.class::SUITS
+ ranks = self.class::RANKS
+ suits.map { |suit| ranks.map { |rank| Card.new(rank, suit) } }.flatten
+ end
+
+ def size
+ @cards.size
+ end
+
+ def draw_top_card
+ @cards.shift
+ end
+
+ def draw_bottom_card
+ @cards.pop
+ end
+
+ def top_card
+ @cards.first
+ end
+
+ def bottom_card
+ @cards.last
+ end
+
+ def shuffle
+ @cards.shuffle!
+ end
+
+ def sort
+ @cards.sort! { |a, b| a.compare_to(b, self.class::RANKS) }.reverse!
+ end
+
+ def to_s
+ @cards.map(&:to_s).join("\n")
+ end
+
+ def deal(deck_type = Deck)
+ cards_left_to_deal = self.class::NUMBER_OF_CARDS_IN_HAND
+ hand = []
+ cards_left_to_deal.times { hand << draw_top_card }
+ deck_type.new(hand)
+ end
+end
+
+class WarDeck < Deck
+ RANKS = Card::RANKS
+ SUITS = Card::SUITS
+ NUMBER_OF_CARDS_IN_HAND = 26
+
+ def deal
+ super (WarDeck)
+ end
+
+ def play_card
+ draw_top_card
+ end
+
+ def allow_face_up?
+ @cards.size <= 3
+ end
+end
+
+class BeloteDeck < Deck
+ RANKS = [7, 8, 9, :jack, :queen, :king, 10, :ace]
+ SUITS = Card::SUITS
+ NUMBER_OF_CARDS_IN_HAND = 8
+
+ def deal
+ super (BeloteDeck)
+ end
+
+ def highest_of_suit(suit)
+ cards_of_suit = @cards.select { |card| card.suit == suit }
+ cards_of_suit.sort { |a, b| a.compare_to(b, self.class::RANKS) }.pop
+ end
+
+ def belote?
+ grouped_by_suit = @cards.group_by(&:suit)
+ sets = grouped_by_suit.values.map { |cards| cards.permutation(2).to_a }
+ sets.flatten!(1)
+ sets.any? { |set| set.all? { |card| [:queen, :king].include?(card.rank) } }
+ end
+
+ def consecutive_cards(amount_cons_cards)
+ cards = @cards.sort { |a, b| a.compare_to(b, self.class::RANKS) }
+ groups = cards.group_by(&:suit).values
+ cons_cards = groups.map { |group| group.each_cons(amount_cons_cards).to_a }
+ cons_cards.flatten!(1)
+ cons_cards.any? { |set| set.each_cons(2).all? { |x, y| y == x.next } }
+ end
+
+ def tierce?
+ consecutive_cards(3)
+ end
+
+ def quarte?
+ consecutive_cards(4)
+ end
+
+ def quint?
+ consecutive_cards(5)
+ end
+
+ def carre(rank)
+ spades = Card.new(rank, :spades)
+ hearts = Card.new(rank, :hearts)
+ diamonds = Card.new(rank, :diamonds)
+ clubs = Card.new(rank, :clubs)
+ @cards.include?(spades && hearts && diamonds && clubs)
+ end
+
+ def carre_of_jacks?
+ carre(:jack)
+ end
+
+ def carre_of_nines?
+ carre(9)
+ end
+
+ def carre_of_aces?
+ carre(:ace)
+ end
+end
+
+class SixtySixDeck < Deck
+ RANKS = [9, :jack, :queen, :king, 10, :ace]
+ SUITS = Card::SUITS
+ NUMBER_OF_CARDS_IN_HAND = 6
+
+ def deal
+ super (SixtySixDeck)
+ end
+
+ def twenty?(trump_suit)
+ grouped_by_suit = @cards.group_by(&:suit)
+ grouped_by_suit.delete(trump_suit)
+ sets = grouped_by_suit.values.map { |cards| cards.permutation(2).to_a }
+ sets.flatten(1).any? { | (a, b) | a.rank == :queen && b.rank == :king }
+ end
+
+ def forty?(trump_suit)
+ cards = @cards.select { |card| card.suit == trump_suit }
+ cards.permutation(2).any? { | (a, b) | a.rank == :queen && b.rank == :king }
+ end
+end
Добре, но прегледай решенията на колеги и нашето примерно решение за алтернативни идеи.