-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
94 lines (87 loc) · 2.23 KB
/
token.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package go_tsuro
import (
"errors"
"math"
"math/rand"
)
type token struct {
Row int
Col int
Notch string
}
func newToken(row, col int, notch string) *token {
return &token{
Row: row,
Col: col,
Notch: notch,
}
}
func randomToken(random *rand.Rand) *token {
options := "ABCDEFGH"
notch := string(options[random.Intn(8)]) // which notch to lie on
side := random.Intn(int(math.Min(float64(rows), float64(columns)))) // which side to lie on
var row = 0
var col = 0
switch notch {
case "A", "B":
row = 0
col = side
case "C", "D":
row = side
col = columns - 1
case "E", "F":
row = rows - 1
col = side
case "G", "H":
row = side
col = 0
}
return newToken(row, col, notch)
}
func (t *token) equals(t2 *token) bool {
if t.Row == t2.Row && t.Col == t2.Col && t.Notch == t2.Notch {
return true
}
return false
}
func (t *token) collided(t2 *token) bool {
adjacent := map[string]string{"A": "F", "B": "E", "C": "H", "D": "G", "E": "B", "F": "A", "G": "D", "H": "C"}
switch t.Notch {
case "A", "B":
return t2.equals(&token{Row: t.Row - 1, Col: t.Col, Notch: adjacent[t.Notch]})
case "C", "D":
return t2.equals(&token{Row: t.Row, Col: t.Col + 1, Notch: adjacent[t.Notch]})
case "E", "F":
return t2.equals(&token{Row: t.Row + 1, Col: t.Col, Notch: adjacent[t.Notch]})
case "G", "H":
return t2.equals(&token{Row: t.Row, Col: t.Col - 1, Notch: adjacent[t.Notch]})
}
return false
}
func (t *token) getAdjacent() (*token, error) {
adjacent := map[string]string{"A": "F", "B": "E", "C": "H", "D": "G", "E": "B", "F": "A", "G": "D", "H": "C"}
invalidErr := errors.New("invalid token")
switch t.Notch {
case "A", "B":
if t.Row <= 0 {
return nil, invalidErr
}
return &token{Row: t.Row - 1, Col: t.Col, Notch: adjacent[t.Notch]}, nil
case "C", "D":
if t.Col >= columns {
return nil, invalidErr
}
return &token{Row: t.Row, Col: t.Col + 1, Notch: adjacent[t.Notch]}, nil
case "E", "F":
if t.Row >= rows {
return nil, invalidErr
}
return &token{Row: t.Row + 1, Col: t.Col, Notch: adjacent[t.Notch]}, nil
case "G", "H":
if t.Col <= 0 {
return nil, invalidErr
}
return &token{Row: t.Row, Col: t.Col - 1, Notch: adjacent[t.Notch]}, nil
}
return nil, invalidErr
}