From e7208c72db7877ccfb01fdf7edfe28580caae3b4 Mon Sep 17 00:00:00 2001 From: John Zaitseff Date: Tue, 19 Jul 2011 22:29:36 +1000 Subject: [PATCH] Implement the --max-turn command line option --- src/game.c | 2 +- src/globals.c | 5 +++++ src/globals.h | 2 ++ src/trader.c | 25 ++++++++++++++++++++----- 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/game.c b/src/game.c index bb9e24b..48c9f3b 100644 --- a/src/game.c +++ b/src/game.c @@ -381,7 +381,7 @@ void init_game (void) // Miscellaneous initialisation interest_rate = INITIAL_INTEREST_RATE; - max_turn = DEFAULT_MAX_TURN; + max_turn = option_max_turn ? option_max_turn : DEFAULT_MAX_TURN; turn_number = 1; // Select who is to go first diff --git a/src/globals.c b/src/globals.c index c7d1f9b..9a3c6b6 100644 --- a/src/globals.c +++ b/src/globals.c @@ -86,3 +86,8 @@ bool quit_selected = false; // Is a player trying to quit the game? bool abort_game = false; // Abort game without declaring winner? bool option_no_color = false; // True if --no-color was specified +int option_max_turn = 0; // Max. turns if --max-turn was specified + + +/***********************************************************************/ +// End of file diff --git a/src/globals.h b/src/globals.h index 9f5f00f..3b08b4f 100644 --- a/src/globals.h +++ b/src/globals.h @@ -45,6 +45,7 @@ #define NUMBER_MOVES 20 // Number of choices on map per turn #define DEFAULT_MAX_TURN 50 // Default number of turns per game +#define MIN_MAX_TURN 10 // Minimum that can be specified for max_turn #define MAX_PLAYERS 8 // Maximum number of players #define INITIAL_CASH 6000.00 // Initial cash per player @@ -197,6 +198,7 @@ extern bool quit_selected; // Is a player trying to quit the game? extern bool abort_game; // Abort game without declaring winner? extern bool option_no_color; // True if --no-color was specified +extern int option_max_turn; // Max. turns if --max-turn was specified #endif /* included_GLOBALS_H */ diff --git a/src/trader.c b/src/trader.c index 9cc4842..cece88b 100644 --- a/src/trader.c +++ b/src/trader.c @@ -43,6 +43,7 @@ enum options_char { OPTION_NO_COLOR = 1, + OPTION_MAX_TURN }; static const char options_short[] = "hV"; @@ -50,11 +51,12 @@ static const char options_short[] = "hV"; // -V, --version static struct option const options_long[] = { - { "help", no_argument, NULL, 'h' }, - { "version", no_argument, NULL, 'V' }, - { "no-color", no_argument, NULL, OPTION_NO_COLOR }, - { "no-colour", no_argument, NULL, OPTION_NO_COLOR }, - { NULL, 0, NULL, 0 } + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'V' }, + { "no-color", no_argument, NULL, OPTION_NO_COLOR }, + { "no-colour", no_argument, NULL, OPTION_NO_COLOR }, + { "max-turn", required_argument, NULL, OPTION_MAX_TURN }, + { NULL, 0, NULL, 0 } }; @@ -187,6 +189,8 @@ int main (int argc, char *argv[]) void process_cmdline (int argc, char *argv[]) { int c; + char *p; + // Process arguments starting with "-" or "--" opterr = true; @@ -211,6 +215,17 @@ void process_cmdline (int argc, char *argv[]) option_no_color = true; break; + case OPTION_MAX_TURN: + // --max-turn: specify the maximum turn number + option_max_turn = strtol(optarg, &p, 10); + + if (option_max_turn < MIN_MAX_TURN || p == NULL || *p != '\0') { + fprintf(stderr, "%s: invalid value for --max-turn: `%s'\n", + program_name(), optarg); + show_usage(EXIT_FAILURE); + } + break; + default: show_usage(EXIT_FAILURE); }