Start detecting checks.

This commit is contained in:
Marco 2023-06-14 19:46:46 +02:00
parent c205c4dc86
commit d5b8bdf630
2 changed files with 53 additions and 17 deletions

View File

@ -45,32 +45,60 @@ func (b Board) GetPieceAt(coord types.Coordinate) (types.Piece, bool) {
return piece, found return piece, found
} }
func (b *Board) CheckMove(move types.Move) (bool, string) {
func (b Board) CheckMove(move types.Move) (bool, string) {
pieceAtStartSquare, found := b.GetPieceAt(move.StartSquare) pieceAtStartSquare, found := b.GetPieceAt(move.StartSquare)
if !found { if !found {
return false, "no piece at start square" return false, "no piece at start square"
} }
movingColor := pieceAtStartSquare.Color
pieceAtEndSquare, found := b.GetPieceAt(move.EndSquare) pieceAtEndSquare, found := b.GetPieceAt(move.EndSquare)
if found { if found {
if pieceAtEndSquare.Color == pieceAtStartSquare.Color { if pieceAtEndSquare.Color == pieceAtStartSquare.Color {
return false, "same-coloured piece at end square" return false, "same-coloured piece at end square"
} }
}
// At the moment, we do not need to check if the correct color is moving,
//since we are only reading moves from the player whose turn it is.
//Check if king of moving color is in check -> move not allowed
//Do that by checking if the king is in a square attacked by the other color.
oppKingCoordinate := b.getSquareOfPiece(types.Piece{
Class: types.King,
Color: movingColor})
if oppKingCoordinate == nil {
return false, string(movingColor) + " king not found"
} }
b.isSquareAttacked(*oppKingCoordinate, movingColor.Opposite())
//Check if king of moving color is in check -> move not allowed //Check for checkmate
//Do that by checking if the king is in a square attacked by the other color. //Is every square that the king can move to attacked? And can no other
//piece block? -> checkmate
//Check for checkmate //Maybe for checking checkmate, we have to check the 'path' in which the
//Is every square that the king can move to attacked? And can no other //checkmate is given
//piece block? -> checkmate
//Maybe for checking checkmate, we have to check the 'path' in which the // |K| | | | |Q|
//checkmate is given // in this scenaria the path are all the squares between queen and king.
// If a piece can be moved into the path, it is no checkmate
// |K| | | | |Q|
// in this scenaria the path are all the squares between queen and king.
// If a piece can be moved into the path, it is no checkmate
return true, "" return true, ""
} }
func (b Board) getSquareOfPiece(piece types.Piece) *types.Coordinate {
for k, v := range b {
if v == piece {
return &k
}
}
return nil
}
func (b Board) isSquareAttacked(square types.Coordinate, byColor types.ChessColor) bool {
attacked := false
//get every legal move of color to check if this square is attacked
return attacked
}

View File

@ -28,6 +28,14 @@ const (
Black ChessColor = "black" Black ChessColor = "black"
) )
func (c ChessColor) Opposite() ChessColor {
if c == White {
return Black
} else {
return White
}
}
type Piece struct { type Piece struct {
Class PieceClass Class PieceClass
Color ChessColor Color ChessColor