ref: 5c15b730b8254ace1a3345a013bdd22a39b6a015 netris/pkg/mino/mino.go -rw-r--r-- 5.1 KiB View raw
                                                                                
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package mino

import (
	"sort"
	"strconv"
	"strings"
)

type Mino []Point

const (
	Monomino = "(0,0)"

	Domino = "(0,0),(1,0)"

	TrominoI = "(0,0),(1,0),(2,0)"
	TrominoL = "(0,0),(1,0),(0,1)"

	TetrominoI = "(0,0),(1,0),(2,0),(3,0)"
	TetrominoO = "(0,0),(1,0),(0,1),(1,1)"
	TetrominoT = "(0,0),(1,0),(2,0),(1,1)"
	TetrominoS = "(0,0),(1,0),(1,1),(2,1)"
	TetrominoZ = "(1,0),(2,0),(0,1),(1,1)"
	TetrominoJ = "(0,0),(1,0),(2,0),(0,1)"
	TetrominoL = "(0,0),(1,0),(2,0),(2,1)"

	PentominoF = "(0,0),(1,0),(1,1),(2,1),(1,2)"
	PentominoE = "(1,0),(2,0),(0,1),(1,1),(1,2)"
	PentominoJ = "(0,0),(1,0),(2,0),(3,0),(0,1)"
	PentominoL = "(0,0),(1,0),(2,0),(3,0),(3,1)"
	PentominoP = "(0,0),(1,0),(2,0),(0,1),(1,1)"
	PentominoZ = "(1,0),(2,0),(1,1),(0,2),(1,2)"
	PentominoI = "(0,0),(1,0),(2,0),(3,0),(4,0)"
	PentominoX = "(1,0),(0,1),(1,1),(2,1),(1,2)"
	PentominoV = "(0,0),(1,0),(2,0),(0,1),(0,2)"
	PentominoB = "(0,0),(1,0),(2,0),(1,1),(2,1)"
	PentominoN = "(1,0),(2,0),(3,0),(0,1),(1,1)"
	PentominoG = "(0,0),(1,0),(2,0),(2,1),(3,1)"
	PentominoS = "(0,0),(1,0),(1,1),(1,2),(2,2)"
	PentominoT = "(0,0),(1,0),(2,0),(1,1),(1,2)"
	PentominoU = "(0,0),(1,0),(2,0),(0,1),(2,1)"
	PentominoW = "(1,0),(2,0),(0,1),(1,1),(0,2)"
	PentominoY = "(0,0),(1,0),(2,0),(3,0),(2,1)"
	PentominoR = "(0,0),(1,0),(2,0),(3,0),(1,1)"
)

func NewMino(points string) Mino {
	var m Mino

	var last int
	for i, p := range strings.Split(strings.ReplaceAll(strings.ReplaceAll(points, "(", ""), ")", ""), ",") {
		v, err := strconv.Atoi(p)
		if err != nil {
			return nil
		}
		if i%2 == 0 {
			last = v
			continue
		}

		m = append(m, Point{last, v})
	}

	return m
}

func (m Mino) Equal(other Mino) bool {
	if len(m) != len(other) {
		return false
	}

	for i := 0; i < len(m); i++ {
		if !m.HasPoint(other[i]) {
			return false
		}
	}

	return true
}

func (m Mino) String() string {
	sort.Sort(m)

	var b strings.Builder
	b.Grow(5*len(m) + (len(m) - 1))

	for i := range m {
		if i > 0 {
			b.WriteRune(',')
		}

		b.WriteRune('(')
		b.WriteString(strconv.Itoa(m[i].X))
		b.WriteRune(',')
		b.WriteString(strconv.Itoa(m[i].Y))
		b.WriteRune(')')
	}

	return b.String()
}

func (m Mino) Len() int      { return len(m) }
func (m Mino) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
func (m Mino) Less(i, j int) bool {
	return m[i].Y < m[j].Y || (m[i].Y == m[j].Y && m[i].X < m[j].X)
}

