1010
Check-in [f6067db581]
Not logged in
Public Repositories
mwm's Repositories

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Ok, it's working and ready for a first release.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:f6067db581d1cef4bc55fa3442c251ba51e3721e
User & Date: mwm 2016-10-31 17:07:52
Context
2016-10-31
17:28
Fix new shape generation to generate distinct shapes in each set. check-in: e8fcbb0e83 user: mwm tags: trunk
17:07
Ok, it's working and ready for a first release. check-in: f6067db581 user: mwm tags: trunk
2016-10-30
18:52
It played a game! Time to add UI frills. check-in: 626ae7acf8 user: mwm tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to Tiny-1010.ino.

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
...
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
...
155
156
157
158
159
160
161
162










163
164
165
166
167
168
169
170
171
172
173
174
...
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
...
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
333




334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
...
360
361
362
363
364
365
366
367
368
369









370



371
372
373
374

375
376
 * point.
 *
 * Shapes are presented in groups of three. After all three have been placed,
 * another set of three is presented. If it isn't possible to place any existing
 * block, the game ends.
 */


#include <Arduboy2.h>

Arduboy2 arduboy;


/* The  board */
const byte BOARD_SIZE = 10;	// a board_size by board_size game
const byte BLOCK_SIZE = 6;	// Each tile is tile_size pixels on a side.
const byte BOARD_X = 62;	// Left edge of board
const byte BOARD_Y = 2;		// Top edge of the board.
const byte EMPTY = 0;		// Name of the empty tile.

const byte REMOVED = 255;	// A tile about to be emptied.

// Tile bitmaps. All 7x7, a 1-pixel border around a 5x5 color.
const byte TILE_SIZE = 7;			// Size of a color tile to draw.
const byte PROGMEM colors[][TILE_SIZE] = {
     {0x7F, 0x41, 0x41, 0x41, 0x41, 0x41, 0x7F},// EMPTY must be in position 0.
     {0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F},// 3x3 square
     {0x7F, 0x41, 0x5D, 0x5D, 0x5D, 0x41, 0x7F},// 2x2 square
     {0x7F, 0x41, 0x41, 0x49, 0x41, 0x41, 0x7F},// 1x1 square
     {0x7F, 0x7F, 0x7F, 0x41, 0x7F, 0x7F, 0x7F},// vertical 5 long
     {0x7F, 0x41, 0x7F, 0x7F, 0x7F, 0x41, 0x7F},// vertical 4 long
     {0x7F, 0x41, 0x7F, 0x41, 0x7F, 0x41, 0x7F},// vertical 3 long
     {0x7F, 0x41, 0x41, 0x7F, 0x41, 0x41, 0x7F},// vertical 2 long
     {0x7F, 0x77, 0x77, 0x77, 0x77, 0x77, 0x7F},// horizontal 4 long
................................................................................
     {2, 2, fours[4]},
     {2, 2, fours[5]},
     {2, 2, fours[6]},
};


/* The game */
typedef enum {DONE, SELECTING, MOVING} game_state;

const byte WAIT_LEN = 3;

struct game {
     game_state state;
     unsigned high, score;
     byte delay;
     int8_t waiting[WAIT_LEN];	// Shapes ready to be played.
     byte selected;	        // Current queue entry
     byte x, y;	// And it's location on the board.
     byte board[BOARD_SIZE][BOARD_SIZE];
} game;





/* And the code */
// Returns true if the shape is over clear tiles.
byte
shape_clear(const shape *const s, const byte x, const byte y) {
     if (x + s->width > BOARD_SIZE || y + s->height > BOARD_SIZE)
          return 0;
................................................................................
}
     
// Place a shape on the board
void
new_shapes(void) {
     for (byte i = 0; i < WAIT_LEN; i += 1)
          game.waiting[i] = random(sizeof(shapes) / sizeof(shape));
     game.selected = 0;










}

