56 lines
2.1 KiB
Go
56 lines
2.1 KiB
Go
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) {
|
|
_, found := b.GetPieceAt(move.StartSquare)
|
|
if !found {
|
|
return false, "no piece at start square"
|
|
}
|
|
|
|
return true, ""
|
|
}
|