Example #1
0
int main(int argc, char * argv[]){
    Board * board;
    int x, y;
    Pos pos;
    UCTNode * uct_tree = 0;
    UCTNode * ptr;

    board_initialize();
    uct_initialize();

    board = board_new();

    while(1) {
        board_print(board, stdout);
        printf("\nBlack: ");
        if(scanf("%d %d", &x, &y) == -1)
            break;
        board_play(board, to_pos(x, y), BLACK);

        board_print(board, stdout);
        pos = uct_search(board, WHITE);
        board_play(board, pos, WHITE);
    }

    //for(ptr = uct_tree->child; ptr; ptr = ptr->next)
    //    printf("%d ", ptr->move);

    puts("");

    board_destroy();
    uct_destroy();
    return 0;
}
Example #2
0
int
board_load (window_board_t *win, char *fname)
{
	FILE *f;
	char buf[1024];
	if (! (f = fopen (fname, "r")))
		return 0;
	if (fgets (buf, 1023, f) == NULL)
		return 0;

	int ret;
	if (!strncmp(buf, "pn|", 3) || !strncmp(buf, "vg|", 3) ||
	    !strncmp(buf, "st|", 3) || !strncmp(buf, "qx|", 3)) {
		ret = board_parse_lin (win, buf, f);
	} else {
		board *b = board_new (win->n_boards + 1);
		ret = board_parse_line(buf, b, ' ', '.');
		if (ret)
			board_window_append_board (win, b);
		else {
			errno = EMEDIUMTYPE;
			board_free (b);
		}
	}
	int e = errno;
	fclose(f);
	if (ret) {
		if (win->filename)
			free (win->filename);
		win->filename = strdup (fname);
	}
	errno = e;
	return ret;
}
Example #3
0
// Read from the replay file to set the board parameters
Board *board_load (FILE *input_file) {
    
    char buffer[LINE1_LENGTH];   // for reading the first line
    
    // Check if the first 7 characters form the word REPLAY
    fgets (buffer, 7, input_file);
    if ( strcmp(buffer, "REPLAY") != 0 ) {
        fprintf(stderr, "connectnk: The file provided is not a replay file.\n");
        exit(1);
    }
    
    // Set the board parameters
    int replay_win_length, replay_width, replay_height;
    replay_height = 0;
    
    fgets (buffer, LINE1_LENGTH, input_file);
    int chars_read = sscanf(buffer, "%d %d %d", &replay_win_length, &replay_width, &replay_height);
    if (chars_read < 2) {
        fprintf(stderr, "connectnk: Invalid replay file. Board parameters are not specified correctly\n");
        exit(1);
    }

    // In case of only two parameters, set height the same as width.
    if((replay_height) == 0)
        replay_height = replay_width;

    Board *b = board_new(replay_width, replay_height, replay_win_length);
    
    return b;
}
Example #4
0
int
board_window_append_board (window_board_t *win, board *b)
{
	if (!b) {
		b = board_new (win->n_boards + 1);
		g_string_printf (b->name, _("Board %d"), win->n_boards + 1);
	}

	if (! win->n_boards_alloc) {
		win->n_boards_alloc = 4; /* start with 4 entries */
		win->boards = malloc (win->n_boards_alloc * sizeof (board *));
		assert(win->boards);
		win->board_numbers = malloc (win->n_boards_alloc * sizeof (int));
		assert(win->board_numbers);
	} else if (win->n_boards >= win->n_boards_alloc) {
		win->n_boards_alloc <<= 2;
		win->boards = realloc(win->boards, win->n_boards_alloc * sizeof (board*));
		assert(win->boards);
		win->board_numbers = realloc(win->board_numbers, win->n_boards_alloc * sizeof (int));
		assert(win->board_numbers);
	}
	win->boards[win->n_boards] = b;
	win->board_numbers[win->n_boards] = win->n_boards; /* store array index */
	return win->n_boards++;
}
Example #5
0
int main(int argc, char **argv)
{
    board *board = board_new();
    FILE *f;
    if (argc < 2 || strcmp(argv[argc-1], "--") == 0)
    {
        f = stdin;
    }
    else
    {
        f = fopen(argv[argc-1], "r");
        if (f == NULL)
        {
            perror("open");
            return 1;
        }
    }
    if (strcmp(argv[1], "-d") == 0 || strcmp(argv[1], "--debug") == 0)
        debugflag = 1;
    char c;
    int x = 0;
    int y = 0;
    while ((c = fgetc(f)) != EOF)
    {
        if (c == '\n')
        {
            y++;
            x = 0;
            continue;
        }
        else
        {
            board_put(board, x, y, c);
        }
        if (x > BOARD_WIDTH || y > BOARD_HEIGHT)
        {
            fprintf(stderr, "Error: File too large (%i, %i)", x, y);
            return 1;
        }
        x++;
    }
    if (f != stdin)
    {
        fclose(f);
    }
    if (debugflag)
    {
        debugstream = tmpfile();
    }
    process(board);
    if (debugflag)
    {
        fclose(debugstream);
    }
    return 0;
}
Example #6
0
void test_board_get_square(void)
{
  Board *b;

  b = board_new(1LLU, 2LLU);

  CU_ASSERT_EQUAL(board_get_square(b, A1), BLACK_SQUARE);
  CU_ASSERT_EQUAL(board_get_square(b, B1), WHITE_SQUARE);
  CU_ASSERT_EQUAL(board_get_square(b, C1), EMPTY_SQUARE);
  b = board_delete(b);
}
Example #7
0
void test_board_count_pieces(void)
{
  Board *b;

  b = board_new(1LLU, 2LLU);

  CU_ASSERT_EQUAL(board_count_pieces(b, BLACK_SQUARE),  1);
  CU_ASSERT_EQUAL(board_count_pieces(b, WHITE_SQUARE),  1);
  CU_ASSERT_EQUAL(board_count_pieces(b, EMPTY_SQUARE), 62);
  b = board_delete(b);
}
Example #8
0
void test_board_count_difference(void)
{
  Board *b;
  SquareSet blacks, whites;

  blacks = 0xFFFFFFFFFFFFFFFFULL;
  whites = 0x0000000000000000ULL;
  b = board_new(blacks, whites);

  CU_ASSERT_EQUAL(board_count_difference(b, BLACK_PLAYER),  64);
  CU_ASSERT_EQUAL(board_count_difference(b, WHITE_PLAYER), -64);
  b = board_delete(b);
}
Example #9
0
void test_board_new(void)
{
  SquareSet b, w;
  Board *empty_board;

  b = 0LLU;
  w = 0LLU;
  empty_board = board_new(b, w);
  CU_ASSERT_PTR_NOT_NULL(empty_board);
  empty_board = board_delete(empty_board);

  CU_ASSERT_PTR_NULL(empty_board)
}
AIMoves *ai_threats(const Board *original)
{
        AIMoves *moves;
        AIWEIGHT u_sum = 0;
        int i;

        b = board_new();
        board_copy(original, b);

        /* Clear threat tallys */
        for (i = 0; i < connect_k; i++) {
                threat_counts[i][0] = 0;
                threat_counts[i][1] = 0;
        }

        /* Horizontal lines */
        for (i = 0; i < board_size; i++)
                u_sum += threat_line(0, i, 1, 0);

        /* Vertical lines */
        for (i = 0; i < board_size; i++)
                u_sum += threat_line(i, 0, 0, 1);

        /* SE diagonals */
        for (i = 0; i < board_size - connect_k + 1; i++)
                u_sum += threat_line(i, 0, 1, 1);
        for (i = 1; i < board_size - connect_k + 1; i++)
                u_sum += threat_line(0, i, 1, 1);

        /* SW diagonals */
        for (i = connect_k - 1; i < board_size; i++)
                u_sum += threat_line(i, 0, -1, 1);
        for (i = 1; i < board_size - connect_k + 1; i++)
                u_sum += threat_line(board_size - 1, i, -1, 1);

        moves = ai_marks(b, PIECE_THREAT(1));
        moves->utility = u_sum;
        board_free(b);
        return moves;
}
Example #11
0
void test_board_compare(void)
{
  Board *a, *b;

  a = board_new(0xFFFFFFFFFFFFFFFFULL, 0x0000000000000000ULL);
  b = board_new(0xFFFFFFFFFFFFFFFFULL, 0x0000000000000000ULL);
  CU_ASSERT_EQUAL(board_compare(a, b), 0);
  a = board_delete(a);
  b = board_delete(b);

  a = board_new(0xFFFFFFFFFFFFFFFFULL, 0x0000000000000000ULL);
  b = board_new(0x0000000000000000ULL, 0x0000000000000000ULL);
  CU_ASSERT_EQUAL(board_compare(a, b), +1);
  a = board_delete(a);
  b = board_delete(b);

  a = board_new(0x0000000000000000ULL, 0x0000000000000000ULL);
  b = board_new(0x0000000000000001ULL, 0x0000000000000000ULL);
  CU_ASSERT_EQUAL(board_compare(a, b), -1);
  a = board_delete(a);
  b = board_delete(b);

  a = board_new(0x0000000000000007ULL, 0x0000000000000000ULL);
  b = board_new(0x0000000000000007ULL, 0x0000000000000000ULL);
  CU_ASSERT_EQUAL(board_compare(a, b), 0);
  a = board_delete(a);
  b = board_delete(b);

  a = board_new(0x0000000000000007ULL, 0x0100000000000000ULL);
  b = board_new(0x0000000000000007ULL, 0x0000000000000000ULL);
  CU_ASSERT_EQUAL(board_compare(a, b), +1);
  a = board_delete(a);
  b = board_delete(b);

  a = board_new(0x0000000000000007ULL, 0x0000000000000000ULL);
  b = board_new(0x0000000000000007ULL, 0x0100000000000000ULL);
  CU_ASSERT_EQUAL(board_compare(a, b), -1);
  a = board_delete(a);
  b = board_delete(b);
}
Example #12
0
static int
board_parse_lin (window_board_t *win, char *line, FILE *f)
{
	char *saveptr;
	char *tok;
	int card_nr = 0;

	setlocale (LC_NUMERIC, "C");

	board *b = board_new (win->n_boards + 1);
	int board_filled = 0;
	board_window_append_board (win, b);

	/* global list of names for vugraph files */
	char *name_arr[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
	int name_n = 0;
	/* IMP/MP list */
	char *mp_str = NULL;
	char *mp_ptr = NULL;

	do {
	for (tok = sane_strtok_r(line, "|", &saveptr); tok; tok = STRTOK) {
		/* single hand */
		if (!strcmp(tok, "pn")) { /* SWNE */
			tok = STRTOK;
			char *nameptr;
			char *name = sane_strtok_r(tok, ",", &nameptr);
			int i = 0;
			do {
				if (i < 4)
					g_string_printf(b->hand_name[seat_mod (i++) - 1], "%s", name);
				if (name_n < 8)
					name_arr[name_n++] = strdup(name);
			} while ((name = sane_strtok_r(NULL, ",", &nameptr)));

		} else if (!strcmp(tok, "md")) { /* make deal */
			tok = STRTOK;
			if (*tok == '0') {
				printf ("md|0| (keep deal) mode not supported\n");
				continue;
			}
			if (board_filled) {
				/* start new board */
				card_nr = 0;
				board_filled = 0;

				b = board_new (win->n_boards + 1);
				board_window_append_board (win, b);
				/* initialize player names, required for vugraph files */
				int i;
				for (i = 0; i < 4; i++) {
					g_string_printf(b->hand_name[seat_mod (i) - 1],
						"%s", name_arr[i + (tok[0] == 'c' ? 4 : 0)]);
				}
			}
			char *c;
			seat se = south;
			suit su = spade;
			b->dealer = seat_mod(*tok - '0' - 1);
			for (c = tok + 1; *c; c++) {
				int i;
				if ((i = parse_suit(*c)) != -1) {
					su = i;
				} else if ((i = parse_rank_char(*c)) != -1) {
					if (add_card(b, se, (su * 13) + i) != 1)
						goto error;
				} else if (*c == ',') {
					se = seat_mod(se + 1);
				} else {
					printf("Parse error: %s", tok);
					goto error;
				}
			}
			// TODO: end positions
			deal_random(b); /* compute east hand */
			board_filled = 1; /* consider this board finished on next qx token */

		} else if (!strcmp(tok, "ah")) { /* board name */
			g_string_printf(b->name, "%s", STRTOK);

		} else if (!strcmp(tok, "qx")) { /* board number, o1, c1, o2, ... */
			tok = STRTOK;
			if (board_filled) { /* first token in new vugraph board */
				card_nr = 0;
				board_filled = 0;

				b = board_new (win->n_boards + 1);
				board_window_append_board (win, b);
				/* initialize player names, required for vugraph files */
				int i;
				for (i = 0; i < 4; i++) {
					g_string_printf(b->hand_name[seat_mod (i) - 1],
						"%s", name_arr[i + (tok[0] == 'c' ? 4 : 0)]);
				}
			}
			if (strlen (tok) >= 1)
				g_string_printf(b->name, "%s %s",
					tok[0] == 'c' ? _("Closed") :
						(tok[0] == 'o' ? _("Open") : _("Board")),
					tok + 1);

			/* for now assume qx|| is present in all lin files with mp|| */
			if (mp_str) {
				// FIXME: skip leading boards that were kibitzed but not played
				// (01-26-08-3.lin)
				char *score = mp_ptr ? sane_strtok_r (NULL, ",", &mp_ptr) :
						sane_strtok_r (mp_str, ",", &mp_ptr);
				b->mp[0] = score ? round (strtod (score, NULL) * 100.0) : 0;
				score = sane_strtok_r (NULL, ",", &mp_ptr);
				b->mp[1] = score ? round (strtod (score, NULL) * 100.0) : 0;
			}

		} else if (!strcmp(tok, "sv")) {
			tok = STRTOK;
			switch (*tok) {
				case '0':
				case 'o': b->vuln[0] = 0; b->vuln[1] = 0; break;
				case 'n': b->vuln[0] = 1; b->vuln[1] = 0; break;
				case 'e': b->vuln[0] = 0; b->vuln[1] = 1; break;
				case 'b': b->vuln[0] = 1; b->vuln[1] = 1; break;
				default: printf("Unknown vulnerability: sv|%s|\n", tok);
			}

		} else if (!strcmp(tok, "mb")) {
			/* mb|-ppp1Cp1Hp3Np4Dp4Hppp| */
			tok = STRTOK;
			char *bidp = tok;
			char *al = strchr (bidp, '!');
			if (al) {
				*al++ = '\0';
			}
			do {
				int bid = parse_bid(&bidp);
				if (bid == -1) {
					printf("Invalid bid %s/%s\n", tok, bidp);
					break;
				}
				board_append_bid(b, bid, 1);
				if (al) {
					board_set_alert (b, al);
					al = NULL;
				}
			} while (*bidp);

		} else if (!strcmp(tok, "an")) {
			tok = STRTOK;
			board_set_alert (b, !strcmp (tok, "!") ? "" : tok); /* filter uninteresting ! */

		} else if (!strcmp(tok, "pc")) {
			int c = parse_card(tok = STRTOK);
			if (c == -1) {
				printf("Invalid card %s\n", tok);
				continue;
			}
			if (card_nr < 52)
				b->played_cards[card_nr++] = c;

		} else if (!strcmp(tok, "mc")) {
			tok = STRTOK; // TODO: store number of (total) claimed tricks
			b->played_cards[card_nr] = claim_rest; // no card_nr increment here
			b->declarer_tricks = atoi (tok);

		/* vugraph file */
		} else if (!strcmp(tok, "vg")) { /* match title */
			tok = STRTOK;
			//printf ("Match title: %s\n", tok);
			if (win->title)
				free (win->title);
			if (win->subtitle)
				free (win->subtitle);
			if (win->team1)
				free (win->team1);
			if (win->team2)
				free (win->team2);

			char *t_ptr;
			char *title = sane_strtok_r (tok, ",", &t_ptr);
			if (title)
				win->title = strdup (title);
			char *subtitle = sane_strtok_r (NULL, ",", &t_ptr);
			if (subtitle)
				win->subtitle = strdup (subtitle);
			sane_strtok_r (NULL, ",", &t_ptr); /* scoring I IMPs P MPs B board-a-match */
			sane_strtok_r (NULL, ",", &t_ptr); /* first board nr */
			sane_strtok_r (NULL, ",", &t_ptr); /* last board nr */
			char *team1 = sane_strtok_r (NULL, ",", &t_ptr);
			if (team1)
				win->team1 = strdup (team1);
			sane_strtok_r (NULL, ",", &t_ptr); /* carry-over score team 1 */
			char *team2 = sane_strtok_r (NULL, ",", &t_ptr);
			if (team2)
				win->team2 = strdup (team2);
			/* carry-over score team 2 */

		} else if (!strcmp(tok, "pw")) { /* more player names */
			tok = STRTOK;
			//printf ("Players: %s\n", tok);
		} else if (!strcmp(tok, "bn")) { /* board numbers */
			tok = STRTOK;
			//printf ("Board numbers: %s\n", tok);
		} else if (!strcmp(tok, "rs")) { /* results */
			tok = STRTOK;
			//printf ("Results: %s\n", tok);
		} else if (!strcmp(tok, "mp")) { /* MP result */
			tok = STRTOK;
			//printf ("Scores: %s\n", tok);
			mp_str = strdup (tok);
		} else if (!strcmp(tok, "nt")) { /* comment (new text) */
			tok = STRTOK;
			//printf ("Comment: %s\n", tok);

		} else if (!strcmp(tok, "at")) { /* add text */
			STRTOK;
		} else if (!strcmp(tok, "cr") || !strcmp(tok, "cg") ||
			   !strcmp(tok, "cb")) { /* color */
			STRTOK;
		} else if (!strcmp(tok, "hc") || !strcmp(tok, "lc") ||
			   !strcmp(tok, "hs") || !strcmp(tok, "ls")) {
			STRTOK; /* hilight card, suit */
		} else if (!strcmp(tok, "pg")) {
			STRTOK; /* page break, e.g. after trick or comment */
		} else if (!strcmp(tok, "rh")) { /* reset heading */
			STRTOK;
		} else if (!strcmp(tok, "sk")) { /* set kibitzed */
			STRTOK;
		} else if (!strcmp(tok, "st")) { /* small text */
			STRTOK;
		} else if (!strcmp(tok, "up")) { /* undo play */
			tok = STRTOK;
		} else if (!*tok || *tok == '\n' || *tok == '\r') {
			/* empty token, hopefully end of line */
		} else {
			printf("Unknown token '%s|%s|'\n", tok, STRTOK);
		}
	}
	} while (fgets(line, 1023, f));

	int ret = 1;
	int i;
	goto ok;
error:
	ret = 0;
ok:
	for (i = 0; i < name_n; i++)
		free (name_arr[i]);
	if (mp_str)
		free (mp_str);

	setlocale (LC_NUMERIC, "");

	return ret;
}