Implement king moves

This commit is contained in:
ekzyis 2024-09-26 00:49:24 +02:00
parent 45c0dfce6f
commit e12946ca5d
2 changed files with 117 additions and 2 deletions

View File

@ -711,8 +711,100 @@ func (b *Board) moveQueen(position string) error {
}
func (b *Board) moveKing(position string) error {
// TODO: implement king moves
return nil
var (
x int
y int
xPrev int
yPrev int
piece *Piece
err error
)
if x, y, err = getXY(position); err != nil {
return err
}
// ^
xPrev = x + 0
yPrev = y - 1
piece = b.getPiece(xPrev, yPrev)
if piece != nil && piece.Name == King && piece.Color == b.turn {
b.tiles[xPrev][yPrev] = nil
b.tiles[x][y] = piece
return nil
}
// ^>
xPrev = x + 1
yPrev = y - 1
piece = b.getPiece(xPrev, yPrev)
if piece != nil && piece.Name == King && piece.Color == b.turn {
b.tiles[xPrev][yPrev] = nil
b.tiles[x][y] = piece
return nil
}
// >
xPrev = x + 1
yPrev = y + 0
piece = b.getPiece(xPrev, yPrev)
if piece != nil && piece.Name == King && piece.Color == b.turn {
b.tiles[xPrev][yPrev] = nil
b.tiles[x][y] = piece
return nil
}
// v>
xPrev = x + 1
yPrev = y + 1
piece = b.getPiece(xPrev, yPrev)
if piece != nil && piece.Name == King && piece.Color == b.turn {
b.tiles[xPrev][yPrev] = nil
b.tiles[x][y] = piece
return nil
}
// v
xPrev = x + 0
yPrev = y + 1
piece = b.getPiece(xPrev, yPrev)
if piece != nil && piece.Name == King && piece.Color == b.turn {
b.tiles[xPrev][yPrev] = nil
b.tiles[x][y] = piece
return nil
}
// <v
xPrev = x - 1
yPrev = y + 1
piece = b.getPiece(xPrev, yPrev)
if piece != nil && piece.Name == King && piece.Color == b.turn {
b.tiles[xPrev][yPrev] = nil
b.tiles[x][y] = piece
return nil
}
// <
xPrev = x - 1
yPrev = y + 0
piece = b.getPiece(xPrev, yPrev)
if piece != nil && piece.Name == King && piece.Color == b.turn {
b.tiles[xPrev][yPrev] = nil
b.tiles[x][y] = piece
return nil
}
// <^
xPrev = x - 1
yPrev = y - 1
piece = b.getPiece(xPrev, yPrev)
if piece != nil && piece.Name == King && piece.Color == b.turn {
b.tiles[xPrev][yPrev] = nil
b.tiles[x][y] = piece
return nil
}
return fmt.Errorf("no king found that can move to %s", position)
}
func (b *Board) mustSetPiece(name PieceName, color Color, position string) {

View File

@ -265,6 +265,29 @@ func TestBoardMoveQueenInvalid(t *testing.T) {
assertMoveError(t, b, "Qd3", "no queen found that can move to d3")
}
func TestBoardMoveKing(t *testing.T) {
b := chess.NewBoard()
b.Parse("e4 e5 Ke2 Ke7 Kf3 Kd6 Kg3 Kc6")
assertNoPiece(t, b, "e1")
assertNoPiece(t, b, "e8")
assertNoPiece(t, b, "e2")
assertNoPiece(t, b, "e7")
assertNoPiece(t, b, "f3")
assertNoPiece(t, b, "d6")
assertPiece(t, b, "g3", chess.King, chess.Light)
assertPiece(t, b, "c6", chess.King, chess.Dark)
}
func TestBoardMoveKingInvalid(t *testing.T) {
b := chess.NewBoard()
assertMoveError(t, b, "Ke1", "e1 blocked by white king")
assertMoveError(t, b, "Ke2", "e2 blocked by white pawn")
assertMoveError(t, b, "Ke3", "no king found that can move to e3")
}
func assertPiece(t *testing.T, b *chess.Board, position string, name chess.PieceName, color chess.Color) {
p := b.At(position)