mchess-server/chess/board.go

77 lines
2.9 KiB
Go
Raw Normal View History

2023-06-12 20:32:31 +00:00
package chess
import "local/m/mchess_server/types"
type Board map[types.Coordinate]types.Piece
func (b Board) Init() {
var coord types.Coordinate
for i := 1; i <= 8; i++ {
coord.Row = 2
coord.Col = i
b[coord] = types.Piece{Class: types.Pawn, Color: types.White}
coord.Row = 7
coord.Col = i
b[coord] = types.Piece{Class: types.Pawn, Color: types.Black}
}
b[types.Coordinate{Row: 1, Col: 1}] = types.Piece{Class: types.Rook, Color: types.White}
b[types.Coordinate{Row: 1, Col: 2}] = types.Piece{Class: types.Knight, Color: types.White}
b[types.Coordinate{Row: 1, Col: 3}] = types.Piece{Class: types.Bishop, Color: types.White}
b[types.Coordinate{Row: 1, Col: 4}] = types.Piece{Class: types.Queen, Color: types.White}
b[types.Coordinate{Row: 1, Col: 5}] = types.Piece{Class: types.King, Color: types.White}
b[types.Coordinate{Row: 1, Col: 6}] = types.Piece{Class: types.Bishop, Color: types.White}
b[types.Coordinate{Row: 1, Col: 7}] = types.Piece{Class: types.Knight, Color: types.White}
b[types.Coordinate{Row: 1, Col: 8}] = types.Piece{Class: types.Rook, Color: types.White}
b[types.Coordinate{Row: 8, Col: 1}] = types.Piece{Class: types.Rook, Color: types.Black}
b[types.Coordinate{Row: 8, Col: 2}] = types.Piece{Class: types.Knight, Color: types.Black}
b[types.Coordinate{Row: 8, Col: 3}] = types.Piece{Class: types.Bishop, Color: types.Black}
b[types.Coordinate{Row: 8, Col: 4}] = types.Piece{Class: types.Queen, Color: types.Black}
b[types.Coordinate{Row: 8, Col: 5}] = types.Piece{Class: types.King, Color: types.Black}
b[types.Coordinate{Row: 8, Col: 6}] = types.Piece{Class: types.Bishop, Color: types.Black}
b[types.Coordinate{Row: 8, Col: 7}] = types.Piece{Class: types.Knight, Color: types.Black}
b[types.Coordinate{Row: 8, Col: 8}] = types.Piece{Class: types.Rook, Color: types.Black}
}
func (b Board) GetPieceAt(coord types.Coordinate) (types.Piece, bool) {
piece, found := b[coord]
if !found {
piece = types.Piece{}
}
return piece, found
}
func (b *Board) CheckMove(move types.Move) (bool, string) {
pieceAtStartSquare, found := b.GetPieceAt(move.StartSquare)
2023-06-12 20:32:31 +00:00
if !found {
return false, "no piece at start square"
}
pieceAtEndSquare, found := b.GetPieceAt(move.EndSquare)
if found {
if pieceAtEndSquare.Color == pieceAtStartSquare.Color {
return false, "same-coloured piece at end square"
}
}
//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
//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
2023-06-12 20:32:31 +00:00
return true, ""
}