forked from eugene-kirzhanov/flipper-zero-2048-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_state.c
110 lines (90 loc) · 3.07 KB
/
game_state.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
#define __game_state_c
#include "game_state.h"
#include <furi.h>
void game_state_reset(GameState* const game_state);
void game_state_post_update(GameState* const state);
void game_state_undo(GameState* const state);
void game_state_apply_move_result(GameState* const state, const MoveResult* const move_result);
void game_state_save_score(GameState* const state);
void game_state_init(GameState* const game_state) {
game_state_reset(game_state);
}
void game_state_send(GameState* state, GameEvent event) {
MoveResult move_result;
switch(event) {
case GameMoveLeft:
game_board_table_move_left(state->board.table, &move_result);
game_state_apply_move_result(state, &move_result);
break;
case GameMoveRight:
game_board_table_move_right(state->board.table, &move_result);
game_state_apply_move_result(state, &move_result);
break;
case GameMoveUp:
game_board_table_move_up(state->board.table, &move_result);
game_state_apply_move_result(state, &move_result);
break;
case GameMoveDown:
game_board_table_move_down(state->board.table, &move_result);
game_state_apply_move_result(state, &move_result);
break;
case GameMoveUndo:
game_state_undo(state);
break;
case GameReset:
game_state_reset(state);
break;
default:
break;
}
}
bool game_state_dump(GameState* const state, GameStateDumpCallback cb) {
return cb(state);
}
bool game_state_load(GameState* state, GameStateLoadCallback cb) {
GameState tmp;
game_state_init(&tmp);
if(cb(&tmp)) {
memcpy(state, &tmp, sizeof(GameState));
return true;
}
return false;
}
void game_state_reset(GameState* const state) {
// Reset board
game_state_board_init(&state->board);
// Reset history stack
game_state_board_history_init(&state->history);
// Reset game state
state->is_over = false;
}
void game_state_undo(GameState* const state) {
game_state_board_history_pop(&state->history, &state->board);
game_state_post_update(state);
}
void game_state_apply_move_result(GameState* const state, const MoveResult* const move_result) {
if(!move_result->is_table_updated) return;
// Save the current state to the history stack
game_state_board_history_push(&state->history, &state->board);
// Apply the move to the board
state->board.score += move_result->score_points;
state->board.moves++;
game_board_table_copy(move_result->new_table, state->board.table);
// Add a new random digit to the board
game_board_table_push_random_digit(state->board.table);
// Apply the move to the game state
game_state_post_update(state);
}
void game_state_post_update(GameState* const state) {
if(game_state_board_is_over(&state->board)) {
state->is_over = true;
game_state_save_score(state);
} else {
state->is_over = false;
}
}
void game_state_save_score(GameState* const state) {
if(state->board.score >= state->top_score) {
state->top_score = state->board.score;
}
}