diff --git a/src/2048_engine.c b/src/2048_engine.c index 5001f1b..118b4fe 100644 --- a/src/2048_engine.c +++ b/src/2048_engine.c @@ -224,6 +224,8 @@ struct gameoptions* gameoptions_default(void) opt->goal = 2048; opt->spawn_value = 2; opt->spawn_rate = 1; + opt->enable_color = 0; + opt->animate = 1; return opt; } @@ -245,3 +247,73 @@ void gamestate_clear(struct gamestate *g) free(g->grid); free(g); } + +/* The following may be moved into own file */ +void reset_highscore(void) +{ + printf("Are you sure you want to reset your highscores? (Y)es/(N)o: "); + + int response; + if ((response = getchar()) == 'y' || response == 'Y') { + printf("Resetting highscore...\n"); + } +} + +void print_usage(void) +{ + printf( + "usage: 2048 [-cCrh] [-b ] [-s ]\n" + "\n" + "controls\n" + " hjkl movement keys\n" + " q quit current game\n" + "\n" + "options\n" + " -s set the grid side lengths\n" + " -b set the block spawn rate\n" + " -a enable animations (default)\n" + " -A disable animations\n" + " -c enable color support\n" + " -C disable color support (default)\n" + ); +} + +#include + +struct gameoptions* parse_options(struct gameoptions *opt, int argc, char **argv) +{ + int c; + while ((c = getopt(argc, argv, "aArcChs:b:")) != -1) { + switch (c) { + case 'a': + opt->animate = 1; + break; + case 'A': + opt->animate = 0; + break; + case 'c': + opt->enable_color = 1; + break; + case 'C': + opt->enable_color = 0; + break; + case 's':; + /* Stick with square for now */ + int optint = strtol(optarg, NULL, 10); + opt->grid_height = optint > 4 ? optint : 4; + opt->grid_width = optint > 4 ? optint : 4; + break; + case 'b': + opt->spawn_rate = strtol(optarg, NULL, 10); + break; + case 'r': + reset_highscore(); + exit(0); + case 'h': + print_usage(); + exit(0); + } + } + + return opt; +} diff --git a/src/2048_engine.h b/src/2048_engine.h index 015c34e..cb9b277 100644 --- a/src/2048_engine.h +++ b/src/2048_engine.h @@ -18,6 +18,8 @@ struct gameoptions { long goal; long spawn_value; int spawn_rate; + int enable_color; + int animate; }; struct gamestate { @@ -30,11 +32,9 @@ struct gamestate { int print_width; /* Options */ struct gameoptions *opts; - /* Draw functions */ - void (*ds_draw)(struct gamestate*); - void (*ds_clear)(void); }; +struct gameoptions* parse_options(struct gameoptions*, int, char**); void gravitate(struct gamestate*, direction, void (*callback)(struct gamestate*)); void merge(struct gamestate*, direction, void (*callback)(struct gamestate*)); int moves_available(struct gamestate *); diff --git a/src/2048_rewrite.c b/src/2048_rewrite.c index 98ccc52..fc4eee2 100644 --- a/src/2048_rewrite.c +++ b/src/2048_rewrite.c @@ -6,7 +6,7 @@ #include #include "2048_engine.h" -#define ITER(x, expr)\ +#define iterate(x, expr)\ do {\ int i;\ for (i = 0; i < x; ++i) { expr; }\ @@ -31,7 +31,7 @@ void draw_screen(struct gamestate *g) mvwprintw(gamewin, 0, 0, scr, g->score, g->score_last); mvwprintw(gamewin, 1, 0, "HISCR: %d\n", g->score_high); - ITER(g->opts->grid_width*(g->print_width + 2) + 1, waddch(gamewin, '-')); + iterate(g->opts->grid_width*(g->print_width + 2) + 1, waddch(gamewin, '-')); int x, y, xps = 0, yps = 3; for (y = 0; y < g->opts->grid_height; y++, xps = 0, yps++) { mvwprintw(gamewin, yps, xps++, "|"); @@ -41,13 +41,13 @@ void draw_screen(struct gamestate *g) mvwprintw(gamewin, yps, xps + g->print_width, " |"); } else { - ITER(g->print_width + 1, waddch(gamewin, ' ')); + iterate(g->print_width + 1, waddch(gamewin, ' ')); waddch(gamewin, '|'); } xps += (g->print_width + 2); } } - ITER(g->opts->grid_height*(g->print_width + 2) + 1, waddch(gamewin, '-')); + iterate(g->opts->grid_height*(g->print_width + 2) + 1, waddch(gamewin, '-')); wrefresh(gamewin); } @@ -93,7 +93,6 @@ int get_keypress(void) return fgetc(stdin); } - void draw_screen(struct gamestate *g) { /* Clear the screen each draw if we are able to */ @@ -107,7 +106,7 @@ void draw_screen(struct gamestate *g) printf("\n"); // alter this grid_size + 1 to match abitrary grid size - ITER(g->opts->grid_width, printf("------")); + iterate(g->opts->grid_width, printf("------")); printf("-\n"); int x, y; for (y = 0; y < g->opts->grid_height; y++) { @@ -120,7 +119,7 @@ void draw_screen(struct gamestate *g) } printf("\n"); } - ITER(g->opts->grid_width, printf("------")); + iterate(g->opts->grid_width, printf("------")); printf("-\n\n"); } #endif /* CURSES */ @@ -128,15 +127,17 @@ void draw_screen(struct gamestate *g) void ddraw(struct gamestate *g) { draw_screen(g); - usleep(30000); + usleep(40000); } int main(int argc, char **argv) { - struct gamestate *g = gamestate_init(gameoptions_default()); + struct gamestate *g = gamestate_init( + parse_options( + gameoptions_default(), argc, argv)); drawstate_init(); - + while (1) { draw_screen(g); @@ -152,7 +153,7 @@ int main(int argc, char **argv) break; } - gamestate_tick(g, ch, ddraw); + gamestate_tick(g, ch, g->opts->animate ? ddraw : NULL); } endloop: