-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtile.go
211 lines (196 loc) · 7.4 KB
/
tile.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package go_carcassonne
import "fmt"
const OutOfBounds = 999
type tile struct {
// X and Y represent the location of the tile on the board, OutOfBounds means not in board
X, Y int
// Sides is a map from side i.e. Top, Right, Bottom, Left to structure type i.e. Farm, City, Road
Sides map[string]string
// Center represents the structure at the center, i.e. Cloister, NilStructure
Center string
// ConnectedCitySides determines whether the city sides are connected together or separate
ConnectedCitySides bool
// Banner determines whether this is a banner tile
Banner bool
// Teams is a map from side to list of teams that have won that side after completing the given structure
Teams map[string][]string
// FarmTeams is a map from farm side to list of teams that have won that farmland at the end of the game
FarmTeams map[string][]string
// CenterTeam represents the team that has won the structure in the center of the tile after completing the given structure
CenterTeam string
// adjacent is a map from side to adjacent tiles
adjacent map[string]*tile
}
func emptySpaceTile(x, y int) *tile {
return &tile{
X: x,
Y: y,
adjacent: make(map[string]*tile),
}
}
func newTile(topStructure, rightStructure, bottomStructure, leftStructure, centerStructure string, connectedCitySides, banner bool) *tile {
return &tile{
X: OutOfBounds,
Y: OutOfBounds,
Sides: map[string]string{SideTop: topStructure, SideRight: rightStructure, SideBottom: bottomStructure, SideLeft: leftStructure},
Center: centerStructure,
ConnectedCitySides: connectedCitySides,
Banner: banner,
Teams: make(map[string][]string),
FarmTeams: make(map[string][]string),
CenterTeam: "",
adjacent: make(map[string]*tile),
}
}
func (t *tile) copy() *tile {
return newTile(t.Sides[SideTop], t.Sides[SideRight], t.Sides[SideBottom], t.Sides[SideLeft], t.Center, t.ConnectedCitySides, t.Banner)
}
func (t *tile) RotateRight() {
newSides := make(map[string]string)
for _, side := range Sides {
newSides[ClockwiseSide[side]] = t.Sides[side]
}
t.Sides = newSides
}
func (t *tile) RotateLeft() {
newSides := make(map[string]string)
for _, side := range Sides {
newSides[CounterClockwiseSide[side]] = t.Sides[side]
}
t.Sides = newSides
}
// given a city side on the tile get all connected city sides
func (t *tile) connectedCitySides(side string) ([]string, error) {
sides := make([]string, 0)
if !contains(Sides, side) {
return nil, fmt.Errorf("invalid side %s", side)
}
if t.Sides[side] != City {
return nil, fmt.Errorf("cannot enter tile on a non-city side")
}
// city sides are not connected so city ends here
if !t.ConnectedCitySides {
return sides, nil
}
for _, s := range Sides {
if s != side && t.Sides[s] == City {
sides = append(sides, s)
}
}
return sides, nil
}
// given a road side on the tile get all connected road sides
func (t *tile) connectedRoadSides(side string) ([]string, error) {
sides := make([]string, 0)
if !contains(Sides, side) {
return nil, fmt.Errorf("invalid side %s", side)
}
if t.Sides[side] != Road {
return nil, fmt.Errorf("cannot enter tile on a non-road side")
}
for _, s := range Sides {
if s != side && t.Sides[s] == Road {
sides = append(sides, s)
}
}
// more than one so road intersection
if len(sides) != 1 {
return make([]string, 0), nil
}
return sides, nil
}
func (t *tile) connectedFarmSides(farmSide string) ([]string, error) {
points := make([]string, 0)
if !contains(FarmSides, farmSide) {
return nil, fmt.Errorf("invalid farm side %s", farmSide)
}
side := farmSideToSide(farmSide)
ab := farmSideToAB(farmSide)
if t.Sides[side] == City {
return nil, fmt.Errorf("cannot enter on a city side")
}
// special case in which all sides are returned
if t.Center == Cloister {
points = append(points, sideToFarmSide(side, inverseAB(ab)),
sideToFarmSide(ClockwiseSide[side], FarmNotchA), sideToFarmSide(ClockwiseSide[side], FarmNotchB),
sideToFarmSide(CounterClockwiseSide[side], FarmNotchA), sideToFarmSide(CounterClockwiseSide[side], FarmNotchB),
sideToFarmSide(AcrossSide[side], FarmNotchA), sideToFarmSide(AcrossSide[side], FarmNotchB))
return points, nil
}
switch t.Sides[side] {
case Farm:
points = append(points, sideToFarmSide(side, inverseAB(ab)))
clockwiseSide := ClockwiseSide[side]
counterClockwiseSide := CounterClockwiseSide[side]
acrossSide := AcrossSide[side]
// clockwise check
if t.Sides[clockwiseSide] == Road {
points = append(points, sideToFarmSide(clockwiseSide, FarmNotchA))
} else if t.Sides[clockwiseSide] == Farm {
points = append(points, sideToFarmSide(clockwiseSide, FarmNotchA), sideToFarmSide(clockwiseSide, FarmNotchB))
}
// counterclockwise check
if t.Sides[counterClockwiseSide] == Road {
points = append(points, sideToFarmSide(counterClockwiseSide, FarmNotchB))
} else if t.Sides[counterClockwiseSide] == Farm {
points = append(points, sideToFarmSide(counterClockwiseSide, FarmNotchA), sideToFarmSide(counterClockwiseSide, FarmNotchB))
}
// if access to across side blocked return
if (t.Sides[clockwiseSide] == Road && t.Sides[counterClockwiseSide] == Road) ||
(t.Sides[clockwiseSide] == City && t.Sides[counterClockwiseSide] == City && t.ConnectedCitySides) {
return points, nil
}
// across check
if t.Sides[clockwiseSide] == Road && t.Sides[acrossSide] == Road {
points = append(points, sideToFarmSide(acrossSide, FarmNotchB))
} else if t.Sides[counterClockwiseSide] == Road && t.Sides[acrossSide] == Road {
points = append(points, sideToFarmSide(acrossSide, FarmNotchA))
} else if t.Sides[acrossSide] == Road || t.Sides[acrossSide] == Farm {
points = append(points, sideToFarmSide(acrossSide, FarmNotchA), sideToFarmSide(acrossSide, FarmNotchB))
}
case Road:
adjacentSide := ClockwiseSide[side]
blockedAdjacentSide := CounterClockwiseSide[side]
acrossSide := AcrossSide[side]
if ab == FarmNotchA {
adjacentSide = CounterClockwiseSide[side]
blockedAdjacentSide = ClockwiseSide[side]
}
// adjacent side check
if t.Sides[adjacentSide] == Road {
points = append(points, sideToFarmSide(adjacentSide, inverseAB(ab)))
return points, nil
} else if t.Sides[adjacentSide] == Farm {
points = append(points, sideToFarmSide(adjacentSide, FarmNotchA), sideToFarmSide(adjacentSide, FarmNotchB))
}
// across side check - return if blocked by road
if t.Sides[acrossSide] == Road {
points = append(points, sideToFarmSide(acrossSide, inverseAB(ab)))
return points, nil
} else if t.Sides[acrossSide] == Farm {
points = append(points, sideToFarmSide(acrossSide, FarmNotchA), sideToFarmSide(acrossSide, FarmNotchB))
}
// blocked side check
if t.Sides[blockedAdjacentSide] == Road {
points = append(points, sideToFarmSide(blockedAdjacentSide, inverseAB(ab)))
} else if t.Sides[blockedAdjacentSide] == Farm {
points = append(points, sideToFarmSide(blockedAdjacentSide, FarmNotchA), sideToFarmSide(blockedAdjacentSide, FarmNotchB))
}
}
return points, nil
}
func (t tile) equals(t2 *tile) bool {
for i := 0; i < 4; i++ {
if t.Banner == t2.Banner &&
t.ConnectedCitySides == t2.ConnectedCitySides &&
t.Center == t2.Center &&
t.Sides[SideTop] == t2.Sides[SideTop] &&
t.Sides[SideRight] == t2.Sides[SideRight] &&
t.Sides[SideBottom] == t2.Sides[SideBottom] &&
t.Sides[SideLeft] == t2.Sides[SideLeft] {
return true
}
t.RotateRight()
}
return false
}