Skip to content

Commit

Permalink
feat: horizontal moves should not allow own pieces capturing
Browse files Browse the repository at this point in the history
  • Loading branch information
Roland Studer committed Dec 23, 2021
1 parent ad7fb00 commit b8521ba
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
11 changes: 8 additions & 3 deletions lib/move/horizontal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
module Move
# unlimited horizontal movement for a piece
class Horizontal
attr_reader :board, :position
attr_reader :board, :position, :piece

def initialize(board, position)
@board = board
@position = Position.parse(position)
@piece = board.get(position).piece
end

def legal_moves
Expand All @@ -19,17 +20,21 @@ def legal_moves
def legal_left_moves
legals = []
position.positions_to_the_left.each do |to_the_left|
other_piece = board.get(to_the_left).piece
break if other_piece && other_piece.color == piece.color
legals << to_the_left
break if board.get(to_the_left).piece
break if other_piece && other_piece.color != piece.color
end
legals
end

def legal_right_moves
legals = []
position.positions_to_the_right.map do |to_the_right|
other_piece = board.get(to_the_right).piece
break if other_piece && other_piece.color == piece.color
legals << to_the_right
break if board.get(to_the_right).piece
break if other_piece && other_piece.color != piece.color
end
legals
end
Expand Down
4 changes: 3 additions & 1 deletion lib/piece.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

# Base class for chess pieces
class Piece
def initialize(color: :white)
attr_reader :color

def initialize(color = :white)
@color = color
end
end
2 changes: 2 additions & 0 deletions main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

require_relative 'lib/board'
require_relative 'lib/position'

require_relative 'lib/piece'
require_relative 'lib/piece/rook'

require_relative 'lib/move/horizontal'
18 changes: 14 additions & 4 deletions test/move/horizontal_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,33 @@ module Move
class HorizontalTest < Minitest::Test
def test_legal_move_blocked_by_other_piece_to_the_left
board = Board.new
board.place(Rook.new(:black), "C1")

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

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

def test_legal_move_blocked_by_other_piece_to_the_right
def test_legal_move_blocked_by_enemy_piece_to_the_right
board = Board.new
board.place(Rook.new(:black), "C1")

move = Move::Horizontal.new(board, "C1")

# to the right
assert_includes move.legal_moves, Position.parse("H1")
board.place(Piece.new, "F1")
board.place(Piece.new(:white), "F1")
assert_includes move.legal_moves, Position.parse("F1")
assert !move.legal_moves.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"))
end
end
end

0 comments on commit b8521ba

Please sign in to comment.