From d5b8bdf6304d70e75ff666d862528c8d373df473 Mon Sep 17 00:00:00 2001 From: Marco Date: Wed, 14 Jun 2023 19:46:46 +0200 Subject: [PATCH] Start detecting checks. --- chess/board.go | 62 +++++++++++++++++++++++++++++++++++-------------- types/common.go | 8 +++++++ 2 files changed, 53 insertions(+), 17 deletions(-) diff --git a/chess/board.go b/chess/board.go index 3e65695..3a1c13a 100644 --- a/chess/board.go +++ b/chess/board.go @@ -45,32 +45,60 @@ func (b Board) GetPieceAt(coord types.Coordinate) (types.Piece, bool) { 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) if !found { return false, "no piece at start square" } + movingColor := pieceAtStartSquare.Color - pieceAtEndSquare, found := b.GetPieceAt(move.EndSquare) - if found { - if pieceAtEndSquare.Color == pieceAtStartSquare.Color { - return false, "same-coloured piece at end square" - } + pieceAtEndSquare, found := b.GetPieceAt(move.EndSquare) + if found { + if pieceAtEndSquare.Color == pieceAtStartSquare.Color { + 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 - //Do that by checking if the king is in a square attacked by the other color. + //Check for checkmate + //Is every square that the king can move to attacked? And can no other + //piece block? -> checkmate - //Check for checkmate - //Is every square that the king can move to attacked? And can no other - //piece block? -> checkmate + //Maybe for checking checkmate, we have to check the 'path' in which the + //checkmate is given - //Maybe for checking checkmate, we have to check the 'path' in which the - //checkmate is given - - // |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 + // |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, "" } + +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 +} diff --git a/types/common.go b/types/common.go index db03bd2..a38b767 100644 --- a/types/common.go +++ b/types/common.go @@ -28,6 +28,14 @@ const ( Black ChessColor = "black" ) +func (c ChessColor) Opposite() ChessColor { + if c == White { + return Black + } else { + return White + } +} + type Piece struct { Class PieceClass Color ChessColor