class Tak::Move

Constants

OBSTRUCTIONS

Pieces that get in the way of a road

Attributes

move[R]
origin[R]

Public Class Methods

new(ptn, tak_board, color) click to toggle source
# File lib/tak/move.rb, line 9
def initialize(ptn, tak_board, color)
  @move   = Tak::PTN.new(ptn, tak_board.size)
  @board  = tak_board.board
  @origin = [@move.x, @move.y]
  @color  = color.to_s[0]
end

Public Instance Methods

coordinates() click to toggle source
# File lib/tak/move.rb, line 55
def coordinates
  x, y  = move.position
  times = move.size.times

  return [[x,y]] if move.size.zero?

  case move.direction
  when '+' then times.map { |n| [x,     y + n] }
  when '-' then times.map { |n| [x,     y - n] }
  when '<' then times.map { |n| [x - n, y]     }
  when '>' then times.map { |n| [x + n, y + n] }
  end
end
obstructed?() click to toggle source
# File lib/tak/move.rb, line 51
def obstructed?
  !!coordinates.find { |(x,y)| OBSTRUCTIONS.include?(@board[x][y].last) }
end
piece() click to toggle source
# File lib/tak/move.rb, line 28
def piece
  "#{move.special_piece}#{@color}"
end
piece_type() click to toggle source
# File lib/tak/move.rb, line 20
def piece_type
  case piece
  when /C/ then :capstone
  when /S/ then :standing
  else :flatstone
  end
end
type() click to toggle source
# File lib/tak/move.rb, line 16
def type
  move.type
end
valid?() click to toggle source
# File lib/tak/move.rb, line 32
def valid?
  return true unless obstructed?
  return false if coordinates.flatten.any? { |n|
    n > @board.size || n < 0
  }

  x, y      = move.position
  top_piece = @board[x][y].last

  if %w(Cw Cb).include?(top_piece)
    x2, y2      = coordinates.last
    obstruction = @board[x2][y2].last

    %w(Sw Sb).include?(obstruction)
  else
    false
  end
end