Start detecting checks.
This commit is contained in:
parent
c205c4dc86
commit
d5b8bdf630
@ -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"
|
||||||
}
|
}
|
||||||
|
|
||||||
//Check if king of moving color is in check -> move not allowed
|
b.isSquareAttacked(*oppKingCoordinate, movingColor.Opposite())
|
||||||
//Do that by checking if the king is in a square attacked by the other color.
|
|
||||||
|
|
||||||
//Check for checkmate
|
//Check for checkmate
|
||||||
//Is every square that the king can move to attacked? And can no other
|
//Is every square that the king can move to attacked? And can no other
|
||||||
//piece block? -> checkmate
|
//piece block? -> checkmate
|
||||||
|
|
||||||
//Maybe for checking checkmate, we have to check the 'path' in which the
|
//Maybe for checking checkmate, we have to check the 'path' in which the
|
||||||
//checkmate is given
|
//checkmate is given
|
||||||
|
|
||||||
// |K| | | | |Q|
|
// |K| | | | |Q|
|
||||||
// in this scenaria the path are all the squares between queen and king.
|
// 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
|
// 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
|
||||||
|
}
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user