void
make_move(void)  {
     const shape *const s = &shapes[game.waiting[game.selected]];
     byte x;

     for (byte y = 0; y < s->height; y += 1)
          for (byte x = 0; x < s->width; x += 1)
               if (s->tiles[x + y * s->width] != EMPTY) {
                    game.score += 1;
                    game.board[game.x + x][game.y + y] 
................................................................................
     if (game.score > game.high)
          game.high = game.score;
     for (byte y = 0; y < BOARD_SIZE; y += 1)
          for (byte x = 0; x < BOARD_SIZE; x += 1)
               if (game.board[x][y] == REMOVED)
                    game.board[x][y] = EMPTY;

     game.waiting[game.selected] = -1;
     for (x = 0; x < WAIT_LEN; x += 1)
          if (game.waiting[x] >= 0) {
               game.selected = x;
               break;
          }
     if (x >= WAIT_LEN)
          new_shapes();
     game.state = SELECTING;
     for (byte s = 0; s < WAIT_LEN; s += 1)
          if (game.waiting[s] >= 0)
               for (byte x = 0; x < BOARD_SIZE; x += 1)
                    for (byte y = 0; y < BOARD_SIZE; y += 1)
                         if (shape_clear(&shapes[game.waiting[s]], x, y)) {
                              game.state = SELECTING;
                              return;
                         }
     game.state = DONE;
}

void
reset_game(void) {
     arduboy.initRandomSeed();
     game.delay = 150;
     game.score = 0;
     for (byte i = 0; i < BOARD_SIZE; i += 1)
          for (byte j = 0; j < BOARD_SIZE; j += 1)
               game.board[i][j] = EMPTY;

     new_shapes();
     game.state = SELECTING;
}

// Color things not on the game board
void
color_tile(const byte color, const byte x, const byte y) {
     arduboy.drawBitmap(x, y, colors[color % (sizeof(colors) / TILE_SIZE)],
                        TILE_SIZE, TILE_SIZE);
}

void
color_shape(const shape *const s, const byte x, const byte y) {
     for (byte j = 0; j < s->height; j += 1)
          for (byte i = 0; i < s->width; i += 1)
               if (s->tiles[i + j * s->width] != EMPTY)


                    color_tile(s->tiles[i + j * s->width],
                               x + i * BLOCK_SIZE, y + j * BLOCK_SIZE);

}

// Draw things on the game board
void
draw_tile(const byte color, const byte x, const byte y) {
     color_tile(color, BOARD_X + x * BLOCK_SIZE, BOARD_Y + y * BLOCK_SIZE);
}
................................................................................
          arduboy.write(score + '0');
     } else {
          draw_score(score / 10);
          arduboy.write(score % 10 + '0');
     }
}
















byte
draw_game(void) {
     byte can_place = 0;





     arduboy.clear();
     draw_score(game.high);




     arduboy.setCursor(0, 54);
     draw_score(game.score);



     const shape *const s = &shapes[game.waiting[game.selected]];


     for (byte y = 0; y < BOARD_SIZE; y += 1)
          for (byte x = 0; x < BOARD_SIZE; x += 1)
               draw_tile(game.board[x][y], x, y);













     if (game.state == DONE) {
          arduboy.setCursor(0, 32);
          for (char *p = "Game Over!"; *p; p += 1)
               arduboy.write(*p);
     } else {
          color_shape(s, 32 - s->width * BLOCK_SIZE/2,
                         27 - s->height * BLOCK_SIZE / 2);
          if (game.state == SELECTING)
               arduboy.digitalWriteRGB(RGB_OFF, RGB_OFF, RGB_OFF);




          else {


               can_place = shape_clear(s, game.x, game.y);
               arduboy.digitalWriteRGB(can_place ? RGB_OFF : RGB_ON,
                                       can_place ? RGB_ON : RGB_OFF, RGB_OFF);
               draw_shape(&shapes[game.waiting[game.selected]], game.x, game.y);
          }
     }

     arduboy.display();
     return can_place;
}













/* Arduino API */



void
setup(void) {


     arduboy.begin();










     reset_game();



     game.high = 0;







}

