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

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

Overview
Comment:Initial version of help system. Needs more work.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:44de8814a6cc300480c5efb517069829ab53ef60
User & Date: mwm 2016-11-05 13:28:19
Context
2016-11-06
10:01
Rewrite the menu code so that the DONE state can use it, so we now get a New/Help menu when the game is over. check-in: 9f2d78daf6 user: mwm tags: trunk
2016-11-05
13:28
Initial version of help system. Needs more work. check-in: 44de8814a6 user: mwm tags: trunk
2016-11-04
10:40
Shorten the autorepeat delay. check-in: 4b3ee94d70 user: mwm tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to Tiny-1010.ino.

15
16
17
18
19
20
21

22
23
24
25
26
27
28
29
30
..
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
...
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
...
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
...
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
...
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
...
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
...
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
...
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465


466
467
468
469
470
471
472
473
474
475
476
477
478

479
480
481
482
483

 */

#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.
................................................................................
     {0x7F, 0x49, 0x49, 0x49, 0x49, 0x49, 0x7F},// horizontal 2 long
     {0x7F, 0x7F, 0x63, 0x6B, 0x63, 0x7F, 0x7F},// 3x3 corners
     {0x7F, 0x41, 0x5D, 0x55, 0x5D, 0x41, 0x7F},// 2x2 corners
     {0x7F, 0x55, 0x6B, 0x55, 0x6B, 0x55, 0x7F},// L shapes
};


/* The shapes */
const byte ss = 3;	// 1x1 square

const byte threes[][3] = {
     { 6,  6,  6},	// vertical 3 in a row
     { 7,  7,  0},	// vertical 2 in a row
     {10, 10, 10},	// horizontal 3 in a row
     {11, 11, 0},	// horizontal 2 in a row
................................................................................
     {2, 2, fours[3]},	// small 2x2 corners
     {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.
boolean
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;
     for (byte j = 0; j < s->height; j += 1)
          for (byte i = 0; i < s->width; i += 1)
................................................................................
     for (byte w = 0; w < WAIT_LEN; w += 1)
          if (game.waiting[w] >= 0 && find_clear(&shapes[game.waiting[w]])) {
               state = SELECTING;
               return;
          }
     state = DONE;
}


void
reset_game(void) {
     game.delay = 100;
     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
................................................................................
                    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);
}

void
draw_shape(const shape *const s, const byte x, const byte y) {
................................................................................
          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);
}

void
draw_msg(const char *const msg, byte top = 18) {
     arduboy.setCursor(0, top);
     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,
................................................................................
          }
     }
     arduboy.digitalWriteRGB(red, green, blue);
     arduboy.display();
     return can_place;
}

void
draw_title(void) {
     arduboy.clear();
     draw_board();
     draw_msg("1010 v0.9b\n\n  by mwm\n\nA to start", 10);
     arduboy.display();

     // use pollButtons to interact properly with the main loop.
     do arduboy.pollButtons();
     while (!arduboy.justPressed(A_BUTTON));
}


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

void
setup(void) {
     boolean reset = 1;

     arduboy.begin();


     if (EEPROM.read(store) != flagbyte || EEPROM.read(store + 1) != flagbyte) {
          // No saved game
          game.high = 0;
          EEPROM.put(saved_game, game);
          EEPROM.write(store, flagbyte);
          EEPROM.write(store + 1, flagbyte);
     } else {
          EEPROM.get(saved_game, game);
          for (byte i = 0; i < WAIT_LEN; i += 1) {
               if (game.waiting[i] >= 0) {
                    reset = 0;
                    state = PAUSED;
                    break;
               }
          }
     }

     draw_title();



     if (reset)
          reset_game();
     arduboy.initRandomSeed();

}

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

     arduboy.pollButtons();

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

     switch (state) {
     case SELECTING:
          if (arduboy.justPressed(UP_BUTTON))
               do {
................................................................................
                    else
                         selected = WAIT_LEN - 1;
               } while (game.waiting[selected] < 0);
          else if (arduboy.justPressed(DOWN_BUTTON))
               do
                    selected = (selected + 1) % WAIT_LEN;
               while (game.waiting[selected] < 0);
          else if (arduboy.justPressed(A_BUTTON)) {
               game.x = game.y = 0;
               state = MOVING;
          } else if (arduboy.justPressed(RIGHT_BUTTON)) {
               arduboy.digitalWriteRGB(RGB_ON, RGB_OFF, RGB_OFF);
               if (find_clear(s))
                    state = MOVING;
          }
          break;
     case MOVING:
          if (arduboy.justPressed(A_BUTTON) && can_place)
................................................................................
          else if (arduboy.pressed(LEFT_BUTTON)) {
               if (game.x > 0)
                    game.x -= 1;
               else
                    state = SELECTING;
          }
          break;
     case PAUSED:
          if (arduboy.justPressed(A_BUTTON)) {
               if (selected)
                    reset_game();
               state = SELECTING;
               select_next();
          } else if (arduboy.justPressed(LEFT_BUTTON)
                  || arduboy.justPressed(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.justPressed(A_BUTTON))
               reset_game();
          break;
     }

     EEPROM.put(saved_game, game);
     if (state == MOVING) {
          unsigned long end = millis() + game.delay;

          while (millis() < end)
               if (arduboy.pressed(A_BUTTON) || arduboy.pressed(B_BUTTON))
                    break;
     }
}








