打摆器

· · 休闲·娱乐

打钢琴井号块练习器。

键位:12/90,对应从左到右四列。可在 109~120 修改键位对应 ASCLL。

:::info[Linux环境]

在终端下直接编译即可。

#include <bits/stdc++.h>
#include <termio.h>
#include <stdio.h>
using namespace std;
int getch(void)
{
    struct termios tm, tm_old;
    int fd = 0, ch;

    if (tcgetattr(fd, &tm) < 0) {
        return -1;
    }

    tm_old = tm;
    cfmakeraw(&tm);
    if (tcsetattr(fd, TCSANOW, &tm) < 0) {
        return -1;
    }

    ch = getchar();
    if (tcsetattr(fd, TCSANOW, &tm_old) < 0) {
        return -1;
    }

    return ch;
}
int c[200][4];
int len = 100, id = 1;
int score = 0;
void init() {
    int doublecnt = 0;
    for (int i = 100; i >= 1; i--) {
        vector <int> num;
        for (int j = 0; j <= 3; j++) {
            if (!c[i + 1][j]) {
                num.push_back(j);
            }
        }
        if ((rand() & 1) && doublecnt < 20) {
            int id1 = rand() % num.size(), id2 = rand() % num.size();
            while (id2 == id1) {
                id2 = rand() % num.size();
            }
            c[i][num[id1]] = c[i][num[id2]] = 1;
            doublecnt++;
        }else {
            c[i][num[rand() % num.size()]] = 1;
           // cout << i <<":" << id1 << " " << id2 << endl;
        }
    }
    vector <pair <int, int> > emp;
    for (int i = 1; i <= 100; i++) {
        for (int j = 0; j <= 3; j++) {
            if (!c[i][j]) {
                emp.push_back({i, j});
            }
        }
    }
    // for (int i = 1; i <= 20; i++) {//添加x障碍
    //     int id = rand() % emp.size();
    //     c[emp[id].first][emp[id].second] = 2;
    // }
    return;
}
void print(double cps, double time) {
    system("clear");
    printf("score : %d\n", score);
    int baifen = id;
    for (int i = 1; i <= id / 10; i++) {
        printf("$");
    }printf("%d", id - 1);
    putchar('%');putchar('\n');
    int E = 12;
    for (int i = id + 5; i >= id; i--) {
        printf("|");
        for (int j = 0; j <= 3; j++) {
            if (c[i][j] == 1) {
                printf("##");
            }
            else if (c[i][j]) {
                printf("\\/");
            }
            else {
                printf("  ");
            }
        }
        printf("|");
        E--;
        if (cps > E) {
            printf("  ^");
        }
        printf("\n|");
        for (int j = 0; j <= 3; j++) {
            if (c[i][j] == 1) {
                printf("##");
            }
            else if (c[i][j]) {
                printf("/\\");
            }
            else {
                printf("  ");
            }
        }
        printf("|");
        E--;
        if (!E)printf(" kps: %.4lf", cps);
        else if (cps > E)printf("  ^");
        printf("\n");
    }
    printf("\n%.4lfs\n", time);
    return;
}
int change(int x) {
    if (x == 49) {
        return 0;
    }
    if (x == 50) {
        return 1;
    }
    if (x == 57) {
        return 2;
    }
    return 3;
}
void solve1() {
    init();
    print(0, 0);
    int ch = getch();
    auto start = chrono::high_resolution_clock::now();
    while (1) {
        if (ch == 49 || ch == 50 || ch == 57 || ch == 48) {
            if (c[id][change(ch)] == 1) {
                c[id][change(ch)] = 0;
                score++;
                if (c[id][0] != 1 && c[id][1] != 1 && c[id][2] != 1 && c[id][3] != 1) {
                    id++;
                    if (id > len) {
                        auto end = chrono::high_resolution_clock::now();
                        auto duration = chrono::duration_cast<chrono::microseconds>(end - start).count();
                        double cps = score / (duration * 1.0 / 1000000.0), tim = duration * 1.0 / 1000000.0;
                        print(cps, tim);
                        cout << "Accept!\n";
                        break;
                    }
                }
            }else {
                auto end = chrono::high_resolution_clock::now();
                auto duration = chrono::duration_cast<chrono::microseconds>(end - start).count();
                double cps = score / (duration * 1.0 / 1000000.0), tim = duration * 1.0 / 1000000.0;
                print(cps, tim);
                cout << "Wrong!\n";
                break;
            }
        }
        auto end = chrono::high_resolution_clock::now();
        auto duration = chrono::duration_cast<chrono::microseconds>(end - start).count();
        double cps = score / (duration * 1.0 / 1000000.0), tim = duration * 1.0 / 1000000.0;
        print(cps, tim);
        ch = getch();
    }
    return;
}
void solve2() {
    cout << "暂未开放!\n";
    int ch = getch();
    return;
}
void solve3() {
    return;
}
signed main() {
    srand(time(NULL));
    solve1();
    // while (1) {
    //     system("clear");
    //     cout << "#----------------#\n";
    //     cout << "|  Piano blocks  |\n";
    //     cout << "#----------------#\n";
    //     cout << "|   1.经典模式   |\n";
    //     cout << "#----------------#\n";
    //     cout << "|   2.无尽模式   |\n";
    //     cout << "#----------------#\n";
    //     cout << "|    3.自定义    |\n";
    //     cout << "#----------------#\n";
    //     cout << "|   4.退出游戏   |\n";
    //     cout << "#----------------#\n";
    //     int ch = getch();
    //     while (ch != 49 && ch != 50 && ch != 51 && ch != 52) {
    //         ch = getch();
    //     }
    //     if (ch == 49) {
    //         solve1();
    //     }else if (ch == 50) {
    //         solve2();
    //     }else if (ch == 51) {
    //         solve3();
    //     }else {
    //         break;
    //     }
    // }
    return 0;
}

