Skip to content

Commit

Permalink
Allow custom grid dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
zedr committed Jan 21, 2024
1 parent 997a072 commit cb4ef60
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 34 deletions.
2 changes: 1 addition & 1 deletion include/tinycols/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct game {
*
* @return The new grid, or NULL if memory allocation fails.
*/
struct game *game_alloc(void);
struct game *game_alloc(uint8_t grid_rows, uint8_t grid_cols);

/**
* game_init() - Initialize a game.
Expand Down
48 changes: 40 additions & 8 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
#include "../include/queue.h"
#include "../include/gfx.h"

#define TINYCOLS_VERSION "0.7.0"
#define TINYCOLS_VERSION "0.8.0"
#define TICK_TIME 10000
#define MAX_TIMER 180

WINDOW *win;
uint8_t tmp_res[GRID_DEFAULT_COLS * GRID_DEFAULT_ROWS];
struct grid_drop tmp_drs[GRID_DEFAULT_COLS * GRID_DEFAULT_ROWS];
uint8_t *tmp_res;
struct grid_drop *tmp_drs;
struct game_event_queue qu;
int tick_time;
int chain_num;
Expand Down Expand Up @@ -153,7 +153,6 @@ void game_tick(struct game *gm)
if (piece_persist(&gm->current_piece, gm->grid)) {
chain_num = 0;
game_queue_push(&qu, gm->tick, scan_grid);

} else {
gm->status = GAME_OVER;
}
Expand Down Expand Up @@ -182,17 +181,19 @@ static uint32_t get_delay_usec(struct timeval start, struct timeval end)
/**
* run() - Run the game.
*/
static game_score_t run(enum game_class cls)
static game_score_t run(enum game_class cls, uint8_t game_rows, uint8_t game_cols)
{
win = setup_gfx();

struct game *gm = game_alloc();
struct game *gm = game_alloc(game_rows, game_cols);
if (gm == NULL) {
perror("Out of memory");
exit(EXIT_FAILURE);
}
game_init(gm, GAME_DEFAULT_LEVEL, cls);
game_queue_init(&qu, gm);
tmp_res = malloc(sizeof(*tmp_res) * gm->grid->size);
tmp_drs = malloc(sizeof(*tmp_drs) * gm->grid->size);

struct timeval time_start, time_end;
tick_time = get_tick_time(gm->level);
Expand Down Expand Up @@ -233,6 +234,8 @@ static game_score_t run(enum game_class cls)
}
game_score_t final_score = gm->score;
game_free(gm);
free(tmp_res);
free(tmp_drs);
teardown_gfx(win);
return final_score;
}
Expand Down Expand Up @@ -262,6 +265,10 @@ static void usage(char *prog_name)
"The default is 2.\n",
stderr);
fputs(" -d\t\tRun in debug mode.\n", stderr);
fputs(" -C\t\tUse a grid with the given number of columns.\n",
stderr);
fputs(" -R\t\tUse a grid with the given number of rows.\n",
stderr);
fputs(" -V\t\tPrint the program version.\n", stderr);
}

Expand All @@ -270,9 +277,11 @@ int main(int argc, char **argv)
init_rand();
int opt;
enum game_class cls = CLASS_AMATEUR;
uint8_t game_cols = GRID_DEFAULT_COLS;
uint8_t game_rows = GRID_DEFAULT_ROWS;

