Skip to content

Commit

Permalink
feat: get legal moves via move_types from piece for board
Browse files Browse the repository at this point in the history
  • Loading branch information
Roland Studer committed Dec 23, 2021
1 parent 51a3173 commit cd0a933
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/board.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ def get(position)
end

def place(piece, position)
position = Position.parse(position) if position.is_a? String
get(position).piece = piece
end

def legal_moves_for(position)
piece = get(position).piece
piece.move_types.map do |type|
type.new(self, position).legal_moves
end
end

private

def create_fields
Expand Down
13 changes: 13 additions & 0 deletions test/piece/rook_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require "minitest/autorun"

class Piece
class RookTest < Minitest::Test
def test_rook_can_move_horizontally
board = Board.new
board.place(Rook.new, "A1")
assert_includes board.legal_moves_for("A1"), Position.parse("H8")
end
end
end

0 comments on commit cd0a933

Please sign in to comment.