1
1
#include <stdint.h>
2
2
#include <gui/gui.h>
3
3
#include <time.h>
4
+ #include <math.h>
4
5
5
6
#include "font.h"
6
7
17
18
9 512
18
19
10 1024
19
20
11 2048
21
+ 12 4096
22
+ ...
20
23
*/
21
24
typedef uint8_t cell_state ;
22
25
23
26
/* DirectionLeft <--
24
- +????++????++????++????+
25
- ? ?? ?? ?? ?
26
- +????++????++????++????+
27
- +????++????++????++????+
28
- ? ?? ?? ?? ?
29
- +????++????++????++????+
30
- +??+????+??++????++????+
31
- ? 2? 2 ? ?? ?? ?
32
- +??+????+??++????++????+
33
- +??+????++??+????++????+
34
- ? 4? 4 ?? 2? 2 ?? ?
35
- +??+????++??+????++????+
27
+ ┌╌╌╌╌┐┌╌╌╌╌┐┌╌╌╌╌┐┌╌╌╌╌┐
28
+ ╎ ╎╎ ╎╎ ╎╎ ╎
29
+ └╌╌╌╌┘└╌╌╌╌┘└╌╌╌╌┘└╌╌╌╌┘
30
+ ┌╌╌╌╌┐┌╌╌╌╌┐┌╌╌╌╌┐┌╌╌╌╌┐
31
+ ╎ ╎╎ ╎╎ ╎╎ ╎
32
+ └╌╌╌╌┘└╌╌╌╌┘└╌╌╌╌┘└╌╌╌╌┘
33
+ ┌╌╌┌╌╌╌╌┐╌╌┐┌╌╌╌╌┐┌╌╌╌╌┐
34
+ ╎ 2╎ 2 ╎ ╎╎ ╎╎ ╎
35
+ └╌╌└╌╌╌╌┘╌╌┘└╌╌╌╌┘└╌╌╌╌┘
36
+ ┌╌╌┌╌╌╌╌┐┌╌╌┌╌╌╌╌┐┌╌╌╌╌┐
37
+ ╎ 4╎ 4 ╎╎ 2╎ 2 ╎╎ ╎
38
+ └╌╌└╌╌╌╌┘└╌╌└╌╌╌╌┘└╌╌╌╌┘
36
39
*/
37
40
typedef enum {
38
41
DirectionIdle ,
@@ -58,6 +61,8 @@ typedef struct {
58
61
59
62
uint8_t next_field [4 ][4 ];
60
63
64
+ uint16_t score ; // original scoring
65
+
61
66
Direction direction ;
62
67
/*
63
68
field {
@@ -96,6 +101,11 @@ static void game_2048_render_callback(Canvas* const canvas, ValueMutex* const vm
96
101
}
97
102
}
98
103
}
104
+
105
+ // display score
106
+ char buffer2 [6 ];
107
+ snprintf (buffer2 , sizeof (buffer2 ), "%u" , game_state -> score );
108
+ canvas_draw_str_aligned (canvas , 127 , 8 , AlignRight , AlignBottom , buffer2 );
99
109
} else { // if animation
100
110
// for animation
101
111
// (osKernelGetSysTimerCount() - game_state->animation_start_ticks) / osKernelGetSysTimerFreq();
@@ -222,6 +232,7 @@ static void game_2048_process_move(GameState* const game_state) {
222
232
223
233
if (field == game_state -> next_field [next_y ][x ]) {
224
234
game_state -> next_field [next_y ][x ]++ ;
235
+ game_state -> score += pow (2 , game_state -> next_field [next_y ][x ]);
225
236
next_y ++ ;
226
237
continue ;
227
238
}
@@ -249,6 +260,7 @@ static void game_2048_process_move(GameState* const game_state) {
249
260
250
261
if (field == game_state -> next_field [y ][next_x ]) {
251
262
game_state -> next_field [y ][next_x ]++ ;
263
+ game_state -> score += pow (2 , game_state -> next_field [y ][next_x ]);
252
264
next_x -- ;
253
265
continue ;
254
266
}
@@ -276,6 +288,7 @@ static void game_2048_process_move(GameState* const game_state) {
276
288
277
289
if (field == game_state -> next_field [next_y ][x ]) {
278
290
game_state -> next_field [next_y ][x ]++ ;
291
+ game_state -> score += pow (2 , game_state -> next_field [next_y ][x ]);
279
292
next_y -- ;
280
293
continue ;
281
294
}
@@ -306,6 +319,7 @@ static void game_2048_process_move(GameState* const game_state) {
306
319
307
320
if (field == game_state -> next_field [y ][next_x ]) {
308
321
game_state -> next_field [y ][next_x ]++ ;
322
+ game_state -> score += pow (2 , game_state -> next_field [y ][next_x ]);
309
323
next_x ++ ;
310
324
continue ;
311
325
}
@@ -331,6 +345,7 @@ static void game_2048_restart(GameState* const game_state) {
331
345
}
332
346
333
347
// start next game
348
+ game_state -> score = 0 ;
334
349
game_2048_set_new_number (game_state );
335
350
game_2048_set_new_number (game_state );
336
351
}
@@ -358,30 +373,30 @@ int32_t game_2048_app(void* p) {
358
373
Gui * gui = furi_record_open ("gui" );
359
374
gui_add_view_port (gui , view_port , GuiLayerFullscreen );
360
375
376
+
361
377
game_state -> direction = DirectionIdle ;
362
- game_2048_set_new_number (game_state );
363
- game_2048_set_new_number (game_state );
378
+ game_2048_restart (game_state );
364
379
365
380
/* <debug>
366
381
game_state->field[0][0] = 0;
367
- game_state->field[0][1] = 1 ;
368
- game_state->field[0][2] = 2 ;
369
- game_state->field[0][3] = 3 ;
370
-
371
- game_state->field[1][0] = 4 ;
372
- game_state->field[1][1] = 5 ;
373
- game_state->field[1][2] = 6 ;
374
- game_state->field[1][3] = 7 ;
382
+ game_state->field[0][1] = 0 ;
383
+ game_state->field[0][2] = 0 ;
384
+ game_state->field[0][3] = 0 ;
385
+
386
+ game_state->field[1][0] = 1 ;
387
+ game_state->field[1][1] = 2 ;
388
+ game_state->field[1][2] = 3 ;
389
+ game_state->field[1][3] = 4 ;
375
390
376
- game_state->field[2][0] = 8 ;
377
- game_state->field[2][1] = 9 ;
378
- game_state->field[2][2] = 10 ;
379
- game_state->field[2][3] = 11 ;
391
+ game_state->field[2][0] = 5 ;
392
+ game_state->field[2][1] = 6 ;
393
+ game_state->field[2][2] = 7 ;
394
+ game_state->field[2][3] = 8 ;
380
395
381
- game_state->field[3][0] = 0 ;
382
- game_state->field[3][1] = 0 ;
383
- game_state->field[3][2] = 0 ;
384
- game_state->field[3][3] = 0 ;
396
+ game_state->field[3][0] = 9 ;
397
+ game_state->field[3][1] = 10 ;
398
+ game_state->field[3][2] = 11 ;
399
+ game_state->field[3][3] = 12 ;
385
400
</debug> */
386
401
387
402
InputEvent event ;
0 commit comments