2015-02-21 02:36:15 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2015-02-25 01:51:48 +00:00
|
|
|
#include <stdbool.h>
|
2015-02-21 22:29:15 +00:00
|
|
|
#include "highscore.h"
|
2015-02-21 02:36:15 +00:00
|
|
|
#include "options.h"
|
|
|
|
|
|
|
|
void print_usage(void)
|
|
|
|
{
|
2015-02-25 01:51:48 +00:00
|
|
|
printf("usage: 2048 [-cCaAiIrh] [-s SIZE] [-b RATE]\n");
|
2015-02-21 02:36:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Initial game options */
|
|
|
|
struct gameoptions* gameoptions_default(void)
|
|
|
|
{
|
|
|
|
struct gameoptions *opt = malloc(sizeof(struct gameoptions));
|
|
|
|
if (!opt) return NULL;
|
|
|
|
|
|
|
|
opt->grid_height = DEFAULT_GRID_HEIGHT;
|
|
|
|
opt->grid_width = DEFAULT_GRID_WIDTH;
|
|
|
|
opt->spawn_value = DEFAULT_SPAWN_VALUE;
|
|
|
|
opt->spawn_rate = DEFAULT_SPAWN_RATE;
|
2015-02-25 01:51:48 +00:00
|
|
|
opt->enable_color = DEFAULT_COLOR_FLAG;
|
|
|
|
opt->animate = DEFAULT_ANIMATE_FLAG;
|
|
|
|
opt->ai = DEFAULT_AI_FLAG;
|
|
|
|
opt->interactive = DEFAULT_INTERACTIVE_FLAG;
|
2015-02-21 02:36:15 +00:00
|
|
|
|
|
|
|
return opt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gameoptions_destroy(struct gameoptions *opt)
|
|
|
|
{
|
|
|
|
free(opt);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct gameoptions* parse_options(struct gameoptions *opt, int argc, char **argv)
|
|
|
|
{
|
|
|
|
int c;
|
2015-02-25 01:51:48 +00:00
|
|
|
while ((c = getopt(argc, argv, "aArcCiIhHs:b:")) != -1) {
|
2015-02-21 02:36:15 +00:00
|
|
|
switch (c) {
|
|
|
|
case 'a':
|
2015-02-25 01:51:48 +00:00
|
|
|
opt->animate = true;
|
2015-02-21 02:36:15 +00:00
|
|
|
break;
|
|
|
|
case 'A':
|
2015-02-25 01:51:48 +00:00
|
|
|
opt->animate = false;
|
2015-02-21 02:36:15 +00:00
|
|
|
break;
|
|
|
|
case 'c':
|
2015-02-25 01:51:48 +00:00
|
|
|
opt->enable_color = true;
|
2015-02-21 02:36:15 +00:00
|
|
|
break;
|
|
|
|
case 'C':
|
2015-02-25 01:51:48 +00:00
|
|
|
opt->enable_color = false;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
opt->ai = true;
|
|
|
|
opt->interactive = false;
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
opt->ai = true;
|
|
|
|
opt->interactive = true;
|
2015-02-21 02:36:15 +00:00
|
|
|
break;
|
|
|
|
case 's':;
|
|
|
|
/* Stick with square for now */
|
|
|
|
int optint = strtol(optarg, NULL, 10);
|
|
|
|
if (optint < CONSTRAINT_GRID_MAX && optint > CONSTRAINT_GRID_MIN) {
|
|
|
|
opt->grid_height = optint;
|
|
|
|
opt->grid_width = optint;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
opt->spawn_rate = strtol(optarg, NULL, 10);
|
|
|
|
break;
|
|
|
|
case 'r':
|
2015-02-21 22:29:15 +00:00
|
|
|
highscore_reset();
|
2015-02-21 02:36:15 +00:00
|
|
|
exit(0);
|
|
|
|
case 'h':
|
|
|
|
print_usage();
|
|
|
|
exit(0);
|
2015-02-25 01:51:48 +00:00
|
|
|
case 'H':
|
|
|
|
printf("%ld\n", highscore_load(NULL));
|
|
|
|
exit(0);
|
2015-02-21 02:36:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return opt;
|
|
|
|
}
|