Removed unsigned/signed compare warnings. Change all to size_t types

This commit is contained in:
Tiehuis 2014-12-07 09:55:18 +13:00
parent a116923c69
commit 3085bd4eb6
3 changed files with 56 additions and 38 deletions

View File

@ -15,7 +15,7 @@ void gravitate(struct gamestate *g, direction d, void (*callback)(struct gamesta
}\ }\
} while (0) } while (0)
int x, y; size_t x, y;
int done = 0; int done = 0;
if (d == dir_left) { if (d == dir_left) {
@ -89,7 +89,7 @@ void merge(struct gamestate *g, direction d, void (*callback)(struct gamestate *
}\ }\
} while (0) } while (0)
int x, y; size_t x, y;
g->score_last = 0; g->score_last = 0;
if (d == dir_left) { if (d == dir_left) {
@ -137,7 +137,7 @@ int end_condition(struct gamestate *g)
{ {
int ret = -1; int ret = -1;
int x, y; size_t x, y;
for (x = 0; x < g->opts->grid_width; ++x) { for (x = 0; x < g->opts->grid_width; ++x) {
for (y = 0; y < g->opts->grid_height; ++y) { for (y = 0; y < g->opts->grid_height; ++y) {
if (g->grid[x][y] >= g->opts->goal) if (g->grid[x][y] >= g->opts->goal)
@ -164,8 +164,8 @@ void random_block(struct gamestate *g)
srand(time(NULL)); srand(time(NULL));
} }
int x = rand() % g->opts->grid_width; size_t x = (size_t)rand() % g->opts->grid_width;
int y = rand() % g->opts->grid_height; size_t y = (size_t)rand() % g->opts->grid_height;
while (g->grid[x][y]) { while (g->grid[x][y]) {
x++; x++;
@ -183,7 +183,7 @@ static int flog10(unsigned int n)
{ {
int l = 0; int l = 0;
while (n) n /= 10, ++l; while (n) n /= 10, ++l;
return l; return l + 1;
} }
struct gamestate* gamestate_init(struct gameoptions *opt) struct gamestate* gamestate_init(struct gameoptions *opt)
@ -199,7 +199,7 @@ struct gamestate* gamestate_init(struct gameoptions *opt)
g->grid = malloc(opt->grid_width * sizeof(long*)); g->grid = malloc(opt->grid_width * sizeof(long*));
if (!g->grid) goto grid_alloc_fail; if (!g->grid) goto grid_alloc_fail;
int i; size_t i;
for (i = 0; i < opt->grid_height; ++i) for (i = 0; i < opt->grid_height; ++i)
g->grid[i] = calloc(opt->grid_height, sizeof(long)); g->grid[i] = calloc(opt->grid_height, sizeof(long));
@ -210,11 +210,10 @@ struct gamestate* gamestate_init(struct gameoptions *opt)
g->print_width = flog10(opt->goal); g->print_width = flog10(opt->goal);
g->opts = opt; g->opts = opt;
random_block(g);
return g; return g;
grid_alloc_fail: grid_alloc_fail:
grid_back_alloc_fail: //grid_back_alloc_fail:
free(g); free(g);
gamestate_alloc_fail: gamestate_alloc_fail:
return NULL; return NULL;
@ -225,13 +224,13 @@ struct gameoptions* gameoptions_default(void)
struct gameoptions *opt = malloc(sizeof(struct gameoptions)); struct gameoptions *opt = malloc(sizeof(struct gameoptions));
if (!opt) return NULL; if (!opt) return NULL;
opt->grid_height = 4; opt->grid_height = DEFAULT_GRID_HEIGHT;
opt->grid_width = 4; opt->grid_width = DEFAULT_GRID_WIDTH;
opt->goal = 2048; opt->goal = DEFAULT_GOAL;
opt->spawn_value = 2; opt->spawn_value = DEFAULT_SPAWN_VALUE;
opt->spawn_rate = 1; opt->spawn_rate = DEFAULT_SPAWN_RATE;
opt->enable_color = 0; opt->enable_color = DEFAULT_COLOR_TOGGLE;
opt->animate = 1; opt->animate = DEFAULT_ANIMATE_TOGGLE;
return opt; return opt;
} }
@ -242,7 +241,6 @@ int gamestate_tick(struct gamestate *g, direction d, void (*callback)(struct gam
gravitate(g, d, callback); gravitate(g, d, callback);
merge(g, d, callback); merge(g, d, callback);
gravitate(g, d, callback); gravitate(g, d, callback);
random_block(g);
return g->moved; return g->moved;
} }
@ -310,8 +308,10 @@ struct gameoptions* parse_options(struct gameoptions *opt, int argc, char **argv
case 's':; case 's':;
/* Stick with square for now */ /* Stick with square for now */
int optint = strtol(optarg, NULL, 10); int optint = strtol(optarg, NULL, 10);
opt->grid_height = optint > 4 ? optint : 4; if (optint < CONSTRAINT_GRID_MAX && optint > CONSTRAINT_GRID_MIN) {
opt->grid_width = optint > 4 ? optint : 4; opt->grid_height = optint;
opt->grid_width = optint;
}
break; break;
case 'b': case 'b':
opt->spawn_rate = strtol(optarg, NULL, 10); opt->spawn_rate = strtol(optarg, NULL, 10);

View File

@ -3,7 +3,21 @@
#include <stdio.h> #include <stdio.h>
#define fatal(msg) do { fprintf(stderr, "line %d: %s\n", __LINE__, msg); abort(); } while (0) #define CONSTRAINT_GRID_MIN 4
#define CONSTRAINT_GRID_MAX 20
#define DEFAULT_GRID_HEIGHT 4
#define DEFAULT_GRID_WIDTH 4
#define DEFAULT_GOAL 2048
#define DEFAULT_SPAWN_VALUE 2
#define DEFAULT_SPAWN_RATE 1
#define DEFAULT_COLOR_TOGGLE 0
#define DEFAULT_ANIMATE_TOGGLE 1
#define fatal(msg)\
do {\
fprintf(stderr, "line %d: %s\n", __LINE__, msg);\
abort();\
} while (0)
typedef enum { typedef enum {
dir_left = 'h', dir_left = 'h',

View File

@ -8,7 +8,7 @@
#define iterate(x, expr)\ #define iterate(x, expr)\
do {\ do {\
int i;\ size_t i;\
for (i = 0; i < x; ++i) { expr; }\ for (i = 0; i < x; ++i) { expr; }\
} while (0) } while (0)
@ -26,13 +26,15 @@ void draw_screen(struct gamestate *g)
keypad(gamewin, TRUE); keypad(gamewin, TRUE);
} }
// mvwprintw will sometimes have a useless arg, this is warned, but doesn't affect the program if (g->score_last)
char *scr = g->score_last ? "SCORE: %d (+%d)\n" : "SCORE: %d\n"; mvwprintw(gamewin, 0, 0, "SCORE: %d (+%d)\n", g->score, g->score_last);
mvwprintw(gamewin, 0, 0, scr, g->score, g->score_last); else
mvwprintw(gamewin, 0, 0, "SCORE: %d\n", g->score);
mvwprintw(gamewin, 1, 0, "HISCR: %d\n", g->score_high); mvwprintw(gamewin, 1, 0, "HISCR: %d\n", g->score_high);
iterate(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; size_t x, y, xps = 0, yps = 3;
for (y = 0; y < g->opts->grid_height; y++, xps = 0, yps++) { for (y = 0; y < g->opts->grid_height; y++, xps = 0, yps++) {
mvwprintw(gamewin, yps, xps++, "|"); mvwprintw(gamewin, yps, xps++, "|");
for (x = 0; x < g->opts->grid_width; x++) { for (x = 0; x < g->opts->grid_width; x++) {
@ -106,7 +108,7 @@ void draw_screen(struct gamestate *g)
// alter this grid_size + 1 to match abitrary grid size // alter this grid_size + 1 to match abitrary grid size
iterate((g->print_width + 2) * g->opts->grid_width + 1, printf("-")); iterate((g->print_width + 2) * g->opts->grid_width + 1, printf("-"));
printf("\n"); printf("\n");
int x, y; size_t x, y;
for (y = 0; y < g->opts->grid_height; y++) { for (y = 0; y < g->opts->grid_height; y++) {
printf("|"); printf("|");
for (x = 0; x < g->opts->grid_width; x++) { for (x = 0; x < g->opts->grid_width; x++) {
@ -131,22 +133,14 @@ void ddraw(struct gamestate *g)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
struct gamestate *g = gamestate_init( struct gameoptions *o = gameoptions_default();
parse_options( struct gamestate *g = gamestate_init(parse_options(o, argc, argv));
gameoptions_default(), argc, argv));
drawstate_init(); drawstate_init();
while (1) { while (1) {
draw_screen(g); draw_screen(g);
int e;
if ((e = end_condition(g))) {
drawstate_clear();
printf(e > 0 ? "You win\n" : "You lose\n");
goto endloop;
}
/* abstract getting keypress */ /* abstract getting keypress */
int ch; int ch;
do { do {
@ -155,9 +149,19 @@ int main(int argc, char **argv)
} while (strchr("hjkl", ch) == NULL); } while (strchr("hjkl", ch) == NULL);
gamestate_tick(g, ch, g->opts->animate ? ddraw : NULL); gamestate_tick(g, ch, g->opts->animate ? ddraw : NULL);
}
endloop:
int e;
if ((e = end_condition(g))) {
drawstate_clear();
gamestate_clear(g);
printf(e > 0 ? "You win\n" : "You lose\n");
goto endloop;
}
random_block(g);
}
endloop:
drawstate_clear(); drawstate_clear();
gamestate_clear(g); gamestate_clear(g);
return 0; return 0;