Skip to content

Commit

Permalink
refactor: rename legal_moves to legal_target_positions
Browse files Browse the repository at this point in the history
  • Loading branch information
Roland Studer committed Dec 25, 2021
1 parent 600c79f commit 39d9f3f
Show file tree
Hide file tree
Showing 17 changed files with 36 additions and 36 deletions.
2 changes: 1 addition & 1 deletion lib/board.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def place(piece, position)
def legal_target_positions_for(position)
piece = get(position).piece
piece.move_types.map do |type|
type.new(self, position).legal_moves
type.new(self, position).legal_target_positions
end.flatten
end

Expand Down
4 changes: 2 additions & 2 deletions lib/move/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def initialize(board, position)

private

def legal_moves_in_line(positions)
def legal_target_positions_in_line(positions)
legals = []
positions.each do |target_position|
break if occupied_by_friend?(target_position)
Expand All @@ -38,7 +38,7 @@ def occupied_by_enemy?(target_position)
other_piece && other_piece.color != piece.color
end

def without_illegal_moves(positions)
def without_illegal_target_positions(positions)
positions -= target_positions_that_are_occupied_by_friend(positions)
positions
end
Expand Down
4 changes: 2 additions & 2 deletions lib/move/diagonal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
module Move
# unlimited horizontal movement for a piece
class Diagonal < Base
def legal_moves
def legal_target_positions
[
position.positions_up_left,
position.positions_up_right,
position.positions_down_left,
position.positions_down_right
].map do |positions|
legal_moves_in_line(positions)
legal_target_positions_in_line(positions)
end.flatten
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/move/horizontal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
module Move
# unlimited horizontal movement for a piece
class Horizontal < Base
def legal_moves
legal_moves_in_line(position.positions_to_the_left) +
legal_moves_in_line(position.positions_to_the_right)
def legal_target_positions
legal_target_positions_in_line(position.positions_to_the_left) +
legal_target_positions_in_line(position.positions_to_the_right)
end
end
end
4 changes: 2 additions & 2 deletions lib/move/knight.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
module Move
# knight moves in ls
class Knight < Base
def legal_moves
def legal_target_positions
positions = directions.map do |direction|
position.go(*direction)
end
without_illegal_moves(positions.compact)
without_illegal_target_positions(positions.compact)
end

private
Expand Down
4 changes: 2 additions & 2 deletions lib/move/one_down.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
module Move
# king moves
class OneDown < Base
def legal_moves
without_illegal_moves([position.down])
def legal_target_positions
without_illegal_target_positions([position.down])
end
end
end
4 changes: 2 additions & 2 deletions lib/move/one_into_any_direction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
module Move
# king moves
class OneIntoAnyDirection < Base
def legal_moves
def legal_target_positions
positions = directions.map do |direction|
position.go(*direction)
end
without_illegal_moves(positions.compact)
without_illegal_target_positions(positions.compact)
end

private
Expand Down
4 changes: 2 additions & 2 deletions lib/move/one_up.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
module Move
# king moves
class OneUp < Base
def legal_moves
without_illegal_moves([position.down])
def legal_target_positions
without_illegal_target_positions([position.down])
end
end
end
2 changes: 1 addition & 1 deletion lib/move/two_down.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Move
# move down two squares if not occupied
class TwoDown < Base
def legal_moves
def legal_target_positions
if no_piece_in_the_way? && target_piece_not_occupied?
[position.down.down]
else
Expand Down
2 changes: 1 addition & 1 deletion lib/move/two_up.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Move
# move down two squares if not occupied
class TwoUp < Base
def legal_moves
def legal_target_positions
if no_piece_in_the_way? && target_piece_not_occupied?
[position.up.up]
else
Expand Down
6 changes: 3 additions & 3 deletions lib/move/vertical.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
module Move
# unlimited vertical movement for a piece
class Vertical < Base
def legal_moves
legal_moves_in_line(position.positions_upwards) +
legal_moves_in_line(position.positions_downwards)
def legal_target_positions
legal_target_positions_in_line(position.positions_upwards) +
legal_target_positions_in_line(position.positions_downwards)
end
end
end
4 changes: 2 additions & 2 deletions test/move/diagonal_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ def test_legal_move_blocked_by_other_piece_to_the_upper_right
board.place(Piece.new(:black), "C1")

