Skip to content

Commit

Permalink
refactor: use position candidates to remove illegal moves from
Browse files Browse the repository at this point in the history
  • Loading branch information
Roland Studer committed Dec 25, 2021
1 parent 1ae7d43 commit 36a31c4
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 29 deletions.
11 changes: 4 additions & 7 deletions lib/move/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/move/diagonal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions lib/move/horizontal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 3 additions & 4 deletions lib/move/knight.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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_target_positions
without_illegal_target_positions([position.down])
def position_candidates
[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_target_positions
def position_candidates
positions = directions.map do |direction|
position.go(*direction)
end
without_illegal_target_positions(positions.compact)
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_target_positions
without_illegal_target_positions([position.down])
def position_candidates
[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_target_positions
def position_candidates
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_target_positions
def position_candidates
if no_piece_in_the_way? && target_piece_not_occupied?
[position.up.up]
else
Expand Down
10 changes: 4 additions & 6 deletions lib/move/vertical.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 36a31c4

Please sign in to comment.