Клара обнови решението на 10.11.2015 15:35 (преди около 9 години)
+class Card
+
+ def initialize(rank, suit)
+ @rank = rank
+ @suit = suit
+ end
+
+ attr_reader :rank, :suit
+
+ def to_s()
+ @rank.to_s.capitalize + " of " + @suit.to_s.capitalize
+ end
+
+ def ==(other_card)
+ if other_card == nil then return false end
+ if @rank == other_card.rank and @suit == other_card.suit
+ return true
+ end
+ false
+ end
+end
+
+class WarDeal
+
+ def initialize(size_of_all_cards, cards_in_hand = [])
+ if size_of_all_cards - 26 >= 0
+ @size = 26
+ else @size = size_of_all_cards end
+ @cards_in_hand = cards_in_hand
+ end
+
+ def size
+ @cards_in_hand.length
+ end
+
+ def draw_top_card(cards)
+ cards.delete(cards.first)
+ end
+
+ def deal(array_of_cards)
+ size = @size
+ while size > 0
+ @cards_in_hand.push(draw_top_card(array_of_cards))
+ size -= 1
+ end
+ WarDeal.new(@size, @cards_in_hand)
+ end
+
+ def play_card
+ @cards_in_hand.delete(@cards_in_hand.sample)
+ end
+
+ def allow_face_up?
+ if @size <= 3 then true else false end
+ end
+end
+
+class Deck
+
+ include Enumerable
+
+ def all_cards
+ ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
+ suits = [:spades, :hearts, :diamonds, :clubs]
+ ranks.flat_map { |rank| suits.map { |suit| Card.new(rank, suit) } }.shuffle
+ end
+
+ def initialize(array_of_cards = all_cards)
+ @ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace].reverse
+ @suits = [:spades, :hearts, :diamonds, :clubs]
+ @array_of_cards = array_of_cards
+ end
+
+ attr_reader :array_of_cards
+
+ def size
+ @array_of_cards.length
+ end
+
+ def draw_top_card
+ @array_of_cards.delete(@array_of_cards.first)
+ end
+
+ def draw_bottom_card
+ @array_of_cards.delete(@array_of_cards.last)
+ end
+
+ def top_card
+ @array_of_cards.first
+ end
+
+ def bottom_card
+ @array_of_cards.last
+ end
+
+ def shuffle
+ @array_of_cards.shuffle!
+ end
+
+ def sort
+ @array_of_cards.sort_by!{ |card|
+ [@suits.index(card.suit), @ranks.index(card.rank)] }
+ end
+
+ def each
+ @array_of_cards.each{ |x| yield x }
+ end
+
+ def to_s
+ @array_of_cards.each{ |x| x.to_s + "\n" }
+ end
+
+ def deal
+ WarDeal.new(size).deal(@array_of_cards)
+ end
+end
+
+class WarDeck < Deck
+
+ def initialize(array_of_cards = all_cards)
+ super
+ end
+end
+
+class BeloteDealHelper
+
+ def initialize(cards_in_hand)
+ @ranks = [7 ,8 ,9 ,:jack ,:queen ,:king, 10 ,:ace].reverse
+ @suits = [:spades, :hearts, :diamonds, :clubs]
+ @cards_in_hand = cards_in_hand
+ end
+
+ def check_if_card_has_some_suit(card, temp_array_of_cards, suit)
+ if card.suit == suit then temp_array_of_cards.push(card) end
+ end
+
+ def check_all_cards_for_some_suit(sorted_cards, suit, temp_array_of_cards)
+ sorted_cards.each{ |card|
+ check_if_card_has_some_suit(card, temp_array_of_cards, suit)
+ }
+ end
+
+ def check_for_sequence_of_cards(sequence_size)
+ sorted_cards = @cards_in_hand.sort_by{ |card|
+ @ranks.index(card.rank) }.reverse
+ array_of_cards_with_same_suit, index = [], 0
+ @suits.each{ |suit| temp_array_of_cards = []
+ check_all_cards_for_some_suit(sorted_cards, suit, temp_array_of_cards)
+ array_of_cards_with_same_suit.push(temp_array_of_cards.sort_by{ |card|
+ @ranks.index(card.rank) }.reverse)
+ }
+ sequence_helper(array_of_cards_with_same_suit, sequence_size)
+ end
+
+ def iterating_one_set_of_cards(step, array_of_cards, temp_arr)
+ while step + 1 <= array_of_cards.length - 1
+ if @ranks.index(array_of_cards.at(step).rank) -
+ @ranks.index(array_of_cards.at(step + 1).rank) == 1
+ temp_arr.push(1)
+ end
+ step += 1
+ end
+ end
+
+ def is_end_of_iteration(temp_arr, size)
+ if temp_arr.length == size - 1 then return true end
+ end
+
+ def iterating_cards(temp_arr, array_of_cards, size, step)
+ if array_of_cards.length >= size
+ iterating_one_set_of_cards(step, array_of_cards, temp_arr)
+ return true if is_end_of_iteration(temp_arr, size)
+ end
+ end
+
+ def sequence_helper(array, size)
+ array.each{ |array_of_cards|
+ step, temp_arr = 0, []
+ return true if iterating_cards(temp_arr, array_of_cards, size, step)
+ }
+ false
+ end
+end
+
+class BeloteDeal
+
+ def initialize(size_of_all_cards, cards_in_hand = [])
+ if size_of_all_cards - 8 >= 0
+ @size = 8
+ else @size = @size_of_all_cards end
+ @cards_in_hand = cards_in_hand
+ @ranks = [7 ,8 ,9 ,:jack ,:queen ,:king, 10 ,:ace].reverse
+ @suits = [:spades, :hearts, :diamonds, :clubs]
+ end
+
+ def size
+ @cards_in_hand.length
+ end
+
+ def draw_top_card(cards)
+ cards.delete(cards.first)
+ end
+
+ def deal(array_of_cards)
+ size = @size
+ while size > 0
+ @cards_in_hand.push(draw_top_card(array_of_cards))
+ size -= 1
+ end
+ BeloteDeal.new(@size, @cards_in_hand)
+ end
+
+ def highest_of_suit(suit)
+ array = @cards_in_hand.sort_by{ |card| [@ranks.index(card.rank)]}
+ array_of_suits = []
+ array.each{ | x | if x.suit == suit then array_of_suits.push(x) end }
+ array_of_suits.first
+ end
+
+ def belote?
+ @suits.each{ |suit| if (@cards_in_hand.include? Card.new(:king,suit) and
+ @cards_in_hand.include? Card.new(:queen,suit)) then return true end }
+ false
+ end
+
+ def tierce?
+ BeloteDealHelper.new(@cards_in_hand).check_for_sequence_of_cards(3)
+ end
+
+ def quarte?
+ BeloteDealHelper.new(@cards_in_hand).check_for_sequence_of_cards(4)
+ end
+
+ def quint?
+ BeloteDealHelper.new(@cards_in_hand).check_for_sequence_of_cards(5)
+ end
+
+ def has_four_cards_of_some_rank(rank)
+ rank_counter = 0
+ @cards_in_hand.each{ |card|
+ if card.rank == rank then rank_counter += 1 end
+ if rank_counter == 4 then return true end
+ }
+ false
+ end
+
+ def carre_of_jacks?
+ has_four_cards_of_some_rank(:jack)
+ end
+
+ def carre_of_nines?
+ has_four_cards_of_some_rank(9)
+ end
+
+ def carre_of_aces?
+ has_four_cards_of_some_rank(:ace)
+ end
+end
+
+class BeloteDeck < Deck
+
+ def all_cards
+ ranks = [7, 8, 9, :jack, :queen, :king, 10, :ace]
+ suits = [:spades, :hearts, :diamonds, :clubs]
+ ranks.flat_map { |rank| suits.map { |suit| Card.new(rank, suit) } }
+ end
+
+ def initialize(array_of_cards = all_cards)
+ @ranks = [7 ,8 ,9 ,:jack ,:queen ,:king, 10 ,:ace]
+ @suits = [:spades, :hearts, :diamonds, :clubs]
+ @array_of_cards = array_of_cards
+ end
+
+ def deal
+ BeloteDeal.new(size).deal(@array_of_cards)
+ end
+end
+
+
+class SixtySixDeal
+
+ def initialize(size_of_all_cards, cards_in_hand = [])
+ if size_of_all_cards - 6 >= 0
+ @size = 6
+ else @size = size_of_all_cards end
+ @cards_in_hand = cards_in_hand
+ @ranks = [9, :jack, :queen, :king, 10, :ace].reverse
+ @suits = [:spades, :hearts, :diamonds, :clubs]
+ end
+
+ def size
+ @cards_in_hand.length
+ end
+
+ def draw_top_card(cards)
+ cards.delete(cards.first)
+ end
+
+ def deal(array_of_cards)
+ size = @size
+ while size > 0
+ @cards_in_hand.push(draw_top_card(array_of_cards))
+ size -= 1
+ if array_of_cards.length == 0 then break end
+ end
+ SixtySixDeal.new(@size, @cards_in_hand)
+ end
+
+ def king_queen_with_same_suit(suits)
+ suits.each{ |suit|
+ if @cards_in_hand.include? Card.new(:queen,suit) and
+ @cards_in_hand.include? Card.new(:king,suit) then return true end
+ }
+ false
+ end
+
+ def twenty?(trump_suit)
+ suits = [:spades, :hearts, :diamonds, :clubs]
+ suits.delete(trump_suit)
+ king_queen_with_same_suit(suits)
+ end
+
+ def forty?(trump_suit)
+ suits = [:spades, :hearts, :diamonds, :clubs]
+ suits.delete(trump_suit)
+ king_queen_with_same_suit(Array(trump_suit))
+ end
+end
+
+class SixtySixDeck < Deck
+
+ def all_cards
+ ranks = [9 ,:jack ,:queen,:king, 10 ,:ace]
+ suits = [:spades, :hearts, :diamonds, :clubs]
+ ranks.flat_map { |rank| suits.map { |suit|
+ Card.new(rank, suit) } }.shuffle
+ end
+
+ def initialize(array_of_cards = all_cards)
+ @ranks = [9 ,:jack ,:queen,:king, 10 ,:ace]
+ @suits = [:spades, :hearts, :diamonds, :clubs]
+ @array_of_cards = array_of_cards
+ end
+
+ def deal
+ SixtySixDeal.new(size).deal(@array_of_cards)
+ end
+end
Имаш проблеми със спазването на конвенциите на места, направи справка с ръководството за стил.
Прегледай решенията на колеги и нашето примерно решение за алтернативни идеи.