-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.go
49 lines (42 loc) · 833 Bytes
/
color.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package chess
import (
"fmt"
"math/rand"
"reflect"
)
// Color represents a color - either white or black.
type Color uint8
const (
White Color = iota
Black
)
func ParseColorFEN(s string) (Color, error) {
switch s {
case "w":
return White, nil
case "b":
return Black, nil
default:
return 0, fmt.Errorf("invalid color %s", s)
}
}
// Flipped returns the opposite color.
func (c Color) Flipped() Color {
return c ^ 1
}
// FEN returns the FEN representation of a color. It returns an empty string if
// the color is invalid.
func (c Color) FEN() string {
switch c {
case White:
return "w"
case Black:
return "b"
default:
return ""
}
}
// Generate lets Color satisfy testing/quick.Generator.
func (Color) Generate(rand *rand.Rand, size int) reflect.Value {
return reflect.ValueOf(Color(rand.Intn(2)))
}