Fix missing boundary checks
This commit is contained in:
parent
8f5075d668
commit
5e9e4a14d5
|
@ -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
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue