Logo 李凌宇的博客

博客

2048有点慢,但能直接按上下左右运行(中)

2024-02-17 08:32:28 By 李凌宇
void mergeDown() {
    for (int c = 0; c < 4; c++) {
        copy_col(c);
        delete_space_by_paste_to_col(c, DOWN);
        for (int r = 3; r > 0; r--) {
            if (arr[r][c] != 0 && arr[r][c] == arr[r - 1][c]) {
                arr[r][c] *= 2;
                score += arr[r][c];
                arr[r - 1][c] = 0;
            }
        }
        copy_col(c);
        delete_space_by_paste_to_col(c, DOWN);
    }
}
int dest[ROW_COUNT];

void copy_row(int r) {
    for (int c = 0; c < ROW_COUNT; ++c) {
        dest[c] = arr[r][c];
    }
}

void copy_col(int c) {
    for (int r = 0; r < ROW_COUNT; ++r) {
        dest[r] = arr[r][c];
    }
}

void delete_space_by_paste_to_row(int r, int direction) {
    if (direction == LEFT) {
        int c_idx = 0;
        for (int c = 0; c < ROW_COUNT; ++c) {
            if (dest[c] == 0) {
                continue;
            }
            arr[r][c_idx] = dest[c];
            c_idx++;
        }
        for (int i = c_idx; i < ROW_COUNT; i++) {
            arr[r][i] = 0;
        }
    } else {
        int c_idx = ROW_COUNT - 1;
        for (int c = c_idx; c >= 0; c--) {
            if (dest[c] == 0) {
                continue;
            }
            arr[r][c_idx] = dest[c];
            c_idx--;
        }
        for (int i = c_idx; i >= 0; i--) {
            arr[r][i] = 0;
        }
    }
}


void delete_space_by_paste_to_col(int c, int direction) {
    if (direction == UP) {
        int r_idx = 0;
        for (int r = 0; r < ROW_COUNT; ++r) {
            if (dest[r] == 0) {
                continue;
            }
            arr[r_idx][c] = dest[r];
            r_idx++;
        }
        for (int i = r_idx; i < ROW_COUNT; i++) {
            arr[i][c] = 0;
        }
    } else {
        int r_idx = ROW_COUNT - 1;
        for (int r = r_idx; r >= 0; --r) {
            if (dest[r] == 0) {
                continue;
            }
            arr[r_idx][c] = dest[r];
            r_idx--;
        }
        for (int i = r_idx; i >= 0; i--) {
            arr[i][c] = 0;
        }
    }
}

评论

Joker
下呢
cls2013
@李凌宇 那么久了,下呢

发表评论

可以用@mike来提到mike这个用户,mike会被高亮显示。如果你真的想打“@”这个字符,请用“@@”。