From 30ef1a01d01c15dea587d5627cd42b69a72bcb6a Mon Sep 17 00:00:00 2001 From: raandoom Date: Fri, 19 Dec 2014 22:28:11 +0000 Subject: [PATCH] 2048: - added last game loading TODO: - optimize tile drawing: maybe separate canvas for every tile git-svn-id: svn://kolibrios.org@5248 a494cfbc-eb01-0410-851d-a64ba20cac60 --- programs/games/2048/board.c | 74 +++++++++++++++++++++++++++--------- programs/games/2048/board.h | 4 ++ programs/games/2048/config.c | 20 +++++----- programs/games/2048/config.h | 10 ++++- 4 files changed, 77 insertions(+), 31 deletions(-) diff --git a/programs/games/2048/board.c b/programs/games/2048/board.c index 91c045b5bb..d0ab8cbc1f 100644 --- a/programs/games/2048/board.c +++ b/programs/games/2048/board.c @@ -1,6 +1,8 @@ #include "board.h" #include "config.h" +__u8 board_need_config = true; + rect base_cell = {0}; tile null_tile = {0}; @@ -58,10 +60,6 @@ __u32 random_u32(__u32 max); void board_init(rect* r) { - __u32 high = config_load_highscore(); - if (high > board.highscore) - board.highscore = high; - // seed for random number generator srand(__menuet__getsystemclock()); @@ -87,10 +85,37 @@ void board_init(rect* r) board.tile_map[i] = null_tile; } - i = 0; - for (i = 0; i < START_COUNT; i++) + __u8 loaded = false; + __u8 empty_config = true; + if (board_need_config) { - board_add_random_tile(); + board_need_config = false; + config_state state = {0}; + loaded = config_load(&state); + if(loaded) + { + board.score = state.score; + board.highscore = state.highscore; + + i = 0; + for (i = 0; i < BOARD_MAP_SIZE; i++) + { + if (state.value_map[i]) + { + empty_config = false; + board_add_tile(state.value_map[i],i); + } + } + } + } + + if (!loaded || empty_config) + { + i = 0; + for (i = 0; i < START_COUNT; i++) + { + board_add_random_tile(); + } } board_redraw(); @@ -98,7 +123,14 @@ void board_init(rect* r) void board_delete() { - config_save_highscore(board.highscore); + config_state state = {0}; + state.score = board.score; + state.highscore = board.highscore; + int i = 0; + for (i = 0; i < BOARD_MAP_SIZE; i++) + state.value_map[i] = board.tile_map[i].value; + config_save(&state); + canvas_delete(); } @@ -351,21 +383,27 @@ __u8 board_add_random_tile() { __u16 rnd_av = random_u32(board.empty_count); rnd_av = board.empty_index[rnd_av]; + __u32 rnd_value = (random_u32(10) < 9) ? 2 : 4; - tile* av_tile = &board.tile_map[rnd_av]; - av_tile->value = (random_u32(10) < 9) ? 2 : 4; - - av_tile->animate = true; - av_tile->ani_step = ANI_APPEAR_STEP; - av_tile->transition = position2cell(board_position(rnd_av)); - av_tile->cell.x = av_tile->transition.x + base_cell.width / 2; - av_tile->cell.y = av_tile->transition.y + base_cell.height / 2; - av_tile->cell.width = 0; - av_tile->cell.height = 0; + board_add_tile(rnd_value,rnd_av); } return board.empty_count; } +void board_add_tile(__u32 value, __u16 index) +{ + tile* av_tile = &board.tile_map[index]; + av_tile->value = value; + + av_tile->animate = true; + av_tile->ani_step = ANI_APPEAR_STEP; + av_tile->transition = position2cell(board_position(index)); + av_tile->cell.x = av_tile->transition.x + base_cell.width / 2; + av_tile->cell.y = av_tile->transition.y + base_cell.height / 2; + av_tile->cell.width = 0; + av_tile->cell.height = 0; +} + __u8 board_has_moves() { __u16 ind = 0; diff --git a/programs/games/2048/board.h b/programs/games/2048/board.h index 4a259ff975..22c9661c23 100644 --- a/programs/games/2048/board.h +++ b/programs/games/2048/board.h @@ -17,6 +17,10 @@ void board_redraw(); // Return true if tile added, false - if no more place for tile __u8 board_add_random_tile(); +// Add one tile with needed value to needed position +// No return value. Used for loading from file. +void board_add_tile(__u32 value, __u16 index); + // Check for available moves // Return true if board has moves, false - if not __u8 board_has_moves(); diff --git a/programs/games/2048/config.c b/programs/games/2048/config.c index cf841322a5..c87bc434a6 100644 --- a/programs/games/2048/config.c +++ b/programs/games/2048/config.c @@ -16,14 +16,12 @@ typedef struct { char path[] = "/sys/games/2048.dat"; -__u32 config_load_highscore() +__u8 config_load(config_state* st) { - __u32 highscore = 0; - fs_info cfg = {0}; cfg.func = 0; - cfg.size = sizeof(__u32); - cfg.data = (char*)&highscore; + cfg.size = sizeof(config_state); + cfg.data = (char*)st; cfg.name = path; __u32 ret = 0; @@ -34,17 +32,15 @@ __u32 config_load_highscore() "b"((__u32)(&cfg)): "memory"); - if (ret || (rnum != 4)) highscore = 0; - - return highscore; + return !ret || (rnum == cfg.size); } -void config_save_highscore(__u32 score) +__u8 config_save(config_state* st) { fs_info cfg = {0}; cfg.func = 2; - cfg.size = sizeof(__u32); - cfg.data = (char*)&score; + cfg.size = sizeof(config_state); + cfg.data = (char*)st; cfg.name = path; __u32 ret = 0; @@ -54,4 +50,6 @@ void config_save_highscore(__u32 score) "a"(70), "b"((__u32)(&cfg)): "memory"); + + return !ret || (wnum == cfg.size); } diff --git a/programs/games/2048/config.h b/programs/games/2048/config.h index 346802c6e5..0a444d224a 100644 --- a/programs/games/2048/config.h +++ b/programs/games/2048/config.h @@ -3,10 +3,16 @@ #include "defines.h" +typedef struct { + __u32 score; + __u32 highscore; + __u32 value_map[BOARD_COUNT * BOARD_COUNT]; +} config_state; + // Get saved highscore -__u32 config_load_highscore(); +__u8 config_load(config_state* st); // Save current highscore -void config_save_highscore(__u32 score); +__u8 config_save(config_state* st); #endif // CONFIG_H