void
loop(void) {
     const shape *const s = &shapes[game.waiting[game.selected]];
     const byte can_place = draw_game();

     arduboy.pollButtons();







     switch (game.state) {
     case SELECTING:
          if (arduboy.pressed(UP_BUTTON))
               do
                    game.selected = (game.selected + 1) % WAIT_LEN;
               while (game.waiting[game.selected] < 0);
          else if (arduboy.pressed(DOWN_BUTTON))
               do {
                    if (game.selected)
                         game.selected -= 1;
                    else
                         game.selected = WAIT_LEN - 1;
               } while (game.waiting[game.selected] < 0);




          else if (arduboy.pressed(RIGHT_BUTTON)) {
               game.state = MOVING;
               game.x = game.y = 0;
               while (game.board[game.x][game.y] != EMPTY) {
                    game.x = (game.x + 1) % (BOARD_SIZE - s->width + 1);
                    if (game.x == 0)
                         game.y = (game.y + 1) % (BOARD_SIZE - s->height + 1);
               }
          } else if (arduboy.pressed(LEFT_BUTTON)) {
               game.state = MOVING;
               game.x = game.y = 0;
               while (game.board[game.x][game.y] != EMPTY) {
                    game.y = (game.y + 1) % (BOARD_SIZE - s->height + 1);
                    if (game.y == 0)
                         game.x = (game.x + 1) % (BOARD_SIZE - s->width + 1);
               }
          }
................................................................................
          else if (arduboy.pressed(RIGHT_BUTTON)
                   && (game.x < BOARD_SIZE - s->width))
               game.x += 1;
          else if (arduboy.pressed(LEFT_BUTTON)) {
               if (game.x > 0)
                    game.x -= 1;
               else
                    game.state = SELECTING;
          }
          break;









     case DONE:



          if (!arduboy.notPressed(A_BUTTON | B_BUTTON))
               reset_game();
          break;
     }

     delay(game.delay);
}







>

>









>






|







 







|
<

>
|
<
|


<




>
>
>







 







|
>
>
>
>
>
>
>
>
>
>




|







 







|
<
<
|
<
|
<
<
<





|


|




<







|













|
>
>


>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



>
>
>
>
>

<
>
>
>
>
|
<
>
>

<
>

<
<
<
>
>
>
>
>
>
>
>
>
>
>
>
|
<
<
<
<
<
|
|
<
<
>
>
>
>
|
>
>
|
<
<
<


>




>
>
>
>
>
>
>
>
>
>
>
>

>
>
>


>
>

>
>
>
>
>
>
>
>
>
>
|
>
>
>
|
>
>
>
>
>
>
>




|




>
>
>
>
>
>
|


<
<
<
<

|
|

|
|
>
>
>
>

|







|







 







|


>
>
>
>
>
>
>
>
>

>
>
>
|



>


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
...
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
...
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
...
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
...
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
333
334
335
336
337
338



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406




407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
...
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
 * point.
 *
 * Shapes are presented in groups of three. After all three have been placed,
 * another set of three is presented. If it isn't possible to place any existing
 * block, the game ends.
 */

#include <EEPROM.h>
#include <Arduboy2.h>

Arduboy2 arduboy;


/* The  board */
const byte BOARD_SIZE = 10;	// a board_size by board_size game
const byte BLOCK_SIZE = 6;	// Each tile is tile_size pixels on a side.
const byte BOARD_X = 62;	// Left edge of board
const byte BOARD_Y = 2;		// Top edge of the board.
const byte EMPTY = 0;		// Name of the empty tile.
const byte CLEAR = 1;		// WHITE tile for clearing.
const byte REMOVED = 255;	// A tile about to be emptied.

// Tile bitmaps. All 7x7, a 1-pixel border around a 5x5 color.
const byte TILE_SIZE = 7;			// Size of a color tile to draw.
const byte PROGMEM colors[][TILE_SIZE] = {
     {0x7F, 0x41, 0x41, 0x41, 0x41, 0x41, 0x7F},// EMPTY must be in position 0.
     {0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F},// 3x3 square, CLEAR in position 1.
     {0x7F, 0x41, 0x5D, 0x5D, 0x5D, 0x41, 0x7F},// 2x2 square
     {0x7F, 0x41, 0x41, 0x49, 0x41, 0x41, 0x7F},// 1x1 square
     {0x7F, 0x7F, 0x7F, 0x41, 0x7F, 0x7F, 0x7F},// vertical 5 long
     {0x7F, 0x41, 0x7F, 0x7F, 0x7F, 0x41, 0x7F},// vertical 4 long
     {0x7F, 0x41, 0x7F, 0x41, 0x7F, 0x41, 0x7F},// vertical 3 long
     {0x7F, 0x41, 0x41, 0x7F, 0x41, 0x41, 0x7F},// vertical 2 long
     {0x7F, 0x77, 0x77, 0x77, 0x77, 0x77, 0x7F},// horizontal 4 long
................................................................................
     {2, 2, fours[4]},
     {2, 2, fours[5]},
     {2, 2, fours[6]},
};


/* The game */
typedef enum {PAUSED, DONE, SELECTING, MOVING} game_state;

const byte WAIT_LEN = 3;

struct game_data {

     unsigned long high, score;
     byte delay;
     int8_t waiting[WAIT_LEN];	// Shapes ready to be played.

     byte x, y;	// And it's location on the board.
     byte board[BOARD_SIZE][BOARD_SIZE];
} game;

