From 39d9f3fe2bc82d6e90333c6102f39bf60fe6c7b3 Mon Sep 17 00:00:00 2001 From: Roland Studer Date: Sat, 25 Dec 2021 14:10:33 +0100 Subject: [PATCH] refactor: rename legal_moves to legal_target_positions --- lib/board.rb | 2 +- lib/move/base.rb | 4 ++-- lib/move/diagonal.rb | 4 ++-- lib/move/horizontal.rb | 6 +++--- lib/move/knight.rb | 4 ++-- lib/move/one_down.rb | 4 ++-- lib/move/one_into_any_direction.rb | 4 ++-- lib/move/one_up.rb | 4 ++-- lib/move/two_down.rb | 2 +- lib/move/two_up.rb | 2 +- lib/move/vertical.rb | 6 +++--- test/move/diagonal_test.rb | 4 ++-- test/move/horizontal_test.rb | 10 +++++----- test/move/knight_test.rb | 4 ++-- test/move/one_into_any_direction_text.rb | 4 ++-- test/move/two_down_test.rb | 4 ++-- test/move/vertical_test.rb | 4 ++-- 17 files changed, 36 insertions(+), 36 deletions(-) diff --git a/lib/board.rb b/lib/board.rb index 82a1a86..aa5a390 100644 --- a/lib/board.rb +++ b/lib/board.rb @@ -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 diff --git a/lib/move/base.rb b/lib/move/base.rb index b093904..7334e2a 100644 --- a/lib/move/base.rb +++ b/lib/move/base.rb @@ -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) @@ -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 diff --git a/lib/move/diagonal.rb b/lib/move/diagonal.rb index f44a606..e79cdeb 100644 --- a/lib/move/diagonal.rb +++ b/lib/move/diagonal.rb @@ -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 diff --git a/lib/move/horizontal.rb b/lib/move/horizontal.rb index 7acd4e4..70180f6 100644 --- a/lib/move/horizontal.rb +++ b/lib/move/horizontal.rb @@ -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 diff --git a/lib/move/knight.rb b/lib/move/knight.rb index 4cac60a..1a9ebd3 100644 --- a/lib/move/knight.rb +++ b/lib/move/knight.rb @@ -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 diff --git a/lib/move/one_down.rb b/lib/move/one_down.rb index dd83721..596f3e8 100644 --- a/lib/move/one_down.rb +++ b/lib/move/one_down.rb @@ -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 diff --git a/lib/move/one_into_any_direction.rb b/lib/move/one_into_any_direction.rb index af1e81c..fb68468 100644 --- a/lib/move/one_into_any_direction.rb +++ b/lib/move/one_into_any_direction.rb @@ -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 diff --git a/lib/move/one_up.rb b/lib/move/one_up.rb index b3bc477..43aa651 100644 --- a/lib/move/one_up.rb +++ b/lib/move/one_up.rb @@ -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 diff --git a/lib/move/two_down.rb b/lib/move/two_down.rb index fa78f45..1382c49 100644 --- a/lib/move/two_down.rb +++ b/lib/move/two_down.rb @@ -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 diff --git a/lib/move/two_up.rb b/lib/move/two_up.rb index 17b6ba5..07a62db 100644 --- a/lib/move/two_up.rb +++ b/lib/move/two_up.rb @@ -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 diff --git a/lib/move/vertical.rb b/lib/move/vertical.rb index ee4567b..609527e 100644 --- a/lib/move/vertical.rb +++ b/lib/move/vertical.rb @@ -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 diff --git a/test/move/diagonal_test.rb b/test/move/diagonal_test.rb index 1779fad..7a6cc4c 100644 --- a/test/move/diagonal_test.rb +++ b/test/move/diagonal_test.rb @@ -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 diff --git a/test/move/horizontal_test.rb b/test/move/horizontal_test.rb index 7ec2fa0..b80ad95 100644 --- a/test/move/horizontal_test.rb +++ b/test/move/horizontal_test.rb @@ -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 @@ -21,8 +21,8 @@ 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 @@ -30,7 +30,7 @@ def test_legal_move_blocked_by_own_piece 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 diff --git a/test/move/knight_test.rb b/test/move/knight_test.rb index 42e3664..7c2e1a9 100644 --- a/test/move/knight_test.rb +++ b/test/move/knight_test.rb @@ -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 diff --git a/test/move/one_into_any_direction_text.rb b/test/move/one_into_any_direction_text.rb index 423bbf8..ec842a7 100644 --- a/test/move/one_into_any_direction_text.rb +++ b/test/move/one_into_any_direction_text.rb @@ -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 diff --git a/test/move/two_down_test.rb b/test/move/two_down_test.rb index 59f6eb3..403a456 100644 --- a/test/move/two_down_test.rb +++ b/test/move/two_down_test.rb @@ -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 diff --git a/test/move/vertical_test.rb b/test/move/vertical_test.rb index 7370361..e41bab4 100644 --- a/test/move/vertical_test.rb +++ b/test/move/vertical_test.rb @@ -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