-
Notifications
You must be signed in to change notification settings - Fork 1
/
tileSet.go
178 lines (156 loc) · 3.33 KB
/
tileSet.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
package gridspech
import (
"strings"
)
// TileSet represents a mathematical set of tiles. Tiles are compared using ==.
type TileSet struct {
set map[Tile]struct{}
}
// NewTileSet returns a TileSet containing only tiles.
func NewTileSet(tiles ...Tile) TileSet {
var ts TileSet
for _, tile := range tiles {
ts.Add(tile)
}
return ts
}
// Init initializes the tileset.
func (ts *TileSet) checkInit() {
if ts.set == nil {
ts.set = make(map[Tile]struct{})
}
}
// Add adds t to the TileSet ts.
func (ts *TileSet) Add(t Tile) {
ts.checkInit()
ts.set[t] = struct{}{}
}
// Has returns if ts contains t.
func (ts TileSet) Has(t Tile) bool {
_, ok := ts.set[t]
return ok
}
// Remove removes t from ts.
func (ts *TileSet) Remove(t Tile) {
ts.checkInit()
delete(ts.set, t)
}
// RemoveIf removes each value for which pred returns true.
func (ts *TileSet) RemoveIf(pred func(t Tile) bool) {
for tile := range ts.set {
if pred(tile) {
ts.Remove(tile)
}
}
}
// RemoveAll removes all of the elements in o from ts (making ts the intersection of ts and o)
func (ts *TileSet) RemoveAll(o TileSet) {
if ts.Len() < o.Len() {
for tile := range ts.set {
if o.Has(tile) {
ts.Remove(tile)
}
}
} else {
for tile := range o.set {
if ts.Has(tile) {
ts.Remove(tile)
}
}
}
}
// Len returns the number of tiles in ts.
func (ts TileSet) Len() int {
return len(ts.set)
}
// Merge adds all tiles in other into ts.
func (ts *TileSet) Merge(other TileSet) {
ts.checkInit()
for tile := range other.set {
ts.set[tile] = struct{}{}
}
}
// Eq returns if ts contains exactly the same contents as other.
func (ts TileSet) Eq(other TileSet) bool {
if ts.Len() != other.Len() {
return false
}
for tile := range ts.set {
if !other.Has(tile) {
return false
}
}
return true
}
// Iter returns an iterator for this TileSet.
func (ts TileSet) Iter() <-chan Tile {
iter := make(chan Tile, 5)
go func() {
for tile := range ts.set {
iter <- tile
}
close(iter)
}()
return iter
}
// Slice returns a slice representation of ts
func (ts TileSet) Slice() []Tile {
slice := make([]Tile, 0, len(ts.set))
for tile := range ts.set {
slice = append(slice, tile)
}
return slice
}
// ToTileCoordSet converts ts into a TileCoordSet
func (ts TileSet) ToTileCoordSet() TileCoordSet {
var result TileCoordSet
for val := range ts.set {
result.Add(val.Coord)
}
return result
}
func (ts TileSet) String() string {
slice := ts.Slice()
var maxX, maxY int
for _, tile := range slice {
if tile.Coord.X > maxX {
maxX = tile.Coord.X
}
if tile.Coord.Y > maxY {
maxY = tile.Coord.Y
}
}
maxX++
maxY++
tilesAt := make([][]Tile, maxX)
for x := range tilesAt {
tilesAt[x] = make([]Tile, maxY)
}
for _, v := range slice {
tilesAt[v.Coord.X][v.Coord.Y] = v
}
var sb strings.Builder
sb.WriteByte('{')
for y := maxY - 1; y >= 0; y-- {
for x := 0; x < maxX; x++ {
if tile := tilesAt[x][y]; tile.Data.Type != TypeHole {
sb.WriteByte(byte(tile.Data.Color) + '0')
} else {
sb.WriteByte(' ')
}
}
if y > 0 {
sb.WriteByte('|')
}
}
sb.WriteByte('}')
return sb.String()
}
// MultiLineString returns a string representation of this tileset on multiple lines
func (ts TileSet) MultiLineString() string {
next := ts.String()
next = next[1 : len(next)-1]
next = strings.ReplaceAll(next, "|", "\n")
next += "\n"
return next
}