:::

:::info[Windows环境]

编译后运行 exe 文件。

#include <bits/stdc++.h>
#include <windows.h>
#include <stdio.h>
#include <fstream>
#include <conio.h>
#include <time.h>
#include <random>
using namespace std;

inline void SetPos(int x,int y)
{
    COORD pos;
    pos.X=y*2,pos.Y=x;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}

inline void HideCursor() {
    CONSOLE_CURSOR_INFO cursor_info = {1, 0};
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

int c[200][4];
int len = 100, id = 1;
int score = 0;
void init() {
    int doublecnt = 0;
    for (int i = 100; i >= 1; i--) {
        vector <int> num;
        for (int j = 0; j <= 3; j++) {
            if (!c[i + 1][j]) {
                num.push_back(j);
            }
        }
        if ((rand() & 1) && doublecnt < 20) {
            int id1 = rand() % num.size(), id2 = rand() % num.size();
            while (id2 == id1) {
                id2 = rand() % num.size();
            }
            c[i][num[id1]] = c[i][num[id2]] = 1;
            doublecnt++;
        }else {
            c[i][num[rand() % num.size()]] = 1;
           // cout << i <<":" << id1 << " " << id2 << endl;
        }
    }
    vector <pair <int, int> > emp;
    for (int i = 1; i <= 100; i++) {
        for (int j = 0; j <= 3; j++) {
            if (!c[i][j]) {
                emp.push_back({i, j});
            }
        }
    }
    // for (int i = 1; i <= 20; i++) {//添加x障碍
    //     int id = rand() % emp.size();
    //     c[emp[id].first][emp[id].second] = 2;
    // }
    return;
}
void print(double cps, double time) {
    system("cls");
    SetPos(1, 1);
    printf("score : %d\n", score);
    int baifen = id;
    for (int i = 1; i <= id / 10; i++) {
        putchar('$');
    }printf("%d", id - 1);
    putchar('%');putchar('\n');
    int E = 12;
    for (int i = id + 5; i >= id; --i) {
        putchar('|');
        for (int j = 0; j <= 3; ++j) {
            if (c[i][j] == 1) {
                putchar('#');putchar('#');
            }
            else if (c[i][j]) {
                printf("\\/");
            }
            else {
                putchar(' ');putchar(' ');
            }
        }
        putchar('|');
        E--;
        if (cps > E) {
            printf("  ^");
        }
        printf("\n|");
        for (int j = 0; j <= 3; j++) {
            if (c[i][j] == 1) {
                putchar('#');putchar('#');
            }
            else if (c[i][j]) {
                printf("/\\");
            }
            else {
                putchar(' ');putchar(' ');
            }
        }
        putchar('|');
        E--;
        if (!E)printf(" cps: %.4lf", cps);
        else if (cps > E)printf("  ^");
        printf("\n");
    }
    printf("\n%.4lfs\n", time);
    return;
}
int change(int x) {
    if (x == 49) {
        return 0;
    }
    if (x == 50) {
        return 1;
    }
    if (x == 57) {
        return 2;
    }
    return 3;
}
void solve1() {
    init();
    print(0, 0);
    int ch = getch();
    DWORD start = GetTickCount();
    while (1) {
        if (ch == 49 || ch == 50 || ch == 57 || ch == 48) {
            if (c[id][change(ch)] == 1) {
                c[id][change(ch)] = 0;
                score++;
                if (c[id][0] != 1 && c[id][1] != 1 && c[id][2] != 1 && c[id][3] != 1) {
                    id++;
                    if (id > len) {
                        DWORD end = GetTickCount();
                        DWORD duration = end - start;
                        double cps = score / (duration * 1.0 / 1000.0), tim = duration * 1.0 / 1000.0;
                        print(cps, tim);
                        cout << "Accept!\n";
                    Sleep(100000);
                        break;
                    }
                }
            }else {
                DWORD end = GetTickCount();
                DWORD duration = end - start;
                double cps = score / (duration * 1.0 / 1000.0), tim = duration * 1.0 / 1000.0;
                print(cps, tim);
                cout << "Wrong!\n";
                Sleep(1000);
                break;
            }
        }
        DWORD end = GetTickCount();
        DWORD duration = end - start;
        double cps = score / (duration * 1.0 / 1000.0), tim = duration * 1.0 / 1000.0;
        print(cps, tim);
        ch = getch();
    }
    return;
}
void solve2() {
    cout << "暂未开放!\n";
    int ch = getch();
    return;
}
void solve3() {
    return;
}
signed main() {
    HideCursor();
    srand(time(NULL));
    solve1();
    // while (1) {
    //     system("clear");
    //     cout << "#----------------#\n";
    //     cout << "|  Piano blocks  |\n";
    //     cout << "#----------------#\n";
    //     cout << "|   1.经典模式   |\n";
    //     cout << "#----------------#\n";
    //     cout << "|   2.无尽模式   |\n";
    //     cout << "#----------------#\n";
    //     cout << "|    3.自定义    |\n";
    //     cout << "#----------------#\n";
    //     cout << "|   4.退出游戏   |\n";
    //     cout << "#----------------#\n";
    //     int ch = getch();
    //     while (ch != 49 && ch != 50 && ch != 51 && ch != 52) {
    //         ch = getch();
    //     }
    //     if (ch == 49) {
    //         solve1();
    //     }else if (ch == 50) {
    //         solve2();
    //     }else if (ch == 51) {
    //         solve3();
    //     }else {
    //         break;
    //     }
    // }
    return 0;
}

:::

未完待续。