78 lines
1.5 KiB
Go
78 lines
1.5 KiB
Go
package types
|
|
|
|
// coordinates starting at 1:1 and end at 8:8
|
|
type Coordinate struct {
|
|
Col int `json:"col"`
|
|
Row int `json:"row"`
|
|
}
|
|
|
|
const (
|
|
RangeLastValid = 8
|
|
RangeFirstValid = 1
|
|
|
|
RangeUpperInvalid = 9
|
|
RangeLowerInvalid = 0
|
|
)
|
|
|
|
func (c Coordinate) Up(number int) *Coordinate {
|
|
check := c.Row + number
|
|
if check <= RangeLastValid {
|
|
return &Coordinate{Row: check, Col: c.Col}
|
|
}
|
|
return nil
|
|
}
|
|
func (c Coordinate) Down(number int) *Coordinate {
|
|
check := c.Row - number
|
|
if check >= RangeFirstValid {
|
|
return &Coordinate{Row: check, Col: c.Col}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Right and left is seen from a board where row 1 is on the bottom
|
|
func (c Coordinate) Right(number int) *Coordinate {
|
|
check := c.Col + number
|
|
if check >= RangeFirstValid {
|
|
return &Coordinate{Row: c.Row, Col: check}
|
|
}
|
|
return nil
|
|
}
|
|
func (c Coordinate) Left(number int) *Coordinate {
|
|
check := c.Col - number
|
|
if check >= RangeFirstValid {
|
|
return &Coordinate{Row: c.Row, Col: check}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type Move struct {
|
|
StartSquare Coordinate `json:"startSquare"`
|
|
EndSquare Coordinate `json:"endSquare"`
|
|
}
|
|
|
|
type PieceClass string
|
|
|
|
const (
|
|
Pawn PieceClass = "pawn"
|
|
Rook PieceClass = "rook"
|
|
Knight PieceClass = "knight"
|
|
Bishop PieceClass = "bishop"
|
|
Queen PieceClass = "queen"
|
|
King PieceClass = "king"
|
|
)
|
|
|
|
type ChessColor string
|
|
|
|
const (
|
|
White ChessColor = "white"
|
|
Black ChessColor = "black"
|
|
)
|
|
|
|
func (c ChessColor) Opposite() ChessColor {
|
|
if c == White {
|
|
return Black
|
|
} else {
|
|
return White
|
|
}
|
|
}
|