move = Move::Diagonal.new(board, "C1")
assert_includes move.legal_moves, Position.parse("A3")
assert_includes move.legal_target_positions, Position.parse("A3")

board.place(Piece.new(:white), "B2")
assert !move.legal_moves.include?(Position.parse("A1"))
assert !move.legal_target_positions.include?(Position.parse("A1"))
end
end
end
10 changes: 5 additions & 5 deletions test/move/horizontal_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ def test_legal_move_blocked_by_other_piece_to_the_left
board.place(Piece.new(:black), "C1")

move = Move::Horizontal.new(board, "C1")
assert_includes move.legal_moves, Position.parse("A1")
assert_includes move.legal_target_positions, Position.parse("A1")

board.place(Piece.new(:white), "B1")
assert !move.legal_moves.include?(Position.parse("A1"))
assert !move.legal_target_positions.include?(Position.parse("A1"))
end

def test_legal_move_blocked_by_enemy_piece_to_the_right
Expand All @@ -21,16 +21,16 @@ def test_legal_move_blocked_by_enemy_piece_to_the_right
move = Move::Horizontal.new(board, "C1")
board.place(Piece.new(:white), "F1")

assert_includes move.legal_moves, Position.parse("F1")
assert !move.legal_moves.include?(Position.parse("H1"))
assert_includes move.legal_target_positions, Position.parse("F1")
assert !move.legal_target_positions.include?(Position.parse("H1"))
end

def test_legal_move_blocked_by_own_piece
board = Board.new
board.place(Rook.new(:black), "C1")
board.place(Piece.new(:black), "B1")
move = Move::Horizontal.new(board, "C1")
assert !move.legal_moves.include?(Position.parse("B1"))
assert !move.legal_target_positions.include?(Position.parse("B1"))
end
end
end
4 changes: 2 additions & 2 deletions test/move/knight_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ def test_knight_move
board.place(Piece.new(:black), "C1")
move = Move::Knight.new(board, "C1")

assert_includes move.legal_moves, Position.parse("D3")
assert_includes move.legal_target_positions, Position.parse("D3")

board.place(Piece.new(:black), "D3")
assert !move.legal_moves.include?(Position.parse("D3"))
assert !move.legal_target_positions.include?(Position.parse("D3"))
end
end
end
4 changes: 2 additions & 2 deletions test/move/one_into_any_direction_text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def test_move_right
board.place(Piece.new(:black), "A1")
move = Move::OneIntoAnyDirection.new(board, "A1")

assert_includes move.legal_moves, Position.parse("B1")
assert !move.legal_moves.include?(Position.parse("C1"))
assert_includes move.legal_target_positions, Position.parse("B1")
assert !move.legal_target_positions.include?(Position.parse("C1"))
end
end
end
4 changes: 2 additions & 2 deletions test/move/two_down_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ def test_move_down
board = Board.new
board.place(Piece.new(:black), "A8")
move = Move::TwoDown.new(board, "A8")
assert_includes move.legal_moves, Position.parse("A6")
assert_includes move.legal_target_positions, Position.parse("A6")

board.place(Piece.new(:black), "A7")
assert !move.legal_moves.include?(Position.parse("A6"))
assert !move.legal_target_positions.include?(Position.parse("A6"))
end
end
end
4 changes: 2 additions & 2 deletions test/move/vertical_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ def test_move_vertical
board.place(Piece.new(:black), "C1")
move = Move::Vertical.new(board, "C1")

assert_includes move.legal_moves, Position.parse("C8")
assert_includes move.legal_target_positions, Position.parse("C8")

board.place(Piece.new(:black), "C2")
assert !move.legal_moves.include?(Position.parse("C2"))
assert !move.legal_target_positions.include?(Position.parse("C2"))
end
end
end

0 comments on commit 39d9f3f

Please sign in to comment.