class SolitairePlay::Engine
Constants
- GAME_ID
- GAME_TITLE
- RED_SUITS
Attributes
Public Class Methods
Source
# File lib/solitaire/engine.rb, line 14 def initialize(game_id: self.class::GAME_ID, piles: nil) @game_id = game_id @piles = piles end
Source
# File lib/solitaire/engine.rb, line 19 def self.new_game(deck: nil, rand: ->(max) { Random.rand(0...max) }) raise NotImplementedError end
Public Instance Methods
Source
# File lib/solitaire/engine.rb, line 83 def alternating_sequence?(cards) return true if cards.length <= 1 cards.each_cons(2) do |lower, upper| return false unless card_color(lower) != card_color(upper) return false unless rank_value(lower[:rank]) + 1 == rank_value(upper[:rank]) end true end
Source
# File lib/solitaire/engine.rb, line 79 def append_cards(pile, cards) (pile || []) + cards end
Source
# File lib/solitaire/engine.rb, line 31 def board_text(user: nil) raise NotImplementedError end
Source
# File lib/solitaire/engine.rb, line 93 def build_shuffled_deck(rand: ->(max) { Random.rand(0...max) }) Game.shuffle_deck(Game.build_deck, rand: rand) end
Source
# File lib/solitaire/engine.rb, line 45 def card_color(card) RED_SUITS.include?(card[:suit]) ? :red : :black end
Source
# File lib/solitaire/engine.rb, line 53 def compact_card(card) "#{card[:rank]}#{Game::SUIT_SYMBOLS[card[:suit]]}" end
Source
# File lib/solitaire/engine.rb, line 62 def empty_pile?(pile) pile.nil? || pile.empty? end
Source
# File lib/solitaire/engine.rb, line 57 def format_compact_card(card) style = RED_SUITS.include?(card[:suit]) ? Game::STYLE_CARD_RED : Game::STYLE_CARD_BLACK "#{style}#{compact_card(card)}#{Game::IRC_RESET}" end
Source
# File lib/solitaire/engine.rb, line 27 def move(_from_label, _to_label, count: 1) raise NotImplementedError end
Source
# File lib/solitaire/engine.rb, line 49 def opposite_color(color) color == :red ? :black : :red end
Source
# File lib/solitaire/engine.rb, line 35 def rank_value(rank) case rank when "A" then 1 when "K" then 13 when "Q" then 12 when "J" then 11 else rank.to_i end end
Source
# File lib/solitaire/engine.rb, line 70 def take_cards(pile, count) return [[], pile] if empty_pile?(pile) || count <= 0 count = [count, pile.length].min taken = pile[-count..] remaining = pile[0...-count] || [] [taken, remaining] end