Skip to content

Commit 594ac11

Browse files
author
root
committed
Remove all the dynamic allocation.
Randomize the starting orientation of the piece.
1 parent 0ca4b07 commit 594ac11

File tree

2 files changed

+74
-133
lines changed

2 files changed

+74
-133
lines changed

lib/examples/LoLShield_Tetris/LoLShield_Tetris.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ typedef struct pieceView {
4242
* One Tetris piece object, made of one to four views.
4343
*/
4444
typedef struct piece {
45-
pieceView_t** views;
46-
uint8_t numViews;
45+
pieceView_t views[4];
4746
} piece_t;
4847

4948
/**

lib/examples/LoLShield_Tetris/LoLShield_Tetris.pde

Lines changed: 73 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -42,57 +42,73 @@ boolean playGrid[GRID_HEIGHT][GRID_WIDTH];
4242
const uint8_t NUM_LEVEL_STEPS = 4;
4343
const uint32_t LEVEL_STEPS[NUM_LEVEL_STEPS] = {200,600,1800,5400};
4444

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+
};
93109

94110
/** The piece being played. */
95-
piece_t* currentPiece;
111+
const piece_t* currentPiece;
96112
/** The current position and view of the piece being played. */
97113
pos_t position;
98114

@@ -102,9 +118,9 @@ pos_t position;
102118
* @param position the position and view of the piece to draw or remove.
103119
* @param set 1 or 0 to draw or remove the piece.
104120
*/
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) {
106122
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];
108124
LedSign::Set(
109125
13-(element.y+position.coord.y),
110126
element.x+position.coord.x,
@@ -147,79 +163,6 @@ void endGame() {
147163
Figure::Scroll90(score);
148164
}
149165

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-
223166
/* ----------------------------------------------------------------- */
224167
/**
225168
* Game initialization, or reinitialization after a game over.
@@ -239,11 +182,11 @@ void startGame() {
239182
* @param piece the piece to try and put on the play grid.
240183
* @param position the position and view to try and put the piece.
241184
*/
242-
boolean checkPieceMove(struct piece* piece, const pos_t& position) {
185+
boolean checkPieceMove(const piece_t* piece, const pos_t& position) {
243186
boolean isOk = true;
244187

245188
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];
247190
// Check x boundaries.
248191
uint8_t eltXPos = element.x+position.coord.x;
249192
if (eltXPos>8 || eltXPos<0) {
@@ -280,7 +223,7 @@ void playerMovePiece()
280223
if (analogRead(4)>1000) {
281224
status = 1;
282225
newPos = position;
283-
newPos.view = (newPos.view+1)%currentPiece->numViews;
226+
newPos.view = (newPos.view+1)&3;
284227
moveSuccess = checkPieceMove(currentPiece, newPos);
285228
if (moveSuccess) {
286229
switchPiece(currentPiece, position, 0);
@@ -344,7 +287,7 @@ void timerPieceDown(uint32_t& count) {
344287
} else {
345288
// Drop the piece on the grid.
346289
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];
348291
uint8_t eltXPos = element.x+position.coord.x;
349292
uint8_t eltYPos = element.y+position.coord.y;
350293
playGrid[eltYPos][eltXPos] = true;
@@ -435,9 +378,9 @@ void processEndPiece() {
435378
void nextPiece() {
436379
position.coord.x = 3;
437380
position.coord.y = 0;
438-
position.view = 0;
381+
position.view = random(0,3);
439382

440-
currentPiece = pieces[random(0,7)];
383+
currentPiece = &pieces[random(0,7)];
441384

442385
if (!checkPieceMove(currentPiece, position)) {
443386
endGame();
@@ -453,7 +396,6 @@ void setup() // run once, when the sketch starts
453396
{
454397
LedSign::Init();
455398
randomSeed(analogRead(2));
456-
initGame();
457399
startGame();
458400
nextPiece();
459401
}

0 commit comments

Comments
 (0)