Start detecting checks.
This commit is contained in:
parent
c205c4dc86
commit
d5b8bdf630
@ -45,11 +45,13 @@ 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 {
|
||||
@ -57,9 +59,19 @@ func (b *Board) CheckMove(move types.Move) (bool, string) {
|
||||
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 for checkmate
|
||||
//Is every square that the king can move to attacked? And can no other
|
||||
@ -74,3 +86,19 @@ func (b *Board) CheckMove(move types.Move) (bool, string) {
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
func (c ChessColor) Opposite() ChessColor {
|
||||
if c == White {
|
||||
return Black
|
||||
} else {
|
||||
return White
|
||||
}
|
||||
}
|
||||
|
||||
type Piece struct {
|
||||
Class PieceClass
|
||||
Color ChessColor
|
||||
|
Loading…
Reference in New Issue
Block a user