u_ttt/src/utictactoe.c
Yorick Barbanneau 81c34439a3 First commit
2021-09-13 22:49:50 +02:00

75 lines
2.5 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>
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 main(int argc, char* argv[]) {
int optc;
int inception_level = 0;
bool contest, round_ai, cross_ai = false;
char * end_ptr;
static struct option long_opts[] =
{
{"inception-level", optional_argument, 0, 'i'},
{"cross-ai", no_argument, 0, 'x'},
{"round-ai", no_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:xocvVh", long_opts, NULL)) != -1){
switch (optc) {
case 'i': /* 'a' option */
printf ("Inception activated\n");
if ( optarg ) {
inception_level = strtol(optarg, &end_ptr, 10);
assert ( strcmp(end_ptr,"\0") == 0);
assert ( inception_level >= 1 );
assert ( inception_level <= 3 );
}
else {
inception_level = 2;
}
break;
case 'x': /* 'a' option */
printf ("contest activated\n");
cross_ai = true;
break;
case 'o': /* 'a' option */
printf ("contest activated\n");
round_ai = true;
break;
case 'c': /* 'a' option */
printf ("contest activated\n");
contest = true;
break;
case 'h':
help();
exit(EXIT_SUCCESS);
default:
exit (EXIT_FAILURE);
}
}
printf("Hello World!\n");
}