void unmake_move(Move move) { turn_to_move = not_turn_to_move; ply -= 1; int direction_of_pawns = turn_to_move == WHITE? -10: 10; int from = move_from(move); int to = move_to(move); int figure = board[move_to(move)]; int broken = move_broken(move); int turn = move_turn(move); if(turn) board[from] = create_figure(turn_to_move, PAWN); else board[from] = figure; if(get_value(figure) == PAWN && to == ply->en_passant) { board[to] = EMPTY; board[to - direction_of_pawns] = create_figure(not_turn_to_move, PAWN); } else board[to] = broken; if(get_value(figure) == KING) { int horizontal; if(turn_to_move == WHITE) { place_of_white_king = from; horizontal = 90; } else { place_of_black_king = from; horizontal = 20; } if(from - to == 2) { board[horizontal + 1] = board[horizontal + 4]; board[horizontal + 4] = EMPTY; } else if(to - from == 2) { board[horizontal + 8] = board[horizontal + 6]; board[horizontal + 6] = EMPTY; } } }
void move_to_str(Move move, char *string) { int from = move_from(move); int to = move_to(move); int turn = move_turn(move); string[0] = (from % 10) - 1 + 'a'; string[1] = 9 - (from / 10) + '1'; string[2] = (to % 10) - 1 + 'a'; string[3] = 9 - (to / 10) + '1'; switch(get_value(turn)) { case QUEEN : string[4] = 'q'; break; case ROOK : string[4] = 'r'; break; case BISHOP: string[4] = 'b'; break; case KNIGHT: string[4] = 'n'; break; default: string[4] = '\0'; } string[5] = '\0'; }
void make_move(Move move) { U64 hash = ply->hash; int direction_of_pawns = turn_to_move == WHITE? -10: 10; int from = move_from(move); int figure = board[from]; int to = move_to(move); int turn = move_turn(move); int broken = move_broken(move); board[from] = EMPTY; hash ^= zobrist_piecesquare[figure][from]; if(turn) { board[to] = turn; hash ^= zobrist_piecesquare[turn][to]; } else { board[to] = figure; hash ^= zobrist_piecesquare[figure][to]; } if(get_value(figure) == PAWN && to == ply->en_passant) { int tmp = to - direction_of_pawns; board[tmp] = EMPTY; hash ^= zobrist_piecesquare[create_figure(not_turn_to_move, PAWN)][tmp]; } else if(broken) { hash ^= zobrist_piecesquare[broken][to]; } if(ply->en_passant) { hash ^= zobrist_en_passant[ply->en_passant]; } ply += 1; if(get_value(figure) == PAWN && to - from == direction_of_pawns * 2) { ply->en_passant = from + direction_of_pawns; hash ^= zobrist_en_passant[ply->en_passant]; } else { ply->en_passant = 0; } ply->castlings = (ply - 1)->castlings; if(get_value(figure) == KING) { int horizontal; if(turn_to_move == WHITE) { place_of_white_king = to; make_white_castlings_is_incorrect(); horizontal = 90; } else { place_of_black_king = to; make_black_castlings_is_incorrect(); horizontal = 20; } if(from - to == 2) { board[horizontal + 4] = board[horizontal + 1]; board[horizontal + 1] = EMPTY; } else if(to - from == 2) { board[horizontal + 6] = board[horizontal + 8]; board[horizontal + 8] = EMPTY; } } if(board[98] != create_figure(WHITE, ROOK)) make_K_castling_is_incorrect(); if(board[91] != create_figure(WHITE, ROOK)) make_Q_castling_is_incorrect(); if(board[28] != create_figure(BLACK, ROOK)) make_k_castling_is_incorrect(); if(board[21] != create_figure(BLACK, ROOK)) make_q_castling_is_incorrect(); if(broken || get_value(figure) == PAWN) ply->number_of_insignificant_plies = 0; else ply->number_of_insignificant_plies = (ply - 1)->number_of_insignificant_plies + 1; turn_to_move = not_turn_to_move; hash ^= zobrist_color; hash ^= zobrist_castlings[(ply - 1)->castlings]; hash ^= zobrist_castlings[ply->castlings]; ply->hash = hash; }
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(); } }