Beispiel #1
0
void generate_moves(int captures_only, int depth)
	{
	int x, y, piece;

	if (attacked_squares_only)
		{
		for (y = 0; y < board.height; ++y)
		for (x = 0; x < board.width; ++x)
			attacked_squares[depth][x][y] = 0;
		}
	else
		move_counter[depth] = 0;

	// generate spawns
	if (!captures_only && board.resource[board.side] >= 100)
		{
		int piece_row = board.height - 1;
		int start_row = board.height - 2;
		int piece_color = 0;
		if (board.side)
			{
			start_row = 1;
			piece_row = 0;
			piece_color = 6;
			}
		for (x = 0; x < board.width; ++x) if (board.cells[x][start_row] == 0)
			record_a_spawn(1 + piece_color, x, start_row, depth);
		if (board.resource[board.side] >= 300)
		for (x = 0; x < board.width; ++x) if (board.cells[x][piece_row] == 0)
			{
			record_a_spawn(2 + piece_color, x, piece_row, depth);
			record_a_spawn(3 + piece_color, x, piece_row, depth);
			if (board.resource[board.side] >= 500)
				record_a_spawn(4 + piece_color, x, piece_row, depth);
			if (board.resource[board.side] >= 900)
				record_a_spawn(5 + piece_color, x, piece_row, depth);
			}
		}

	// generate moves
	for (y = 0; y < board.height; ++y)
	for (x = 0; x < board.width; ++x)
		{
		piece = board.cells[x][y];
		if ((board.side == 0 && piece > 0 && piece < 7) || (board.side != 0 && piece > 6))
			{
			generate_move(x, y, piece, captures_only, depth);
			}
		}

/*	for (x = 0; x < move_counter[depth]; ++x)
		{
		printf("Move %d: ", x + 1);
		print_algebraic(moves[depth][x]);
		printf("\n");
		}*/
	}
Beispiel #2
0
/* Put free placement handicap stones on the board. We do this simply
 * by generating successive black moves.
 */