// Ephemeral values.
byte selected;	        // Current queue entry
game_state state;

/* And the code */
// Returns true if the shape is over clear tiles.
byte
shape_clear(const shape *const s, const byte x, const byte y) {
     if (x + s->width > BOARD_SIZE || y + s->height > BOARD_SIZE)
          return 0;
................................................................................
}
     
// Place a shape on the board
void
new_shapes(void) {
     for (byte i = 0; i < WAIT_LEN; i += 1)
          game.waiting[i] = random(sizeof(shapes) / sizeof(shape));
     selected = 0;
}

void
select_next(void) {
     for (byte x = 0; x < WAIT_LEN; x += 1)
          if (game.waiting[x] >= 0) {
               selected = x;
               return;
          }
     new_shapes();
}

void
make_move(void)  {
     const shape *const s = &shapes[game.waiting[selected]];
     byte x;

     for (byte y = 0; y < s->height; y += 1)
          for (byte x = 0; x < s->width; x += 1)
               if (s->tiles[x + y * s->width] != EMPTY) {
                    game.score += 1;
                    game.board[game.x + x][game.y + y] 
................................................................................
     if (game.score > game.high)
          game.high = game.score;
     for (byte y = 0; y < BOARD_SIZE; y += 1)
          for (byte x = 0; x < BOARD_SIZE; x += 1)
               if (game.board[x][y] == REMOVED)
                    game.board[x][y] = EMPTY;

     game.waiting[selected] = -1;


     select_next();





     for (byte s = 0; s < WAIT_LEN; s += 1)
          if (game.waiting[s] >= 0)
               for (byte x = 0; x < BOARD_SIZE; x += 1)
                    for (byte y = 0; y < BOARD_SIZE; y += 1)
                         if (shape_clear(&shapes[game.waiting[s]], x, y)) {
                              state = SELECTING;
                              return;
                         }
     state = DONE;
}

void
reset_game(void) {

     game.delay = 150;
     game.score = 0;
     for (byte i = 0; i < BOARD_SIZE; i += 1)
          for (byte j = 0; j < BOARD_SIZE; j += 1)
               game.board[i][j] = EMPTY;

     new_shapes();
     state = SELECTING;
}

// Color things not on the game board
void
color_tile(const byte color, const byte x, const byte y) {
     arduboy.drawBitmap(x, y, colors[color % (sizeof(colors) / TILE_SIZE)],
                        TILE_SIZE, TILE_SIZE);
}

void
color_shape(const shape *const s, const byte x, const byte y) {
     for (byte j = 0; j < s->height; j += 1)
          for (byte i = 0; i < s->width; i += 1)
               if (s->tiles[i + j * s->width] != EMPTY) {
                    arduboy.drawBitmap(x + i * BLOCK_SIZE, y + j * BLOCK_SIZE,
                                       colors[CLEAR], TILE_SIZE, TILE_SIZE, BLACK);
                    color_tile(s->tiles[i + j * s->width],
                               x + i * BLOCK_SIZE, y + j * BLOCK_SIZE);
               }
}

// Draw things on the game board
void
draw_tile(const byte color, const byte x, const byte y) {
     color_tile(color, BOARD_X + x * BLOCK_SIZE, BOARD_Y + y * BLOCK_SIZE);
}
................................................................................
          arduboy.write(score + '0');
     } else {
          draw_score(score / 10);
          arduboy.write(score % 10 + '0');
     }
}

void
draw_board(void) {
     for (byte y = 0; y < BOARD_SIZE; y += 1)
          for (byte x = 0; x < BOARD_SIZE; x += 1)
               draw_tile(game.board[x][y], x, y);
}

const byte TOP_LINE = 16;
void
draw_msg(const char *const msg) {
     arduboy.setCursor(0, TOP_LINE);
     arduboy.print(msg);
}


