Class: Tak::Move
- Inherits:
-
Object
- Object
- Tak::Move
- 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
-
#move ⇒ Object
readonly
Returns the value of attribute move.
-
#origin ⇒ Object
readonly
Returns the value of attribute origin.
Instance Method Summary collapse
- #coordinates ⇒ Object
-
#initialize(ptn, tak_board, color) ⇒ Move
constructor
A new instance of Move.
- #obstructed? ⇒ Boolean
- #piece ⇒ Object
- #piece_type ⇒ Object
- #type ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
Instance Attribute Details
#move ⇒ Object (readonly)
Returns the value of attribute move.
3 4 5 |
# File 'lib/tak/move.rb', line 3 def move @move end |
#origin ⇒ Object (readonly)
Returns the value of attribute origin.
4 5 6 |
# File 'lib/tak/move.rb', line 4 def origin @origin end |
Instance Method Details
#coordinates ⇒ Object
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
51 52 53 |
# File 'lib/tak/move.rb', line 51 def obstructed? !!coordinates.find { |(x,y)| OBSTRUCTIONS.include?(@board[x][y].last) } end |
#piece ⇒ Object
28 29 30 |
# File 'lib/tak/move.rb', line 28 def piece "#{move.special_piece}#{@color}" end |
#piece_type ⇒ Object
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 |
#type ⇒ Object
16 17 18 |
# File 'lib/tak/move.rb', line 16 def type move.type end |
#valid? ⇒ 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 |