forked from KolibriOS/kolibrios
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
This commit is contained in:
parent
5cd0c307f0
commit
30ef1a01d0
@ -1,6 +1,8 @@
|
|||||||
#include "board.h"
|
#include "board.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
|
__u8 board_need_config = true;
|
||||||
|
|
||||||
rect base_cell = {0};
|
rect base_cell = {0};
|
||||||
tile null_tile = {0};
|
tile null_tile = {0};
|
||||||
|
|
||||||
@ -58,10 +60,6 @@ __u32 random_u32(__u32 max);
|
|||||||
|
|
||||||
void board_init(rect* r)
|
void board_init(rect* r)
|
||||||
{
|
{
|
||||||
__u32 high = config_load_highscore();
|
|
||||||
if (high > board.highscore)
|
|
||||||
board.highscore = high;
|
|
||||||
|
|
||||||
// seed for random number generator
|
// seed for random number generator
|
||||||
srand(__menuet__getsystemclock());
|
srand(__menuet__getsystemclock());
|
||||||
|
|
||||||
@ -87,18 +85,52 @@ void board_init(rect* r)
|
|||||||
board.tile_map[i] = null_tile;
|
board.tile_map[i] = null_tile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
__u8 loaded = false;
|
||||||
|
__u8 empty_config = true;
|
||||||
|
if (board_need_config)
|
||||||
|
{
|
||||||
|
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;
|
i = 0;
|
||||||
for (i = 0; i < START_COUNT; i++)
|
for (i = 0; i < START_COUNT; i++)
|
||||||
{
|
{
|
||||||
board_add_random_tile();
|
board_add_random_tile();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
board_redraw();
|
board_redraw();
|
||||||
}
|
}
|
||||||
|
|
||||||
void board_delete()
|
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();
|
canvas_delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -351,19 +383,25 @@ __u8 board_add_random_tile()
|
|||||||
{
|
{
|
||||||
__u16 rnd_av = random_u32(board.empty_count);
|
__u16 rnd_av = random_u32(board.empty_count);
|
||||||
rnd_av = board.empty_index[rnd_av];
|
rnd_av = board.empty_index[rnd_av];
|
||||||
|
__u32 rnd_value = (random_u32(10) < 9) ? 2 : 4;
|
||||||
|
|
||||||
tile* av_tile = &board.tile_map[rnd_av];
|
board_add_tile(rnd_value,rnd_av);
|
||||||
av_tile->value = (random_u32(10) < 9) ? 2 : 4;
|
}
|
||||||
|
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->animate = true;
|
||||||
av_tile->ani_step = ANI_APPEAR_STEP;
|
av_tile->ani_step = ANI_APPEAR_STEP;
|
||||||
av_tile->transition = position2cell(board_position(rnd_av));
|
av_tile->transition = position2cell(board_position(index));
|
||||||
av_tile->cell.x = av_tile->transition.x + base_cell.width / 2;
|
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.y = av_tile->transition.y + base_cell.height / 2;
|
||||||
av_tile->cell.width = 0;
|
av_tile->cell.width = 0;
|
||||||
av_tile->cell.height = 0;
|
av_tile->cell.height = 0;
|
||||||
}
|
|
||||||
return board.empty_count;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
__u8 board_has_moves()
|
__u8 board_has_moves()
|
||||||
|
@ -17,6 +17,10 @@ void board_redraw();
|
|||||||
// Return true if tile added, false - if no more place for tile
|
// Return true if tile added, false - if no more place for tile
|
||||||
__u8 board_add_random_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
|
// Check for available moves
|
||||||
// Return true if board has moves, false - if not
|
// Return true if board has moves, false - if not
|
||||||
__u8 board_has_moves();
|
__u8 board_has_moves();
|
||||||
|
@ -16,14 +16,12 @@ typedef struct {
|
|||||||
|
|
||||||
char path[] = "/sys/games/2048.dat";
|
char path[] = "/sys/games/2048.dat";
|
||||||
|
|
||||||
__u32 config_load_highscore()
|
__u8 config_load(config_state* st)
|
||||||
{
|
{
|
||||||
__u32 highscore = 0;
|
|
||||||
|
|
||||||
fs_info cfg = {0};
|
fs_info cfg = {0};
|
||||||
cfg.func = 0;
|
cfg.func = 0;
|
||||||
cfg.size = sizeof(__u32);
|
cfg.size = sizeof(config_state);
|
||||||
cfg.data = (char*)&highscore;
|
cfg.data = (char*)st;
|
||||||
cfg.name = path;
|
cfg.name = path;
|
||||||
|
|
||||||
__u32 ret = 0;
|
__u32 ret = 0;
|
||||||
@ -34,17 +32,15 @@ __u32 config_load_highscore()
|
|||||||
"b"((__u32)(&cfg)):
|
"b"((__u32)(&cfg)):
|
||||||
"memory");
|
"memory");
|
||||||
|
|
||||||
if (ret || (rnum != 4)) highscore = 0;
|
return !ret || (rnum == cfg.size);
|
||||||
|
|
||||||
return highscore;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void config_save_highscore(__u32 score)
|
__u8 config_save(config_state* st)
|
||||||
{
|
{
|
||||||
fs_info cfg = {0};
|
fs_info cfg = {0};
|
||||||
cfg.func = 2;
|
cfg.func = 2;
|
||||||
cfg.size = sizeof(__u32);
|
cfg.size = sizeof(config_state);
|
||||||
cfg.data = (char*)&score;
|
cfg.data = (char*)st;
|
||||||
cfg.name = path;
|
cfg.name = path;
|
||||||
|
|
||||||
__u32 ret = 0;
|
__u32 ret = 0;
|
||||||
@ -54,4 +50,6 @@ void config_save_highscore(__u32 score)
|
|||||||
"a"(70),
|
"a"(70),
|
||||||
"b"((__u32)(&cfg)):
|
"b"((__u32)(&cfg)):
|
||||||
"memory");
|
"memory");
|
||||||
|
|
||||||
|
return !ret || (wnum == cfg.size);
|
||||||
}
|
}
|
||||||
|
@ -3,10 +3,16 @@
|
|||||||
|
|
||||||
#include "defines.h"
|
#include "defines.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
__u32 score;
|
||||||
|
__u32 highscore;
|
||||||
|
__u32 value_map[BOARD_COUNT * BOARD_COUNT];
|
||||||
|
} config_state;
|
||||||
|
|
||||||
// Get saved highscore
|
// Get saved highscore
|
||||||
__u32 config_load_highscore();
|
__u8 config_load(config_state* st);
|
||||||
|
|
||||||
// Save current highscore
|
// Save current highscore
|
||||||
void config_save_highscore(__u32 score);
|
__u8 config_save(config_state* st);
|
||||||
|
|
||||||
#endif // CONFIG_H
|
#endif // CONFIG_H
|
||||||
|
Loading…
Reference in New Issue
Block a user