• 个人简介

    此生无悔入MC,来世还做方块人

    my luogu

    小游戏: 1.猜数字

    #include <iostream>
    #include <cstdlib> 
    int main() {
        srand(time(0));
        int secretNumber = rand() % 100 + 1;
        int guess = 0;
        int attempts = 0;
     
        std::cout << "猜数字游戏!我已经选择了一个1到100之间的数字。\n";
     
        while (guess != secretNumber) {
            std::cout << "请输入你的猜测:";
            std::cin >> guess;
            attempts++;
     
            if (guess < secretNumber) {
                std::cout << "太小了!再试一次。\n";
            } else if (guess > secretNumber) {
                std::cout << "太大了!再试一次。\n";
            } else {
                std::cout << "恭喜你!你猜对了!数字是 " << secretNumber << "。\n";
                std::cout << "你总共尝试了 " << attempts << " 次。\n";
            }
        }
     
        return 0;
    }
    

    2.井字棋

    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
     
    int s[5][5] = {}; // 网格上的小盒子 0 无 -1 电脑 +1 玩家 下同
    int m[5][5] = {}; // 网格上的中盒子
    int l[5][5] = {}; // 网格上的大盒子
    int p1[4] = {0, 2, 2, 2}, p2[4] = {0, 2, 2, 2}; // 电脑和玩家的①小②中③大盒个数
    int p[5][5] = {}; // 当前网格被谁占据
    int px[2][5], py[2][5], bx[3], by[3];
    struct node {
        int x, y, h;
    } aa[15];
     
    bool cmp(node a, node b) { return a.h > b.h; }
     
    // 输出当前网格
    void showSquare()
    {
        // 计算当前网格被谁的盒子占据
        int a[5][5] = {}; // 当前网格上的盒子 + - 表示角色 数字表示箱子大小
        for (int i = 1; i <= 3; i++)
        {
            for (int j = 1; j <= 3; j++)
            {
                if (l[i][j] != 0)
                {
                    a[i][j] = l[i][j] * 3;
                }
                else if (m[i][j] != 0)
                {
                    a[i][j] = m[i][j] * 2;
                }
                else
                {
                    a[i][j] = s[i][j] * 1;
                }
            }
        }
        // 绘制网格
        string border[4] = {"", "┌────┬────┬────┐", "├────┼────┼────┤", "├────┼────┼────┤"};
        for (int i = 1; i <= 3; i++)
        {
            cout << border[i] << endl;
            cout << "│";
            for (int j = 1; j <= 3; j++)
            {
                if (a[i][j] > 0)
                {
                    cout << " +" << a[i][j] << " │";
                }
                else if (a[i][j] < 0)
                {
                    cout << " " << a[i][j] << " │";
                }
                else
                {
                    cout << "    │";
                }
            }
            cout << endl;
        }
        cout << "└────┴────┴────┘" << endl;
    }
     
    // 对行、列、对角线求和,若和为3则玩家胜利返回1,若和为-3则电脑胜利返回-1,否则返回0继续游戏
    int findWinner()
    {
        // 计算当前网格被谁占据,只记录角色-1 +1,不记录箱子大小
        int b[5][5] = {}; // 当前网格被谁占据
        for (int i = 1; i <= 3; i++)
        {
            for (int j = 1; j <= 3; j++)
            {
                if (l[i][j] != 0)
                {
                    b[i][j] = l[i][j];
                }
                else if (m[i][j] != 0)
                {
                    b[i][j] = m[i][j];
                }
                else
                {
                    b[i][j] = s[i][j];
                }
            }
        }
        // 检查行
        for (int i = 1; i <= 3; i++)
        {
            int sum = 0;
            for (int j = 1; j <= 3; j++)
            {
                sum += b[i][j];
            }
            if (sum == 3) return 1;
            if (sum == -3) return -1;
        }
        // 检查列
        for (int j = 1; j <= 3; j++)
        {
            int sum = 0;
            for (int i = 1; i <= 3; i++)
            {
                sum += b[i][j];
            }
            if (sum == 3) return 1;
            if (sum == -3) return -1;
        }
        // 检查对角线
        int diag1 = 0, diag2 = 0;
        for (int i = 1; i <= 3; i++)
        {
            diag1 += b[i][i];
            diag2 += b[i][4-i];
        }
        if (diag1 == 3 || diag2 == 3) return 1;
        if (diag1 == -3 || diag2 == -3) return -1;
        return 0;
    }
     
    // 角色q把size箱子放到x行y列的位置
    bool place(int x, int y, int size, int q)
    {
        if (x < 1 || x > 3 || y < 1 || y > 3) return false;
        if (q == -1)
        {
            printf("电脑试图在(%d,%d)下%d\n", x, y, size);
        }
        if (size == 3)
        {
            if (l[x][y] != 0) return false;
            l[x][y] = q;
        }
        if (size == 2)
        {
            if (l[x][y] != 0 || m[x][y] != 0) return false;
            m[x][y] = q;
        }
        if (size == 1)
        {
            if (l[x][y] != 0 || m[x][y] != 0 || s[x][y] != 0) return false;
            s[x][y] = q;
        }
        return true;
    }
     
    // 角色q把size箱子从x1行y1列转移到x2行y2列
    bool move(int x1, int y1, int x2, int y2, int size, int q)
    {
        if (x1 < 1 || x1 > 3 || y1 < 1 || y1 > 3) return false;
        if (x2 < 1 || x2 > 3 || y2 < 1 || y2 > 3) return false;
        if (q == -1)
        {
            cout << "电脑试图将(" << x1 << "," << y1 << ")" << "转移到(" << x2 << "," << y2 << ")" << endl;
        }
        if (size == 3)
        {
            if (l[x1][y1] != q || l[x2][y2] != 0) return false;
            l[x1][y1] = 0;
            l[x2][y2] = q;
        }
        if (size == 2)
        {
            if (l[x1][y1] != 0 || l[x2][y2] != 0 || m[x1][y1] != q || m[x2][y2] != 0) return false;
            m[x1][y1] = 0;
            m[x2][y2] = q;
        }
        if (size == 1)
        {
            if (l[x1][y1] != 0 || l[x2][y2] != 0 || m[x1][y1] != 0 || m[x2][y2] != 0 || s[x1][y1] != q || s[x2][y2] != 0) return false;
            s[x1][y1] = 0;
            s[x2][y2] = q;
        }
        return true;
    }
    int winning(int x)
    {
        // 计算当前网格被谁占据
        for (int i = 1; i <= 3; i++)
        {
            for (int j = 1; j <= 3; j++)
            {
                if (l[i][j] != 0) p[i][j] = l[i][j];
                else if (m[i][j] != 0) p[i][j] = m[i][j];
                else p[i][j] = s[i][j];
            }
        }
        int a = 0;
        if (x == 1) a = 1;
        for (int i = 1; i <= 3; i++)
            px[a][i] = py[a][i] = 0;
        bx[a] = by[a] = 0;
        for (int i = 1; i <= 3; i++)
            for (int j = 1; j <= 3; j++)
                if (p[i][j] == x)
                {
                    px[a][i]++;
                    py[a][j]++;
                    if (i == j) bx[a]++;
                    if (i + j == 4) by[a]++;
                }
        int s = bx[a] >= 2;
        s += by[a] >= 2;
        for (int i = 1; i <= 3; i++)
        {
            s += px[a][i] >= 2;
            s += py[a][i] >= 2;
        }
        return s;
    }
     
    int fill(int x, int y, int size) //电脑至少用size大小的棋子占领x行y列
    {
        // cout << "电脑试图在" <<  x << ',' << y << "下棋" << endl;
        if (l[x][y] == 1) return 0;
        for (int k = size; k <= 3; k++)
            if (p1[k] && place(x, y, k, -1))
            {
                p1[k]--;
                return 1;
            }
        if (s[x][y] == 1) size = max(size, 2);
        if (m[x][y] == 1) size = 3;
        if (size == 1)
        {
            for (int i = 1; i <= 3; i++)
                for (int j = 1; j <= 3; j++)
                    if (s[i][j] == -1 && move(i, j, x, y, 1, -1))
                    {
                        if (winning(1) == 0)
                            return 1;
                        move(x, y, i, j, 1, -1);
                    }
        }
        if (size <= 2)
        {
            for (int i = 1; i <= 3; i++)
                for (int j = 1; j <= 3; j++)
                    if (m[i][j] == -1 && move(i, j, x, y, 2, -1))
                    {
                        if (winning(1) == 0)
                            return 1;
                        move(x, y, i, j, 2, -1);
                    }
        }
        if (size <= 3)
        {
            for (int i = 1; i <= 3; i++)
                for (int j = 1; j <= 3; j++)
                    if (l[i][j] == -1 && move(i, j, x, y, 3, -1))
                    {
                        if (winning(1) == 0)
                            return 1;
                        move(x, y, i, j, 3, -1);
                    }
        }
        return 0;
    }
    void computer()
    {
        if (winning(-1) > 0)
        {
            cout << "电脑认为自己快赢了" << endl;
            for (int i = 1; i <= 3; i++)
                for (int j = 1; j <= 3; j++)
                    if (p[i][j] != -1 && (px[0][i] == 2 || py[0][j] == 2))
                    {
                        if (fill(i, j, 1))
                            return;
                    }
            if (bx[0] == 2)
            {
                for (int i = 1; i <= 3; i++)
                    if (p[i][i] != -1 && fill(i, i, 1))
                        return ;
            }
            if (by[0] == 2)
            {
                for (int i = 1; i <= 3; i++)
                    if (p[i][4 - i] != -1 && fill(i, 4 - i, 1))
                        return ;
            }
        }
        if (winning(1) > 0)
        {
            cout << "电脑认为你快赢了" << endl;
            int cnt = 0;
            for (int i = 1; i <= 3; i++)
                for (int j = 1; j <= 3; j++)
                {
                    int w = (px[1][i] == 2) + (py[1][j] == 2);
                    if (i == j && bx[1] == 2) w++;
                    if (i + j == 4 && by[1] == 2) w++;
                    if (w == 0) continue;
                    aa[++cnt].x = i; aa[cnt].y = j; aa[cnt].h = w;
                }
            sort(aa + 1, aa + cnt + 1, cmp);
            for (int i = 1; i <= cnt; i++)
            {
                // printf("电脑想在%d,%d阻止你,w=%d\n",aa[i].x,aa[i].y,aa[i].h);
                if (p2[3] > 0)
                {
                    if (fill(aa[i].x, aa[i].y, 3))
                        return ;
                    else continue;
                }
                if (p2[2] > 0)
                {
                    if (fill(aa[i].x, aa[i].y, 2))
                        return ;
                    else continue;
                }
                if (p2[1] > 0)
                {
                    if (fill(aa[i].x, aa[i].y, 1))
                        return ;
                    else continue;
                }
            }
        }
        if (l[2][2] == 0)
        {
            cout << "电脑决定占领中心" << endl;
            if (p1[3] > 0) {
                p1[3]--;
                place(2, 2, 3, -1);
                return;
            }
            if (fill(2, 2, 3)) return ;
            if (fill(2, 2, 2)) return ;
        }
        int cnt = 0;
        for (int i = 1; i <= 3; i++)
            for (int j = 1; j <= 3; j++)
                if (p[i][j] != -1)
                {
                    if (l[i][j] > 0) continue;
                    int t = p[i][j];
                    p[i][j] = -1;
                    aa[++cnt].x = i;
                    aa[cnt].y = j;
                    aa[cnt].h = winning(-1);
                    p[i][j] = t;
                }
        sort(aa + 1, aa + cnt + 1, cmp);
        int j = 1;
        for (int i = 1; i <= cnt; i++)
        {
            if (aa[i].h == aa[j].h)
                j = i;
            else break;
        }
        j = rand() % j + 1;
        if (fill(aa[j].x, aa[j].y, 1))
            return;
        for (int i = 1; i <= cnt; i++)
            if (fill(aa[i].x, aa[i].y, 1))
                return;
    }
    int game()
    {
        srand((unsigned int)time(NULL));
        while (true)
        {
            cout << "请选择 ① 放一个盒子 还是 ② 移动一个盒子 (输入数字)" << endl;
            int choice, size, x1, y1, x2, y2;
            cin >> choice;
            if (choice == 1)
            {
                cout << "你现在有盒子 小:" << p2[1] << " 中:" << p2[2] << " 大:" << p2[3] << endl;
                cout << "请选择盒子的大小 ① 小 ② 中 ③ 大 (输入数字)" << endl;
                cin >> size;
                if (size > 3 || size < 1) {cout << "非法输入" << endl; continue;}
                if (p2[size] <= 0) {cout << "盒子没了" << endl; continue;}
                cout << "请输入放置的位置(输入两个整数,如:1 1)" << endl;
                cin >> x1 >> y1;
                if (!place(x1, y1, size, 1)) {cout << "放置失败" << endl; continue;}
                cout << "放置成功" << endl;
                p2[size]--;
                showSquare();
            }
            else if (choice == 2)
            {
                cout << "请输入移走的位置(输入两个整数,如:1 1)" << endl;
                cin >> x1 >> y1;
                cout << "请输入放置的位置(输入两个整数,如:1 1)" << endl;
                cin >> x2 >> y2;
                // 判断移动的箱子
                if (l[x1][y1] == 1) size = 3;
                else if (m[x1][y1] == 1 && l[x1][y1] == 0) size = 2;
                else if (s[x1][y1] == 1 && l[x1][y1] == 0 && m[x1][y1] == 0) size = 1;
                else {cout << "没有可以移动的盒子" << endl; continue;}
                if (!move(x1, y1, x2, y2, size, 1)) {cout << "移动失败" << endl; continue;}
                showSquare();
            }
            else continue;
            int winner = findWinner();
            if (winner != 0)
            {
                return winner;
            }
            computer();
            cout << "电脑下棋" << endl;
            showSquare();
            winner = findWinner();
            if (winner != 0)
            {
                return winner;
            }
        }
    }
     
    void showWinner(int winner)
    {
        if (winner == 1)
        {
            cout << "玩家胜利" << endl;
        }
        else
        {
            cout << "电脑胜利" << endl;
        }
    }
     
    void intro()
    {
        cout << "玩家和电脑各拥有大中小号盒子各2个," << endl;
        cout << "双方轮流在九宫格的棋盘中放入或移动一个盒子,每个盒子都可盖住任意比他小的盒子。" << endl;
        cout << "若你方团队的盒子率先在棋盘上连成一条直线则挑战成功,否则挑战失败。" << endl;
        cout << "用正数表示玩家的盒子,负数表示电脑的盒子,1、2、3表示盒子的小中大。" << endl;
        cout << "开始吧!" << endl;
        showSquare();
    }
     
    int main()
    {
        intro();
        int winner = game();
        showWinner(winner);
        return 0;
    }
    

    3.汉诺塔

    #include <bits/stdc++.h>
    #include <conio.h>
    #include <windows.h>
    using namespace std;
    const int COLUMN[4] = { 0, 2, 5, 8 };
    const int DISC_CNT_MAX = 10;
    const int ROW_OP_CNT = 2, COL_OP_CNT = 16;
    const int ROW_MESSAGE = 3, COL_MESSAGE = 16;
    const int ROW_HELP = 15, COL_HELP = 1;
    const int ROW_MAX = 30, COL_MAX = 120;
    const int BLUE = 1;
    const int GREEN = 2;
    const int CYAN = 3;
    const int AQUA = 3;
    const int RED = 4;
    const int PURPLE = 5;
    const int YELLOW = 6;
    const int WHITE = 7;
    int n;
    stack<int> rod[4];
    int sz[4] = { 0 };
    int pos1, pos2;
    int key;
    bool prev_key_is_esc;
    int op_cnt;
    bool is_moving;
    int moved_disc;
    template <typename T>
    inline T read() {
    T x = 0;
    T multiplier = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-') {
            multiplier = -1;
        }
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 3) + (x << 1) + (ch & 15);
        ch = getchar();
    }
    return x * multiplier;
    }
    void set_caret_pos(int row = 1, int col = 1) {
    COORD pos;
    pos.X = col - 1;
    pos.Y = row - 1;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
    }
    int get_caret_row() {
    CONSOLE_SCREEN_BUFFER_INFO info;
    GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
    return info.dwCursorPosition.Y + 1;
    }
    int get_caret_col() {
    CONSOLE_SCREEN_BUFFER_INFO info;
    GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
    return info.dwCursorPosition.X + 1;
    }
    pair<int, int> get_caret_pos() {
    CONSOLE_SCREEN_BUFFER_INFO info;
    GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
    return make_pair(info.dwCursorPosition.Y + 1, info.dwCursorPosition.X + 1);
    }
    void set_foreground_color(int x, bool intensity = false) {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
                            x | (intensity << 3));
    }
    void set_cursor_visibility(bool visibility = true) {
    CONSOLE_CURSOR_INFO cc_info;
    cc_info.bVisible = visibility;
    cc_info.dwSize = 1;
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cc_info);
    }
    void disp_init_state(int n) {
    for (int i = 1; i <= n; i++) {
        set_caret_pos(i, COLUMN[1]);
        printf("%d", i);
    }
    for (int i = 1; i <= 3; i++) {
        set_caret_pos(n + 1, COLUMN[i] - 1);
        printf("---");
        set_caret_pos(n + 2, COLUMN[i]);
        putchar('A' + i - 1);
    }
     set_caret_pos(ROW_OP_CNT, COL_OP_CNT);
    printf("0");
    }
    void disp_help() {
    set_caret_pos(ROW_HELP, COL_HELP);
    printf("如何玩:\n"
           "数字表示光盘的尺寸.\n"
           "将A杆上圆盘移动到C杆上,每次一个.\n"
           "每个杆上的圆盘必须按大小升序堆叠.\n"
           "使用左右箭头键选择杆.\n"
           "按Enter键拾取选定杆上的顶部圆盘.\n"
           "然后使用左右键移动.\n"
           "按ESC取消当前移动.\n"
           "再次按Enter键可将圆盘放置.\n"
           "按R重新启动.\n"
           "按ESC键两次退出.\n");
    }
    void disp_pos(int pos1, int pos2 = 0) {
    for (int i = 1; i <= 3; i++) {
        set_caret_pos(n + 3, COLUMN[i]);
        printf(" ");
    }
    set_caret_pos(n + 3, COLUMN[pos1]);
    printf("^");
    if (pos2) {
        set_caret_pos(n + 3, COLUMN[pos2]);
        set_foreground_color(GREEN, true);
        printf("^");
        set_foreground_color(WHITE);
    }
    }
    void clear() {
    for (int i = 1; i <= DISC_CNT_MAX + 3; i++) {
        for (int j = 1; j <= COL_MAX; j++) {
            set_caret_pos(i, j);
            putchar(' ');
        }
    }
    }
    void moving_disc(int pos1, int pos2) {
    int x = rod[pos1].top();
     set_caret_pos(n + 1 - sz[pos1], COLUMN[pos1]);
    set_foreground_color(RED, true);
    printf("%d", x);
    set_foreground_color(WHITE);
     set_caret_pos(n - sz[pos2] + (pos1 == pos2), COLUMN[pos2]);
    set_foreground_color(GREEN, true);
    printf("%d", x);
    set_foreground_color(WHITE);
    }
    void update_discs(int pos1, int pos2) {
    int x = rod[pos1].top();
     set_caret_pos(n + 1 - sz[pos1], COLUMN[pos1]);
    printf("  ");
    rod[pos1].pop();
    sz[pos1]--;
     rod[pos2].push(x);
    sz[pos2]++;
    set_caret_pos(n + 1 - sz[pos2], COLUMN[pos2]);
    printf("%d", x);
    }
    void remove_temp_disc(int pos) {
    set_caret_pos(n - sz[pos], COLUMN[pos]);
    printf("  ");
    }
    void update_op_cnt() {
    op_cnt++;
    set_caret_pos(ROW_OP_CNT, COL_OP_CNT);
    printf("%d", op_cnt);
    }
    int main() {
    printf("输入光盘数量(不超过 %d): ", DISC_CNT_MAX);
    n = min(read<int>(), DISC_CNT_MAX);
     set_cursor_visibility(false);
    disp_help();
     for (; n <= DISC_CNT_MAX; n++) {
        clear();
         for (int i = 1; i <= 3; i++) {
            while (!rod[i].empty()) {
                rod[i].pop();
            }
            sz[i] = 0;
        }
         for (int i = n; i >= 1; i--) {
            rod[1].push(i);
        }
        sz[1] = n;
         is_moving = false;
        pos1 = 1;
        op_cnt = 0;
        prev_key_is_esc = false;
         disp_init_state(n);
        disp_pos(1);
        while (true) {
            if (sz[3] == n) {
                set_caret_pos(ROW_MESSAGE, COL_MESSAGE);
                if (op_cnt != (1 << n) - 1) {
                    printf("你用%d个动作完成了谜题.",op_cnt);
                } else {
                    printf("祝贺你用最少的动作完成了谜题 " );
                }
                Sleep(2000);
                break;
            }
             key = getch();
            if (key == 224) {
                key = getch();
                if (!is_moving) {
                    if (key == 75) { // Left arrow key
                        pos1 = (pos1 + 1) % 3 + 1;
                    } else if (key == 77) { // Right arrow key
                        pos1 = pos1 % 3 + 1;
                    }
                    disp_pos(pos1);
                } else {
                    remove_temp_disc(pos2);
                    if (key == 75) { // Left arrow key
                        pos2 = (pos2 + 1) % 3 + 1;
                    } else if (key == 77) { // Right arrow key
                        pos2 = pos2 % 3 + 1;
                    }
                    moving_disc(pos1, pos2);
                    disp_pos(pos1, pos2);
                }
            } else if (key == 13) { // Enter key
                if (!is_moving) {
                    if (rod[pos1].empty()) {
                        continue;
                    }
                    is_moving = true;
                    moved_disc = rod[pos1].top();
                    pos2 = pos1;
                    moving_disc(pos1, pos2);
                    disp_pos(pos1, pos2);
                } else {
                    if (!rod[pos2].empty() && rod[pos2].top() < moved_disc) {
                        set_caret_pos(ROW_MESSAGE, COL_MESSAGE);
                        printf("提示:光盘未按升序堆叠在棒上。");
                        continue;
                    }
                    is_moving = false;
                    update_discs(pos1, pos2);
                    update_op_cnt();
                    pos1 = pos2;
                    disp_pos(pos1);
                }
            } else if (key == 27) { // Escape key
                if (prev_key_is_esc) { // Double ESC
                    break;
                }
                if (is_moving) {
                    is_moving = false;
                    remove_temp_disc(pos2);
                    update_discs(pos1, pos1);
                    disp_pos(pos1);
                } else {
                    prev_key_is_esc = true;
                }
            } else if (key == 'R' || key == 'r') {
                n--;
                break;
            }
        }
         if (prev_key_is_esc && key == 27) { // Double ESC
            break;
        }
    }
     set_caret_pos(ROW_MAX - 1, 1);
     return 0;
    }
    

    4.贪吃蛇

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <ctime>
    #include <conio.h>
    #include <cmath>
    #include <windows.h>
    using namespace std;
    HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
    COORD coord;
    void locate(int x,int y){
        coord.X=y;
        coord.Y=x;
        SetConsoleCursorPosition(hout,coord);
    };
    void hide(){
        CONSOLE_CURSOR_INFO cursor_info={1,0};
        SetConsoleCursorInfo(hout, &cursor_info);
    }
    double random(double start, double end){
        return start+(end-start)*rand()/(RAND_MAX + 1.0);
    }
    int m=25,n=40;
    struct node{
        int x,y;
    }snake[1000];
    int snake_length,dir;
    node food;
    int direct[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
    void print_wall(){
        cout<<" ";
        for(int i=1;i<=n;i++)cout<<"-";
        cout<<endl;
        for (int j=0;j<=m-1;j++){
            cout<<"|";
            for(int i=1;i<=n;i++)cout<<" ";
            cout<<"|"<<endl;
        }
        cout<<" ";
        for(int i=1;i<=n;i++)cout<< "-";
    }
    void print_snake(){
        locate(snake[0].x,snake[0].y);
        cout<<"@";
        for(int i=1;i<=snake_length-1;i++)
        {
            locate(snake[i].x,snake[i].y);
            cout << "*";
        }
    }
    bool is_correct(){
        if (snake[0].x==0 || snake[0].y==0 || snake[0].x==m+1 || snake[0].y==n+1) return 0;
        for(int i=1;i<=snake_length-1;i++)if(snake[0].x==snake[i].x&&snake[0].y==snake[i].y)return 0;
        return 1;
    }
    bool print_food(){
        srand((unsigned)time(0));
        bool e;
        while (1)
        {
            e=true;
            int i=(int)random(0,m)+1,j=(int)random(0,n)+1;
            food.x=i;food.y=j;
            for (int k=0;k<=snake_length-1;k++){
                if(snake[k].x==food.x && snake[k].y==food.y)
                {
    				e=false;
    				break;
                }
            }
            if(e)break;
        }
        locate(food.x,food.y);
        cout<<"$";
        return true;
    }
    bool go_ahead(){
        node temp;
        bool e=false;
        temp=snake[snake_length-1];
        for(int i=snake_length-1;i>=1;i--)snake[i]=snake[i-1];
        snake[0].x+=direct[dir][0];
        snake[0].y+=direct[dir][1];
        locate(snake[1].x,snake[1].y);
        cout << "*";
        if(snake[0].x==food.x&&snake[0].y==food.y)
        {
            snake_length++;
            e=true;
            snake[snake_length-1]=temp;
        }
        if (!e)
        {
            locate(temp.x,temp.y);
            cout << " ";
        }
        else print_food();
        locate(snake[0].x,snake[0].y);
        cout<<"@";
        if (!is_correct())
        {
            system("cls");
            cout << "You lose!" << endl << "Length: " << snake_length << endl;
            return false;
        }
        return true;
    }
    int main()
    {
        cout<<"--------------------贪吃蛇---------------------"<<endl;
        cout<<"请注意窗口大小,以免发生错位.建议将窗口调为最大."<<endl;
        cout<<"先选择难度.请在1-10中输入1个数,1最简单,10则最难"<<endl;
        cout<<"然后进入游戏画面,以方向键控制方向.祝你游戏愉快!"<<endl;
        cout<<"-----------------------------------------------"<<endl;
        if(m<10||n<10||m>25||n>40){
        	cout<<"ERROR"<<endl;
            system("pause");
            return 0;
        }
        int hard;
        cin>>hard;
        if(hard<=0||hard>100){
            cout<<"ERROR"<<endl;
            system("pause");
            return 0;
        }
        snake_length=5;
        clock_t a,b;
        char ch;
        double hard_len;
        for(int i=0;i<=4;i++){
            snake[i].x=1;
            snake[i].y=5-i;
        }
        dir=3;
        system("cls");
        hide();
        print_wall();
        print_food();
        print_snake();
        locate(m+2,0);
        cout<<"Now length: ";
        while (1)
        {
            hard_len=(double)snake_length/(double)(m*n);
            a=clock();
            while (1)
            {
                b=clock();
                if(b-a>=(int)(400-30*hard)*(1-sqrt(hard_len)))break;
            }
            if(kbhit())
            {
                ch=getch();
                if(ch==-32)
                {
                    ch=getch();
                    switch(ch)
                    {
                    case 72:
                        if(dir==2||dir==3)
                            dir=0;
                        break;
                    case 80:
                        if(dir==2||dir==3)
                            dir=1;
                        break;
                    case 75:
                        if(dir==0||dir==1)
                            dir=2;
                        break;
                    case 77:
                        if(dir==0||dir==1)
                            dir=3;
                        break;
                    }
                }
            }
            if(!go_ahead())break;
            locate(m+2,12);
            cout<<snake_length;
        }
        system("pause");
        return 0;
    }
    

    5.2DMC

    #include <iostream>
    #include <conio.h>
    #include <string>
    #include <map>
    #include <cmath>
    #include <windows.h>
    #include <time.h> 
    #define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
    void Clear_Screen() {    
        HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        COORD coordScreen = {0, 0};
        SetConsoleCursorPosition(hConsole, coordScreen);
    }
    using namespace std;
    int fallspeed;
    int jumpspeed = -3;
    int gravity = 1;
    int y = 400, x = 500;
    int restartposy, restartposx, face, health = 1000, lasthealth = 1000, breath = 100, hungry = 114514, dienum;
    bool attack, defense, hurt, mode;
    struct TNT
    {
        int y;
        int x;
        int time;
        int issave;
    };
    struct BLOCK
    {
        int color;
        string ch;
        string type;
    };
    struct MOB
    {
        int fallspeed;
        int health;
        bool hurt;
        int y;
        int x;
        int attack;
        string shap;
        bool isenemy;
        int color;
        string name;
    };
    struct ARROW
    {
        string shap;
        double y;
        double x;
        double fallspeed;
        double plusx;
    };
    TNT tnt[20];
    string die;
    ARROW arrow[100];
    MOB mobs[50] = {
        {0,1000,0,0,0,100,"危",true,7,"危"},
        {0,10,0,0,0,10,"  ",true,7,"  "},
        {0,1000,0,0,0,100,"MM",false,7,"MM"},
        {0,100000,0,90,70,-100,"AC",true,7,"Accept"},
        {0,10000,0,90,70,500,"BO",true,7,"BOSS"},
        {0,100000,0,90,70,-1000,"AK",true,7,"AK"},
    };
    MOB mob[100] = {
        {0,1000,0,92,4,100,"WA",true,7,"Wrong Anwser"},
        {0,1000,0,92,4,100,"TL",true,7,"Time Limit Error"},
        {0,2000,0,92,4,300,"CE",true,7,"Compile Error"},
        {0,1000,0,45,9,100,"WA",true,7,"Wrong Anwser"},
        {0,100000,0,90,70,-100,"AC",true,7,"Accept"},
        {0,100000,0,90,70,-1000,"AK",true,7,"AK"},
        {0,10000,0,90,70,500,"UK",true,7,"Unknown Error"},
        {0,1000,0,92,3,0,"MM",false,7,"MM"},
        {0,1000,0,92,3,0,"MM",false,7,"MM"},
        {0,1000,0,90,15,0,"MM",false,7,"MM"},
        {0,1000,0,90,80,0,"MM",false,7,"MM"},
    };
    BLOCK block[32] = {
        {0,"  ","air"},//空气
        {6,"██","block"},//土块
        {8,"██","block"},//石头
        {2,"██","block"},//草方块
        {15,"██","block"},//雪块 
        {4,"██","block"},//岩浆块
        {14,"▓▓","fallblock"},//沙块 
        {8,"II","fallblock"},//铁砧
        {9,"██","water"},//水
        {9,"▇▇","water"},//水
        {9,"▆▆","water"},//水
        {9,"▅▅","water"},//水
        {9,"▄▄","water"},//水
        {9,"▃▃","water"},//水
        {9,"▂▂","water"},//水
        {9,"▁▁","water"},//水
        {12,"██","lava"},//岩浆
        {12,"▇▇","lava"},//岩浆
        {12,"▆▆","lava"},//岩浆
        {12,"▅▅","lava"},//岩浆
        {12,"▄▄","lava"},//岩浆
        {12,"▃▃","lava"},//岩浆
        {12,"▂▂","lava"},//岩浆
        {12,"▁▁","lava"},//岩浆  
        {12,"危","background"},//危
        {6,"██","background"},//木头
        {10,"▓▓","background"},//树叶 
        {15,"▓▓","background"},//带雪树叶
        {15,"▅▅","bomb"},//TNT爆炸 
        {12,"Ⅲ","TNT"},//TNT
        {7,"Ⅲ","TNT"},//TNT2
        {6,"∷","ladder"},//梯子
    };
    int board[1005][1005];
    int setboard[1005][1005];
    int bag[100];
    int clear_buffer()
    {
        while(kbhit())
        {
            if(getch() != EOF); 
            for(int i = 1; i <= 256; i++)
            {
                if(GetAsyncKeyState(i));
            }
        }
        return 0;
    }
    void color(int a)
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
    /*    1    深蓝色
        2    深绿色
        3    深青色 
        4    深红色
        5    深粉色
        i    黄色
        7    深白色
        8    灰色
        9    浅蓝色
        10    浅绿色 
        11    浅青色 
        12    浅红色 
        13    浅粉色 
        14    浅黄色 
        15    浅白色 
        背景
        1~15        黑色 
        16~31        深蓝色 
        32~47        深绿色
        48~63        深青色
        64~79        深红色
        'S'~95        深粉色
        96~111        深黄色
        112~127     深白色
        128~143     灰色
        144~159     浅蓝色
        160~1'A'     浅绿色
        176~191     浅青色
        192~207     浅红色
        208~223     浅粉色
        224~239     浅黄色
        240~255     浅白色
    */
    }
    int init()//听说有人要我增加地图生成的注释,所以我就写了。 
    {
        for(int j = 0; j < 100; j++)
        {
            bag[j] = 0;//这个...初始化背包 
        }
        for(int i = 0; i < 1000; i++)
        {
            for(int j = 0; j < 1000; j++)
            {
                board[i][j] = 0;//初始化地图 (我们的y是倒着来的) 
            }
        } 
        double lasty = rand() % 101 + 400;//lasty代表上一个我们选择的点的高度。 
        for(int i = 5; i < 1000; i += 5)//i每次加5,每隔5个点连一条线 
        {
            double y = rand() % 21 - 10 + lasty;//y代表这个点我们选择的高度,为了不出现太陡峭的山,我们只允许这个高度在刚才的点的高低10格内。 
            y = min(450.0, y);//这是最小高度,防止整个地图都在水里。 
            double high = lasty;//这个high是用来统计当前高度的,用double可以更加精确。
            int dirt = rand() % 5 + 2;//dirt代表这一列上泥土高度。 
            for(int j = i - 5; j < i; j++)
            {
                high += (y - lasty) / 5;//high每次增加差距的1/5。 
                for(int k = 999; k >= (int)high; k--)
                {
                    if(k == (int)high)//如果是最高点 
                    {
                        setboard[k][j] = 3;//就用草地 
                        if(high <= 350)//如果high比较高 
                        {
                            setboard[k][j] = 4;//就用雪地 
                        }
                    }
                    else if(k - dirt <= (int)high)//泥土 
                    {
                        setboard[k][j] = 1;
                    }
                    else 
                    {
                        setboard[k][j] = 2;//石头 
                    }
                }
            }
            lasty = y;//赋值 
        }
        //再来一边,填满最后几格 
        int dirt = rand() % 5 + 2;
        double high = lasty;
        for(int j = 995; j < 999; j++)
        {
            for(int k = 999; k >= (int)high; k--)
            {
                if(k == (int)high)
                {
                    setboard[k][j] = 3;
                    if(high <= 350)
                    {
                        setboard[k][j] = 4;
                    }
                }
                else if(k - dirt <= (int)high)
                {
                    setboard[k][j] = 1;
                }
                else 
                {
                    setboard[k][j] = 2;
                }
            }
        }
        //填满水,这里默认把海平面高度设为410。 
        for(int i = 0; i < 1000; i++)
        {
            for(int j = 600; j >= 410; j--)
            {
                if(setboard[j][i] == 0)
                {
                    setboard[j][i] = 8;
                }
            }
        }
        //沙子 
        for(int i = 0; i < 1000; i++)
        {
            bool a = 0;
            for(int j = 999; j >= 0; j--)
            {
                if(a && setboard[j][i] != 0  && setboard[j][i] != 8)
                {
                    setboard[j][i] = 6;
                    continue;
                }
                if(setboard[j][i] == 8)
                {
                    continue;
                }
                if(setboard[j][i + 3] == 8)
                {
                    a = true;
                    setboard[j][i] = 6;
                }
                if(setboard[j][i + 2] == 8)
                {
                    a = true;
                    setboard[j][i] = 6;
                }
                if(setboard[j][i + 1] == 8)
                {
                    a = true;
                    setboard[j][i] = 6;
                }
                if(setboard[j][i - 1] == 8)
                {
                    a = true;
                    setboard[j][i] = 6;
                }
                if(setboard[j][i - 2] == 8)
                {
                    a = true;
                    setboard[j][i] = 6;
                }
                if(setboard[j][i - 3] == 8)
                {
                    a = true;
                    setboard[j][i] = 6;
                }
                if(setboard[j - 2][i] == 8)
                {
                    a = true;
                    setboard[j][i] = 6;
                }
                if(setboard[j - 1][i] == 8)
                {
                    a = true;
                    setboard[j][i] = 6;
                }
                if(setboard[j - 3][i] == 8)
                {
                    a = true;
                    setboard[j][i] = 6;
                }
                if(setboard[j - 4][i] == 8)
                {
                    a = true;
                    setboard[j][i] = 6;
                }
            }
        }
        //树 
        for(int i = 0; i < 1000; i++)
        {
            for(int j = 0; j < 1000; j++)
            {
                if(setboard[j][i] == 0 && block[setboard[j + 1][i]].type == "block")
                {
                    if(rand() % 10 == 1)
                    {
                        setboard[j][i] = 25;
                        setboard[j - 1][i] = 25;
                        setboard[j - 2][i] = 25;
                        setboard[j - 3][i] = 25;
                        setboard[j - 3][i + 1] = 26;
                        setboard[j - 3][i - 1] = 26;
                        setboard[j - 4][i + 2] = 26; 
                        setboard[j - 4][i + 1] = 26;
                        setboard[j - 4][i] = 26;
                        setboard[j - 4][i - 1] = 26;
                        setboard[j - 4][i - 2] = 26;
                        setboard[j - 5][i + 2] = 26;
                        setboard[j - 5][i + 1] = 26;
                        setboard[j - 5][i] = 26;
                        setboard[j - 5][i - 1] = 26;
                        setboard[j - 5][i - 2] = 26;
                        setboard[j - 6][i + 1] = 26;
                        setboard[j - 6][i] = 26;
                        setboard[j - 6][i - 1] = 26;
                        if(j <= 350)
                        {
                            setboard[j - 3][i + 1] = 27;
                            setboard[j - 3][i - 1] = 27;
                            setboard[j - 4][i + 2] = 27; 
                            setboard[j - 4][i + 1] = 27;
                            setboard[j - 4][i] = 27;
                            setboard[j - 4][i - 1] = 27;
                            setboard[j - 4][i - 2] = 27;
                            setboard[j - 5][i + 2] = 27;
                            setboard[j - 5][i + 1] = 27;
                            setboard[j - 5][i] = 27;
                            setboard[j - 5][i - 1] = 27;
                            setboard[j - 5][i - 2] = 27;
                            setboard[j - 6][i + 1] = 27;
                            setboard[j - 6][i] = 27;
                            setboard[j - 6][i - 1] = 27;
                        }
                    }
                }
            }
        } 
        //地洞 
        for(int j = 999; j >= 700; j--)
        {
            if(rand() % 20 == 1)
            {
                setboard[j - 3][2] = 0;
                setboard[j - 2][2] = 0; setboard[j - 2][3] = 0;
                setboard[j - 1][2] = 0; setboard[j - 1][3] = 0; setboard[j - 1][4] = 0;
                setboard[j][2] = 0; setboard[j][3] = 0; setboard[j][4] = 0; setboard[j][5] = 0;
                setboard[j + 1][2] = 0; setboard[j + 1][3] = 0; setboard[j + 1][4] = 0;
                setboard[j + 2][2] = 0; setboard[j + 2][3] = 0;
                setboard[j + 3][2] = 0;
            }
        }
        for(int i = 3; i < 997; i++)
        {
            for(int j = 996; j >= 500; j--)
            {
                if(block[setboard[j + 3][i]].type == "block" && block[setboard[j - 3][i]].type == "block" && setboard[j][i] == 0 && setboard[j + 1][i] == 0 && setboard[j + 2][i] == 0 && setboard[j - 1][i] == 0  && setboard[j - 2][i] == 0)
                {
                    j += rand() % 5 - 2;
                    j = max(3, j);
                    j = min(996, j);
                    if(rand() % 500 <= 499)
                    {
                        setboard[j - 3][i] = 0;
                        setboard[j - 2][i - 1] = 0; setboard[j - 2][i] = 0; setboard[j - 2][i + 1] = 0;
                        setboard[j - 1][i - 2] = 0; setboard[j - 1][i - 1] = 0; setboard[j - 1][i] = 0; setboard[j - 1][i + 1] = 0; setboard[j - 1][i + 2] = 0;
                        setboard[j][i - 3] = 0; setboard[j][i - 2] = 0; setboard[j][i - 1] = 0; setboard[j][i] = 0; setboard[j][i + 1] = 0; setboard[j][i + 2] = 0; setboard[j][i + 3] = 0;
                        setboard[j + 1][i - 2] = 0; setboard[j + 1][i - 1] = 0; setboard[j + 1][i] = 0; setboard[j + 1][i + 1] = 0; setboard[j + 1][i + 2] = 0;
                        setboard[j + 2][i - 1] = 0; setboard[j + 2][i] = 0; setboard[j + 2][i + 1] = 0;
                        setboard[j + 3][i] = 0;
                        if(rand() % 20 == 1)
                        {
                            for(int k = 0; k < 100; k++)
                            {
                                if(mob[k].shap == "")
                                {
                                    mob[k].x = i;
                                    mob[k].y = j;
                                    break;
                                }
                            }
                        }
                    }
                }
                else if(block[setboard[j + 3][i]].type == "block" && block[setboard[j + 2][i]].type == "block" && block[setboard[j + 1][i]].type == "block" && block[setboard[j][i]].type == "block" && block[setboard[j - 1][i]].type == "block" && block[setboard[j - 2][i]].type == "block" && block[setboard[j - 3][i]].type == "block")
                {
                    if(rand() % 500 == 1)
                    {
                        setboard[j - 3][i] = 0;
                        setboard[j - 2][i - 1] = 0; setboard[j - 2][i] = 0; setboard[j - 2][i + 1] = 0;
                        setboard[j - 1][i - 2] = 0; setboard[j - 1][i - 1] = 0; setboard[j - 1][i] = 0; setboard[j - 1][i + 1] = 0; setboard[j - 1][i + 2] = 0;
                        setboard[j][i - 3] = 0; setboard[j][i - 2] = 0; setboard[j][i - 1] = 0; setboard[j][i] = 0; setboard[j][i + 1] = 0; setboard[j][i + 2] = 0; setboard[j][i + 3] = 0;
                        setboard[j + 1][i - 2] = 0; setboard[j + 1][i - 1] = 0; setboard[j + 1][i] = 0; setboard[j + 1][i + 1] = 0; setboard[j + 1][i + 2] = 0;
                        setboard[j + 2][i - 1] = 0; setboard[j + 2][i] = 0; setboard[j + 2][i + 1] = 0;
                        setboard[j + 3][i] = 0;
                    }
                }
            }
        }
        //出生点 
        bool a = false;
        for(int i = 400; i < 1000; i++)
        {
            for(int j = 0; j < 1000; j++)
            {
                if(setboard[j][i] == 0 && block[setboard[j + 1][i]].type == "block" && (setboard[j + 1][i] == 3 || setboard[j + 1][i] == 4))
                {
                    restartposy = j;
                    restartposx = i;
                    y = j;
                    x = i;
                    a = true;
                    break;
                }
            }
            if(a)
            {
                break;
            }
        }
        return 0;
    }
    int Arrowmove()
    {
        for(int i = 0; i < 100; i++)
        {
            if(arrow[i].shap == "")
            {
                continue;
            }
            arrow[i].x += (int)arrow[i].plusx;
            arrow[i].plusx -= 0.1;
            for(int j = (int)arrow[j].y + 1; j <= (int)arrow[j].y + (int)arrow[j].fallspeed; j++)
            {
                if(block[board[j][(int)arrow[j].x]].type == "block" || block[board[j][(int)arrow[j].x]].type == "fallblock")
                {
                    arrow[i].fallspeed = 0;
                    return 0;
                }
            }
            arrow[i].y += arrow[i].fallspeed;
            arrow[i].fallspeed += 0.1;
            if(arrow[i].y > 999)
            {
                arrow[i].shap = "";
            }
            if(block[board[(int)arrow[i].y + 1][(int)arrow[i].x]].type == "block" || block[board[(int)arrow[i].y + 1][(int)arrow[i].x]].type == "fallblock")
            {
                arrow[i].shap = "";
            }
        }
        return 0;
    }
    int mobmove()
    {
        for(int j = 0; j < 100; j++)
        {
            if(mob[j].shap == "")
            {
                continue;
            }
            mob[j].hurt = false;
            mob[j].color = 7;
            if(mob[j].health <= 0 || mob[j].y > 999)
            {
                mob[j].shap = "";
                mob[j].color = 7;
                continue;
            }
            if(block[board[mob[j].y][mob[j].x]].type == "lava")
            {
                mob[j].health -= 200;
                mob[j].hurt = true;
            }
            else if((board[mob[j].y + 1][mob[j].x] == 5) || ((mob[j].y == y || mob[j].y == y - 1) && (mob[j].x == x + 1 || mob[j].x == x || mob[j].x == x - 1) && KEY_DOWN(' ')))
            {
                mob[j].health -= 100;
                mob[j].hurt = true;
            }
            if(mob[j].y == y && mob[j].x == x && mob[j].isenemy)
            {
                health -= mob[j].attack;
                hurt = true;
                die = "被 " + mob[j].name + " 杀死了";
            }
            for(int i = mob[j].y - 1; i >= mob[j].y + mob[j].fallspeed; i--)
            {
                if(block[board[i - 1][mob[j].x]].type == "block")
                {
                    mob[j].fallspeed = 0 - mob[j].y + i + 1;
                    return 0;
                }
            }
            for(int i = mob[j].y + 1; i <= mob[j].y + mob[j].fallspeed; i++)
            {
                if(block[board[i][mob[j].x]].type == "block" || block[board[i][mob[j].x]].type == "fallblock")
                {
                    if(mob[j].fallspeed >= 5 && block[board[i - 1][mob[j].x]].type == "water")
                    {
                        mob[j].health -= 50 * (mob[j].fallspeed - 4);
                        mob[j].hurt = true;
                    }
                    mob[j].fallspeed = 0;
                    return 0;
                }
            }
            mob[j].y += mob[j].fallspeed;
            mob[j].fallspeed += gravity;
            if(mob[j].isenemy)
            {
                if(mob[j].y > y && (block[board[mob[j].y + 1][mob[j].x]].type == "block" || block[board[mob[j].y + 1][mob[j].x]].type == "fallblock"))
                {
                    if(mob[j].y > y + 50)
                    {
                        mob[j].fallspeed = -7;
                    }
                    mob[j].fallspeed = -3;
                }
                if(mob[j].x < x)
                {
                    if(block[board[mob[j].y][mob[j].x + 1]].type != "block" || block[board[mob[j].y][mob[j].x + 1]].type != "fallblock")
                    {
                        mob[j].x++;
                    }
                    else if(block[board[mob[j].y + 1][mob[j].x]].type == "block" || block[board[mob[j].y + 1][mob[j].x]].type != "fallblock")
                    {
                        mob[j].fallspeed = -3;
                    }
                }
                else if(mob[j].x > x)
                {
                    if(block[board[mob[j].y][mob[j].x - 1]].type != "block" || block[board[mob[j].y][mob[j].x - 1]].type != "fallblock")
                    {
                        mob[j].x--;
                    }
                    else if(block[board[mob[j].y + 1][mob[j].x]].type == "block" || block[board[mob[j].y + 1][mob[j].x]].type != "fallblock")
                    {
                        mob[j].fallspeed = -3;
                    }
                }
            }
            else
            {
                if(rand() % 3 == 0)
                {
                    if(block[board[mob[j].y][mob[j].x + 1]].type != "block" || block[board[mob[j].y][mob[j].x + 1]].type != "fallblock")
                    {
                        mob[j].x++;
                    }
                    else if(block[board[mob[j].y + 1][mob[j].x]].type == "block" || block[board[mob[j].y + 1][mob[j].x]].type != "fallblock")
                    {
                        mob[j].fallspeed = -3;
                    }
                }
                else if(rand() % 3 == 1)
                {
                    if(block[board[mob[j].y][mob[j].x - 1]].type != "block" || block[board[mob[j].y][mob[j].x - 1]].type != "fallblock")
                    {
                        mob[j].x--;
                    }
                    else if(block[board[mob[j].y + 1][mob[j].x]].type == "block" || block[board[mob[j].y + 1][mob[j].x]].type != "fallblock")
                    {
                        mob[j].fallspeed = -3;
                    }
                }
            }
            if(mob[j].hurt)
            {
                mob[j].color = 12;
            }
        }
        return 0;
    }
    int print()
    {
        if(!mode)
        {
            color(8);
            cout << "HP:";
            color(12);
            cout << health << endl; 
            for(int i = 0; i < health; i += 100)
            {
                cout << "◆";
            }
            color(7);
            for(int i = health; i < lasthealth; i += 100)
            {
                cout << "◇";
            }
            cout << endl;
            color(8);
            cout << "AIR:";
            color(11);
            cout << breath << endl;
            for(int i = 0; i < breath; i += 10)
            {
                cout << "●";
            }
            for(int i = breath; i < 91; i += 10)
            {
                cout << "○";
            }
            cout << endl;
            color(8);
            cout << "HUNGRY:";
            color(14);
            cout << hungry << endl;
            for(int i = 0; i < hungry; i += 100)
            {
                cout << "";
            }
            cout << endl;
        } 
        color(8);
        cout << "DIENUM:";
        color(4);
        cout << dienum << endl;
        for(int i = y - 6; i <= y + 6; i++)
        {
            for(int j = x - 6; j <= x + 6; j++)
            {
                bool ismob = false;
                for(int s = 0; s < 100; s++)
                {
                    if(mob[s].shap == "")
                    {
                        continue;
                    }
                    else if(mob[s].x == j && mob[s].y == i)
                    {
                        color(mob[s].color);
                        cout << mob[s].shap;
                        ismob = true;
                        break;
                    }
                }
                if(ismob)
                {
                    continue;
                }
                for(int s = 0; s < 100; s++)
                {
                    if(arrow[s].shap == "")
                    {
                        continue;
                    }
                    else if((int)arrow[s].x == j && (int)arrow[s].y == i)
                    {
                        color(7);
                        cout << arrow[s].shap;
                        ismob = true;
                        break;
                    }
                }
                if(ismob)
                {
                    continue;
                }
                else if(i == y && j == x)
                {
                    if (KEY_DOWN('S'))
                    {
                        color(14);
                        if(hurt)
                        {
                            color(12);
                        }
                        cout << "()";
                    }
                    else
                    {
                        color(9);
                        if(hurt)
                        {
                            color(12);
                        }
                        cout << "∏";
                    }
                }
                else if(i == y - 1 && j == x)
                {
                    if (!KEY_DOWN('S'))
                    {
                        color(14);
                        if(hurt)
                        {
                            color(12);
                        }
                        cout << "()";
                    }
                    else
                    {
                        color(block[board[i][j]].color);
                        cout << block[board[i][j]].ch;
                    }
                }
                else
                {
                    if(i < 0 || i >= 1000 || j < 0 || j >= 1000)
                    {
                        cout << "  ";
                        continue;
                    }
                    color(block[board[i][j]].color);
                    cout << block[board[i][j]].ch;
                }
            }
            cout << endl;
        }
        color(7);
        cout << "Y:";
        color(6);
        cout << 1000 - y << endl;
        color(7);
        cout << "X:";
        color(6);
        cout << x << endl;
        return 0;
    }
    int move()
    {
        if(board[y][x] == 31)
        {
            y += fallspeed;
            return 0;
        }
        if(block[board[y][x]].type == "water")
        {
            if(fallspeed > 1)
            {
                fallspeed = 1;
            }
        }
        else
        {
            for(int i = y - 1; i >= y + fallspeed; i--)
            {
                if(block[board[i - 1][x]].type == "block" || block[board[i - 1][x]].type == "fallblock")
                {
                    fallspeed = 0 - y + i + 1;
                    return 0;
                }
            }
        }
        for(int i = y + 1; i <= y + fallspeed; i++)
        {
            if(block[board[i][x]].type == "block" || block[board[i][x]].type == "fallblock")
            {
                if(fallspeed >= 5 && block[board[i - 1][x]].type != "water")
                {
                    health -= 50 * (fallspeed - 4);
                    if(fallspeed >= 7)
                    {
                        die = "落地过猛!";
                    }
                    else
                    {
                        die = "从高处摔了下来!";
                    }
                    hurt = true;
                }
                fallspeed = 0;
                return 0;
            }
        }
        y += fallspeed;
        fallspeed += gravity;
        return 0;
    }
    int bomb()
    {
        for(int i = 0; i < 20; i++)
        {
            if(tnt[i].time == 0)
            {
                int atk = 0;
                if(abs(x - tnt[i].x) + abs(y - tnt[i].y) == 4)
                {
                    atk = 50;
                    fallspeed -= 1;
                    x += x - tnt[i].x;
                }
                if(abs(x - tnt[i].x) + abs(y - tnt[i].y) == 3)
                {
                    atk = 100;
                    fallspeed -= 2;
                    x += x - tnt[i].x;
                }
                if(abs(x - tnt[i].x) + abs(y - tnt[i].y) == 2)
                {
                    atk = 300;
                    fallspeed -= 4;
                    x += 2 * (x - tnt[i].x);
                }
                if(abs(x - tnt[i].x) + abs(y - tnt[i].y) == 1)
                {
                    atk = 500;
                    fallspeed -= 7;
                    x += 5 * (x - tnt[i].x);
                }
                if(abs(x - tnt[i].x) + abs(y - tnt[i].y) == 0)
                {
                    atk = 800;
                    fallspeed -= 10;
                }
                tnt[i].time--;
                if(!tnt[i].issave)
                {
                    health -= atk;
                }
                die = "被TNT炸死了";
                if(atk > 0 && !tnt[i].issave)
                {
                    hurt = true;
                }
                if(!tnt[i].issave)
                {
                    for(int yy = tnt[i].y - 3; yy <= tnt[i].y + 3; yy++)
                    {
                        for(int xx = tnt[i].x - 3; xx <= tnt[i].x + 3; xx++)
                        {
                            if((int)(sqrt(abs(yy - tnt[i].y) * abs(yy - tnt[i].y) + abs(xx - tnt[i].x) * abs(xx - tnt[i].x)) + 0.9999) == 3)
                            {
                                setboard[yy][xx] = 28;
                            }
                            if((int)(sqrt(abs(yy - tnt[i].y) * abs(yy - tnt[i].y) + abs(xx - tnt[i].x) * abs(xx - tnt[i].x)) + 0.9999) < 3)
                            {
                                setboard[yy][xx] = 0;
                            }
                        }
                    }
                }
            }
            else if(tnt[i].time == -1)
            {
                setboard[tnt[i].y][tnt[i].x] = 0;
                if(!tnt[i].issave)
                {
                    for(int yy = tnt[i].y - 3; yy <= tnt[i].y + 3; yy++)
                    {
                        for(int xx = tnt[i].x - 3; xx <= tnt[i].x + 3; xx++)
                        {
                            if((int)(sqrt(abs(yy - tnt[i].y) * abs(yy - tnt[i].y) + abs(xx - tnt[i].x) * abs(xx - tnt[i].x)) + 0.9999) <= 3)
                            {
                                setboard[yy][xx] = 0;
                            }
                        }
                    }
                }
                tnt[i].time--;
            }
            else if(tnt[i].time > 0)
            {
                tnt[i].time--;
                if(tnt[i].time % 2 == 0)
                {
                    setboard[tnt[i].y][tnt[i].x] = 29;
                }
                else
                {
                    setboard[tnt[i].y][tnt[i].x] = 30;
                }
            }
        }
        return 0;
    }
    int check()
    {
        for(int i = 0; i < 1000; i++)
        {
            for(int j = 0; j < 1000; j++)
            {
                if(block[board[i][j]].type == "water" && board[i][j] != 15)
                {
                    if(board[i + 1][j] == 0)
                    {
                        setboard[i + 1][j] = board[i][j];
                        setboard[i + 1][j] = 8;
                    }
                    else if(block[board[i + 1][j]].type == "lava")
                    {
                        setboard[i + 1][j] = 2;
                    }
                    else if(block[board[i + 1][j]].type == "block" || block[board[i + 1][j]].type == "fallblock")
                    {
                        if(board[i][j + 1] == 0)
                        {
                            setboard[i][j + 1] = board[i][j] + 1;
                        }
                        else if(block[board[i][j + 1]].type == "lava")
                        {
                            setboard[i][j + 1] = 2;
                        }
                        if(board[i][j - 1] == 0)
                        {
                            setboard[i][j - 1] = board[i][j] + 1;
                        }
                        else if(block[board[i][j - 1]].type == "lava")
                        {
                            setboard[i][j - 1] = 2;
                        }
                    }
                }
                if(block[board[i][j]].type == "lava" && board[i][j] != 23)
                {
                    if(board[i + 1][j] == 0)
                    {
                        setboard[i + 1][j] = board[i][j];
                        setboard[i + 1][j] = 16;
                    }
                    else if(block[board[i + 1][j]].type == "water")
                    {
                        setboard[i + 1][j] = 2;
                    }
                    else if(block[board[i + 1][j]].type == "block" || block[board[i + 1][j]].type == "fallblock")
                    {
                        if(board[i][j + 1] == 0)
                        {
                            setboard[i][j + 1] = board[i][j] + 1;
                        }
                        else if(block[board[i][j + 1]].type == "water")
                        {
                            setboard[i][j + 1] = 2;
                        }
                        if(board[i][j - 1] == 0)
                        {
                            setboard[i][j - 1] = board[i][j] + 1;
                        }
                        else if(block[board[i][j - 1]].type == "water")
                        {
                            setboard[i][j - 1] = 2;
                        }
                    }
                }
                if(block[board[i][j]].type == "fallblock")
                {
                    if(board[i + 2][j] == 0 && board[i + 1][j] == 0 && i + 2 < 100)
                    {
                        setboard[i][j] = 0;
                        setboard[i + 2][j] = board[i][j];
                        if(board[i][j] == 7 && j == x && i + 2 == y)
                        {
                            health -= 600;
                            hurt = true;
                            die = "被压扁了";
                        }
                    }
                    else if(board[i + 1][j] == 0 && i + 1 < 100)
                    {
                        setboard[i][j] = 0;
                        setboard[i + 1][j] = board[i][j];
                        if(board[i][j] == 7 && j == x && i + 1 == y)
                        {
                            health -= 600;
                            hurt = true;
                            die = "被压扁了";
                        }
                        else if(board[i][j] == 7 && j == x && i + 2 == y)
                        {
                            health -= 600;
                            hurt = true;
                            die = "被压扁了";
                        }
                    }
                }
            }
        }
        for(int i = 0; i < 1000; i++)
        {
            for(int j = 0; j < 1000; j++)
            {
                board[i][j] = setboard[i][j];
            }
        }
    }
    int main()
    {
        srand((int)time(0));
        for(int i = 0; i < 20; i++)
        {
            tnt[i].time = -2;
        }
        init();
        while(1)
        {
    Clear_Screen();
    if(KEY_DOWN('L')){
        system("cls");
    }
            if(!mode)
            {
                move();
            } 
            if(KEY_DOWN('F'))
            {
                setboard[y + 1][x] = 2;
            }
            check();
            bomb();
            Arrowmove();
            mobmove();
            if(mode)
            {
                hurt = false;
            } 
            print();
            hungry--;
            hungry = max(hungry, 0);
            if(hungry == 0)
            {
                die = "被饿死了";
                hurt = true;
                health -= 10;
            }
            if(mode)
            {
                health = 1000;
            }
            if(health <= 0)
            {
                Sleep(500); 
                Clear_Screen();
                color(12);
                cout << "           GAME OVER           " << endl;
                color(7);
                cout << "   STEVE " << die << endl;
                dienum++;
                Sleep(2000);
                x = restartposx;
                y = restartposy;
                health = 1000;
                hungry = 1000;
                breath = 100;
                fallspeed = 0;
            }
            health += (hungry + 201) / 300; 
            if(health > 1000)
            {
                health = 1000; 
            } 
            if(attack)
            {
                attack = 0;
            }
            if(defense)
            {
                defense = 0;
            }
            if(hurt)
            {
                hurt = false;
                lasthealth = health;
            }
            if(block[board[y][x]].type == "water")
            {
                fallspeed = 1;
                if(KEY_DOWN('W'))
                {
                    fallspeed = -1;
                }
            }
            if(block[board[y - 1][x]].type == "water")
            {
                die = "被水淹死了!"; 
                breath--; 
            }
            else
            {
                breath++; 
                if(breath > 100)
                {
                    breath = 100; 
                } 
            } 
            if(board[y][x] == 31)
            {
                fallspeed = 0;
                if(KEY_DOWN('W'))
                {
                    fallspeed = -1;
                }
                if(KEY_DOWN('S'))
                {
                    fallspeed = 1;
                }
            }
            if(block[board[y][x]].type == "lava")
            {
                fallspeed = 1;
                if(KEY_DOWN('W'))
                {
                    fallspeed = -1;
                }
                die = "试图在岩浆里游泳!"; 
                hurt = true;
                health -= 100; 
            }
            if (KEY_DOWN('W') && !KEY_DOWN('S'))
            {
                if(mode)
                {
                    y--;
                }
                else
                {
                    if(block[board[y][x]].type != "water" && board[y][x] != 31)
                    {
                        if(block[board[y + 1][x]].type == "block" || block[board[y + 1][x]].type == "fallblock")
                        {
                            fallspeed = jumpspeed;
                        }
                    }
                }
            }
            if (KEY_DOWN('A'))
            {
                if((block[board[y][x - 1]].type != "block" && block[board[y][x - 1]].type != "fallblock") || mode)
                {
                    if(KEY_DOWN('S') || (block[board[y - 1][x - 1]].type != "block" && block[board[y - 1][x - 1]].type != "fallblock") || mode)
                    {
                        x -= 1;
                    }
                }
                face = -1;
            }
            if (KEY_DOWN('D'))
            {
                if((block[board[y][x + 1]].type != "block" && block[board[y][x + 1]].type != "fallblock") || mode)
                {
                    if(KEY_DOWN('S') || (block[board[y - 1][x + 1]].type != "block"  && block[board[y - 1][x + 1]].type != "fallblock") || mode)
                    {
                        x += 1;
                    }
                }
                face = 1;
            }
            if(KEY_DOWN('S'))
            {
                if(mode)
                {
                    y++;
                }
                else
                {
                    fallspeed += 1;
                }
            }
            if(KEY_DOWN(' '))
            {
                attack = true;
            }
            if(KEY_DOWN('E'))
            {
                Clear_Screen();
                color(7);
                cout << "┌──┬──┬──┬──┬──┬──┬──┬──┬──┬──┐" << endl;
                for(int i = 0; i < 9; i++)
                {
                    color(7);
                    cout << "│";
                    for(int j = 0; j < 10; j++)
                    {
                        color(block[bag[i * 10 + j]].color);
                        cout << block[bag[i * 10 + j]].ch;
                        color(7);
                        cout << "│";
                    }
                    cout << endl;
                    color(7);
                    cout << "├──┼──┼──┼──┼──┼──┼──┼──┼──┼──┤" << endl;
                }
                color(7);
                cout << "│";
                for(int j = 0; j < 10; j++)
                {
                    color(block[bag[90 + j]].color);
                    cout << block[bag[90 + j]].ch;
                    color(7);
                    cout << "│";
                }
                color(7);
                cout << endl;
                cout << "└──┴──┴──┴──┴──┴──┴──┴──┴──┴──┘" << endl;
                Sleep(3000);
            }
            if(KEY_DOWN('C'))
            {
                hungry += 10000;
                hungry = min(hungry, 114514);
            }
            if(KEY_DOWN('Q'))
            {
                for(int i = 0; i < 20; i++)
                {
                    if(tnt[i].time == -2)
                    {
                        tnt[i] = {y, x, 10, 0};
                        break;
                    }
                }
            }
            if(KEY_DOWN('i'))
            {
                for(int i = 0; i < 20; i++)
                {
                    if(tnt[i].time == -2)
                    {
                        tnt[i] = {y, x, 10, 1};
                        break;
                    }
                }
            }
            if(KEY_DOWN('8'))
            {
                for(int i = 0; i < 20; i++)
                {
                    if(tnt[i].time == -2)
                    {
                        tnt[i] = {y, x, 10, 2};
                        break;
                    }
                }
            }
            if(KEY_DOWN('9'))
            {
                for(int i = 0; i < 20; i++)
                {
                    if(tnt[i].time == -2)
                    {
                        tnt[i] = {y, x, 10, 3};
                        break;
                    }
                }
            }
            if(KEY_DOWN('0'))
            {
                for(int i = 0; i < 20; i++)
                {
                    if(tnt[i].time == -2)
                    {
                        tnt[i] = {y, x, 10, 4};
                        break;
                    }
                }
            }
            if(KEY_DOWN('I'))
            {
                for(int i = 0; i < 20; i++)
                {
                    if(tnt[i].time == -2)
                    {
                        tnt[i] = {y, x, 10, 5};
                        break;
                    }
                }
            }
            if(KEY_DOWN('Z'))
            {
                setboard[y + 1][x] = 0;
            }
            if(KEY_DOWN('X'))
            {
                setboard[y][x] = 24;
            }
            if(KEY_DOWN('3'))
            {
                setboard[y + 1][x] = 6;
            }
            if(KEY_DOWN('4'))
            {
                setboard[y + 1][x] = 7;
            }
            if(KEY_DOWN('1'))
            {
                setboard[y + 1][x] = 8;
            }
            if(KEY_DOWN('2'))
            {
                setboard[y + 1][x] = 16;
            }
            if(KEY_DOWN('5'))
            {
                if(face == 1)
                {
                    for(int i = 0; i < 100; i++)
                    {
                        if(arrow[i].shap == "")
                        {
                            arrow[i] = {"→",y,x,-0.7,2};
                            break; 
                        }
                    }
                }
                else
                {
                    for(int i = 0; i < 100; i++)
                    {
                        if(arrow[i].shap == "")
                        {
                            arrow[i] = {"←",y,x,-0.7,-2};
                            break;
                        }
                    }
                }
            }
            if(KEY_DOWN('T'))
            {
                clear_buffer();
                Clear_Screen();
                cout << "请输入指令:" << endl;
                string a;
                cin >> a;
                if(a == "kill")
                {
                    die = "失败了。";
                    hurt = true;
                    health = 0; 
                }
                if(a == "full_health")
                {
                    health = 1000; 
                }
                if(a == "creativemode")
                {
                    mode = !mode;
                }
                if(a == "move")
                {
                    cin >> y >> x;
                    y = 1000 - y;
                }
                if(a == "summom")
                {
                    int a;
                    cin >> a;
                    for(int i = 0; i < 100; i++)
                    {
                        if(mob[i].shap == "")
                        {
                            mob[i] = mobs[a];
                            mob[i].x = x;
                            mob[i].y = y;
                            break;
                        }
                    }
                }
            }
            if(KEY_DOWN('O'))
            {
                for(int i = 0; i < 1000; i++)
                {
                    for(int j = 0; j < 1000; j++)
                    {
                        setboard[i][j] = 0;
                    }
                }
            }
            if(KEY_DOWN('P'))
            {
                for(int i = 0; i < 1000; i++)
                {
                    for(int j = 0; j < 1000; j++)
                    {
                        setboard[i][j] = 2;
                    }
                }
            }
            if(y > 1000)
            {
                die = "掉出了这个世界!";
                hurt = true;
                health -= 200; 
            }
            if(breath <= 0)
            {
                breath = 0;
                hurt = true;
                health -= 10; 
            }
            if(!KEY_DOWN('S') && block[board[y - 1][x]].type == "block")
            {
                die = "在墙里窒息死亡!";
                hurt = true;
                health -= 50;
            }
            if(board[y + 1][x] == 5)
            {
                hurt = true;
                die = "发现了地板是熔岩做的。";
                health -= 30; 
            }
            clear_buffer();
        }
        return 0;
    }
    

    MC不灭!

  • 通过的题目

  • 最近活动

  • 最近编写的题解

题目标签

语言基础
146
循环语句
73
其他
43
选择语句
36
一维数组
32
字符串
29
语言入门
23
字符数组
23
竞赛
22
递归
19
NOIP
19
二维数组
19
排序
16
位运算
15
贪心
12
普及组
11
搜索
10
基础语法
8
输入输出
8
模拟
7