mchess-server/chess/board.go

220 lines
6.1 KiB
Go
Raw Normal View History

2023-06-12 20:32:31 +00:00
package chess
import (
2023-06-27 20:32:24 +00:00
"errors"
2023-06-25 14:11:29 +00:00
"mchess_server/types"
2023-06-12 20:32:31 +00:00
"github.com/samber/lo"
)
2023-06-25 22:51:20 +00:00
type Position map[types.Coordinate]Piece
2023-06-12 20:32:31 +00:00
2023-06-25 22:51:20 +00:00
type Board struct {
position Position
history []types.Move
colorToMove types.ChessColor
2023-07-05 19:15:01 +00:00
state types.AdditionalState
2023-06-25 22:51:20 +00:00
}
func newBoard() Board {
return Board{
position: make(Position),
history: make([]types.Move, 0),
colorToMove: types.White,
2023-07-05 19:15:01 +00:00
state: types.AdditionalState{},
2023-06-25 22:51:20 +00:00
}
}
func (b *Board) Init() {
2023-06-12 20:32:31 +00:00
var coord types.Coordinate
for i := 1; i <= 8; i++ {
coord.Row = 2
coord.Col = i
2023-06-25 22:51:20 +00:00
b.position[coord] = Pawn{Color: types.White}
2023-06-12 20:32:31 +00:00
coord.Row = 7
coord.Col = i
2023-06-25 22:51:20 +00:00
b.position[coord] = Pawn{Color: types.Black}
}
b.position[types.Coordinate{Row: 1, Col: 1}] = Rook{Color: types.White}
b.position[types.Coordinate{Row: 1, Col: 2}] = Knight{Color: types.White}
b.position[types.Coordinate{Row: 1, Col: 3}] = Bishop{Color: types.White}
b.position[types.Coordinate{Row: 1, Col: 4}] = Queen{Color: types.White}
b.position[types.Coordinate{Row: 1, Col: 5}] = King{Color: types.White}
b.position[types.Coordinate{Row: 1, Col: 6}] = Bishop{Color: types.White}
b.position[types.Coordinate{Row: 1, Col: 7}] = Knight{Color: types.White}
b.position[types.Coordinate{Row: 1, Col: 8}] = Rook{Color: types.White}
b.position[types.Coordinate{Row: 8, Col: 1}] = Rook{Color: types.Black}
b.position[types.Coordinate{Row: 8, Col: 2}] = Knight{Color: types.Black}
b.position[types.Coordinate{Row: 8, Col: 3}] = Bishop{Color: types.Black}
b.position[types.Coordinate{Row: 8, Col: 4}] = Queen{Color: types.Black}
b.position[types.Coordinate{Row: 8, Col: 5}] = King{Color: types.Black}
b.position[types.Coordinate{Row: 8, Col: 6}] = Bishop{Color: types.Black}
b.position[types.Coordinate{Row: 8, Col: 7}] = Knight{Color: types.Black}
b.position[types.Coordinate{Row: 8, Col: 8}] = Rook{Color: types.Black}
2023-06-12 20:32:31 +00:00
}
2023-06-14 17:46:46 +00:00
2023-06-25 22:51:20 +00:00
func (b *Board) CheckAndPlay(move types.Move) (bool, string) {
2023-06-25 14:11:29 +00:00
// We make a copy of the original board to play moves on it,
// We can play the move on it and then check if it is invalid
tempBoard := b.getCopyOfBoard()
//Check start square of move
2023-06-25 22:51:20 +00:00
pieceAtStartSquare := tempBoard.getPieceAt(move.StartSquare)
if pieceAtStartSquare == nil {
2023-06-12 20:32:31 +00:00
return false, "no piece at start square"
}
2023-06-25 22:51:20 +00:00
move.ColorMoved = pieceAtStartSquare.GetColor()
if move.ColorMoved != tempBoard.colorToMove {
return false, "wrong color moved"
}
2023-06-25 22:51:20 +00:00
move.PieceMoved = GetShortNameForPiece(pieceAtStartSquare)
2023-06-12 20:32:31 +00:00
2023-06-25 14:11:29 +00:00
//Check end square of move
2023-06-25 22:51:20 +00:00
pieceAtEndSquare := tempBoard.getPieceAt(move.EndSquare)
if pieceAtEndSquare != nil {
if pieceAtEndSquare.GetColor() == pieceAtStartSquare.GetColor() {
2023-06-14 17:46:46 +00:00
return false, "same-coloured piece at end square"
}
}
2023-06-25 14:11:29 +00:00
2023-06-25 22:51:20 +00:00
wasSpecialMove := tempBoard.handleSpecialMove(move)
if !wasSpecialMove {
2023-06-25 14:11:29 +00:00
// 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.
2023-06-27 20:32:24 +00:00
allMovesExceptBlocked := pieceAtStartSquare.GetAllNonBlockedMoves(tempBoard, move.StartSquare)
2023-06-25 14:11:29 +00:00
legal := lo.Contains(allMovesExceptBlocked, move.EndSquare)
if !legal {
return false, "not a legal square"
}
2023-06-14 17:46:46 +00:00
2023-06-25 14:11:29 +00:00
//We play the move on the temporary board
2023-06-25 22:51:20 +00:00
delete(tempBoard.position, move.StartSquare)
tempBoard.position[move.EndSquare] = pieceAtStartSquare
}
2023-06-27 20:32:24 +00:00
kingAttacked, err := tempBoard.isKingOfMovingColorInCheck(move.ColorMoved)
if err != nil {
return false, "something went wrong"
}
2023-06-27 20:32:24 +00:00
if kingAttacked {
return false, "king would be in check after move"
}
2023-06-25 14:11:29 +00:00
//Check for checkmat&e
2023-06-14 17:46:46 +00:00
//Is every square that the king can move to attacked? And can no other
//piece block? -> checkmate
//only check if paths of attacking pieces can be blocked
2023-06-14 17:46:46 +00:00
//Maybe for checking checkmate, we have to check the 'path' in which the
//checkmate is given
2023-06-14 17:46:46 +00:00
// |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
2023-06-25 14:11:29 +00:00
//We play the move on the real board
2023-06-25 22:51:20 +00:00
b.position = tempBoard.position
b.history = tempBoard.history
b.colorToMove = b.colorToMove.Opposite()
2023-06-25 22:51:20 +00:00
b.appendMoveToHistory(move)
2023-07-05 19:15:01 +00:00
pieceAtStartSquare.AfterMoveAction(b, move.StartSquare)
2023-06-12 20:32:31 +00:00
return true, ""
}
2023-06-14 17:46:46 +00:00
2023-06-27 20:32:24 +00:00
func (b Board) isKingOfMovingColorInCheck(color types.ChessColor) (bool, error) {
//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.
ownKingCoordinate := b.getSquareOfPiece(King{Color: color})
if ownKingCoordinate == nil {
return false, errors.New("no king found")
}
kingIsAttacked := b.isSquareAttacked(*ownKingCoordinate, color.Opposite())
if kingIsAttacked {
return true, nil
}
return false, nil
}
func (b Board) getSquareOfPiece(piece Piece) *types.Coordinate {
2023-06-25 22:51:20 +00:00
for k, v := range b.position {
2023-06-14 17:46:46 +00:00
if v == piece {
return &k
}
}
return nil
}
func (b Board) isSquareAttacked(square types.Coordinate, byColor types.ChessColor) bool {
var attackedSquares []types.Coordinate
2023-06-25 22:51:20 +00:00
for square, piece := range b.position {
2023-06-27 20:32:24 +00:00
if piece.GetColor() == byColor {
attackedSquares = append(attackedSquares, piece.GetAllAttackedSquares(b, square)...)
}
}
return lo.Contains(attackedSquares, square)
}
func (b Board) getPieceAt(coord types.Coordinate) Piece {
2023-06-25 22:51:20 +00:00
piece, found := b.position[coord]
if !found {
return nil
}
return piece
2023-06-14 17:46:46 +00:00
}
2023-06-25 14:11:29 +00:00
2023-06-25 22:51:20 +00:00
func (b *Board) appendMoveToHistory(move types.Move) {
b.history = append(b.history, move)
}
func (b Board) getLastMove() types.Move {
if len(b.history) < 1 {
return types.Move{}
}
return b.history[len(b.history)-1]
}
func (b Board) getCopyOfBoard() Board {
return Board{
2023-06-28 09:39:44 +00:00
position: b.position.getCopyOfPosition(),
history: b.history,
colorToMove: b.colorToMove,
2023-07-05 19:15:01 +00:00
state: b.state,
2023-06-25 22:51:20 +00:00
}
}
func (p Position) getCopyOfPosition() Position {
position := make(Position)
for coord, piece := range p {
position[coord] = piece
}
return position
}
func (b *Board) handleSpecialMove(move types.Move) bool {
var was bool
var pieceAtStartSquare = b.getPieceAt(move.StartSquare)
2023-06-28 09:39:44 +00:00
switch piece := pieceAtStartSquare.(type) {
2023-06-25 22:51:20 +00:00
case Pawn:
2023-06-28 09:39:44 +00:00
was = piece.HandlePossiblePromotion(b, move)
2023-06-25 22:51:20 +00:00
if !was {
2023-06-28 09:39:44 +00:00
was = piece.HandleEnPassant(b, move, b.getLastMove())
2023-06-25 22:51:20 +00:00
}
2023-07-05 19:15:01 +00:00
case King:
was = piece.HandleCastling(b, move)
2023-06-25 22:51:20 +00:00
}
return was
}