diff --git a/chess/board.go b/chess/board.go index 42123cf..5007d02 100644 --- a/chess/board.go +++ b/chess/board.go @@ -239,7 +239,7 @@ func (b *Board) moveKnight(position string) error { xPrev = x + 1 yPrev = y - 2 - piece = b.tiles[xPrev][yPrev] + piece = b.getPiece(xPrev, yPrev) if piece != nil && piece.Name == Knight && piece.Color == b.turn { b.tiles[xPrev][yPrev] = nil b.tiles[x][y] = piece @@ -248,7 +248,7 @@ func (b *Board) moveKnight(position string) error { xPrev = x + 2 yPrev = y - 1 - piece = b.tiles[xPrev][yPrev] + piece = b.getPiece(xPrev, yPrev) if piece != nil && piece.Name == Knight && piece.Color == b.turn { b.tiles[xPrev][yPrev] = nil b.tiles[x][y] = piece @@ -257,7 +257,7 @@ func (b *Board) moveKnight(position string) error { xPrev = x + 2 yPrev = y + 1 - piece = b.tiles[xPrev][yPrev] + piece = b.getPiece(xPrev, yPrev) if piece != nil && piece.Name == Knight && piece.Color == b.turn { b.tiles[xPrev][yPrev] = nil b.tiles[x][y] = piece @@ -266,7 +266,7 @@ func (b *Board) moveKnight(position string) error { xPrev = x + 1 yPrev = y + 2 - piece = b.tiles[xPrev][yPrev] + piece = b.getPiece(xPrev, yPrev) if piece != nil && piece.Name == Knight && piece.Color == b.turn { b.tiles[xPrev][yPrev] = nil b.tiles[x][y] = piece @@ -275,7 +275,7 @@ func (b *Board) moveKnight(position string) error { xPrev = x - 1 yPrev = y + 2 - piece = b.tiles[xPrev][yPrev] + piece = b.getPiece(xPrev, yPrev) if piece != nil && piece.Name == Knight && piece.Color == b.turn { b.tiles[xPrev][yPrev] = nil b.tiles[x][y] = piece @@ -284,7 +284,7 @@ func (b *Board) moveKnight(position string) error { xPrev = x - 2 yPrev = y + 1 - piece = b.tiles[xPrev][yPrev] + piece = b.getPiece(xPrev, yPrev) if piece != nil && piece.Name == Knight && piece.Color == b.turn { b.tiles[xPrev][yPrev] = nil b.tiles[x][y] = piece @@ -293,7 +293,7 @@ func (b *Board) moveKnight(position string) error { xPrev = x - 2 yPrev = y - 1 - piece = b.tiles[xPrev][yPrev] + piece = b.getPiece(xPrev, yPrev) if piece != nil && piece.Name == Knight && piece.Color == b.turn { b.tiles[xPrev][yPrev] = nil b.tiles[x][y] = piece @@ -302,7 +302,7 @@ func (b *Board) moveKnight(position string) error { xPrev = x - 1 yPrev = y - 2 - piece = b.tiles[xPrev][yPrev] + piece = b.getPiece(xPrev, yPrev) if piece != nil && piece.Name == Knight && piece.Color == b.turn { b.tiles[xPrev][yPrev] = nil b.tiles[x][y] = piece @@ -364,6 +364,13 @@ func getXY(position string) (int, int, error) { return x, y, nil } +func (b *Board) getPiece(x int, y int) *Piece { + if x < 0 || x >= 8 || y < 0 || y >= 8 { + return nil + } + return b.tiles[x][y] +} + func getTileColor(x, y int) Color { if x%2 == y%2 { return Light diff --git a/chess/board_test.go b/chess/board_test.go index 205e32e..fc4de6d 100644 --- a/chess/board_test.go +++ b/chess/board_test.go @@ -103,9 +103,9 @@ func TestBoardMoveKnight(t *testing.T) { assertPiece(t, b, "f3", chess.Knight, chess.Light) assertNoPiece(t, b, "g1") - b.Move("Nf6") + b.Move("Nh6") - assertPiece(t, b, "f6", chess.Knight, chess.Dark) + assertPiece(t, b, "h6", chess.Knight, chess.Dark) assertNoPiece(t, b, "g8") }