Compare commits
6 Commits
eb8834324e
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 4da59836be | |||
| 3232e4f44f | |||
| a47c6bd0b0 | |||
| 811d11af08 | |||
| 016b1eafb5 | |||
| 4c21d309e9 |
@@ -1 +1,3 @@
|
||||
main
|
||||
save.dat
|
||||
munit/
|
||||
|
||||
@@ -4,6 +4,8 @@ LIBS = -lraylib -lm -lpthread -ldl
|
||||
|
||||
TARGET = main
|
||||
SRC = main.c game.c ui.c
|
||||
TEST_TARGET = munit_tests
|
||||
TEST_SRC = munit.c game.c munit/munit.c
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
@@ -16,4 +18,10 @@ clean:
|
||||
run: $(TARGET)
|
||||
./$(TARGET)
|
||||
|
||||
.PHONY: all clean run
|
||||
test: $(TEST_TARGET)
|
||||
./$(TEST_TARGET)
|
||||
|
||||
$(TEST_TARGET): $(TEST_SRC)
|
||||
$(CC) $(CFLAGS) $(TEST_SRC) -o $(TEST_TARGET) $(LIBS)
|
||||
|
||||
.PHONY: all clean run test
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
#include "game.h"
|
||||
#include <raylib.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// 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
|
||||
void move(GameState_t *state, MOVE_DIRECTION direction) {
|
||||
bool move(GameState_t *state, move_direction_t direction) {
|
||||
bool merged[BOARD_HEIGHT][BOARD_WIDTH] = {0};
|
||||
bool moved = false;
|
||||
switch (direction) {
|
||||
case UP: {
|
||||
for (int y = 1; y < BOARD_HEIGHT; y++) {
|
||||
@@ -43,6 +47,7 @@ void move(GameState_t *state, MOVE_DIRECTION direction) {
|
||||
state->board[y][x] = 0;
|
||||
state->score += state->board[target_y][x];
|
||||
}
|
||||
moved = true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -77,6 +82,7 @@ void move(GameState_t *state, MOVE_DIRECTION direction) {
|
||||
state->board[y][x] = 0;
|
||||
state->score += state->board[target_y][x];
|
||||
}
|
||||
moved = true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -111,6 +117,7 @@ void move(GameState_t *state, MOVE_DIRECTION direction) {
|
||||
state->board[y][x] = 0;
|
||||
state->score += state->board[y][target_x];
|
||||
}
|
||||
moved = true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -145,22 +152,23 @@ void move(GameState_t *state, MOVE_DIRECTION direction) {
|
||||
state->board[y][x] = 0;
|
||||
state->score += state->board[y][target_x];
|
||||
}
|
||||
moved = true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
state->moves_made++;
|
||||
if (moved)
|
||||
state->moves_made++;
|
||||
|
||||
return moved;
|
||||
}
|
||||
|
||||
void reset_board(GameState_t *state) {
|
||||
for (int y = 0; y < BOARD_HEIGHT; y++) {
|
||||
for (int x = 0; x < BOARD_WIDTH; x++) {
|
||||
state->board[y][x] = 0;
|
||||
}
|
||||
}
|
||||
memset(state->board, 0, sizeof(state->board));
|
||||
state->score = 0;
|
||||
state->moves_made = 0;
|
||||
state->status = PLAYING;
|
||||
|
||||
int tile_count = 0;
|
||||
while (tile_count != STARTING_TILES) {
|
||||
@@ -172,9 +180,14 @@ void reset_board(GameState_t *state) {
|
||||
state->board[y][x] = (rand() % 10 == 0) ? 4 : 2; // 10% chance to spawn a 4
|
||||
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) {
|
||||
void spawn_tile(GameState_t *state, Vector2 *spawned_tile_pos) {
|
||||
int empty_tiles[BOARD_WIDTH * BOARD_HEIGHT][2];
|
||||
int empty_count = 0;
|
||||
|
||||
@@ -189,11 +202,32 @@ bool spawn_tile(GameState_t *state) {
|
||||
}
|
||||
|
||||
if (empty_count == 0)
|
||||
return false;
|
||||
return;
|
||||
|
||||
int random_index = rand() % empty_count;
|
||||
int spawn_y = empty_tiles[random_index][0];
|
||||
int spawn_x = empty_tiles[random_index][1];
|
||||
state->board[spawn_y][spawn_x] = (rand() % 10 == 0) ? 4 : 2; // 10% chance to spawn a 4
|
||||
return true;
|
||||
}
|
||||
|
||||
spawned_tile_pos->x = spawn_x;
|
||||
spawned_tile_pos->y = spawn_y;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// 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
|
||||
#include <raylib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define BOARD_WIDTH 4
|
||||
#define BOARD_HEIGHT 4
|
||||
#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{
|
||||
int board[BOARD_HEIGHT][BOARD_WIDTH];
|
||||
int score;
|
||||
int moves_made;
|
||||
typedef struct {
|
||||
unsigned int board[BOARD_HEIGHT][BOARD_WIDTH];
|
||||
game_status_t status;
|
||||
unsigned int score;
|
||||
unsigned int moves_made;
|
||||
} GameState_t;
|
||||
|
||||
|
||||
void move(GameState_t *state, MOVE_DIRECTION direction);
|
||||
bool move(GameState_t *state, move_direction_t direction);
|
||||
void reset_board(GameState_t *state);
|
||||
bool spawn_tile(GameState_t *state);
|
||||
void spawn_tile(GameState_t *state, Vector2 *spawned_tile_pos);
|
||||
bool can_move(GameState_t *state);
|
||||
|
||||
@@ -1,71 +1,48 @@
|
||||
#include "raylib.h"
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "game.h"
|
||||
#include "ui.h"
|
||||
|
||||
const Color tile_colors[] = {
|
||||
LIGHTGRAY, // 0 - Empty
|
||||
BEIGE, // 2
|
||||
GOLD, // 4
|
||||
ORANGE, // 8
|
||||
ORANGE, // 16
|
||||
RED, // 32
|
||||
MAROON, // 64
|
||||
YELLOW, // 128
|
||||
GOLD, // 256
|
||||
ORANGE, // 512
|
||||
RED, // 1024
|
||||
MAROON, // 2048
|
||||
PINK, // 4096
|
||||
MAGENTA, // 8192
|
||||
};
|
||||
|
||||
static GameState_t game_state;
|
||||
|
||||
int main() {
|
||||
srand(time(NULL));
|
||||
|
||||
reset_board(&game_state);
|
||||
FILE *game_file = fopen("save.dat", "rb");
|
||||
if (game_file != NULL) {
|
||||
if (fread(&game_state, sizeof(GameState_t), 1, game_file) != 1) {
|
||||
reset_board(&game_state);
|
||||
}
|
||||
fclose(game_file);
|
||||
} else {
|
||||
reset_board(&game_state);
|
||||
}
|
||||
|
||||
InitWindow(800, 600, "2048");
|
||||
SetTargetFPS(30); // So my laptop doesn't burn
|
||||
SetConfigFlags(FLAG_WINDOW_HIGHDPI);
|
||||
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "2048");
|
||||
SetTargetFPS(90); // We have event blocking so we can affort higher FPS
|
||||
EnableEventWaiting();
|
||||
|
||||
while (!WindowShouldClose()) {
|
||||
ClearBackground(RAYWHITE);
|
||||
BeginDrawing();
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
for (int y = 0; y < BOARD_HEIGHT; y++) {
|
||||
for (int x = 0; x < BOARD_WIDTH; x++) {
|
||||
int tile_value = game_state.board[y][x];
|
||||
int pos_x = BOARD_MARGIN + x * (TILE_SIZE + TILE_SPACING);
|
||||
int pos_y = BOARD_MARGIN + y * (TILE_SIZE + TILE_SPACING);
|
||||
int color_index = (tile_value == 0) ? 0 : (int)log2(tile_value);
|
||||
|
||||
if (color_index >= sizeof(tile_colors) / sizeof(tile_colors[0])) {
|
||||
color_index = sizeof(tile_colors) / sizeof(tile_colors[0]) - 1; // Cap to the last color
|
||||
}
|
||||
|
||||
DrawRectangle(pos_x, pos_y, TILE_SIZE, TILE_SIZE, tile_colors[color_index]);
|
||||
if (tile_value != 0) {
|
||||
const char *text = TextFormat("%d", tile_value);
|
||||
DrawText(text, pos_x + TILE_SIZE / 2 - MeasureText(text, 20) / 2, pos_y + TILE_SIZE / 2 - 10, 20, BLACK);
|
||||
}
|
||||
}
|
||||
// If we are not animating, we can wait for input events instead of continuously rendering
|
||||
bool animating = render(&game_state);
|
||||
if (animating) {
|
||||
DisableEventWaiting();
|
||||
} else {
|
||||
EnableEventWaiting();
|
||||
}
|
||||
|
||||
DrawText(TextFormat("Score: %d", game_state.score), 600, BOARD_MARGIN, 20, BLACK);
|
||||
DrawText(TextFormat("Moves used: %d", game_state.moves_made), 600, BOARD_MARGIN + 20, 20, BLACK);
|
||||
|
||||
// Reset button
|
||||
DrawRectangle(500, 455, 275, 24, RED);
|
||||
DrawText("Reset", 500 + 275 / 2 - MeasureText("Reset", 20) / 2, 455 + 4, 20, WHITE);
|
||||
|
||||
EndDrawing();
|
||||
handle_mouse_input(&game_state);
|
||||
handle_keyboard_input(&game_state);
|
||||
}
|
||||
CloseWindow();
|
||||
return 0;
|
||||
|
||||
@@ -0,0 +1,370 @@
|
||||
#include "munit/munit.h"
|
||||
#include "game.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
static void clear_tiles(GameState_t *state) {
|
||||
for (int y = 0; y < BOARD_HEIGHT; y++) {
|
||||
for (int x = 0; x < BOARD_WIDTH; x++) {
|
||||
state->board[y][x] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void *test_setup(const MunitParameter params[], void *user_data) {
|
||||
(void)params;
|
||||
(void)user_data;
|
||||
|
||||
GameState_t *state = (GameState_t *)malloc(sizeof(GameState_t));
|
||||
munit_assert_not_null(state);
|
||||
|
||||
reset_board(state);
|
||||
clear_tiles(state);
|
||||
return state;
|
||||
}
|
||||
|
||||
static void test_tear_down(void *fixture) {
|
||||
free(fixture);
|
||||
}
|
||||
|
||||
static MunitResult test_merge_left_simple(const MunitParameter params[], void *fixture) {
|
||||
(void)params;
|
||||
GameState_t *state = (GameState_t *)fixture;
|
||||
|
||||
state->board[0][0] = 2;
|
||||
state->board[0][1] = 2;
|
||||
|
||||
munit_assert_true(move(state, LEFT));
|
||||
munit_assert_int(state->board[0][0], ==, 4);
|
||||
munit_assert_int(state->board[0][1], ==, 0);
|
||||
munit_assert_uint(state->score, ==, 4);
|
||||
munit_assert_uint(state->moves_made, ==, 1);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static MunitResult test_merge_left_through_gap(const MunitParameter params[], void *fixture) {
|
||||
(void)params;
|
||||
GameState_t *state = (GameState_t *)fixture;
|
||||
|
||||
state->board[0][0] = 2;
|
||||
state->board[0][2] = 2;
|
||||
|
||||
munit_assert_true(move(state, LEFT));
|
||||
munit_assert_int(state->board[0][0], ==, 4);
|
||||
munit_assert_int(state->board[0][1], ==, 0);
|
||||
munit_assert_int(state->board[0][2], ==, 0);
|
||||
munit_assert_int(state->board[0][3], ==, 0);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static MunitResult test_merge_left_double_pair(const MunitParameter params[], void *fixture) {
|
||||
(void)params;
|
||||
GameState_t *state = (GameState_t *)fixture;
|
||||
|
||||
state->board[0][0] = 2;
|
||||
state->board[0][1] = 2;
|
||||
state->board[0][2] = 4;
|
||||
state->board[0][3] = 4;
|
||||
|
||||
munit_assert_true(move(state, LEFT));
|
||||
munit_assert_int(state->board[0][0], ==, 4);
|
||||
munit_assert_int(state->board[0][1], ==, 8);
|
||||
munit_assert_int(state->board[0][2], ==, 0);
|
||||
munit_assert_int(state->board[0][3], ==, 0);
|
||||
munit_assert_uint(state->score, ==, 12);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static MunitResult test_merge_left_no_double_merge_single_move(const MunitParameter params[], void *fixture) {
|
||||
(void)params;
|
||||
GameState_t *state = (GameState_t *)fixture;
|
||||
|
||||
state->board[0][0] = 2;
|
||||
state->board[0][1] = 2;
|
||||
state->board[0][2] = 2;
|
||||
|
||||
munit_assert_true(move(state, LEFT));
|
||||
munit_assert_int(state->board[0][0], ==, 4);
|
||||
munit_assert_int(state->board[0][1], ==, 2);
|
||||
munit_assert_int(state->board[0][2], ==, 0);
|
||||
munit_assert_int(state->board[0][3], ==, 0);
|
||||
munit_assert_uint(state->score, ==, 4);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static MunitResult test_move_left_no_merge_when_different_values(const MunitParameter params[], void *fixture) {
|
||||
(void)params;
|
||||
GameState_t *state = (GameState_t *)fixture;
|
||||
|
||||
state->board[0][1] = 2;
|
||||
state->board[0][2] = 4;
|
||||
|
||||
munit_assert_true(move(state, LEFT));
|
||||
munit_assert_int(state->board[0][0], ==, 2);
|
||||
munit_assert_int(state->board[0][1], ==, 4);
|
||||
munit_assert_int(state->board[0][2], ==, 0);
|
||||
munit_assert_int(state->board[0][3], ==, 0);
|
||||
munit_assert_uint(state->score, ==, 0);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static MunitResult test_move_returns_false_when_nothing_changes(const MunitParameter params[], void *fixture) {
|
||||
(void)params;
|
||||
GameState_t *state = (GameState_t *)fixture;
|
||||
|
||||
state->board[0][0] = 2;
|
||||
state->board[0][1] = 4;
|
||||
state->board[0][2] = 8;
|
||||
state->board[0][3] = 16;
|
||||
|
||||
munit_assert_false(move(state, LEFT));
|
||||
munit_assert_uint(state->moves_made, ==, 0);
|
||||
munit_assert_uint(state->score, ==, 0);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static MunitResult test_merge_right_simple(const MunitParameter params[], void *fixture) {
|
||||
(void)params;
|
||||
GameState_t *state = (GameState_t *)fixture;
|
||||
|
||||
state->board[1][2] = 2;
|
||||
state->board[1][3] = 2;
|
||||
|
||||
munit_assert_true(move(state, RIGHT));
|
||||
munit_assert_int(state->board[1][0], ==, 0);
|
||||
munit_assert_int(state->board[1][1], ==, 0);
|
||||
munit_assert_int(state->board[1][2], ==, 0);
|
||||
munit_assert_int(state->board[1][3], ==, 4);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static MunitResult test_merge_right_through_gap(const MunitParameter params[], void *fixture) {
|
||||
(void)params;
|
||||
GameState_t *state = (GameState_t *)fixture;
|
||||
|
||||
state->board[1][1] = 2;
|
||||
state->board[1][3] = 2;
|
||||
|
||||
munit_assert_true(move(state, RIGHT));
|
||||
munit_assert_int(state->board[1][0], ==, 0);
|
||||
munit_assert_int(state->board[1][1], ==, 0);
|
||||
munit_assert_int(state->board[1][2], ==, 0);
|
||||
munit_assert_int(state->board[1][3], ==, 4);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static MunitResult test_merge_right_double_pair(const MunitParameter params[], void *fixture) {
|
||||
(void)params;
|
||||
GameState_t *state = (GameState_t *)fixture;
|
||||
|
||||
state->board[1][0] = 2;
|
||||
state->board[1][1] = 2;
|
||||
state->board[1][2] = 4;
|
||||
state->board[1][3] = 4;
|
||||
|
||||
munit_assert_true(move(state, RIGHT));
|
||||
munit_assert_int(state->board[1][0], ==, 0);
|
||||
munit_assert_int(state->board[1][1], ==, 0);
|
||||
munit_assert_int(state->board[1][2], ==, 4);
|
||||
munit_assert_int(state->board[1][3], ==, 8);
|
||||
munit_assert_uint(state->score, ==, 12);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static MunitResult test_merge_right_no_double_merge_single_move(const MunitParameter params[], void *fixture) {
|
||||
(void)params;
|
||||
GameState_t *state = (GameState_t *)fixture;
|
||||
|
||||
state->board[1][1] = 2;
|
||||
state->board[1][2] = 2;
|
||||
state->board[1][3] = 2;
|
||||
|
||||
munit_assert_true(move(state, RIGHT));
|
||||
munit_assert_int(state->board[1][0], ==, 0);
|
||||
munit_assert_int(state->board[1][1], ==, 0);
|
||||
munit_assert_int(state->board[1][2], ==, 2);
|
||||
munit_assert_int(state->board[1][3], ==, 4);
|
||||
munit_assert_uint(state->score, ==, 4);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static MunitResult test_merge_up_simple(const MunitParameter params[], void *fixture) {
|
||||
(void)params;
|
||||
GameState_t *state = (GameState_t *)fixture;
|
||||
|
||||
state->board[0][0] = 2;
|
||||
state->board[1][0] = 2;
|
||||
|
||||
munit_assert_true(move(state, UP));
|
||||
munit_assert_int(state->board[0][0], ==, 4);
|
||||
munit_assert_int(state->board[1][0], ==, 0);
|
||||
munit_assert_int(state->board[2][0], ==, 0);
|
||||
munit_assert_int(state->board[3][0], ==, 0);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static MunitResult test_merge_up_through_gap(const MunitParameter params[], void *fixture) {
|
||||
(void)params;
|
||||
GameState_t *state = (GameState_t *)fixture;
|
||||
|
||||
state->board[0][1] = 2;
|
||||
state->board[2][1] = 2;
|
||||
|
||||
munit_assert_true(move(state, UP));
|
||||
munit_assert_int(state->board[0][1], ==, 4);
|
||||
munit_assert_int(state->board[1][1], ==, 0);
|
||||
munit_assert_int(state->board[2][1], ==, 0);
|
||||
munit_assert_int(state->board[3][1], ==, 0);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static MunitResult test_merge_up_double_pair(const MunitParameter params[], void *fixture) {
|
||||
(void)params;
|
||||
GameState_t *state = (GameState_t *)fixture;
|
||||
|
||||
state->board[0][1] = 2;
|
||||
state->board[1][1] = 2;
|
||||
state->board[2][1] = 4;
|
||||
state->board[3][1] = 4;
|
||||
|
||||
munit_assert_true(move(state, UP));
|
||||
munit_assert_int(state->board[0][1], ==, 4);
|
||||
munit_assert_int(state->board[1][1], ==, 8);
|
||||
munit_assert_int(state->board[2][1], ==, 0);
|
||||
munit_assert_int(state->board[3][1], ==, 0);
|
||||
munit_assert_uint(state->score, ==, 12);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static MunitResult test_merge_up_no_double_merge_single_move(const MunitParameter params[], void *fixture) {
|
||||
(void)params;
|
||||
GameState_t *state = (GameState_t *)fixture;
|
||||
|
||||
state->board[0][1] = 2;
|
||||
state->board[1][1] = 2;
|
||||
state->board[2][1] = 2;
|
||||
|
||||
munit_assert_true(move(state, UP));
|
||||
munit_assert_int(state->board[0][1], ==, 4);
|
||||
munit_assert_int(state->board[1][1], ==, 2);
|
||||
munit_assert_int(state->board[2][1], ==, 0);
|
||||
munit_assert_int(state->board[3][1], ==, 0);
|
||||
munit_assert_uint(state->score, ==, 4);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static MunitResult test_merge_down_simple(const MunitParameter params[], void *fixture) {
|
||||
(void)params;
|
||||
GameState_t *state = (GameState_t *)fixture;
|
||||
|
||||
state->board[1][2] = 2;
|
||||
state->board[2][2] = 2;
|
||||
|
||||
munit_assert_true(move(state, DOWN));
|
||||
munit_assert_int(state->board[0][2], ==, 0);
|
||||
munit_assert_int(state->board[1][2], ==, 0);
|
||||
munit_assert_int(state->board[2][2], ==, 0);
|
||||
munit_assert_int(state->board[3][2], ==, 4);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static MunitResult test_merge_down_through_gap(const MunitParameter params[], void *fixture) {
|
||||
(void)params;
|
||||
GameState_t *state = (GameState_t *)fixture;
|
||||
|
||||
state->board[0][2] = 2;
|
||||
state->board[2][2] = 2;
|
||||
|
||||
munit_assert_true(move(state, DOWN));
|
||||
munit_assert_int(state->board[0][2], ==, 0);
|
||||
munit_assert_int(state->board[1][2], ==, 0);
|
||||
munit_assert_int(state->board[2][2], ==, 0);
|
||||
munit_assert_int(state->board[3][2], ==, 4);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static MunitResult test_merge_down_double_pair(const MunitParameter params[], void *fixture) {
|
||||
(void)params;
|
||||
GameState_t *state = (GameState_t *)fixture;
|
||||
|
||||
state->board[0][2] = 2;
|
||||
state->board[1][2] = 2;
|
||||
state->board[2][2] = 4;
|
||||
state->board[3][2] = 4;
|
||||
|
||||
munit_assert_true(move(state, DOWN));
|
||||
munit_assert_int(state->board[0][2], ==, 0);
|
||||
munit_assert_int(state->board[1][2], ==, 0);
|
||||
munit_assert_int(state->board[2][2], ==, 4);
|
||||
munit_assert_int(state->board[3][2], ==, 8);
|
||||
munit_assert_uint(state->score, ==, 12);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static MunitResult test_merge_down_no_double_merge_single_move(const MunitParameter params[], void *fixture) {
|
||||
(void)params;
|
||||
GameState_t *state = (GameState_t *)fixture;
|
||||
|
||||
state->board[1][2] = 2;
|
||||
state->board[2][2] = 2;
|
||||
state->board[3][2] = 2;
|
||||
|
||||
munit_assert_true(move(state, DOWN));
|
||||
munit_assert_int(state->board[0][2], ==, 0);
|
||||
munit_assert_int(state->board[1][2], ==, 0);
|
||||
munit_assert_int(state->board[2][2], ==, 2);
|
||||
munit_assert_int(state->board[3][2], ==, 4);
|
||||
munit_assert_uint(state->score, ==, 4);
|
||||
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static MunitTest tests[] = {
|
||||
{(char *)"/merge/left/simple", test_merge_left_simple, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
|
||||
{(char *)"/merge/left/through-gap", test_merge_left_through_gap, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
|
||||
{(char *)"/merge/left/double-pair", test_merge_left_double_pair, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
|
||||
{(char *)"/merge/left/no-double-merge", test_merge_left_no_double_merge_single_move, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
|
||||
{(char *)"/merge/left/different-values-no-merge", test_move_left_no_merge_when_different_values, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
|
||||
{(char *)"/move/no-change", test_move_returns_false_when_nothing_changes, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
|
||||
{(char *)"/merge/right/simple", test_merge_right_simple, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
|
||||
{(char *)"/merge/right/through-gap", test_merge_right_through_gap, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
|
||||
{(char *)"/merge/right/double-pair", test_merge_right_double_pair, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
|
||||
{(char *)"/merge/right/no-double-merge", test_merge_right_no_double_merge_single_move, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
|
||||
{(char *)"/merge/up/simple", test_merge_up_simple, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
|
||||
{(char *)"/merge/up/through-gap", test_merge_up_through_gap, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
|
||||
{(char *)"/merge/up/double-pair", test_merge_up_double_pair, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
|
||||
{(char *)"/merge/up/no-double-merge", test_merge_up_no_double_merge_single_move, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
|
||||
{(char *)"/merge/down/simple", test_merge_down_simple, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
|
||||
{(char *)"/merge/down/through-gap", test_merge_down_through_gap, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
|
||||
{(char *)"/merge/down/double-pair", test_merge_down_double_pair, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
|
||||
{(char *)"/merge/down/no-double-merge", test_merge_down_no_double_merge_single_move, test_setup, test_tear_down, MUNIT_TEST_OPTION_NONE, NULL},
|
||||
{NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL}};
|
||||
|
||||
static const MunitSuite suite = {
|
||||
(char *)"",
|
||||
tests,
|
||||
NULL,
|
||||
1,
|
||||
MUNIT_SUITE_OPTION_NONE,
|
||||
};
|
||||
|
||||
int main(int argc, char *argv[MUNIT_ARRAY_PARAM(argc + 1)]) {
|
||||
return munit_suite_main(&suite, NULL, argc, argv);
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
@@ -3,19 +3,95 @@
|
||||
#include <math.h>
|
||||
#include <raylib.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
const Color tile_colors[] = {
|
||||
LIGHTGRAY, // 0 - Empty
|
||||
BEIGE, // 2
|
||||
GOLD, // 4
|
||||
ORANGE, // 8
|
||||
ORANGE, // 16
|
||||
RED, // 32
|
||||
MAROON, // 64
|
||||
YELLOW, // 128
|
||||
GOLD, // 256
|
||||
ORANGE, // 512
|
||||
RED, // 1024
|
||||
MAROON, // 2048
|
||||
PINK, // 4096
|
||||
MAGENTA, // 8192
|
||||
};
|
||||
|
||||
static bool is_dragging;
|
||||
static Vector2 drag_start_pos;
|
||||
static bool is_animating;
|
||||
|
||||
// If there isn't a tile to animate then store it as {-1,-1}
|
||||
static Vector2 new_tile = {-1, 1};
|
||||
static float new_tile_animation_progress;
|
||||
|
||||
typedef struct {
|
||||
Vector2 from;
|
||||
Vector2 to;
|
||||
// We need to store this to know when to (show the) merge, if merging
|
||||
Vector2 position;
|
||||
int value;
|
||||
} animated_tile_t;
|
||||
|
||||
static animated_tile_t animated_tiles[BOARD_WIDTH * BOARD_HEIGHT];
|
||||
|
||||
// Common logic shared between both input handlers
|
||||
static void after_input(GameState_t *game_state) {
|
||||
spawn_tile(game_state, &new_tile);
|
||||
if (!can_move(game_state)) {
|
||||
is_animating = false; // No more moves, so we won't be animating anymore
|
||||
game_state->status = LOST;
|
||||
} else {
|
||||
// We moved, merged or spawned a tile, so we will be animating
|
||||
is_animating = true;
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
void handle_mouse_input(GameState_t *game_state) {
|
||||
// Don't allow any input while animating so we stay in sync
|
||||
if (is_animating)
|
||||
return;
|
||||
|
||||
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
|
||||
drag_start_pos = GetMousePosition();
|
||||
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);
|
||||
return;
|
||||
|
||||
// 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) {
|
||||
reset_board(game_state);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
is_dragging = true;
|
||||
if (game_state->status == PLAYING)
|
||||
is_dragging = true;
|
||||
} else if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON) && is_dragging) {
|
||||
Vector2 current_pos = GetMousePosition();
|
||||
|
||||
@@ -39,9 +115,106 @@ void handle_mouse_input(GameState_t *game_state) {
|
||||
move(game_state, UP);
|
||||
}
|
||||
}
|
||||
spawn_tile(game_state);
|
||||
after_input(game_state);
|
||||
}
|
||||
|
||||
is_dragging = false;
|
||||
}
|
||||
}
|
||||
|
||||
void handle_keyboard_input(GameState_t *game_state) {
|
||||
if (IsKeyReleased(KEY_R) || (game_state->status != PLAYING && IsKeyReleased(KEY_ENTER))) {
|
||||
reset_board(game_state);
|
||||
}
|
||||
|
||||
bool moved = false;
|
||||
if (IsKeyReleased(KEY_UP)) {
|
||||
moved = move(game_state, UP);
|
||||
} else if (IsKeyReleased(KEY_DOWN)) {
|
||||
moved = move(game_state, DOWN);
|
||||
} else if (IsKeyReleased(KEY_LEFT)) {
|
||||
moved = move(game_state, LEFT);
|
||||
} else if (IsKeyReleased(KEY_RIGHT)) {
|
||||
moved = move(game_state, RIGHT);
|
||||
}
|
||||
|
||||
if (moved) {
|
||||
after_input(game_state);
|
||||
}
|
||||
}
|
||||
|
||||
static void animate_new_tile() {
|
||||
if (new_tile.x == -1 && new_tile.y == -1)
|
||||
return;
|
||||
|
||||
new_tile_animation_progress += GetFrameTime() * 5;
|
||||
if (new_tile_animation_progress >= 1.0f) {
|
||||
new_tile_animation_progress = 0.0f;
|
||||
new_tile = (Vector2){-1, -1}; // Reset after animation completes
|
||||
is_animating = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Main render loop, responsible ONLY for redering
|
||||
bool render(GameState_t *game_state) {
|
||||
if (is_animating) {
|
||||
animate_new_tile();
|
||||
}
|
||||
|
||||
for (int y = 0; y < BOARD_HEIGHT; y++) {
|
||||
for (int x = 0; x < BOARD_WIDTH; x++) {
|
||||
int tile_value = game_state->board[y][x];
|
||||
int pos_x = BOARD_MARGIN + x * (TILE_SIZE + TILE_SPACING);
|
||||
int pos_y = BOARD_MARGIN + y * (TILE_SIZE + TILE_SPACING);
|
||||
int color_index = (tile_value == 0) ? 0 : (int)log2(tile_value);
|
||||
|
||||
if ((size_t)color_index >= sizeof(tile_colors) / sizeof(tile_colors[0])) {
|
||||
color_index = sizeof(tile_colors) / sizeof(tile_colors[0]) - 1; // Cap to the last color
|
||||
}
|
||||
|
||||
if (is_animating && new_tile.x == x && new_tile.y == y) {
|
||||
float scale = 1.0 - 0.2 * sin(new_tile_animation_progress * PI);
|
||||
int scaled_size = (int)(TILE_SIZE * scale);
|
||||
int offset = (TILE_SIZE - scaled_size) / 2;
|
||||
DrawRectangle(pos_x + offset, pos_y + offset, scaled_size, scaled_size, tile_colors[color_index]);
|
||||
} else {
|
||||
DrawRectangle(pos_x, pos_y, TILE_SIZE, TILE_SIZE, tile_colors[color_index]);
|
||||
}
|
||||
if (tile_value != 0) {
|
||||
const char *text = TextFormat("%d", tile_value);
|
||||
DrawText(text, pos_x + TILE_SIZE / 2 - MeasureText(text, 20) / 2, pos_y + TILE_SIZE / 2 - 10, 20, BLACK);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DrawText(TextFormat("Score: %d", game_state->score), WINDOW_HEIGHT, BOARD_MARGIN, 20, BLACK);
|
||||
DrawText(TextFormat("Moves used: %d", game_state->moves_made), WINDOW_HEIGHT, BOARD_MARGIN + 20, 20, BLACK);
|
||||
|
||||
DrawText("By Piotr Kozak", WINDOW_WIDTH - MeasureText("By Piotr Kozak", 16) - 10, WINDOW_HEIGHT - 24, 16, GRAY);
|
||||
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
|
||||
DrawRectangle(500, 455, 275, 24, RED);
|
||||
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);
|
||||
}
|
||||
return is_animating;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user