>

|







 







|







 







|
|







 







|
>
>
>
>
>
>
>
>
>







 







>













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







 







|







 







>











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






<
<

<


>






<






|
|







 







<
<
<
<
<
<
<
<
<
<
<
<








|


>











|
|





|
>
>
>


<
>










|
|







 







|


|







 







|
|
|
|
|
|
|
|
|
>
>













>





>
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
..
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
...
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
...
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
...
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
...
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
...
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
434
435
436
437
438
439
440
441
442
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
...
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
...
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
 */

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

Arduboy2 arduboy;

/* Data structures */

// 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.
................................................................................
     {0x7F, 0x49, 0x49, 0x49, 0x49, 0x49, 0x7F},// horizontal 2 long
     {0x7F, 0x7F, 0x63, 0x6B, 0x63, 0x7F, 0x7F},// 3x3 corners
     {0x7F, 0x41, 0x5D, 0x55, 0x5D, 0x41, 0x7F},// 2x2 corners
     {0x7F, 0x55, 0x6B, 0x55, 0x6B, 0x55, 0x7F},// L shapes
};


// The shapes
const byte ss = 3;	// 1x1 square

const byte threes[][3] = {
     { 6,  6,  6},	// vertical 3 in a row
     { 7,  7,  0},	// vertical 2 in a row
     {10, 10, 10},	// horizontal 3 in a row
     {11, 11, 0},	// horizontal 2 in a row
................................................................................
     {2, 2, fours[3]},	// small 2x2 corners
     {2, 2, fours[4]},
     {2, 2, fours[5]},
     {2, 2, fours[6]},
};


// The game
typedef enum {MENU, 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;

// The menu
enum {HELP, RESUME, NEW};
const char *const
menu[] = {" Help ",
          "Resume",
          " New  ",
          NULL};


/* Code to play the game. */
// Returns true if the shape is over clear tiles.
boolean
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;
     for (byte j = 0; j < s->height; j += 1)
          for (byte i = 0; i < s->width; i += 1)
................................................................................
     for (byte w = 0; w < WAIT_LEN; w += 1)
          if (game.waiting[w] >= 0 && find_clear(&shapes[game.waiting[w]])) {
               state = SELECTING;
               return;
          }
     state = DONE;
}


void
reset_game(void) {
     game.delay = 100;
     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;
}

void
do_menu(void) {
     void draw_page_msg(const char *const, boolean clear = true);

     switch (selected) {
     case NEW:
          reset_game();
          // Fall through to
     case RESUME:
          state = SELECTING;
          select_next();
          break;
     case HELP:
          draw_page_msg("   Selecting\n"
                        "\30\31: Change shape\n"
                        "\32: Use shape\nA: Fit & use shape\nB: Pause",
                        true);
          draw_page_msg("    Moving Shape\n\30\31\33\32: Move shape\n"
                        "\33: Deselect shape\nA: Place shape\nB: Pause",
                        true);
          selected = RESUME;
          break;
     }
}

/* Code to draw things on the display */
// Coloring in things that aren't on the 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
................................................................................
                    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);
               }
}

// And now draw things on the 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);
}

