Compare commits
2 Commits
eb8834324e
...
016b1eafb5
| Author | SHA1 | Date | |
|---|---|---|---|
| 016b1eafb5 | |||
| 4c21d309e9 |
@@ -1 +1,2 @@
|
|||||||
main
|
main
|
||||||
|
save.dat
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
#include "game.h"
|
#include "game.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
// Depending on the direction, move the tiles and check if we can merge with the
|
// Depending on the direction, move the tiles and check if we can merge with the
|
||||||
// tiles in that direction. We merge the tiles only if they are of the same value
|
// tiles in that direction. We merge the tiles only if they are of the same value
|
||||||
void move(GameState_t *state, MOVE_DIRECTION direction) {
|
void move(GameState_t *state, move_direction_t direction) {
|
||||||
bool merged[BOARD_HEIGHT][BOARD_WIDTH] = {0};
|
bool merged[BOARD_HEIGHT][BOARD_WIDTH] = {0};
|
||||||
|
bool moved = false;
|
||||||
switch (direction) {
|
switch (direction) {
|
||||||
case UP: {
|
case UP: {
|
||||||
for (int y = 1; y < BOARD_HEIGHT; y++) {
|
for (int y = 1; y < BOARD_HEIGHT; y++) {
|
||||||
@@ -43,6 +45,7 @@ void move(GameState_t *state, MOVE_DIRECTION direction) {
|
|||||||
state->board[y][x] = 0;
|
state->board[y][x] = 0;
|
||||||
state->score += state->board[target_y][x];
|
state->score += state->board[target_y][x];
|
||||||
}
|
}
|
||||||
|
moved = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -77,6 +80,7 @@ void move(GameState_t *state, MOVE_DIRECTION direction) {
|
|||||||
state->board[y][x] = 0;
|
state->board[y][x] = 0;
|
||||||
state->score += state->board[target_y][x];
|
state->score += state->board[target_y][x];
|
||||||
}
|
}
|
||||||
|
moved = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -111,6 +115,7 @@ void move(GameState_t *state, MOVE_DIRECTION direction) {
|
|||||||
state->board[y][x] = 0;
|
state->board[y][x] = 0;
|
||||||
state->score += state->board[y][target_x];
|
state->score += state->board[y][target_x];
|
||||||
}
|
}
|
||||||
|
moved = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -145,22 +150,21 @@ void move(GameState_t *state, MOVE_DIRECTION direction) {
|
|||||||
state->board[y][x] = 0;
|
state->board[y][x] = 0;
|
||||||
state->score += state->board[y][target_x];
|
state->score += state->board[y][target_x];
|
||||||
}
|
}
|
||||||
|
moved = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (moved)
|
||||||
state->moves_made++;
|
state->moves_made++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void reset_board(GameState_t *state) {
|
void reset_board(GameState_t *state) {
|
||||||
for (int y = 0; y < BOARD_HEIGHT; y++) {
|
memset(state->board, 0, sizeof(state->board));
|
||||||
for (int x = 0; x < BOARD_WIDTH; x++) {
|
|
||||||
state->board[y][x] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
state->score = 0;
|
state->score = 0;
|
||||||
state->moves_made = 0;
|
state->moves_made = 0;
|
||||||
|
state->status = PLAYING;
|
||||||
|
|
||||||
int tile_count = 0;
|
int tile_count = 0;
|
||||||
while (tile_count != STARTING_TILES) {
|
while (tile_count != STARTING_TILES) {
|
||||||
@@ -172,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) {
|
||||||
@@ -197,3 +206,20 @@ bool spawn_tile(GameState_t *state) {
|
|||||||
state->board[spawn_y][spawn_x] = (rand() % 10 == 0) ? 4 : 2; // 10% chance to spawn a 4
|
state->board[spawn_y][spawn_x] = (rand() % 10 == 0) ? 4 : 2; // 10% chance to spawn a 4
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This function will only run, if every tile is filled (checked by `spawn_tile`). We can take advantage of that and
|
||||||
|
// just check every neighboring cell for equal values for merging
|
||||||
|
bool can_move(GameState_t *state) {
|
||||||
|
for (int y = 0; y < BOARD_HEIGHT; y++) {
|
||||||
|
for (int x = 0; x < BOARD_WIDTH; x++) {
|
||||||
|
if (state->board[y][x] == 0)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (x < BOARD_WIDTH - 1 && state->board[y][x] == state->board[y][x + 1])
|
||||||
|
return true;
|
||||||
|
if (y < BOARD_HEIGHT - 1 && state->board[y][x] == state->board[y + 1][x])
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,19 +1,22 @@
|
|||||||
#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
|
||||||
#define STARTING_TILES 3
|
#define STARTING_TILES 3
|
||||||
|
|
||||||
typedef enum { UP = 0, DOWN, LEFT, RIGHT } MOVE_DIRECTION;
|
typedef enum { UP, DOWN, LEFT, RIGHT } move_direction_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];
|
||||||
int score;
|
game_status_t status;
|
||||||
int moves_made;
|
unsigned int score;
|
||||||
|
unsigned int moves_made;
|
||||||
} GameState_t;
|
} GameState_t;
|
||||||
|
|
||||||
|
void move(GameState_t *state, move_direction_t direction);
|
||||||
void move(GameState_t *state, MOVE_DIRECTION direction);
|
|
||||||
void reset_board(GameState_t *state);
|
void reset_board(GameState_t *state);
|
||||||
bool spawn_tile(GameState_t *state);
|
bool spawn_tile(GameState_t *state);
|
||||||
|
bool can_move(GameState_t *state);
|
||||||
|
|||||||
@@ -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,8 +31,14 @@ 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(800, 600, "2048");
|
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "2048");
|
||||||
SetTargetFPS(30); // So my laptop doesn't burn
|
SetTargetFPS(30); // So my laptop doesn't burn
|
||||||
|
|
||||||
while (!WindowShouldClose()) {
|
while (!WindowShouldClose()) {
|
||||||
@@ -57,12 +64,35 @@ int main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DrawText(TextFormat("Score: %d", game_state.score), 600, BOARD_MARGIN, 20, BLACK);
|
DrawText(TextFormat("Score: %d", game_state.score), WINDOW_HEIGHT, BOARD_MARGIN, 20, BLACK);
|
||||||
DrawText(TextFormat("Moves used: %d", game_state.moves_made), 600, BOARD_MARGIN + 20, 20, BLACK);
|
DrawText(TextFormat("Moves used: %d", game_state.moves_made), WINDOW_HEIGHT, BOARD_MARGIN + 20, 20, BLACK);
|
||||||
|
|
||||||
|
if (game_state.status == PLAYING) {
|
||||||
|
DrawText("Drag with the mouse to move tiles", 400 - MeasureText("Drag with the mouse to move tiles", 20) / 2,
|
||||||
|
WINDOW_HEIGHT - 40, 20, GRAY);
|
||||||
|
|
||||||
// Reset button
|
// Reset button
|
||||||
DrawRectangle(500, 455, 275, 24, RED);
|
DrawRectangle(500, 455, 275, 24, RED);
|
||||||
DrawText("Reset", 500 + 275 / 2 - MeasureText("Reset", 20) / 2, 455 + 4, 20, WHITE);
|
DrawText("Reset", 500 + 275 / 2 - MeasureText("Reset", 20) / 2, 455 + 4, 20, WHITE);
|
||||||
|
} else {
|
||||||
|
Color overlay = Fade(BLACK, 0.7f);
|
||||||
|
DrawRectangle(0, 0, WINDOW_WIDTH, 700, overlay);
|
||||||
|
|
||||||
|
if (game_state.status == WON) {
|
||||||
|
DrawText("You win!", 400 - MeasureText("You win!", 40) / 2, 250, 40, GREEN);
|
||||||
|
} else if (game_state.status == LOST) {
|
||||||
|
DrawText("Game over!", 400 - MeasureText("Game over!", 40) / 2, 250, 40, RED);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw the scores under the win/loss message
|
||||||
|
DrawText(TextFormat("Score: %d", game_state.score),
|
||||||
|
400 - MeasureText(TextFormat("Score: %d", game_state.score), 20) / 2, 290, 20, WHITE);
|
||||||
|
DrawText(TextFormat("Moves used: %d", game_state.moves_made),
|
||||||
|
400 - MeasureText(TextFormat("Moves used: %d", game_state.moves_made), 20) / 2, 310, 20, WHITE);
|
||||||
|
|
||||||
|
DrawRectangle(400 - 100, 450, 200, 40, RED);
|
||||||
|
DrawText("Try again", 400 - MeasureText("Try again", 20) / 2, 455 + 4, 20, WHITE);
|
||||||
|
}
|
||||||
|
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
handle_mouse_input(&game_state);
|
handle_mouse_input(&game_state);
|
||||||
|
|||||||
@@ -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;
|
||||||
@@ -10,11 +11,22 @@ static Vector2 drag_start_pos;
|
|||||||
void handle_mouse_input(GameState_t *game_state) {
|
void handle_mouse_input(GameState_t *game_state) {
|
||||||
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
|
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
|
||||||
drag_start_pos = GetMousePosition();
|
drag_start_pos = GetMousePosition();
|
||||||
|
|
||||||
|
// Check for the normal reset button
|
||||||
|
if (game_state->status == PLAYING) {
|
||||||
if (drag_start_pos.x >= 500 && drag_start_pos.x <= 775 && drag_start_pos.y >= 455 && drag_start_pos.y <= 479) {
|
if (drag_start_pos.x >= 500 && drag_start_pos.x <= 775 && drag_start_pos.y >= 455 && drag_start_pos.y <= 479) {
|
||||||
reset_board(game_state);
|
reset_board(game_state);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// Check for the "Play again" button
|
||||||
|
if (drag_start_pos.x >= 300 && drag_start_pos.x <= 500 && drag_start_pos.y >= 450 && drag_start_pos.y <= 490) {
|
||||||
|
reset_board(game_state);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (game_state->status == PLAYING)
|
||||||
is_dragging = true;
|
is_dragging = true;
|
||||||
} else if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON) && is_dragging) {
|
} else if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON) && is_dragging) {
|
||||||
Vector2 current_pos = GetMousePosition();
|
Vector2 current_pos = GetMousePosition();
|
||||||
@@ -39,7 +51,25 @@ void handle_mouse_input(GameState_t *game_state) {
|
|||||||
move(game_state, UP);
|
move(game_state, UP);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
spawn_tile(game_state);
|
if (!spawn_tile(game_state) && !can_move(game_state)) {
|
||||||
|
game_state->status = LOST;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// TODO: Allow for infinite play
|
||||||
|
for (int y = 0; y < BOARD_HEIGHT; y++) {
|
||||||
|
for (int x = 0; x < BOARD_WIDTH; x++) {
|
||||||
|
if (game_state->board[y][x] == 2048) {
|
||||||
|
game_state->status = WON;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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