-
Notifications
You must be signed in to change notification settings - Fork 1
/
polyominos.ts
332 lines (311 loc) · 9.08 KB
/
polyominos.ts
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// this file was derived from
/**
* Minos
* Built on
* MakeCode Arcade JavaScript Template v. 2.2
* Last update: 05 Jun 2019 ak
* (C) 2019 Robo Technical Group LLC
MIT License
Copyright 2019 Robo Technical Group LLC.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
enum Color {
Transparent, // 0
White, // 1 = RGB(255, 255, 255)
Red, // 2 = RGB(255, 33, 33)
Pink, // 3 = RGB(255, 147, 196)
Orange, // 4 = RGB(255, 129, 53)
Yellow, // 5 = RGB(255, 246, 9)
Aqua, // 6 = RGB(36, 156, 163)
BrightGreen, // 7 = RGB(120, 220, 82)
Blue, // 8 = RGB(0, 63, 173)
LightBlue, // 9 = RGB(135, 242, 255)
Purple, // 10 = RGB(142, 46, 196)
RoseBouquet, // 11 = RGB(164, 131, 159)
Wine, // 12 = RGB(92, 64, 108)
Bone, // 13 = RGB(229, 205, 196)
Brown, // 14 = RGB(145, 70, 61)
Black // 15 = RGB(0, 0, 0)
} // enum Color
function toRGB(c: Color) {
switch(c) {
case Color.Transparent: return 0;
case Color.White: return neopixel.rgb(255, 255, 255);
case Color.Red: return neopixel.rgb(255, 33, 33);
case Color.Pink: return neopixel.rgb(255, 147, 196);
case Color.Orange: return neopixel.rgb(255, 129, 53)
case Color.Yellow: return neopixel.rgb(255, 246, 9)
case Color.Aqua: return neopixel.rgb(36, 156, 163)
case Color.BrightGreen: return neopixel.rgb(120, 220, 82)
case Color.Blue: return neopixel.rgb(0, 63, 173)
case Color.LightBlue: return neopixel.rgb(135, 242, 255)
case Color.Purple: return neopixel.rgb(142, 46, 196)
case Color.RoseBouquet: return neopixel.rgb(164, 131, 159)
case Color.Wine: return neopixel.rgb(92, 64, 108)
case Color.Bone: return neopixel.rgb(229, 205, 196)
case Color.Brown: return neopixel.rgb(145, 70, 61)
case Color.Black: return neopixel.rgb(0, 0, 0)
}
}
interface ActivePolyomino {
change: Coordinate
index: number
location: Coordinate
nextDrop: number
orientation: number
}
interface Coordinate {
column: number
row: number
}
interface Polyomino {
blocks: string[][]
fillColor: number
}
const BLOCK_SIZE: number = 1
const COLUMNS: number = 10
const ROWS: number = 16
const TETROMINOES: Polyomino[] = [
// I
{
blocks: [
[
'XXXX'
], [
'X',
'X',
'X',
'X'
]
],
fillColor: toRGB(Color.LightBlue)
},
// O
{
blocks: [
[
'XX',
'XX'
]
],
fillColor: toRGB(Color.Yellow)
},
// T
{
blocks: [
[
'XXX',
'.X.'
], [
'.X',
'XX',
'.X'
], [
'.X.',
'XXX'
], [
'X.',
'XX',
'X.'
]
],
fillColor: toRGB(Color.Purple)
},
// J
{
blocks: [
[
'.X',
'.X',
'XX'
], [
'X..',
'XXX'
], [
'XX',
'X.',
'X.'
], [
'XXX',
'..X'
]
],
fillColor: toRGB(Color.Aqua)
},
// L
{
blocks: [
[
'X.',
'X.',
'XX'
], [
'XXX',
'X..'
], [
'XX',
'.X',
'.X'
], [
'..X',
'XXX'
]
],
fillColor: toRGB(Color.Orange)
},
// S
{
blocks: [
[
'.XX',
'XX.'
], [
'X.',
'XX',
'.X'
]
],
fillColor: toRGB(Color.BrightGreen)
},
// Z
{
blocks: [
[
'XX.',
'.XX'
], [
'.X',
'XX',
'X.'
]
],
fillColor: toRGB(Color.Red)
}
]
let gameShapes: Polyomino[] = null
let gameState: number[][] = null
// Copies a line in the game state.
// @param {number} source - Row to copy.
// @param {number} destination - Row to place the copied line.
//
function copyLine(source: number, destination: number): void {
for (let index: number = 0; index < COLUMNS; index++) {
setBlock(destination, index, gameState[source][index])
}
}
function drawGameState(): void {
for (let row: number = 0; row < ROWS; row++) {
screen.setPixel(0, row, toRGB(Color.Wine));
screen.setPixel(11, row, toRGB(Color.Wine));
for (let column: number = 0; column < COLUMNS; column++) {
let state: number = gameState[row][column]
screen.setPixel(column+1, row, state > -1 ? gameShapes[state].fillColor : 0)
}
}
}
//
// @param {Polyomino} poly - Polyomino to draw.
// @param {number} row - Row of location to draw polyomino.
// @param {number} column - Column of location to draw polyomino.
//
function drawPoly(poly: Polyomino, row: number, column: number): void {
let blocks: string[] = poly.blocks[0]
let r: number = row
let c: number
for (let s of blocks) {
c = column
for (let char of s) {
if (char !== '.') {
screen.setPixel(c, r, poly.fillColor)
}
c++
}
r++
}
}
//
// Initializes the global game state variable.
//
function initGameState(): void {
gameState = []
for (let row: number = 0; row < ROWS; row++) {
let gameStateRow: number[] = []
for (let column: number = 0; column < COLUMNS; column++) {
gameStateRow.push(-1)
}
gameState.push(gameStateRow)
}
}
//
// Place a block into the game state.
// @param {number} row - Row of cell to update.
// @param {number} column - Column of cell to update.
// @param {number} state - Index of polyomino related to this cell; -1 to clear the state.
//
function setBlock(row: number, column: number, state: number): void {
if (row >= 0 && row < ROWS && column >= 0 && column < COLUMNS) {
gameState[row][column] = state
}
}
//
// Place a polyomino into the game state.
// @param {ActivePolyomino} polyState - Polyomino to place into the game state.
// @param {boolean} erase - True to remove the polyomino; false to place to polyomino.
// @param {boolean} testOnly - True to test if polyomino can be placed; false to place polyomino.
// @return {boolean} - True if polyomino can be placed; false if not
//
function setPoly(polyState: ActivePolyomino, erase: boolean = false, testOnly: boolean = false): boolean {
let poly: Polyomino = gameShapes[polyState.index]
let blocks: string[] = poly.blocks[polyState.orientation]
let r: number = polyState.location.row
let c: number
let toReturn = true
for (let s of blocks) {
c = polyState.location.column
for (let char of s) {
if (char !== '.') {
if (!testBlock(r, c)) {
toReturn = false
}
if (!testOnly) {
setBlock(r, c, erase ? -1 : polyState.index)
}
}
c++
}
r++
}
return toReturn
}
//
// Determines if a block can be placed in the game state.
// @param {number} row - Row of cell to test.
// @param {number} column - Column of cell to test.
// @return {boolean} Cell is empty and within the bounds of the game state
//
function testBlock(row: number, column: number): boolean {
if (row < 0) {
// Only verify that column is in-bounds if row is negative.
if (column >= 0 && column <= COLUMNS) {
return true
} else {
return false
}
} else {
if (row >= 0 && row < ROWS && column >= 0 && column < COLUMNS) {
return gameState[row][column] === -1
} else {
return false
}
}
}