diff --git a/lib/move/base.rb b/lib/move/base.rb index 04d8cb6..7c3d443 100644 --- a/lib/move/base.rb +++ b/lib/move/base.rb @@ -12,7 +12,10 @@ def initialize(board, position) end def legal_target_positions - + positions = position_candidates + positions -= target_positions_that_are_occupied_by_friend(positions) + positions -= target_positions_that_create_check_for_own_king(positions) + positions end private @@ -42,12 +45,6 @@ def occupied_by_enemy?(target_position) other_piece && other_piece.color != piece.color end - def without_illegal_target_positions(positions) - positions -= target_positions_that_are_occupied_by_friend(positions) - positions -= target_positions_that_create_check_for_own_king(positions) - positions - end - def target_positions_that_are_occupied_by_friend(positions) positions.select { |target_position| occupied_by_friend?(target_position) } end diff --git a/lib/move/diagonal.rb b/lib/move/diagonal.rb index e79cdeb..e761f4b 100644 --- a/lib/move/diagonal.rb +++ b/lib/move/diagonal.rb @@ -3,7 +3,7 @@ module Move # unlimited horizontal movement for a piece class Diagonal < Base - def legal_target_positions + def position_candidates [ position.positions_up_left, position.positions_up_right, diff --git a/lib/move/horizontal.rb b/lib/move/horizontal.rb index 3bd2480..1437c5e 100644 --- a/lib/move/horizontal.rb +++ b/lib/move/horizontal.rb @@ -3,10 +3,9 @@ module Move # unlimited horizontal movement for a piece class Horizontal < Base - def legal_target_positions - without_illegal_target_positions( + def position_candidates legal_target_positions_in_line(position.positions_to_the_left) + - legal_target_positions_in_line(position.positions_to_the_right)) + 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 1a9ebd3..1ca3bfa 100644 --- a/lib/move/knight.rb +++ b/lib/move/knight.rb @@ -3,11 +3,10 @@ module Move # knight moves in ls class Knight < Base - def legal_target_positions - positions = directions.map do |direction| + def position_candidates + directions.map do |direction| position.go(*direction) - end - without_illegal_target_positions(positions.compact) + end.compact end private diff --git a/lib/move/one_down.rb b/lib/move/one_down.rb index 596f3e8..7715ba0 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_target_positions - without_illegal_target_positions([position.down]) + def position_candidates + [position.down] end end end diff --git a/lib/move/one_into_any_direction.rb b/lib/move/one_into_any_direction.rb index fb68468..fbbcb77 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_target_positions + def position_candidates positions = directions.map do |direction| position.go(*direction) end - without_illegal_target_positions(positions.compact) + positions.compact end private diff --git a/lib/move/one_up.rb b/lib/move/one_up.rb index 43aa651..bd1d905 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_target_positions - without_illegal_target_positions([position.down]) + def position_candidates + [position.down] end end end diff --git a/lib/move/two_down.rb b/lib/move/two_down.rb index 1382c49..93efe6d 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_target_positions + def position_candidates 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 07a62db..4daeb15 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_target_positions + def position_candidates 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 23ca1fa..76ade6d 100644 --- a/lib/move/vertical.rb +++ b/lib/move/vertical.rb @@ -3,11 +3,9 @@ module Move # unlimited vertical movement for a piece class Vertical < Base - def legal_target_positions - without_illegal_target_positions( - - legal_target_positions_in_line(position.positions_upwards) + - legal_target_positions_in_line(position.positions_downwards)) - end + def position_candidates + legal_target_positions_in_line(position.positions_upwards) + + legal_target_positions_in_line(position.positions_downwards) + end end end