@@ -42,57 +42,73 @@ boolean playGrid[GRID_HEIGHT][GRID_WIDTH];
42
42
const uint8_t NUM_LEVEL_STEPS = 4 ;
43
43
const uint32_t LEVEL_STEPS [NUM_LEVEL_STEPS ] = {200 ,600 ,1800 ,5400 };
44
44
45
- // The single view of the square piece :
46
- // 00
47
- // 00
48
- pieceView_t piece00 = {{{1 ,0 }, {2 ,0 }, {1 ,1 }, {2 ,1 }}};
49
-
50
- // The two views of the bar piece :
51
- // 0000
52
- pieceView_t piece10 = {{{0 ,1 }, {1 ,1 }, {2 ,1 }, {3 ,1 }}};
53
- pieceView_t piece11 = {{{1 ,0 }, {1 ,1 }, {1 ,2 }, {1 ,3 }}};
54
-
55
- // The two views of the first S :
56
- // 00
57
- // 00
58
- pieceView_t piece20 = {{{0 ,0 }, {1 ,0 }, {1 ,1 }, {2 ,1 }}};
59
- pieceView_t piece21 = {{{2 ,0 }, {1 ,1 }, {2 ,1 }, {1 ,2 }}};
60
-
61
- // The two views of the second S :
62
- // 00
63
- // 00
64
- pieceView_t piece30 = {{{1 ,0 }, {2 ,0 }, {0 ,1 }, {1 ,1 }}};
65
- pieceView_t piece31 = {{{0 ,0 }, {0 ,1 }, {1 ,1 }, {1 ,2 }}};
66
-
67
- // The four views of the first L :
68
- // 000
69
- // 0
70
- pieceView_t piece40 = {{{0 ,1 }, {1 ,1 }, {2 ,1 }, {0 ,2 }}};
71
- pieceView_t piece41 = {{{1 ,0 }, {1 ,1 }, {1 ,2 }, {2 ,2 }}};
72
- pieceView_t piece42 = {{{2 ,0 }, {0 ,1 }, {1 ,1 }, {2 ,1 }}};
73
- pieceView_t piece43 = {{{0 ,0 }, {1 ,0 }, {1 ,1 }, {1 ,2 }}};
74
-
75
- // The four views of the T :
76
- // 000
77
- // 0
78
- pieceView_t piece50 = {{{0 ,1 }, {1 ,1 }, {2 ,1 }, {2 ,2 }}};
79
- pieceView_t piece51 = {{{1 ,0 }, {2 ,0 }, {1 ,1 }, {1 ,2 }}};
80
- pieceView_t piece52 = {{{0 ,0 }, {0 ,1 }, {1 ,1 }, {2 ,1 }}};
81
- pieceView_t piece53 = {{{1 ,0 }, {1 ,1 }, {1 ,2 }, {0 ,2 }}};
82
-
83
- // The four views of the second L :
84
- // 000
85
- // 0
86
- pieceView_t piece60 = {{{0 ,1 }, {1 ,1 }, {2 ,1 }, {1 ,2 }}};
87
- pieceView_t piece61 = {{{1 ,0 }, {1 ,1 }, {2 ,1 }, {1 ,2 }}};
88
- pieceView_t piece62 = {{{1 ,0 }, {0 ,1 }, {1 ,1 }, {2 ,1 }}};
89
- pieceView_t piece63 = {{{1 ,0 }, {0 ,1 }, {1 ,1 }, {1 ,2 }}};
90
-
91
- /** The pointer to the array for holding all the pieces declarations. */
92
- piece_t** pieces;
45
+ const piece_t pieces[7 ] = {
46
+ {{
47
+ // The single view of the square piece :
48
+ // 00
49
+ // 00
50
+ {{{1 ,0 }, {2 ,0 }, {1 ,1 }, {2 ,1 }}},
51
+ {{{1 ,0 }, {2 ,0 }, {1 ,1 }, {2 ,1 }}},
52
+ {{{1 ,0 }, {2 ,0 }, {1 ,1 }, {2 ,1 }}},
53
+ {{{1 ,0 }, {2 ,0 }, {1 ,1 }, {2 ,1 }}},
54
+ }},
55
+ {{
56
+ // The two views of the bar piece :
57
+ // 0000
58
+ {{{0 ,1 }, {1 ,1 }, {2 ,1 }, {3 ,1 }}},
59
+ {{{1 ,0 }, {1 ,1 }, {1 ,2 }, {1 ,3 }}},
60
+ {{{0 ,1 }, {1 ,1 }, {2 ,1 }, {3 ,1 }}},
61
+ {{{1 ,0 }, {1 ,1 }, {1 ,2 }, {1 ,3 }}},
62
+ }},
63
+ {{
64
+ // The two views of the first S :
65
+ // 00
66
+ // 00
67
+ {{{0 ,0 }, {1 ,0 }, {1 ,1 }, {2 ,1 }}},
68
+ {{{2 ,0 }, {1 ,1 }, {2 ,1 }, {1 ,2 }}},
69
+ {{{0 ,0 }, {1 ,0 }, {1 ,1 }, {2 ,1 }}},
70
+ {{{2 ,0 }, {1 ,1 }, {2 ,1 }, {1 ,2 }}},
71
+ }},
72
+ {{
73
+ // The two views of the second S :
74
+ // 00
75
+ // 00
76
+ {{{1 ,0 }, {2 ,0 }, {0 ,1 }, {1 ,1 }}},
77
+ {{{0 ,0 }, {0 ,1 }, {1 ,1 }, {1 ,2 }}},
78
+ {{{1 ,0 }, {2 ,0 }, {0 ,1 }, {1 ,1 }}},
79
+ {{{0 ,0 }, {0 ,1 }, {1 ,1 }, {1 ,2 }}},
80
+ }},
81
+ {{
82
+ // The four views of the first L :
83
+ // 000
84
+ // 0
85
+ {{{0 ,1 }, {1 ,1 }, {2 ,1 }, {0 ,2 }}},
86
+ {{{1 ,0 }, {1 ,1 }, {1 ,2 }, {2 ,2 }}},
87
+ {{{2 ,0 }, {0 ,1 }, {1 ,1 }, {2 ,1 }}},
88
+ {{{0 ,0 }, {1 ,0 }, {1 ,1 }, {1 ,2 }}},
89
+ }},
90
+ {{
91
+ // The four views of the T :
92
+ // 000
93
+ // 0
94
+ {{{0 ,1 }, {1 ,1 }, {2 ,1 }, {2 ,2 }}},
95
+ {{{1 ,0 }, {2 ,0 }, {1 ,1 }, {1 ,2 }}},
96
+ {{{0 ,0 }, {0 ,1 }, {1 ,1 }, {2 ,1 }}},
97
+ {{{1 ,0 }, {1 ,1 }, {1 ,2 }, {0 ,2 }}},
98
+ }},
99
+ {{
100
+ // The four views of the second L :
101
+ // 000
102
+ // 0
103
+ {{{0 ,1 }, {1 ,1 }, {2 ,1 }, {1 ,2 }}},
104
+ {{{1 ,0 }, {1 ,1 }, {2 ,1 }, {1 ,2 }}},
105
+ {{{1 ,0 }, {0 ,1 }, {1 ,1 }, {2 ,1 }}},
106
+ {{{1 ,0 }, {0 ,1 }, {1 ,1 }, {1 ,2 }}},
107
+ }},
108
+ };
93
109
94
110
/** The piece being played. */
95
- piece_t* currentPiece;
111
+ const piece_t* currentPiece;
96
112
/** The current position and view of the piece being played. */
97
113
pos_t position;
98
114
@@ -102,9 +118,9 @@ pos_t position;
102
118
* @param position the position and view of the piece to draw or remove.
103
119
* @param set 1 or 0 to draw or remove the piece.
104
120
*/
105
- void switchPiece (struct piece * piece , const pos_t & position , int set =1 ) {
121
+ void switchPiece (const piece_t * piece , const pos_t & position , int set =1 ) {
106
122
for (uint8_t i= 0 ;i< 4 ;i++ ) {
107
- coord_t element = ( * piece- > views) [position. view]. elements[i];
123
+ coord_t element = piece- > views[position. view]. elements[i];
108
124
LedSign :: Set (
109
125
13 - (element. y+ position. coord. y),
110
126
element. x+ position. coord. x,
@@ -147,79 +163,6 @@ void endGame() {
147
163
Figure :: Scroll90 (score);
148
164
}
149
165
150
- /* ----------------------------------------------------------------- */
151
- /**
152
- * Creates a piece structure for game initialization.
153
- * @return a pointer to the created piece structure.
154
- */
155
- struct piece* createPiece () {
156
- piece_t* created = (piece_t* ) malloc(sizeof(piece_t));
157
- memset(created, ' \0 ' , sizeof(piece_t));
158
- return created;
159
- }
160
-
161
- /* ----------------------------------------------------------------- */
162
- /**
163
- * Adds a view to an existing piece structure.
164
- * @param piece the piece which will be added a view.
165
- * @param view the view to add to the given piece.
166
- */
167
- void addView (struct piece * piece , struct pieceView * view ) {
168
- piece- > numViews++ ;
169
- piece- > views = (pieceView_t** ) realloc(piece- > views, (piece- > numViews)* sizeof(pieceView_t* ));
170
- piece- > views[piece- > numViews- 1 ] = view;
171
- }
172
-
173
- /* ----------------------------------------------------------------- */
174
- /**
175
- * Initialize the game data (the game pieces data).
176
- * Some parts are malloc-ed that may as well be on the stack,
177
- * weren't it that I fought with the compiler and complex constant
178
- * initializers.
179
- */
180
- void initGame () {
181
- // Initialize the pieces data structures.
182
- pieces = (piece_t** ) calloc(7 , sizeof(piece_t* ));
183
- pieces[0 ] = createPiece();
184
- addView(pieces[0 ], & piece00);
185
- pieces[1 ] = createPiece();
186
- addView(pieces[1 ], & piece10);
187
- addView(pieces[1 ], & piece11);
188
- pieces[2 ] = createPiece();
189
- addView(pieces[2 ], & piece20);
190
- addView(pieces[2 ], & piece21);
191
- pieces[3 ] = createPiece();
192
- addView(pieces[3 ], & piece30);
193
- addView(pieces[3 ], & piece31);
194
- pieces[4 ] = createPiece();
195
- addView(pieces[4 ], & piece40);
196
- addView(pieces[4 ], & piece41);
197
- addView(pieces[4 ], & piece42);
198
- addView(pieces[4 ], & piece43);
199
- pieces[5 ] = createPiece();
200
- addView(pieces[5 ], & piece50);
201
- addView(pieces[5 ], & piece51);
202
- addView(pieces[5 ], & piece52);
203
- addView(pieces[5 ], & piece53);
204
- pieces[6 ] = createPiece();
205
- addView(pieces[6 ], & piece60);
206
- addView(pieces[6 ], & piece61);
207
- addView(pieces[6 ], & piece62);
208
- addView(pieces[6 ], & piece63);
209
- }
210
-
211
- /* ----------------------------------------------------------------- */
212
- /**
213
- * Cleanup the dynamically allocated data (the game pieces data).
214
- */
215
- void cleanup () {
216
- for (uint8_t i= 0 ; i< 7 ; i++ ) {
217
- piece_t* pieceToFree = pieces[i];
218
- free(currentPiece);
219
- }
220
- free(pieces);
221
- }
222
-
223
166
/* ----------------------------------------------------------------- */
224
167
/**
225
168
* Game initialization, or reinitialization after a game over.
@@ -239,11 +182,11 @@ void startGame() {
239
182
* @param piece the piece to try and put on the play grid.
240
183
* @param position the position and view to try and put the piece.
241
184
*/
242
- boolean checkPieceMove (struct piece * piece , const pos_t & position ) {
185
+ boolean checkPieceMove (const piece_t * piece , const pos_t & position ) {
243
186
boolean isOk = true ;
244
187
245
188
for (uint8_t i= 0 ; i< 4 ; i++ ) {
246
- coord_t element = ( * piece- > views) [position. view]. elements[i];
189
+ coord_t element = piece- > views[position. view]. elements[i];
247
190
// Check x boundaries.
248
191
uint8_t eltXPos = element. x+ position. coord. x;
249
192
if (eltXPos> 8 || eltXPos< 0 ) {
@@ -280,7 +223,7 @@ void playerMovePiece()
280
223
if (analogRead(4 )> 1000 ) {
281
224
status = 1 ;
282
225
newPos = position;
283
- newPos. view = (newPos. view+ 1 )% currentPiece - > numViews ;
226
+ newPos. view = (newPos. view+ 1 )& 3 ;
284
227
moveSuccess = checkPieceMove(currentPiece, newPos);
285
228
if (moveSuccess) {
286
229
switchPiece(currentPiece, position, 0 );
@@ -344,7 +287,7 @@ void timerPieceDown(uint32_t& count) {
344
287
} else {
345
288
// Drop the piece on the grid.
346
289
for (uint8_t i= 0 ; i< 4 ; i++ ) {
347
- coord_t element = ( * currentPiece- > views) [position. view]. elements[i];
290
+ coord_t element = currentPiece- > views[position. view]. elements[i];
348
291
uint8_t eltXPos = element. x+ position. coord. x;
349
292
uint8_t eltYPos = element. y+ position. coord. y;
350
293
playGrid[eltYPos][eltXPos] = true ;
@@ -435,9 +378,9 @@ void processEndPiece() {
435
378
void nextPiece () {
436
379
position. coord. x = 3 ;
437
380
position. coord. y = 0 ;
438
- position. view = 0 ;
381
+ position. view = random ( 0 , 3 ) ;
439
382
440
- currentPiece = pieces[random (0 ,7 )];
383
+ currentPiece = & pieces[random (0 ,7 )];
441
384
442
385
if (! checkPieceMove(currentPiece, position)) {
443
386
endGame();
@@ -453,7 +396,6 @@ void setup() // run once, when the sketch starts
453
396
{
454
397
LedSign :: Init ();
455
398
randomSeed (analogRead(2 ));
456
- initGame();
457
399
startGame();
458
400
nextPiece();
459
401
}
0 commit comments