-
Notifications
You must be signed in to change notification settings - Fork 3
/
2048.c
509 lines (455 loc) · 15.2 KB
/
2048.c
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
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
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*
Application template for Amazfit Bip BipOS
(C) Maxim Volkov 2019 <[email protected]>
Game 2048 for Amazfit Bip BipOS
(C) Claudio Benvenuti 2019 <[email protected]>
*/
#include <libbip.h>
#include "2048.h"
#define SIZE 4
enum {
DOWN,
UP,
LEFT,
RIGHT,
MOVES
};
unsigned long randseed;
unsigned short randint( short max )
{
randseed = randseed * get_tick_count();
randseed++;
return ( ( randseed >> 16 ) * max ) >> 16;
}
void begin(struct game* g, int index_listed)
{
set_display_state_value (8, 1);
set_display_state_value (4, 1);
set_display_state_value (2, 1);
_srand(get_tick_count());
struct game placeholder;
ElfReadSettings(index_listed, g, 0, sizeof(placeholder));
if (g->record == 0){
for (short i = 0; i < SIZE; i++)
for (short y = 0; y < SIZE; y++){
g->tiles[i][y] = NULL;
g->undo[i][y] = NULL;
}
short i,j;
i = random_spot(g);
j = random_spot(g);
g->tiles[i][j] = random_value(g);
g->undo[i][j] = g->tiles[i][j];
g->score = 0;
g->moves = 0;
g->record = 0;
do{
i=random_spot(g);
j=random_spot(g);
}while (g->tiles[i][j]);
g->tiles[i][j] = random_value(g);
g->undo[i][j] = g->tiles[i][j];
}
}
short random_spot()
{
return randint(SIZE);
}
short random_value()
{
return (randint(10)) ? 2 : 4;
}
void twist(struct game* g)
{
struct game g2;
_memcpy (&g2, g, sizeof(g2));
for (short i = 0; i < SIZE; ++i)
for (short j = 0; j < SIZE; ++j)
g2.tiles[i][j] = g->tiles[j][i];
_memcpy (g, &g2, sizeof(g2));
}
void flip(struct game* g)
{
struct game g2;
_memcpy (&g2, g, sizeof(g2));
for (short i = 0; i < SIZE; ++i)
for (short j = 0; j < SIZE; ++j)
g2.tiles[i][j] = g->tiles[i][SIZE - j - 1];
_memcpy (g, &g2, sizeof(g2));
}
unsigned int fall_column(short* a, short* b)
{
short i,j;
short prev = 0;
unsigned int score = 0;
j = 0;
for (i = 0; i < SIZE; ++i)
if (a[i]) {
if (a[i] == prev) {
b[j - 1] *= 2;
prev = 0;
score += b[j - 1];
} else {
b[j++] = a[i];
prev = a[i];
}
}
return score;
}
void fall(struct game* g)
{
struct game g2;
_memcpy (&g2, g, sizeof(g2));
unsigned int score = g->score;
for (int i = 0; i < SIZE; ++i)
for (short j = 0; j < SIZE; ++j)
g2.tiles[i][j] = NULL;
for (short i = 0; i < SIZE; ++i){
score += fall_column(g->tiles[i], g2.tiles[i]);
}
g2.score = score;
_memcpy (g, &g2, sizeof(g2));
}
int same(struct game* a, struct game* b)
{
short i,j;
for (i = 0; i < SIZE; ++i)
for (j = 0; j < SIZE; ++j)
if (a->tiles[i][j] != b->tiles[i][j])
return 0;
return 1;
}
int tryfalling(struct game* g)
{
struct game g2;
_memcpy (&g2, g, sizeof(g2));
fall(g);
if (same(g, &g2))
return 0;
return 1;
}
void popup(struct game* g)
{
short i,j;
while (1) {
i = random_spot(g);
j = random_spot(g);
if (!g->tiles[i][j]) {
g->tiles[i][j] = random_value(g);
return;
}
}
}
void move(struct game* g, short way)
{
short tmp[SIZE][SIZE];
for (short i = 0; i < SIZE; ++i)
for (short j = 0; j < SIZE; ++j)
tmp[i][j] = g->tiles[i][j];
if (way / 2)
twist(g);
if (way % 2)
flip(g);
if (tryfalling(g)){
//create undo
for (short i = 0; i < SIZE; i++)
for (short y = 0; y < SIZE; y++)
g->undo[i][y] = tmp[i][y];
g->moves = g->moves + 1;
popup(g);
}
if (way % 2)
flip(g);
if (way / 2)
twist(g);
if (g->score > g->record) {
g->record = g->score;
}
}
struct regmenu_ screen_data = {
55,
1,
0,
dispatch_screen,
key_press_screen,
screen_job,
0,
show_screen,
0,
0
};
int main(int param0, char** argv){
show_screen((void*) param0);
}
void show_screen (void *param0) {
struct app_data_** app_data_p = get_ptr_temp_buf_2(); // pointer to a pointer to screen data
struct app_data_ * app_data; // pointer to screen data
if ( (param0 == *app_data_p) && get_var_menu_overlay()){
app_data = *app_data_p;
*app_data_p = NULL;
reg_menu(&screen_data, 0);
*app_data_p = app_data;
} else {
reg_menu(&screen_data, 0);
*app_data_p = (struct app_data_ *)pvPortMalloc(sizeof(struct app_data_));
app_data = *app_data_p;
_memclr(app_data, sizeof(struct app_data_));
app_data->proc = param0;
if ( param0 && app_data->proc->ret_f )
app_data->ret_f = app_data->proc->elf_finish;
else
app_data->ret_f = show_watchface;
struct game g;
begin(&g, app_data->proc->index_listed);
app_data->screen = 1;
_memcpy (&app_data->game, &g, sizeof(g));
}
draw_screen(&app_data->game);
}
void key_press_screen(){
struct app_data_** app_data_p = get_ptr_temp_buf_2();
struct app_data_ * app_data = *app_data_p;
struct game *g = &app_data->game;
struct game placeholder;
ElfWriteSettings(app_data->proc->index_listed, g, 0, sizeof(placeholder));
show_menu_animate(app_data->ret_f, (unsigned int)show_screen, ANIMATE_RIGHT);
};
void screen_job(){
}
int dispatch_screen (void *param){
struct app_data_** app_data_p = get_ptr_temp_buf_2();
struct app_data_ * app_data = *app_data_p;
struct gesture_ *gest = param;
int result = 0;
struct game *g = &app_data->game;
switch (gest->gesture){
case GESTURE_CLICK: {
vibrate(1,50,0);
switch (app_data->screen){
case 1: {
app_data->screen = 2;
draw_score_screen(g->moves, g->score, g->record);
break;
}
case 2: {
if ( ( gest->touch_pos_y >143) && ( gest->touch_pos_y <= 176) && ( gest->touch_pos_x >= 1) && ( gest->touch_pos_x <= 176) ){
app_data->screen = 3;
set_bg_color(COLOR_WHITE);
fill_screen_bg();
show_elf_res_by_id(app_data->proc->index_listed, 0, 24, 10);
set_bg_color(COLOR_WHITE);
set_fg_color(COLOR_BLACK);
text_out_center("by Claudio Benvenuti", 88, 145);
repaint_screen_lines(0, 176);
} else if ( ( gest->touch_pos_y >90) && ( gest->touch_pos_y < 133) && ( gest->touch_pos_x >= 1) && ( gest->touch_pos_x <= 100) ){
app_data->screen = 4;
ask_confirmation();
} else if ( ( gest->touch_pos_y >90) && ( gest->touch_pos_y < 133) && ( gest->touch_pos_x >= 110) && ( gest->touch_pos_x <= 176) ){
short tmp;
for (short i = 0; i < SIZE; i++) {
for (short y = 0; y < SIZE; y++) {
tmp = g->tiles[i][y];
g->tiles[i][y] = g->undo[i][y];
g->undo[i][y] = tmp;
}
}
app_data->screen = 1;
draw_board(g);
} else {
app_data->screen = 1;
draw_board(g);
}
break;
}
case 3: {
app_data->screen = 1;
draw_board(g);
break;
}
case 4: {
if ( ( gest->touch_pos_y > 84) && ( gest->touch_pos_y <= 113) && ( gest->touch_pos_x >= 20) && ( gest->touch_pos_x <= 78) ){
for (short i = 0; i < SIZE; i++)
for (short y = 0; y < SIZE; y++){
g->tiles[i][y] = NULL;
g->undo[i][y] = NULL;
}
short i,j;
i = random_spot(g);
j = random_spot(g);
g->tiles[i][j] = random_value(g);
g->undo[i][j] = g->tiles[i][j];
do{
i=random_spot(g);
j=random_spot(g);
}while (g->tiles[i][j]);
g->tiles[i][j] = random_value(g);
g->undo[i][j] = g->tiles[i][j];
g->moves = 0;
g->score = 0;
}
app_data->screen = 1;
draw_board(g);
break;
}
}
break;
};
case GESTURE_SWIPE_DOWN: {
if (app_data->screen == 1){
move(g, 0);
}
app_data->screen = 1;
draw_board(g);
break;
};
case GESTURE_SWIPE_UP: {
if (app_data->screen == 1){
move(g, 1);
}
app_data->screen = 1;
draw_board(g);
break;
};
case GESTURE_SWIPE_LEFT: {
if (app_data->screen == 1){
move(g, 2);
}
app_data->screen = 1;
draw_board(g);
break;
};
case GESTURE_SWIPE_RIGHT: {
if (app_data->screen == 1){
move(g, 3);
}
app_data->screen = 1;
draw_board(g);
break;
};
default:{
vibrate(1,70,0);
break;
};
}
return result;
};
void draw_board(struct game* g){
set_bg_color(COLOR_BLACK);
fill_screen_bg();
char str[sizeof(int) * 4 + 1];
short n, offset;
for (short i = 0; i < SIZE; i++) {
for (short y = 0; y < SIZE; y++) {
n = g->tiles[i][SIZE-1-y];
if (!n) {
set_bg_color(COLOR_WHITE);
} else if(n < 4) {
set_fg_color(COLOR_BLACK);
set_bg_color(COLOR_YELLOW);
} else if (n < 32) {
set_fg_color(COLOR_BLACK);
set_bg_color(COLOR_AQUA);
} else if (n < 128) {
set_fg_color(COLOR_BLACK);
set_bg_color(COLOR_PURPLE);
} else if (n < 512) {
set_fg_color(COLOR_BLACK);
set_bg_color(COLOR_GREEN);
} else if (n < 512) {
set_fg_color(COLOR_BLACK);
set_bg_color(COLOR_BLUE);
} else if (n < 1024) {
set_fg_color(COLOR_WHITE);
set_bg_color(COLOR_RED);
} else if (n < 2048) {
set_fg_color(COLOR_WHITE);
set_bg_color(COLOR_BLACK);
} else if (n >= 2048 ) {
set_fg_color(COLOR_RED);
set_bg_color(COLOR_WHITE);
}
draw_filled_rect_bg(3+(40*i)+(3*i),3+(40*y)+(3*y), 43+(40*i)+(3*i),43+(40*y)+(3*y));
if (n) {
_sprintf (str, "%4d", n);
offset = (n<1000) ? 0 : 5;
text_out_center(str, 19 + offset +(40*i)+(3*i), 13+(40*y)+(3*y));
}
}
}
repaint_screen_lines(0, 176);
};
void draw_score_screen(short moves, unsigned int score, unsigned int record){
char str[sizeof(int) * 4 + 1];
set_fg_color(COLOR_WHITE);
set_bg_color(COLOR_BLACK);
fill_screen_bg();
set_bg_color(COLOR_BLUE);
draw_filled_rect_bg(4, 4, 86, 50);
text_out_center("Score", 45, 6);
_sprintf (str, "%4d", score);
text_out_center(str, 45, 28);
set_bg_color(COLOR_RED);
draw_filled_rect_bg(90, 4, 172, 50);
text_out_center("Moves", 131, 6);
_sprintf (str, "%4d", moves);
text_out_center(str, 131, 28);
set_bg_color(COLOR_GREEN);
set_fg_color(COLOR_BLACK);
draw_filled_rect_bg(4, 54, 172, 80);
text_out("RECORD:", 12, 58);
_sprintf (str, "%4d", record);
text_out(str, 85, 58);
set_bg_color(COLOR_YELLOW);
set_fg_color(COLOR_BLACK);
draw_button(8, 90, 104, 133);
set_bg_color(COLOR_YELLOW);
text_out_center("NEW GAME", 56, 104);
set_bg_color(COLOR_AQUA);
set_fg_color(COLOR_BLACK);
draw_button(114, 90, 168, 133);
set_bg_color(COLOR_AQUA);
text_out_center("UNDO", 141, 104);
set_bg_color(COLOR_WHITE);
draw_button(30, 143, 146, 169);
set_bg_color(COLOR_BLACK);
draw_button(32, 145, 144, 167);
set_fg_color(COLOR_WHITE);
text_out_center("CREDITS", 88, 147);
repaint_screen_lines(0, 176);
}
void ask_confirmation(){
set_fg_color(COLOR_WHITE);
set_bg_color(COLOR_BLACK);
fill_screen_bg();
text_out_center("Are you sure?", 88, 40);
set_bg_color(COLOR_GREEN);
set_fg_color(COLOR_BLACK);
draw_button(20, 84, 78, 113);
set_bg_color(COLOR_GREEN);
text_out_center("Yes" , 49, 89);
set_bg_color(COLOR_RED);
set_fg_color(COLOR_BLACK);
draw_button(98, 84, 156, 113);
set_bg_color(COLOR_RED);
text_out_center("No", 127, 89);
repaint_screen_lines(0, 176);
}
void draw_button(int from_x, int from_y, int to_x, int to_y){
draw_filled_rect_bg(from_x, from_y, to_x, to_y);
set_bg_color(COLOR_BLACK);
draw_horizontal_line(from_y, from_x, from_x+1);
draw_vertical_line(from_x, from_y, from_y+1);
draw_horizontal_line(from_y, to_x-1, to_x);
draw_vertical_line(to_x, from_y, from_y+1);
draw_horizontal_line(to_y, from_x, from_x+1);
draw_vertical_line(from_x, to_y-1, to_y);
draw_horizontal_line(to_y, to_x-1, to_x);
draw_vertical_line(to_x, to_y-1, to_y);
}
void draw_screen(struct game* g){
set_bg_color(COLOR_BLACK);
fill_screen_bg();
set_graph_callback_to_ram_1();
load_font();
draw_board(g);
}