class SolitairePlay::Freecell
Constants
- FREECELL_RANGE
- GAME_ID
- GAME_TITLE
- HOME_RANGE
- PILE_COUNT
- PILE_LABELS
- TABLEAU_RANGE
Public Class Methods
Source
# File lib/solitaire/freecell.rb, line 42 def initialize(game_id: GAME_ID, piles: nil) super(game_id: game_id, piles: piles || Array.new(PILE_COUNT) { [] }) end
Calls superclass method
SolitairePlay::Engine::new
Source
# File lib/solitaire/freecell.rb, line 21 def self.new_game(deck: nil, rand: ->(max) { Random.rand(0...max) }) working_deck = deck ? deck.dup : build_shuffled_deck(rand: rand) piles = Array.new(PILE_COUNT) { [] } TABLEAU_RANGE.each do |index| deal_count = index <= 11 ? 7 : 6 deal_count.times do card = working_deck.shift piles[index] << card end end new(piles: piles) end
Source
# File lib/solitaire/freecell.rb, line 34 def self.parse_pile_label(label) PILE_LABELS[label.to_s.strip.downcase] end
Source
# File lib/solitaire/freecell.rb, line 38 def self.pile_label(index) PILE_LABELS.key(index) end
Public Instance Methods
Source
# File lib/solitaire/freecell.rb, line 69 def board_text(user: nil) heading = "#{Game::STYLE_TITLE}♠ SOLITAIRE — #{GAME_TITLE} ♠#{Game::IRC_RESET}" heading += " #{Game::STYLE_LABEL}│#{Game::IRC_RESET} #{Game::STYLE_LABEL}Player:#{Game::IRC_RESET} " \ "#{Game::STYLE_NICK}#{user}#{Game::IRC_RESET}" if user lines = [heading] lines << format_freecells lines << format_foundations TABLEAU_RANGE.each_slice(4) do |group| lines << group.map { |index| format_tableau(index) }.join(" ") end lines.join("\n") end
Source
# File lib/solitaire/freecell.rb, line 50 def move(from_label, to_label, count: 1) from = self.class.parse_pile_label(from_label) to = self.class.parse_pile_label(to_label) return { error: "Unknown pile label — use fc1-fc4, h1-h4, t1-t8." } if from.nil? || to.nil? return { error: "Cannot move a pile onto itself." } if from == to cards, remaining = take_cards(@piles[from], count) return { error: "No cards to move." } if cards.empty? unless valid_move?(from, to, cards) return { error: "Illegal move." } end @piles[from] = remaining @piles[to] = append_cards(@piles[to], cards) message = won? ? "You win! All cards are in the foundations." : nil { moved: true, message: message, won: won? } end
Source
# File lib/solitaire/freecell.rb, line 46 def won? HOME_RANGE.all? { |index| @piles[index].length == 13 } end