u_ttt/src/utictactoe.c
2021-10-01 17:16:30 +02:00

155 lines
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"
#include "common.h"
#include "model.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){
struct a {
int b;
};
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 verbose, contest_mode = 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", required_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:c:vVh", 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, F_OK | R_OK) == -1) {
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_mode = true;
printf ("contest activated\n");
break;
case 'v':
verbose = true;
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, "Try `utictactie --help' for more information.\n");
exit (EXIT_FAILURE);
}
}
s_move *test_move = create_empty_move();
free_move(test_move);
s_tictactoe *test_ttt = create_empty_tictactoe();
free_tictactoe(test_ttt);
s_utictactoe *test_uttt = create_empty_utictactoe(1);
free_utictactoe(test_uttt);
test_uttt = create_empty_utictactoe(2);
get_next_player_to_play(test_uttt);
free_utictactoe(test_uttt);
test_uttt = create_empty_utictactoe(3);
free_utictactoe(test_uttt);
return EXIT_SUCCESS;
}