mchess-server/types/common.go

62 lines
1.1 KiB
Go
Raw Normal View History

2023-06-12 20:32:31 +00:00
package types
// coordinates starting at 1:1 and end at 8:8
2023-06-12 20:32:31 +00:00
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
}
2023-06-12 20:32:31 +00:00
type ChessColor string
const (
White ChessColor = "white"
Black ChessColor = "black"
)
2023-06-14 17:46:46 +00:00
func (c ChessColor) Opposite() ChessColor {
if c == White {
return Black
} else {
return White
}
}