byte
draw_game(void) {
     byte can_place = 0;
     const shape *const s = &shapes[game.waiting[selected]];
     byte red, green, blue;
     const char *const menu[] = {"  Paused\n\n\33 Resume \32",
                                 "  Paused\n\n\33  New   \32"};

     arduboy.clear();

     red = green = blue = RGB_OFF;

     arduboy.print("Score\n");
     arduboy.print(game.score);
     arduboy.setCursor(0, 44); 

     arduboy.print("High\n");
     arduboy.print(game.high);


     draw_board();




     switch (state) {
     case DONE:
          blue = RGB_ON;
          draw_msg("Game over!\n\nA to start");
          break;
     case PAUSED:
          draw_msg(menu[selected]);
          break;
     default:
          for (byte i = 0; i < WAIT_LEN; i += 1) {
               arduboy.setCursor(BLOCK_SIZE, (i + 3) * BLOCK_SIZE);
               arduboy.write(i == selected ? '+' : game.waiting[i] < 0 ? ' ' : '-');
          }





          color_shape(s, 32 - s->width * BLOCK_SIZE / 2,
                      27 - s->height * BLOCK_SIZE / 2);


          if (state == MOVING) {
               
               if ((can_place = shape_clear(s, game.x, game.y)))
                    green = RGB_ON;
               else
                    red = RGB_ON;
                    
               draw_shape(s, game.x, game.y);



          }
     }
     arduboy.digitalWriteRGB(red, green, blue);
     arduboy.display();
     return can_place;
}

void
draw_title(void) {
     arduboy.clear();
     draw_board();
     draw_msg("1010 v0.9\n\n  by mwm\n\nA to start");
     arduboy.display();
     while (!arduboy.pressed(A_BUTTON))
          ;
     delay(game.delay);
}


/* Arduino API */
const int store = 266;	// 10 (base 256) + 10. Because why not?
const int saved_game = store + 2;

void
setup(void) {
     byte i;

     arduboy.begin();
     if (EEPROM.read(store) == 10 && EEPROM.read(store + 1) == 10) {
          EEPROM.get(saved_game, game);
          for (i = 0; i < WAIT_LEN; i += 1) {
               if (game.waiting[i] >= 0) {
                    state = PAUSED;
                    selected = i;
                    break;
               }
          }
          if (i >= WAIT_LEN)
               reset_game();
     } else{
          // No saved game
          reset_game();
          game.high = 0;
          EEPROM.put(saved_game, game);
          EEPROM.write(store, 10);
          EEPROM.write(store + 1, 10);
     }          

     draw_title();
     arduboy.initRandomSeed();
}

void
loop(void) {
     const shape *const s = &shapes[game.waiting[selected]];
     const byte can_place = draw_game();

     arduboy.pollButtons();

     if (arduboy.pressed(B_BUTTON)) {
          state = PAUSED;
          selected = 0;
          return;
     }

     switch (state) {
     case SELECTING:
          if (arduboy.pressed(UP_BUTTON))




               do {
                    if (selected)
                         selected -= 1;
                    else
                         selected = WAIT_LEN - 1;
               } while (game.waiting[selected] < 0);
          else if (arduboy.pressed(DOWN_BUTTON))
               do
                    selected = (selected + 1) % WAIT_LEN;
               while (game.waiting[selected] < 0);
          else if (arduboy.pressed(RIGHT_BUTTON)) {
               state = MOVING;
               game.x = game.y = 0;
               while (game.board[game.x][game.y] != EMPTY) {
                    game.x = (game.x + 1) % (BOARD_SIZE - s->width + 1);
                    if (game.x == 0)
                         game.y = (game.y + 1) % (BOARD_SIZE - s->height + 1);
               }
          } else if (arduboy.pressed(LEFT_BUTTON)) {
               state = MOVING;
               game.x = game.y = 0;
               while (game.board[game.x][game.y] != EMPTY) {
                    game.y = (game.y + 1) % (BOARD_SIZE - s->height + 1);
                    if (game.y == 0)
                         game.x = (game.x + 1) % (BOARD_SIZE - s->width + 1);
               }
          }
................................................................................
          else if (arduboy.pressed(RIGHT_BUTTON)
                   && (game.x < BOARD_SIZE - s->width))
               game.x += 1;
          else if (arduboy.pressed(LEFT_BUTTON)) {
               if (game.x > 0)
                    game.x -= 1;
               else
                    state = SELECTING;
          }
          break;
     case PAUSED:
          if (arduboy.pressed(A_BUTTON)) {
               if (selected)
                    reset_game();
               state = SELECTING;
               select_next();
          } else if (!arduboy.notPressed(LEFT_BUTTON | RIGHT_BUTTON))
               selected = !selected;
          break;
     case DONE:
          for (byte i = 0; i < WAIT_LEN; i += 1)
               game.waiting[i] = -1;
          EEPROM.put(saved_game, game);
          if (arduboy.pressed(A_BUTTON))
               reset_game();
          break;
     }
     EEPROM.put(saved_game, game);
     delay(game.delay);
}