mchess-server/chess/board.go

207 lines
5.5 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-07-11 20:28:07 +00:00
func (b *Board) CheckAndPlay(move types.Move) (bool, Violation) {
2023-06-25 14:11:29 +00:00
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-07-11 20:28:07 +00:00
return false, NoPieceAtStartSquare
2023-06-12 20:32:31 +00:00
}
2023-06-25 22:51:20 +00:00
move.ColorMoved = pieceAtStartSquare.GetColor()
if move.ColorMoved != tempBoard.colorToMove {
2023-07-11 20:28:07 +00:00
return false, WrongColorMoved
}
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-07-11 20:28:07 +00:00
return false, TargetSquareIsOccupied
2023-06-14 17:46:46 +00:00
}
}
2023-06-25 14:11:29 +00:00
2023-07-11 20:28:07 +00:00
wasSpecialMove, err := tempBoard.handleSpecialMove(move)
if err != nil {
return false, InvalidMove
}
if !wasSpecialMove {
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 {
2023-07-11 20:28:07 +00:00
return false, InvalidMove
2023-06-25 14:11:29 +00:00
}
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 {
2023-07-11 20:28:07 +00:00
return false, SomethingWentWrong
}
2023-06-27 20:32:24 +00:00
if kingAttacked {
2023-07-11 20:28:07 +00:00
return false, KingInCheck
}
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
}
2023-07-11 20:28:07 +00:00
func (b *Board) handleSpecialMove(move types.Move) (bool, error) {
2023-06-25 22:51:20 +00:00
var was bool
var err error
2023-06-25 22:51:20 +00:00
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-07-11 20:28:07 +00:00
was, err = piece.HandlePossiblePromotion(b, move)
2023-06-25 22:51:20 +00:00
if !was {
2023-07-11 20:28:07 +00:00
was, err = piece.HandleEnPassant(b, move, b.getLastMove())
2023-06-25 22:51:20 +00:00
}
2023-07-05 19:15:01 +00:00
case King:
2023-07-11 20:28:07 +00:00
was, err = piece.HandleCastling(b, move)
2023-06-25 22:51:20 +00:00
}
return was, err
2023-06-25 22:51:20 +00:00
}