class TwentyFortyEight::Dsl

Dsl

Attributes

game[R]
settings[R]

Public Class Methods

new(game, settings = {}, &block) click to toggle source
# File lib/TwentyFortyEight/dsl.rb, line 7
def initialize(game, settings = {}, &block)
  @callable = block
  @sequence = []
  @settings = settings
  @game     = game
end

Public Instance Methods

apply(game) click to toggle source
# File lib/TwentyFortyEight/dsl.rb, line 14
def apply(game)
  @queue = []
  instance_eval(&@callable)
end
info(sym) click to toggle source
# File lib/TwentyFortyEight/dsl.rb, line 47
def info(sym)
  game.send sym
end
info?(sym) click to toggle source
# File lib/TwentyFortyEight/dsl.rb, line 43
def info?(sym)
  [:won?, :lost?, :changed?, :available, :score, :prev_score].include? sym
end
method_missing(sym, *args, &block) click to toggle source
# File lib/TwentyFortyEight/dsl.rb, line 59
def method_missing(sym, *args, &block)
  return info sym     if info? sym
  return sym          if game.dup.action(sym, insert: false).changed?
end
quit!() click to toggle source
# File lib/TwentyFortyEight/dsl.rb, line 51
def quit!
  game.quit! && :quit
end
respond_to_missing?(sym, *args, &block) click to toggle source
# File lib/TwentyFortyEight/dsl.rb, line 64
def respond_to_missing?(sym, *args, &block)
  true
end
run_sequence() click to toggle source
# File lib/TwentyFortyEight/dsl.rb, line 24
def run_sequence
  return @poss.shift if @poss && @poss.any?

  copy   = @sequence.dup
  sample = game.dup
  @poss  = []

  while (next_move = copy.shift)
    unless sample.move(next_move).changed?
      @poss = nil
      break
    end

    @poss << next_move
  end

  @poss && @poss.shift
end
sequence(*directions) click to toggle source
# File lib/TwentyFortyEight/dsl.rb, line 19
def sequence(*directions)
  @sequence = directions.flatten.map(&:to_sym)
  run_sequence
end