void
draw_shape(const shape *const s, const byte x, const byte y) {
................................................................................
          draw_score(score / 10);
          arduboy.write(score % 10 + '0');
     }
}

void
draw_board(void) {
     arduboy.clear();
     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);
}

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

void
draw_page_msg(const char *const msg, boolean clear = false) {
     if (clear)
          arduboy.clear();
    
     draw_msg(msg, 0);
     arduboy.print("\n\nPress A");
     arduboy.display();

     // use pollButtons since we do what the button asks.
     do arduboy.pollButtons();
     while (!arduboy.justPressed(A_BUTTON));
}

void
draw_menu() {
     draw_msg("  Paused\n\n");
     arduboy.print("\33 ");
     arduboy.print(menu[selected]);
     arduboy.print(" \32");
}

byte
draw_game(void) {
     byte can_place = 0;
     const shape *const s = &shapes[game.waiting[selected]];
     byte red, green, blue;




     red = green = blue = RGB_OFF;

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



     switch (state) {
     case DONE:
          blue = RGB_ON;
          draw_msg("Game over!\n\nA to start");
          break;
     case MENU:
          draw_menu();
          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,
................................................................................
          }
     }
     arduboy.digitalWriteRGB(red, green, blue);
     arduboy.display();
     return can_place;
}














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

void
setup(void) {
     boolean reset = true;

     arduboy.begin();
     arduboy.setTextWrap(true);

     if (EEPROM.read(store) != flagbyte || EEPROM.read(store + 1) != flagbyte) {
          // No saved game
          game.high = 0;
          EEPROM.put(saved_game, game);
          EEPROM.write(store, flagbyte);
          EEPROM.write(store + 1, flagbyte);
     } else {
          EEPROM.get(saved_game, game);
          for (byte i = 0; i < WAIT_LEN; i += 1) {
               if (game.waiting[i] >= 0) {
                    reset = false;
                    selected = RESUME;
                    break;
               }
          }
     }

     draw_board();
     draw_page_msg("\n\n1010 v1.0\n by mwm", false);
     arduboy.initRandomSeed();

     if (reset)
          reset_game();

     state = MENU;
}

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

     arduboy.pollButtons();

     if (arduboy.justPressed(B_BUTTON)) {
          state = MENU;
          selected = RESUME;
          return;
     }

     switch (state) {
     case SELECTING:
          if (arduboy.justPressed(UP_BUTTON))
               do {
................................................................................
                    else
                         selected = WAIT_LEN - 1;
               } while (game.waiting[selected] < 0);
          else if (arduboy.justPressed(DOWN_BUTTON))
               do
                    selected = (selected + 1) % WAIT_LEN;
               while (game.waiting[selected] < 0);
          else if (arduboy.justPressed(RIGHT_BUTTON)) {
               game.x = game.y = 0;
               state = MOVING;
          } else if (arduboy.justPressed(A_BUTTON)) {
               arduboy.digitalWriteRGB(RGB_ON, RGB_OFF, RGB_OFF);
               if (find_clear(s))
                    state = MOVING;
          }
          break;
     case MOVING:
          if (arduboy.justPressed(A_BUTTON) && can_place)
................................................................................
          else if (arduboy.pressed(LEFT_BUTTON)) {
               if (game.x > 0)
                    game.x -= 1;
               else
                    state = SELECTING;
          }
          break;
     case MENU:
          if (arduboy.justPressed(A_BUTTON)) {
               do_menu();
          } else if (arduboy.justPressed(LEFT_BUTTON)) {
               if (selected)
                    selected -= 1;
               else
                    selected = sizeof(menu) / sizeof(char *) - 2;
          } else if (arduboy.justPressed(RIGHT_BUTTON)) {
               selected = (selected + 1) % (sizeof(menu) / sizeof(char *) - 1);
          }
          break;
     case DONE:
          for (byte i = 0; i < WAIT_LEN; i += 1)
               game.waiting[i] = -1;
          EEPROM.put(saved_game, game);
          if (arduboy.justPressed(A_BUTTON))
               reset_game();
          break;
     }

     EEPROM.put(saved_game, game);
     if (state == MOVING) {
          unsigned long end = millis() + game.delay;
          // Used pressed as the button just exists the loop.
          while (millis() < end)
               if (arduboy.pressed(A_BUTTON) || arduboy.pressed(B_BUTTON))
                    break;
     }
}