char *prog_name = argv[0];
while ((opt = getopt(argc, argv, "c:dhV")) != -1) {
while ((opt = getopt(argc, argv, "c:C:R:dhV")) != -1) {
switch (opt) {
case 'h': {
usage(prog_name);
Expand All @@ -296,13 +305,36 @@ int main(int argc, char **argv)
case 'V': {
fprintf(stderr, "%s\n", TINYCOLS_VERSION);
exit(EXIT_SUCCESS);
break;
}
case 'C': {
const int num = atoi(optarg);
if (num >= 2 && num <= 80) {
game_cols = num;
} else {
fprintf(stderr, "Error: columns must be between "
"2 and 80.\n");
exit(EXIT_FAILURE);
}
break;
}
case 'R': {
const int num = atoi(optarg);
if (num >= 6 && num <= 40) {
game_rows = num;
} else {
fprintf(stderr, "Error: rows must be between "
"6 and 40.\n");
exit(EXIT_FAILURE);
}
break;
}
default:
usage(prog_name);
exit(EXIT_FAILURE);
}
}

printf("Final score: %hu\n", run(cls));
printf("Final score: %hu\n", run(cls, game_rows, game_cols));
return 0;
}
4 changes: 2 additions & 2 deletions src/tinycols/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include "../../include/tinycols/game.h"
#include "../../include/tinycols/game_utils.h"

struct game *game_alloc(void)
struct game *game_alloc(uint8_t grid_rows, uint8_t grid_cols)
{
struct grid *gr = grid_alloc(GRID_DEFAULT_ROWS, GRID_DEFAULT_COLS);
struct grid *gr = grid_alloc(grid_rows, grid_cols);
if (gr == NULL) {
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion src/tinycols/piece.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void piece_randomize(struct piece *pc, unsigned int max_color)
bool piece_persist(struct piece *pc, struct grid *gr)
{
if (pc->col < 0 || pc->row < 0 || pc->col >= gr->cols ||
pc->row >= gr->rows || pc->col + PIECE_SIZE > gr->rows) {
pc->row + PIECE_SIZE > gr->rows) {
return false;
}

Expand Down
43 changes: 21 additions & 22 deletions tests/test_tinycols.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ MU_TEST(test_piece_rotate_1)

MU_TEST(test_game_init_1)
{
struct game *gm = game_alloc();
struct game *gm = game_alloc(GRID_DEFAULT_ROWS, GRID_DEFAULT_COLS);
game_init(gm, GAME_DEFAULT_LEVEL, CLASS_PRO);

mu_assert_int_eq(GAME_DEFAULT_LEVEL, gm->level);
Expand Down Expand Up @@ -389,7 +389,7 @@ MU_TEST(test_game_set_level_1)
mu_assert_int_eq(1, get_level_by_jewels(31));
mu_assert_int_eq(2, get_level_by_jewels(60));

struct game *gm = game_alloc();
struct game *gm = game_alloc(GRID_DEFAULT_ROWS, GRID_DEFAULT_COLS);
game_init(gm, GAME_DEFAULT_LEVEL, CLASS_PRO);

gm->level = get_level_by_jewels(31);
Expand All @@ -398,25 +398,6 @@ MU_TEST(test_game_set_level_1)
game_free(gm);
}

MU_TEST(test_game_adjust_1)
{
struct game *gm = game_alloc();
game_init(gm, GAME_DEFAULT_LEVEL, CLASS_PRO);

mu_assert_int_eq(0, gm->level);

gm->jewels_removed = 10;
mu_assert_int_eq(false, game_adjust(gm));
mu_assert_int_eq(0, gm->level);
mu_assert_int_eq(GAME_DEFAULT_COLOR_MIN, gm->color_max);

gm->jewels_removed = GAME_DEFAULT_JEWELS_FOR_LEVEL;
mu_assert_int_eq(true, game_adjust(gm));
mu_assert_int_eq(1, gm->level);

game_free(gm);
}

MU_TEST(test_game_ticker_1)
{
struct game gm;
Expand All @@ -426,6 +407,24 @@ MU_TEST(test_game_ticker_1)
mu_assert_int_eq(44, gm.tick);
}

MU_TEST(test_game_large_grid_1)
{
struct game *gm = game_alloc(40, 80);
game_init(gm,
GAME_DEFAULT_LEVEL,
CLASS_PRO);

gm->current_piece.row = 40 - 3;
gm->current_piece.col = 40;
enum result res = piece_move_in_grid(&gm->current_piece,
DOWN,
gm->grid);
mu_assert(res == LANDED, "The piece should land");
mu_check(piece_persist(&gm->current_piece, gm->grid));

game_free(gm);
}

MU_TEST_SUITE(test_suite_grid)
{
MU_RUN_TEST(test_grid_create_and_init_1);
Expand Down Expand Up @@ -455,7 +454,7 @@ MU_TEST_SUITE(test_suite_game)
{
MU_RUN_TEST(test_game_init_1);
MU_RUN_TEST(test_game_set_level_1);
MU_RUN_TEST(test_game_adjust_1);
MU_RUN_TEST(test_game_large_grid_1);
}

MU_TEST_SUITE(test_suite_game_ticker)
Expand Down

0 comments on commit cb4ef60

Please sign in to comment.