Skip to content

Commit 2f9b057

Browse files
authored
Merge pull request #186 from DevMilanIan/patch-1
add score functionality for 2048
2 parents 21b3b67 + 52d8dbd commit 2f9b057

File tree

1 file changed

+45
-30
lines changed

1 file changed

+45
-30
lines changed

applications/game2048/game_2048.c

+45-30
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdint.h>
22
#include <gui/gui.h>
33
#include <time.h>
4+
#include <math.h>
45

56
#include "font.h"
67

@@ -17,22 +18,24 @@
1718
9 512
1819
10 1024
1920
11 2048
21+
12 4096
22+
...
2023
*/
2124
typedef uint8_t cell_state;
2225

2326
/* 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+
└╌╌└╌╌╌╌┘└╌╌└╌╌╌╌┘└╌╌╌╌┘
3639
*/
3740
typedef enum {
3841
DirectionIdle,
@@ -58,6 +61,8 @@ typedef struct {
5861

5962
uint8_t next_field[4][4];
6063

64+
uint16_t score; // original scoring
65+
6166
Direction direction;
6267
/*
6368
field {
@@ -96,6 +101,11 @@ static void game_2048_render_callback(Canvas* const canvas, ValueMutex* const vm
96101
}
97102
}
98103
}
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);
99109
} else { // if animation
100110
// for animation
101111
// (osKernelGetSysTimerCount() - game_state->animation_start_ticks) / osKernelGetSysTimerFreq();
@@ -222,6 +232,7 @@ static void game_2048_process_move(GameState* const game_state) {
222232

223233
if(field == game_state->next_field[next_y][x]) {
224234
game_state->next_field[next_y][x]++;
235+
game_state->score += pow(2, game_state->next_field[next_y][x]);
225236
next_y++;
226237
continue;
227238
}
@@ -249,6 +260,7 @@ static void game_2048_process_move(GameState* const game_state) {
249260

250261
if(field == game_state->next_field[y][next_x]) {
251262
game_state->next_field[y][next_x]++;
263+
game_state->score += pow(2, game_state->next_field[y][next_x]);
252264
next_x--;
253265
continue;
254266
}
@@ -276,6 +288,7 @@ static void game_2048_process_move(GameState* const game_state) {
276288

277289
if(field == game_state->next_field[next_y][x]) {
278290
game_state->next_field[next_y][x]++;
291+
game_state->score += pow(2, game_state->next_field[next_y][x]);
279292
next_y--;
280293
continue;
281294
}
@@ -306,6 +319,7 @@ static void game_2048_process_move(GameState* const game_state) {
306319

307320
if(field == game_state->next_field[y][next_x]) {
308321
game_state->next_field[y][next_x]++;
322+
game_state->score += pow(2, game_state->next_field[y][next_x]);
309323
next_x++;
310324
continue;
311325
}
@@ -331,6 +345,7 @@ static void game_2048_restart(GameState* const game_state) {
331345
}
332346

333347
// start next game
348+
game_state->score = 0;
334349
game_2048_set_new_number(game_state);
335350
game_2048_set_new_number(game_state);
336351
}
@@ -358,30 +373,30 @@ int32_t game_2048_app(void* p) {
358373
Gui* gui = furi_record_open("gui");
359374
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
360375

376+
361377
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);
364379

365380
/* <debug>
366381
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;
375390
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;
380395
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;
385400
</debug> */
386401

387402
InputEvent event;

0 commit comments

Comments
 (0)