Class: Tak::Move

Inherits:
Object
  • Object
show all
Defined in:
lib/tak/move.rb

Constant Summary collapse

OBSTRUCTIONS =

Pieces that get in the way of a road

%w(Cw Cb Sw Sb)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ptn, tak_board, color) ⇒ Move

Returns a new instance of Move.



9
10
11
12
13
14
# 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

Instance Attribute Details

#moveObject (readonly)

Returns the value of attribute move.



3
4
5
# File 'lib/tak/move.rb', line 3

def move
  @move
end

#originObject (readonly)

Returns the value of attribute origin.



4
5
6
# File 'lib/tak/move.rb', line 4

def origin
  @origin
end

Instance Method Details

#coordinatesObject



55
56
57
58
59
60
61
62
63
64
65
66
67
# 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?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/tak/move.rb', line 51

def obstructed?
  !!coordinates.find { |(x,y)| OBSTRUCTIONS.include?(@board[x][y].last) }
end

#pieceObject



28
29
30
# File 'lib/tak/move.rb', line 28

def piece
  "#{move.special_piece}#{@color}"
end

#piece_typeObject



20
21
22
23
24
25
26
# File 'lib/tak/move.rb', line 20

def piece_type
  case piece
  when /C/ then :capstone
  when /S/ then :standing
  else :flatstone
  end
end

#typeObject



16
17
18
# File 'lib/tak/move.rb', line 16

def type
  move.type
end

#valid?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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