move_t *fullalg_to_move(board_t *board, char *move_s) { move_t *move; if ((strlen(move_s) < 4) || (strlen(move_s) > 5)) return NULL; if ((move_s[0] < 'a') || (move_s[0] > 'h')) return NULL; if ((move_s[1] < '1') || (move_s[1] > '8')) return NULL; if ((move_s[2] < 'a') || (move_s[2] > 'h')) return NULL; if ((move_s[3] < '1') || (move_s[3] > '8')) return NULL; if ((strlen(move_s) == 5) && (move_s[4] != 'n') && (move_s[4] != 'b') && (move_s[4] != 'r') && (move_s[4] != 'q')) return NULL; move = (move_t *) malloc(sizeof(move_t)); move->source = move_s[0] - 'a' + (move_s[1] - '1') * 8; move->destination = move_s[2] - 'a' + (move_s[3] - '1') * 8; if (strlen(move_s) == 5) { switch (move_s[4]) { case 'n': move->promotion_piece = KNIGHT; break; case 'b': move->promotion_piece = BISHOP; break; case 'r': move->promotion_piece = ROOK; break; case 'q': move->promotion_piece = QUEEN; } move->promotion_piece += board->turn; } else move->promotion_piece = NONE; if (!move_is_valid(board, move)) { free(move); return NULL; } move_set_attr(board, move); return move; }
static int do_move(move_t *move, int ui_update) { char *move_s, *move_f, *move_san; board_t new_board; if (!move_is_valid(history->last->board, move)) { DBG_WARN("Move is illegal"); return 0; } move_set_attr(history->last->board, move); new_board = *history->last->board; move_s = move_to_fullalg(&new_board, move); move_list_play(&fullalg_list, move_s); move_san = move_to_san(&new_board, move); move_f = san_to_fan(&new_board, move_san); DBG_LOG("Processing move %s (%s)", move_s, move_san); move_list_play(&san_list, move_san); move_list_play(&fan_list, move_f); free(move_san); free(move_f); free(move_s); make_move(&new_board, move); if (move->state == MOVE_CHECK) new_board.state = BOARD_CHECK; else if (move->state == MOVE_CHECKMATE) new_board.state = BOARD_CHECKMATE; else new_board.state = BOARD_NORMAL; history_play(history, move, &new_board); if (ui_update) ui->update(history->view->board, move); if (new_board.state == MOVE_CHECKMATE) { history->result = malloc(sizeof(result_t)); if (new_board.turn == WHITE) { history->result->code = RESULT_BLACK_WINS; history->result->reason = strdup("Black mates"); } else { history->result->code = RESULT_WHITE_WINS; history->result->reason = strdup("White mates"); } if (ui_update) ui->show_result(history->result); } else if (new_board.state == MOVE_STALEMATE) { history->result = malloc(sizeof(result_t)); history->result->code = RESULT_DRAW; history->result->reason = strdup("Stalemate"); if (ui_update) ui->show_result(history->result); } return 1; }