Skip to content

Commit

Permalink
Adds -d and -t options to example program.
Browse files Browse the repository at this point in the history
Closes #13.
  • Loading branch information
jazzyb committed Dec 8, 2011
1 parent 5d8c56f commit d746891
Showing 1 changed file with 64 additions and 6 deletions.
70 changes: 64 additions & 6 deletions examples/cli/simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include "forchess/ai.h"
#include "forchess/board.h"
Expand All @@ -12,12 +13,63 @@ void query_human_for_move (fc_game_t *game, fc_player_t player);
void get_move (fc_game_t *game, fc_move_t *move, fc_player_t player);
void str2move (fc_game_t *game, fc_move_t *move, char *str);
fc_piece_t get_pawn_promotion (void);
void make_computer_move (fc_game_t *game, fc_player_t player);
void make_computer_move (fc_game_t *game, fc_player_t player, int depth,
int timeout);
void get_time (char *str, time_t t);
void move2str (fc_game_t *game, char *str, fc_move_t *move);

int get_arguments (int argc, char **argv, int *depth, int *timeout)
{
int c;
*depth = *timeout = 0;
while ((c = getopt(argc, argv, "d:ht:")) != -1) {
switch (c) {
case 'd':
*depth = strtol(optarg, NULL, 10);
break;
case 'h':
fprintf(stderr,
"%s {[-h] | [-d <plycount>] [-t <seconds>] [1] [2] [3] [4]}\n"
" Play forchess!\n"
"\n"
" -h: show this help message\n"
"\n"
" usage: list the players on the command line who\n"
" will be played by humans; the remainder\n"
" will be played by the AI\n"
" options:\n"
" -d: the number of moves that you wish the\n"
" program to search; by default the program\n"
" will search depth = num_players * 2\n"
" -t: the maximum number of seconds the program\n"
" should spend searching for a move\n"
" for example: '%s 1 3' will start a game in\n"
" which players 2 and 4 will be played by\n"
" the computer\n\n", argv[0], argv[0]);
exit(0);
case 't':
*timeout = strtol(optarg, NULL, 10);
break;
case '?':
if (optopt == 'd' || optopt == 't') {
fprintf(stderr, "Option -%c requires an argument.\n", optopt);
} else {
fprintf(stderr, "Unknown option: -%c\n", optopt);
}
default:
abort();
}
}
if (argc - optind > 5) {
fprintf(stderr, "error: too many arguments; see '%s -h'\n", argv[0]);
exit(1);
}
return optind;
}

int main (int argc, char **argv)
{
#if 0
if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
fprintf(stderr,
"simple {[-h] | [1] [2] [3] [4]}\n"
Expand All @@ -36,9 +88,12 @@ int main (int argc, char **argv)
fprintf(stderr, "error: too many arguments; see 'simple -h'\n");
exit(1);
}
#endif
int depth, timeout;
int optind = get_arguments(argc, argv, &depth, &timeout);

int player_is_human[] = {0, 0, 0, 0};
for (int i = 1; i < argc; i++) {
for (int i = optind; i < argc; i++) {
int n = strtol(argv[i], NULL, 10);
if (n > 0 && n <= 4) {
player_is_human[n-1] = 1;
Expand All @@ -62,7 +117,7 @@ int main (int argc, char **argv)
if (player_is_human[player]) {
query_human_for_move(&game, player);
} else {
make_computer_move(&game, player);
make_computer_move(&game, player, depth, timeout);
}
}

Expand Down Expand Up @@ -136,15 +191,18 @@ fc_piece_t get_pawn_promotion (void)
} while (0);
}

void make_computer_move (fc_game_t *game, fc_player_t player)
void make_computer_move (fc_game_t *game, fc_player_t player, int depth,
int timeout)
{
fc_move_t move;
int depth = fc_game_number_of_players(game) * 2;
if (!depth) {
depth = fc_game_number_of_players(game) * 2;
}
fc_ai_t ai;
fc_ai_init(&ai, fc_game_get_board(game));

time_t start = time(NULL);
if (!fc_ai_next_move(&ai, &move, player, depth, 0)) {
if (!fc_ai_next_move(&ai, &move, player, depth, timeout)) {
assert(0);
}
char time_str[100];
Expand Down

0 comments on commit d746891

Please sign in to comment.