-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotations.go
62 lines (51 loc) · 1.51 KB
/
notations.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
50
51
52
53
54
55
56
57
58
59
60
61
62
package othello
import "fmt"
const (
asciiCodeOfLowercaseA = 97
asciiCodeOf1 = 49
DecodeLengthErrorMessage = "length of input variable does'nt equal with 2, length: %d"
DecodeColumnErrorMessage = "wrong column format (between a-h): %s"
DecodeRowErrorMessage = "wrong column format (between 1-8): %s"
)
// Notation is the interface implemented by objects that can
// encode and decode moves.
type Notation interface {
Encoder
Decoder
}
// AlgebraicNotation is the official othello notation.
// Examples: f5
type AlgebraicNotation struct{}
type Decoder interface {
Decode(s string) (Square, error)
}
type Encoder interface {
Encode(s Square) string
}
// Decode implements the Decoder interface.
func (AlgebraicNotation) Decode(s string) (Square, error) {
if len(s) != 2 {
return Square(0), fmt.Errorf(DecodeLengthErrorMessage, len(s))
}
columnChar := s[0]
ascii := int(columnChar)
columnInt := ascii - asciiCodeOfLowercaseA
if columnInt < 0 || columnInt > 7 {
return Square(0), fmt.Errorf(DecodeColumnErrorMessage, string(columnChar))
}
rowChar := s[1]
rowInt := int(rowChar) - asciiCodeOf1
if rowInt < 0 || rowInt > 7 {
return Square(0), fmt.Errorf(DecodeRowErrorMessage, string(rowChar))
}
return Square(rowInt*8 + columnInt), nil
}
// Encode implements the Encoder interface.
func (AlgebraicNotation) Encode(s Square) string {
return ""
}
// String implements the fmt.Stringer interface and returns
// the notation's name.
func (AlgebraicNotation) String() string {
return "Algebraic Notation"
}