Game state is now preserved in a file
This commit is contained in:
@@ -1 +1,2 @@
|
|||||||
main
|
main
|
||||||
|
save.dat
|
||||||
|
|||||||
@@ -176,6 +176,11 @@ void reset_board(GameState_t *state) {
|
|||||||
state->board[y][x] = (rand() % 10 == 0) ? 4 : 2; // 10% chance to spawn a 4
|
state->board[y][x] = (rand() % 10 == 0) ? 4 : 2; // 10% chance to spawn a 4
|
||||||
tile_count++;
|
tile_count++;
|
||||||
}
|
}
|
||||||
|
FILE *f = fopen("save.dat", "wb");
|
||||||
|
if (f) {
|
||||||
|
fwrite(state, sizeof(GameState_t), 1, f);
|
||||||
|
fclose(f);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool spawn_tile(GameState_t *state) {
|
bool spawn_tile(GameState_t *state) {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <raylib.h>
|
#include <raylib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#define BOARD_WIDTH 4
|
#define BOARD_WIDTH 4
|
||||||
#define BOARD_HEIGHT 4
|
#define BOARD_HEIGHT 4
|
||||||
@@ -9,10 +10,10 @@ typedef enum { UP, DOWN, LEFT, RIGHT } move_direction_t;
|
|||||||
typedef enum { PLAYING, WON, LOST } game_status_t;
|
typedef enum { PLAYING, WON, LOST } game_status_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int board[BOARD_HEIGHT][BOARD_WIDTH];
|
unsigned int board[BOARD_HEIGHT][BOARD_WIDTH];
|
||||||
game_status_t status;
|
game_status_t status;
|
||||||
int score;
|
unsigned int score;
|
||||||
int moves_made;
|
unsigned int moves_made;
|
||||||
} GameState_t;
|
} GameState_t;
|
||||||
|
|
||||||
void move(GameState_t *state, move_direction_t direction);
|
void move(GameState_t *state, move_direction_t direction);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
@@ -30,6 +31,13 @@ int main() {
|
|||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
|
||||||
reset_board(&game_state);
|
reset_board(&game_state);
|
||||||
|
FILE* game_file = fopen("save.dat", "rb");
|
||||||
|
if (game_file != NULL) {
|
||||||
|
fseek(game_file, 0, SEEK_SET);
|
||||||
|
fread(&game_state, sizeof(GameState_t), 1, game_file);
|
||||||
|
fclose(game_file);
|
||||||
|
}
|
||||||
|
|
||||||
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "2048");
|
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "2048");
|
||||||
SetTargetFPS(30); // So my laptop doesn't burn
|
SetTargetFPS(30); // So my laptop doesn't burn
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <raylib.h>
|
#include <raylib.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
static bool is_dragging;
|
static bool is_dragging;
|
||||||
static Vector2 drag_start_pos;
|
static Vector2 drag_start_pos;
|
||||||
@@ -64,6 +65,11 @@ void handle_mouse_input(GameState_t *game_state) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FILE *f = fopen("save.dat", "wb");
|
||||||
|
if (f) {
|
||||||
|
fwrite(game_state, sizeof(GameState_t), 1, f);
|
||||||
|
fclose(f);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
is_dragging = false;
|
is_dragging = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user