u_ttt/src/utictactoe.c
2021-09-15 23:55:50 +02:00

135 lines
4.5 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include "utictactoe.h"
#define OPTIONAL_ARGUMENT_IS_PRESENT \
((optarg == NULL && optind < argc && argv[optind][0] != '-') \
? (bool) (optarg = argv[optind++]) \
: (optarg != NULL))
void help(){
printf("usage: utictactoe ([-i LEVEL|-x N|-o N|-v|-V|-h] | -c FILE)\n");
printf("Play a utictactoe game with human or program players.\n");
printf("-i, --inception-level LEVEL deepness of the game (min=1, max=3 (default=2))\n");
printf("-x, --cross-ai N set tactic of cross player 'X'\n");
printf("-o, --round-ai N set tactic of round player 'O'\n");
printf("-c, --contest enable 'contest' mode\n");
printf("-v, --verbose verbose output\n");
printf("-V, --version display version and exit\n");
printf("-h, --help display this help and exit\n");
printf("Tactics list: random (0), clever (1)\n");
}
int check_int_value ( char *value, int min, int max){
char * end_ptr;
int result;
result = strtol(optarg, &end_ptr, 10);
if ( strcmp(end_ptr, "\0") != 0 ) {
return -1;
}
if ( result < min || result > max) {
return -1;
}
return result;
}
int main(int argc, char* argv[]) {
int optc;
int cross_ai, round_ai = 0;
int inception_level = 0;
bool contest = false;
char * contest_file;
static struct option long_opts[] =
{
{"inception-level", optional_argument, 0, 'i'},
{"cross-ai", required_argument, 0, 'x'},
{"round-ai", required_argument, 0, 'o'},
{"contest", no_argument, 0, 'c'},
{"verbose", no_argument, 0, 'v'},
{"version", no_argument, 0, 'V'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
while ((optc = getopt_long (argc, argv, "i::x:o:cvVh", long_opts, NULL)) != -1){
switch (optc) {
case 'i': /* 'a' option */
if ( OPTIONAL_ARGUMENT_IS_PRESENT ) {
inception_level = check_int_value( optarg, 1, 3);
if ( inception_level == -1 ) {
fprintf(stderr, "Error: incorrect inception level\n");
exit (EXIT_FAILURE);
}
}
else {
inception_level = 2;
}
printf ("Inception activated, level: %d\n", inception_level);
break;
case 'x':
printf ("cross ai\n");
if ( optarg ) {
cross_ai = check_int_value(optarg, 0, 1);
if ( cross_ai == -1 ) {
fprintf(stderr, "Error: incorrect cross ai value\n");
exit (EXIT_FAILURE);
}
}
else {
fprintf(stderr, "Error: you must specify a value for cross ai\n");
exit (EXIT_FAILURE);
}
break;
case 'o':
printf ("round ai\n");
if ( optarg ) {
round_ai = check_int_value(optarg, 0, 1);
if ( round_ai == -1 ) {
fprintf(stderr, "Error: incorrect round ai value\n");
exit (EXIT_FAILURE);
}
}
else {
fprintf(stderr, "Error: you must specify a value for round ai\n");
exit (EXIT_FAILURE);
}
break;
case 'c':
if ( OPTIONAL_ARGUMENT_IS_PRESENT ) {
if ( access(optarg, R_OK | W_OK) != -1) {
printf("Contest file valid\n");
}
else {
fprintf(stderr, "Can't access to file %s\n", optarg);
} exit (EXIT_FAILURE);
}
else {
fprintf(stderr, "Error: contest need a file\n");
exit (EXIT_FAILURE);
}
contest = true;
printf ("contest activated\n");
break;
case 'V':
printf("utictactoe %d.%d.%d\n", VERSION, SUBVERSION, REVISION);
printf("This software implements a full ultimate tictactoe game.\n");
exit(EXIT_SUCCESS);
case 'h':
help();
exit(EXIT_SUCCESS);
default:
fprintf(stderr, "Error: unknown option");
exit (EXIT_FAILURE);
}
}
printf("Hello World!\n");
}