/
tris.go
212 lines (196 loc) · 3.53 KB
/
tris.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
package viertris
import (
"fmt"
"image/color"
"math/rand"
)
type ActiveTris struct {
Board *GameBoard
Rotation
X BoardDimension
Y BoardDimension
TrisKind
}
// Offsets on an ActiveTris takes into account rotation
func (at *ActiveTris) Offsets() [4][2]int8 {
return at.TestOffsets(at.Rotation)
}
func (at *ActiveTris) TestOffsets(rotation Rotation) [4][2]int8 {
rawOff := at.TrisKind.Offsets()
switch rotation {
case Rotation270:
for i, off := range rawOff {
off[0], off[1] = off[1], -1*off[0]
rawOff[i] = off
}
fallthrough
case Rotation180:
for i, off := range rawOff {
off[0], off[1] = off[1], -1*off[0]
rawOff[i] = off
}
fallthrough
case Rotation90:
for i, off := range rawOff {
off[0], off[1] = off[1], -1*off[0]
rawOff[i] = off
}
fallthrough
case Rotation0:
return rawOff
default:
panic(fmt.Sprintf("invalid rotation %v", at.Rotation))
}
}
func (at *ActiveTris) RotateLeft() {
newRotation := at.Rotation.RotateLeft()
off := at.TestOffsets(newRotation)
for _, o := range off {
x := int(at.X) + int(o[0])
y := int(at.Y) + int(o[1])
if at.Board.IsSet(x, y) || at.Board.IsOffscreen(x, y) {
return
}
}
at.Rotation = newRotation
}
func (at *ActiveTris) RotateRight() {
newRotation := at.Rotation.RotateRight()
off := at.TestOffsets(newRotation)
for _, o := range off {
x := int(at.X) + int(o[0])
y := int(at.Y) + int(o[1])
if at.Board.IsSet(x, y) || at.Board.IsOffscreen(x, y) {
return
}
}
at.Rotation = newRotation
}
func (at *ActiveTris) MoveLeft() {
minX := int(at.X)
off := at.Offsets()
for _, o := range off {
x := int(at.X) + int(o[0])
y := int(at.Y) + int(o[1])
if at.Board.IsSet(x-1, y) {
return
}
if x < minX {
minX = x
}
}
if minX > 0 {
at.X--
}
}
func (at *ActiveTris) MoveRight() {
maxX := int(at.X)
off := at.Offsets()
for _, o := range off {
x := int(at.X) + int(o[0])
y := int(at.Y) + int(o[1])
if at.Board.IsSet(x+1, y) {
return
}
if x > maxX {
maxX = x
}
}
if maxX < int(at.Board.Width-1) {
at.X++
}
}
func (at *ActiveTris) MoveDown() bool {
maxY := int(at.Y)
off := at.Offsets()
for _, o := range off {
y := int(at.Y) + int(o[1])
if y > maxY {
maxY = y
}
}
if maxY <= int(at.Board.Height-1) {
placed := at.Board.CheckIfTileIsPlaced()
if !placed {
at.Y++
}
return placed
}
return false
}
type TrisKind uint8
const (
KindNone TrisKind = iota
KindT TrisKind = iota
KindLine TrisKind = iota
KindSquare TrisKind = iota
KindZ TrisKind = iota
KindS TrisKind = iota
KindL TrisKind = iota
KindJ TrisKind = iota
KindFinal TrisKind = iota
)
var kindColors = []color.RGBA{
KindNone: {},
KindT: {200, 0, 0, 255},
KindLine: {0, 200, 0, 255},
KindSquare: {0, 0, 200, 255},
KindZ: {200, 200, 0, 255},
KindS: {200, 200, 200, 255},
KindL: {200, 0, 200, 255},
KindJ: {0, 200, 200, 255},
KindFinal: {50, 50, 50, 255},
}
func (tk TrisKind) Color() color.RGBA {
return kindColors[tk]
}
var kindOffsets = [][4][2]int8{
KindT: {
{0, 0},
{-1, 0},
{0, -1},
{1, 0},
},
KindLine: {
{0, 0},
{0, -1},
{0, 1},
{0, 2},
},
KindSquare: {
{0, 0},
{0, 1},
{1, 1},
{1, 0},
},
KindS: {
{0, 0},
{1, 0},
{0, 1},
{-1, 1},
},
KindZ: {
{0, 0},
{-1, 0},
{0, 1},
{1, 1},
},
KindL: {
{0, 0},
{0, -1},
{0, 1},
{1, 1},
},
KindJ: {
{0, 0},
{0, -1},
{0, 1},
{-1, 1},
},
}
func (tk TrisKind) Offsets() [4][2]int8 {
return kindOffsets[tk]
}
func RandomKind() TrisKind {
return TrisKind(rand.Intn(int(KindFinal-1)) + 1)
}