Ejemplo n.º 1
0
int legal_go(float *b, char *ko, int p, int r, int c)
{
    if (b[r*19 + c]) return 0;
    char curr[91];
    char next[91];
    board_to_string(curr, b);
    move_go(b, p, r, c);
    board_to_string(next, b);
    string_to_board(curr, b);
    if(memcmp(next, ko, 91) == 0) return 0;
    return 1;
}
Ejemplo n.º 2
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;
    }
}
Ejemplo n.º 3
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);
    }
}
Ejemplo n.º 4
0
Archivo: ia.cpp Proyecto: lucas8/TIPE1
void mainloop(Robot* robot)
{
    /*** Initialisation ***/
    rb = robot;
    oldE = 0.0f;
    /* Generate a random genom */
    genom = 0;
    for(int i = 0; i < 3; ++i) {
        GENOM_SET (genom, rand_act(), i);
    }
    /* First movement */
    move_reset();
    switch(rand_act()) {
        case ACT_TURN_RIGHT:
            move_turn(PI/2.0f);
            break;
        case ACT_TURN_LEFT:
            move_turn(-PI/2.0f);
            break;
    }
    move_go();

    /*** Loop ***/
    for(;;) {
        if(rb->discover()) {
            char buffer[256];
            int size;

            move_stop();
            buffer[0] = 0xaa;
            rb->send(buffer, 1);
            size = rb->receive(buffer, 256);
            if(size != 1) {
                /* Taille invalide, on ignore */
                rb->close();
                move_go();
                continue;
            } else {
                if(buffer[0] == (char)0xFF) { /* Connection à l'ordi central */
                    /* TODO */
                    rb->close();
                    move_go();
                    continue;
                } else {
                    float E = evalue();
                    buffer[0] = ((genom & 0x3f) << 1);
                    rb->memcpy(buffer + 1, &dist, 4);
                    rb->memcpy(buffer + 5, &E, 4);
                    rb->send(buffer, 9);

                    float rdist;
                    size = rb->receive(buffer, 256);
                    rb->close();
                    if(size != 9 || (buffer[0] & 0x7e) != 0) {
                        move_go();
                        continue;
                    }

                    rb->memcpy(&rdist, buffer + 1, 4);
                    rb->memcpy(&E, buffer + 5, 4);
                    if(evolve((buffer[0] & 0x3f) >> 1, E, rdist)) {
                        move_reset();
                    }
                    move_go();
                    continue;
                }
            }
        }

        float sc = rb->scan();
        Action act;
        if(sc < DNEXT) {
            act = GENOM_GET(genom, NEXT);
            move_stop();
            switch(act) {
                case ACT_TURN_RIGHT:
                    move_turn(PI/2.0f);
                    break;
                case ACT_TURN_LEFT:
                    move_turn(-PI/2.0f);
                    break;
                case ACT_STRAIGHT:
                    move_turn(PI);
                    break;
            }
            move_go();
        } else if(sc < DNEAR) {
            act = GENOM_GET(genom, NEAR);
            move_stop();
            switch(act) {
                case ACT_TURN_RIGHT:
                    move_turn(PI/2.0f);
                    break;
                case ACT_TURN_LEFT:
                    move_turn(-PI/2.0f);
                    break;
            }
            move_go();
        } else if(sc < DFAR) {
            act = GENOM_GET(genom, FAR);
            move_stop();
            switch(act) {
                case ACT_TURN_RIGHT:
                    move_turn(PI/2.0f);
                    break;
                case ACT_TURN_LEFT:
                    move_turn(-PI/2.0f);
                    break;
            }
            move_go();
        }
    }