void
place_free_handicap(int handicap)
{
  int k;
  int i, j;
  
  for (k = 0; k < handicap; k++) {
    generate_move(&i, &j, BLACK);
    play_move(i, j, BLACK);
  }
}
Beispiel #3
0
static void
benchmark(int num_games_per_point)
{
  int i, j;
  unsigned int random_seed = 1U;
  board_size = 9;
  komi = 0.5;

  init_brown(random_seed);

  for (i = 0; i < board_size; i++) {
    for (j = 0; j < board_size; j++) {
      int white_wins = 0;
      int black_wins = 0;
      int k;
      for (k = 0; k < num_games_per_point; k++) {
	int passes = 0;
	int num_moves = 1;
	int color = WHITE;
        clear_board();
	play_move(i, j, BLACK);
        while (passes < 3 && num_moves < 600) {
	  int m, n;
          generate_move(&m, &n, color);
          play_move(m, n, color);
          if (pass_move(m, n)) {
            passes++;
          }
          else {
            passes = 0;
          }
          num_moves++;
          color = OTHER_COLOR(color);
        }
	if (passes == 3) {
	  if (compute_score() > 0) {
	    white_wins++;
	  }
	  else {
	    black_wins++;
	  }
	}
      }
      /*
      printf("%d %d %f\n", i, j,
	     (float) black_wins / (float) (black_wins + white_wins));
      */
    }
  }
}
Beispiel #4
0
static int
gtp_genmove(char *s)
{
  int i, j;
  int color = EMPTY;

  if (!gtp_decode_color(s, &color))
    return gtp_failure("invalid color");

  generate_move(&i, &j, color);
  play_move(i, j, color);

  gtp_start_response(GTP_SUCCESS);
  gtp_mprintf("%m", i, j);
  return gtp_finish_response();
}
Beispiel #5
0
void
init_brown()
{
  int k;
  int i, j;

  /* The GTP specification leaves the initial board configuration as
   * well as the board configuration after a boardsize command to the
   * discretion of the engine. We choose to start with up to 20 random
   * stones on the board.
   */
  clear_board();
  for (k = 0; k < 20; k++) {
    int color = rand() % 2 ? BLACK : WHITE;
    generate_move(&i, &j, color);
    play_move(i, j, color);
  }
}
Beispiel #6
0
void self_go(char *filename, char *weightfile, char *f2, char *w2, int multi)
{
    network net = parse_network_cfg(filename);
    if(weightfile){
        load_weights(&net, weightfile);
    }

    network net2 = net;
    if(f2){
        net2 = parse_network_cfg(f2);
        if(w2){
            load_weights(&net2, w2);
        }
    }
    srand(time(0));
    char boards[300][93];
    int count = 0;
    set_batch_network(&net, 1);
    set_batch_network(&net2, 1);
    float *board = calloc(19*19, sizeof(float));
    char *one = calloc(91, sizeof(char));
    char *two = calloc(91, sizeof(char));
    int done = 0;
    int player = 1;
    int p1 = 0;
    int p2 = 0;
    int total = 0;
    while(1){
        if (done || count >= 300){
            float score = score_game(board);
            int i = (score > 0)? 0 : 1;
            if((score > 0) == (total%2==0)) ++p1;
            else ++p2;
            ++total;
            fprintf(stderr, "Total: %d, Player 1: %f, Player 2: %f\n", total, (float)p1/total, (float)p2/total);
            int j;
            for(; i < count; i += 2){
                for(j = 0; j < 93; ++j){
                    printf("%c", boards[i][j]);
                }
                printf("\n");
            }
            memset(board, 0, 19*19*sizeof(float));
            player = 1;
            done = 0;
            count = 0;
            fflush(stdout);
            fflush(stderr);
        }
        //print_board(board, 1, 0);
        //sleep(1);
        network use = ((total%2==0) == (player==1)) ? net : net2;
        int index = generate_move(use, player, board, multi, .1, .7, two, 0);
        if(index < 0){
            done = 1;
            continue;
        }
        int row = index / 19;
        int col = index % 19;

        char *swap = two;
        two = one;
        one = swap;

        if(player < 0) flip_board(board);
        boards[count][0] = row;
        boards[count][1] = col;
        board_to_string(boards[count] + 2, board);
        if(player < 0) flip_board(board);
        ++count;

        move_go(board, player, row, col);
        board_to_string(one, board);

        player = -player;
    }
}
Beispiel #7
0
void engine_go(char *filename, char *weightfile, int multi)
{
    network net = parse_network_cfg(filename);
    if(weightfile){
        load_weights(&net, weightfile);
    }
    srand(time(0));
    set_batch_network(&net, 1);
    float *board = calloc(19*19, sizeof(float));
    char *one = calloc(91, sizeof(char));
    char *two = calloc(91, sizeof(char));
    int passed = 0;
    while(1){
        char buff[256];
        int id = 0;
        int has_id = (scanf("%d", &id) == 1);
        scanf("%s", buff);
        if (feof(stdin)) break;
        char ids[256];
        sprintf(ids, "%d", id);
        //fprintf(stderr, "%s\n", buff);
        if (!has_id) ids[0] = 0;
        if (!strcmp(buff, "protocol_version")){
            printf("=%s 2\n\n", ids);
        } else if (!strcmp(buff, "name")){
            printf("=%s DarkGo\n\n", ids);
        } else if (!strcmp(buff, "version")){
            printf("=%s 1.0\n\n", ids);
        } else if (!strcmp(buff, "known_command")){
            char comm[256];
            scanf("%s", comm);
            int known = (!strcmp(comm, "protocol_version") || 
                    !strcmp(comm, "name") || 
                    !strcmp(comm, "version") || 
                    !strcmp(comm, "known_command") || 
                    !strcmp(comm, "list_commands") || 
                    !strcmp(comm, "quit") || 
                    !strcmp(comm, "boardsize") || 
                    !strcmp(comm, "clear_board") || 
                    !strcmp(comm, "komi") || 
                    !strcmp(comm, "final_status_list") || 
                    !strcmp(comm, "play") || 
                    !strcmp(comm, "genmove"));
            if(known) printf("=%s true\n\n", ids);
            else printf("=%s false\n\n", ids);
        } else if (!strcmp(buff, "list_commands")){
            printf("=%s protocol_version\nname\nversion\nknown_command\nlist_commands\nquit\nboardsize\nclear_board\nkomi\nplay\ngenmove\nfinal_status_list\n\n", ids);
        } else if (!strcmp(buff, "quit")){
            break;
        } else if (!strcmp(buff, "boardsize")){
            int boardsize = 0;
            scanf("%d", &boardsize);
            //fprintf(stderr, "%d\n", boardsize);
            if(boardsize != 19){
                printf("?%s unacceptable size\n\n", ids);
            } else {
                printf("=%s \n\n", ids);
            }
        } else if (!strcmp(buff, "clear_board")){
            passed = 0;
            memset(board, 0, 19*19*sizeof(float));
            printf("=%s \n\n", ids);
        } else if (!strcmp(buff, "komi")){
            float komi = 0;
            scanf("%f", &komi);
            printf("=%s \n\n", ids);
        } else if (!strcmp(buff, "play")){
            char color[256];
            scanf("%s ", color);
            char c;
            int r;
            int count = scanf("%c%d", &c, &r);
            int player = (color[0] == 'b' || color[0] == 'B') ? 1 : -1;
            if(c == 'p' && count < 2) {
                passed = 1;
                printf("=%s \n\n", ids);
                char *line = fgetl(stdin);
                free(line);
                fflush(stdout);
                fflush(stderr);
                continue;
            } else {
                passed = 0;
            }
            if(c >= 'A' && c <= 'Z') c = c - 'A';
            if(c >= 'a' && c <= 'z') c = c - 'a';
            if(c >= 8) --c;
            r = 19 - r;
            fprintf(stderr, "move: %d %d\n", r, c);

            char *swap = two;
            two = one;
            one = swap;
            move_go(board, player, r, c);
            board_to_string(one, board);

            printf("=%s \n\n", ids);
            print_board(board, 1, 0);
        } else if (!strcmp(buff, "genmove")){
            char color[256];
            scanf("%s", color);
            int player = (color[0] == 'b' || color[0] == 'B') ? 1 : -1;

            int index = generate_move(net, player, board, multi, .1, .7, two, 1);
            if(passed || index < 0){
                printf("=%s pass\n\n", ids);
                passed = 0;
            } else {
                int row = index / 19;
                int col = index % 19;

                char *swap = two;
                two = one;
                one = swap;

                move_go(board, player, row, col);
                board_to_string(one, board);
                row = 19 - row;
                if (col >= 8) ++col;
                printf("=%s %c%d\n\n", ids, 'A' + col, row);
                print_board(board, 1, 0);
            }

        } else if (!strcmp(buff, "p")){
            //print_board(board, 1, 0);
        } else if (!strcmp(buff, "final_status_list")){
            char type[256];
            scanf("%s", type);
            fprintf(stderr, "final_status\n");
            char *line = fgetl(stdin);
            free(line);
            if(type[0] == 'd' || type[0] == 'D'){
                FILE *f = fopen("game.txt", "w");
                int i, j;
                int count = 2;
                fprintf(f, "boardsize 19\n");
                fprintf(f, "clear_board\n");
                for(j = 0; j < 19; ++j){
                    for(i = 0; i < 19; ++i){
                        if(board[j*19 + i] == 1) fprintf(f, "play black %c%d\n", 'A'+i+(i>=8), 19-j);
                        if(board[j*19 + i] == -1) fprintf(f, "play white %c%d\n", 'A'+i+(i>=8), 19-j);
                        if(board[j*19 + i]) ++count;
                    }
                }
                fprintf(f, "final_status_list dead\n");
                fclose(f);
                FILE *p = popen("./gnugo --mode gtp < game.txt", "r");
                for(i = 0; i < count; ++i){
                    free(fgetl(p));
                    free(fgetl(p));
                }
                char *l = 0;
                while((l = fgetl(p))){
                    printf("%s\n", l);
                    free(l);
                }
            } else {
                printf("?%s unknown command\n\n", ids);
            }
        } else {
            char *line = fgetl(stdin);
            free(line);
            printf("?%s unknown command\n\n", ids);
        }
        fflush(stdout);
        fflush(stderr);
    }
}