func (m Mino) Size() (int, int) {
	var x, y int
	for _, p := range m {
		if p.X > x {
			x = p.X
		}
		if p.Y > y {
			y = p.Y
		}
	}

	return x + 1, y + 1
}

func (m Mino) Render() string {
	var (
		w, h = m.Size()
		c    = Point{0, h - 1}
		b    strings.Builder
	)
	for y := h - 1; y >= 0; y-- {
		c.X = 0
		c.Y = y

		for x := 0; x < w; x++ {
			if !m.HasPoint(Point{x, y}) {
				continue
			}

			for i := x - c.X; i > 0; i-- {
				b.WriteRune(' ')
			}

			b.WriteRune('X')
			c.X = x + 1
		}

		b.WriteRune('\n')
	}

	return b.String()
}

func (m Mino) HasPoint(p Point) bool {
	for _, mp := range m {
		if mp == p {
			return true
		}
	}

	return false
}

func (m Mino) minCoords() (int, int) {
	minx := m[0].X
	miny := m[0].Y

	for _, p := range m[1:] {
		if p.X < minx {
			minx = p.X
		}
		if p.Y < miny {
			miny = p.Y
		}
	}

	return minx, miny
}

func (m Mino) Origin() Mino {
	minx, miny := m.minCoords()

	newMino := make(Mino, len(m))
	for i, p := range m {
		newMino[i].X = p.X - minx
		newMino[i].Y = p.Y - miny
	}

	return newMino
}

func (m Mino) Variations() []Mino {
	v := make([]Mino, 3)
	for i := 0; i < 3; i++ {
		v[i] = make(Mino, len(m))
	}

	for j := 0; j < len(m); j++ {
		v[0][j] = m[j].Rotate90()
		v[1][j] = m[j].Rotate180()
		v[2][j] = m[j].Rotate270()
	}

	return v
}

func (m Mino) Canonical() Mino {
	var (
		ms = m.Origin().String()
		c  = -1
		v  = m.Origin().Variations()
		vs string
	)

	for i := 0; i < 3; i++ {
		vs = v[i].Origin().String()
		if vs < ms {
			c = i
			ms = vs
		}
	}

	if c == -1 {
		return m.Origin().Flatten().Origin()
	}

	return v[c].Origin().Flatten().Origin()
}

func (m Mino) Flatten() Mino {
	var (
		w, h  = m.Size()
		sides [4]int // Left Top Right Bottom
	)
	for i := 0; i < len(m); i++ {
		if m[i].Y == 0 {
			sides[3]++
		} else if m[i].Y == (h - 1) {
			sides[1]++
		}

		if m[i].X == 0 {
			sides[0]++
		} else if m[i].X == (w - 1) {
			sides[2]++
		}
	}

	var (
		largestSide   = 3
		largestLength = sides[3]
	)
	for i, s := range sides[:2] {
		if s > largestLength {
			largestSide = i
			largestLength = s
		}
	}

	var rotateFunc func(Point) Point
	switch largestSide {
	case 0: // Left
		rotateFunc = Point.Rotate270
	case 1: // Top
		rotateFunc = Point.Rotate180
	case 2: // Right
		rotateFunc = Point.Rotate90
	default: // Bottom
		return m
	}

	newMino := make(Mino, len(m))
	copy(newMino, m)
	for i := 0; i < len(m); i++ {
		newMino[i] = rotateFunc(newMino[i])
	}

	return newMino
}

func (m Mino) newPoints() Mino {
	var newMino Mino

	for _, p := range m {
		for _, np := range p.Neighborhood() {
			if !m.HasPoint(np) {
				newMino = append(newMino, np)
			}
		}
	}

	return newMino
}

func (m Mino) newMinos() []Mino {
	points := m.newPoints()

	minos := make([]Mino, len(points))
	for i, p := range points {
		minos[i] = append(m, p).Canonical()
	}

	return minos
}