-
Notifications
You must be signed in to change notification settings - Fork 7
/
piece.go
218 lines (184 loc) · 4.03 KB
/
piece.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
212
213
214
215
216
217
package main
import (
"fmt"
)
type Shape [][]uint8
type Piece struct {
Shape Shape
Number uint8
Rotations []Shape
Size int
}
func copyPiece(piece Piece) Piece {
return Piece{piece.Shape, piece.Number, piece.Rotations, piece.Size}
}
func (piece Piece) Flip() Piece {
var flipped = copyPiece(piece)
flipped.Shape = flip(piece.Shape)
return flipped
}
func flip(shape Shape) Shape {
var flipped Shape = copyShape(shape)
var cols = len(shape[0]) - 1
var i, j int
for i = 0; i < len(shape); i++ {
for j = 0; j < len(shape[0]); j++ {
flipped[i][j] = shape[i][cols-j]
}
}
return flipped
}
func (piece Piece) Rotate() Piece {
var rotated = copyPiece(piece)
rotated.Shape = rotate(piece.Shape)
return rotated
}
func rotate(shape Shape) Shape {
var n = len(shape)
var m = len(shape[0])
var rotatedShape Shape = make(Shape, m)
for ii := 0; ii < m; ii++ {
rotatedShape[ii] = make([]uint8, n)
}
for i := 0; i < n; i++ {
for j := 0; j < m; j++ {
rotatedShape[m-j-1][i] = shape[i][j]
}
}
return rotatedShape
}
func copyShape(shape Shape) Shape {
var copiedShape = make(Shape, len(shape))
for i := 0; i < len(shape); i++ {
copiedShape[i] = make([]uint8, len(shape[0]))
}
var i, j int
for i = 0; i < len(shape); i++ {
for j = 0; j < len(shape[0]); j++ {
copiedShape[i][j] = shape[i][j]
}
}
return copiedShape
}
func GetPiecesFromGrid(grid Grid) []Piece {
pieces := []Piece{}
values := getValuesFromGrid(grid)
var i int
for i = 0; i < len(values); i++ {
pieces = append(pieces, getPiece(grid, uint8(values[i])))
}
return pieces
}
func getPiece(grid Grid, pieceNumber uint8) Piece {
var minX = 1000
var minY = 1000
var maxX = 0
var maxY = 0
var i, j int
var size int = 0
for i = 0; i < len(grid); i++ {
for j = 0; j < len(grid[i]); j++ {
if grid[i][j] == pieceNumber {
size ++
if i < minX {
minX = i
}
if i > maxX {
maxX = i
}
if j < minY {
minY = j
}
if j > maxY {
maxY = j
}
}
}
}
pieceGrid := make(Shape, maxX-minX+1)
for i := range pieceGrid {
pieceGrid[i] = make([]uint8, maxY-minY+1)
}
for i = minX; i <= maxX; i++ {
for j = minY; j <= maxY; j++ {
if grid[i][j] == pieceNumber {
pieceGrid[i-minX][j-minY] = pieceNumber
}
}
}
return Piece{pieceGrid, pieceNumber, getRotations(pieceGrid), size}
}
// there's the piece itself, the three 90-degrees rotation of the piece plus the flipped
// piece and its three 90-degrees rotation: 8 in total. Equal rotations are excluded in
// case of some kind of symmetry
func getRotations(piece Shape) []Shape {
var rotations [8]Shape
rotations[0] = copyShape(piece)
var count = 1
var i int
for i = 0; i < 3; i++ {
piece = rotate(piece)
if ! containsShape(rotations[:], piece) {
rotations[count] = copyShape(piece)
count ++
}
}
piece = flip(piece)
if !containsShape(rotations[:], piece) {
rotations[count] = piece
count ++
}
for i = 0; i < 3; i++ {
piece = rotate(piece)
if ! containsShape(rotations[:], piece) {
rotations[count] = copyShape(piece)
count ++
}
}
return rotations[0:count]
}
func containsShape(shapes []Shape, newShape Shape) bool {
for _, shape := range shapes {
if areEqualPieces(shape, newShape) {
return true
}
}
return false
}
func areEqualPieces(shape1 [][]uint8, shape2 [][]uint8) bool {
if len(shape1) != len(shape2) || len(shape1[0]) != len(shape2[0]) {
return false
}
for i := 0; i < len(shape1); i++ {
for j := 0; j < len(shape1[0]); j++ {
if shape1[i][j] != shape2[i][j] {
return false
}
}
}
return true
}
func getValuesFromGrid(grid Grid) []uint8 {
var i, j int
var values = []uint8{}
for i = 0; i < len(grid); i++ {
for j = 0; j < len(grid[i]); j++ {
if !contains(values, grid[i][j]) {
values = append(values, grid[i][j])
}
}
}
return values
}
func contains(values []uint8, value uint8) bool {
var i int
for i = 0; i < len(values); i++ {
if values[i] == value {
return true
}
}
return false
}
func (piece Piece) String() string {
return fmt.Sprintf("%d", piece.Number)
}