小游戏——AK IOI

· · 休闲·娱乐

你能 AK IOI 吗?

修改了一些 Bug,保证游玩体验。
字数都快 10000 了,点个赞吧 QAQ~

注意事项

  1. 打开 Dev-C++,点开最上方的工具[T] -> 编译选项,编译时加入: -O2 -std=c++14 -DONLINE_JUDGE
  2. 建一个文件夹,命名为 AK IOI;
  3. 把下面的代码存在 Main.cpp 中;
  4. 建一个名为 example 的 .txt 文件;
  5. 开玩!

代码

#include <fstream>
#include <conio.h>
#include <windows.h>
#include <bits/stdc++.h>;
using namespace std;
int best, point; // 记录 & 分数 

void cd(int best) { // 存档 
    FILE* fp = fopen("example.txt", "wb");
    fprintf(fp, "%d", best);
    fclose(fp);
}
void dd() { // 读档 
    FILE* fp = fopen("example.txt", "rb");
    fscanf(fp, "%d", &best);
    fclose(fp);
}
char kb() { // 读键位 
    if(_kbhit()) { char key = _getch(); return key; }
    else return '_';
}

void gotoxy(int x, int y) {
    COORD pos = {x, y};
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hOut, pos);
}
void clear() { gotoxy(0, 0); } // 无闪清屏

void setSize(unsigned uCol, unsigned uLine) {
    char cmd[64];
    sprintf(cmd, "mode con cols=%d lines=%d", uCol, uLine);
    system(cmd);
}
void hideCursor() {
    CONSOLE_CURSOR_INFO cursor_info = {1, 0};
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

void SetConsoleFontSize(int width, int height) {
    CONSOLE_FONT_INFOEX cfi;
    cfi.cbSize = sizeof(cfi);
    cfi.nFont = 0;
    cfi.dwFontSize.X = width;
    cfi.dwFontSize.Y = height;
    cfi.FontFamily = FF_DONTCARE;
    cfi.FontWeight = FW_NORMAL;
    wcscpy(cfi.FaceName, L"Hack");
    SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
}

void setColor(int color) {
    if(color == 0) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); // 亮白 
    else if(color == 1) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE); // 蓝 
    else if(color == 2) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN); // 绿 
    else if(color == 3) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE); // 紫(啧~) 
    else if(color == 4) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED); // 红 
    else if(color == 5) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN); // 黄 
    else if(color == 6) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE); // 深蓝 
    else if(color == 7) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN); // 只因黄 
    else if(color == 8) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); // 灰白 
}

int chessboard[6][6]; // 棋盘 
int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, -1, 1, 0}; // 移动方向 
map<int, string> mapping = {{0, "   "}, {2, "C E"}, {4, "..."}, {8, "R E"}, {16, "TLE"}, {32, "MLE"}, {64, "ILE"}, {128, "OLE"}, {256, "UKE"}, {512, "W A"}, {1024, "P C"}, {2048, "A C"}, {4096, "P E"}, {8192, "DoJ"}, {16384, "J F"}, {32768, "A U"}, {65536, "A K"}}; // 数值 -> 评测状态
map<char, int> moveMap = {{'W', 0}, {'A', 1}, {'D', 2}, {'S', 3}, {' ', 114514}}; // 键位 -> dx & dy 下标
void generate(int _x, int _y) { // 按概率生成方块
    int chance = rand() % 10000 + 1;
    if(chance <= 8800) chessboard[_x][_y] = 2;
    else if(chance <= 10000) chessboard[_x][_y] = 4;
}

pair<int, int> choose() { // 随机选择生成方块的位置 
    vector<pair<int, int> > pos;
    for(int i = 1;i <= 4;i++)
        for(int j = 1;j <= 4;j++)
            if(chessboard[i][j] == 0)
                pos.push_back({i, j});
    if(pos.empty()) return {0, 0};
    int chosenPos = rand() % pos.size();
    return pos[chosenPos];
}

void initialization() { // 初始化棋盘 
    memset(chessboard, 0, sizeof(chessboard));
    for(int i = 1;i <= 4;i++)
        chessboard[0][i] = chessboard[i][0] = chessboard[5][i] = chessboard[i][5] = 114514;
    int initialX1, initialX2, initialY1, initialY2;
    initialX1 = rand() % 4 + 1;
    initialX2 = rand() % 4 + 1;
    initialY1 = rand() % 4 + 1;
    initialY2 = rand() % 4 + 1;
    if(initialX1 == initialX2 && initialY1 == initialY2) { // 判两点相同 
        initialX2 = (initialX2 == 4 ? 1 : initialX2 + 1);
        initialY2 = (initialY2 == 1 ? 4 : initialY2 - 1);
    }
    generate(initialX1, initialY1);
    generate(initialX2, initialY2);
}

void draw() { // 显示:棋盘
    setColor(5);
    cout << "AK IOI   版本号:1.8" << endl << endl;
    setColor(0);
    cout << "合并评测状态并得到 AK!" << endl;
    cout << "WSAD  推动方块并合成          " << endl;
    cout << "Space  回炉重造          " << endl;
    cout << "记得切换为 英文 输入法!" << endl << endl;
    setColor(5);
    cout << "分数:" << point << "  记录:" << best << "          " << endl;
    setColor(1);
    cout << "+-----------------------+" << endl;
    for(int i = 1;i <= 4;i++) {
        cout << "|";
        for(int j = 1;j <= 4;j++) {
            string state = mapping[chessboard[i][j]];
            int stateLen = state.size();
            cout << " " << state << " |";
        }
        if(i <= 3)
            cout << endl << "|-----------------------|";
        cout << endl;
    }
    cout << "+-----------------------+" << endl << endl;
}

void print() { // 显示: 数值 -> 评测状态
    cout << "+-------------------------------------------------------+" << endl;
    cout << "|  _CE 00002  |  ... 00004  |  _RE 00008  |  TLE 00016  |" << endl;
    cout << "|-------------------------------------------------------|" << endl;
    cout << "|  MLE 00032  |  ILE 00064  |  OLE 00128  |  UKE 00256  |" << endl;
    cout << "|-------------------------------------------------------|" << endl;
    cout << "|  _WA 00512  |  _PC 01024  |  _AC 02048  |  _PE 04096  |" << endl;
    cout << "|-------------------------------------------------------|" << endl;
    cout << "|  DoJ 08192  |  _JF 16384  |  _AU 32768  |  _AK 65536  |" << endl;
    cout << "+-------------------------------------------------------+" << endl;
}

bool canPush() { // 判 GameOver 
    for(int i = 1;i <= 4;i++)
        for(int j = 1;j <= 4;j++)
            if(!chessboard[i][j]) return 1;
            else for(int k = 0;k < 4;k++)
                    if(chessboard[i][j] == chessboard[i + dx[k]][j + dy[k]]) return 1;
    return 0;
}

int getDirection() { // 获取移动方向 
    char c;
    while(true) {
        c = kb();
        if('a' <= c && c <= 'z') c -= ' ';
        if(c == 'W' || c == 'S' || c == 'A' || c == 'D') break;
        if(c == ' ') break;
    }
    return moveMap[c];
}

void push(int direction) { // 推动方块 
    for(int i = 1;i <= 4;i++)
        for(int j = 1;j <= 4;j++) {
            int _x = i + dx[direction];
            int _y = j + dy[direction];
            if(chessboard[_x][_y] == 0) {
                chessboard[_x][_y] = chessboard[i][j];
                chessboard[i][j] = 0;
            }
        }
}

void mergeW() { // 向上 
    for(int j = 1;j <= 4;j++)
        for(int i = 1;i < 4;i++)
            if(chessboard[i][j] == chessboard[i + 1][j]) {
                chessboard[i][j] *= 2, chessboard[i + 1][j] = 0;
                point += chessboard[i][j], i++;
            }
}

void mergeA() { // 向左 
    for(int i = 1;i <= 4;i++)
        for(int j = 1;j < 4;j++)
            if(chessboard[i][j] == chessboard[i][j + 1]) {
                chessboard[i][j] *= 2, chessboard[i][j + 1] = 0;
                point += chessboard[i][j], j++;
            }
}

void mergeD() { // 向右 
    for(int i = 1;i <= 4;i++)
        for(int j = 4;j > 1;j--)
            if(chessboard[i][j] == chessboard[i][j - 1]) {
                chessboard[i][j] *= 2, chessboard[i][j - 1] = 0;
                point += chessboard[i][j], j--;
            }
}

void mergeS() { // 向前看(划掉)向下 
    for(int j = 1;j <= 4;j++)
        for(int i = 4;i > 1;i--)
            if(chessboard[i][j] == chessboard[i - 1][j]) {
                chessboard[i][j] *= 2, chessboard[i - 1][j] = 0;
                point += chessboard[i][j], i--;
            }
}

void merge(int direction) { // 合并方块
    if(direction == 0) mergeW();
    else if(direction == 1) mergeA();
    else if(direction == 2) mergeD();
    else if(direction == 3) mergeS();
}

int play() { // 游戏过程 
    initialization();
    draw(), print();
    while(canPush()) {
        int dir = getDirection();
        if(dir == 114514) return 114514;
        for(int i = 1;i <= 10;i++) push(dir);
        merge(dir);
        for(int i = 1;i <= 10;i++) push(dir);
        best = max(point, best);
        clear(), cd(best);
        draw(), print();
        for(int i = 1;i <= 4;i++)
            for(int j = 1;j <= 4;j++)
                if(chessboard[i][j] == 65536)
                    return 1;
        pair<int, int> pos = choose();
        generate(pos.first, pos.second);
        clear();
        draw(), print();
    }
    return 0;
}

int main() {
    hideCursor();
    SetConsoleFontSize(50, 50);
    setSize(100, 60);
    system("color 03");
    system("title AK IOI!");
    srand(time(NULL)), dd();
    while(true) { // 重复游戏
        system("cls");
        point = 0;
        setColor(5);
        cout << "AK IOI   版本号:1.8" << endl << endl;
        setColor(0);
        cout << "合并评测状态并得到 AK!" << endl;
        cout << "记得切换为 英文 输入法!" << endl;
        cout << "F  开始游戏" << endl;
        cout << "B  退出游戏" << endl;
        setColor(1);
        bool startOrBack;
        while(true) {
            char c = kb();
            if(c == 'F' || c == 'f') { startOrBack = 1; break; }
            else if(c == 'B' || c == 'b') { startOrBack = 0; break; }
        }
        if(startOrBack) {
            MessageBox(NULL, "即将开始游戏!", "开始游戏", MB_OK);
            Again: point = 0;
            clear();
            int isAKIOI = play();
            if(isAKIOI != 114514) Sleep(1145.14), setColor(4);
            if(isAKIOI == 1) cout << "You AK IOI!!!" << endl;
            else if(isAKIOI == 0) cout << "Game Over." << endl;
            else if(isAKIOI == 114514) goto Again;
            Sleep(2500); //间隔时间两 s 半 
            continue;
        }
        else {
            if(MessageBox(NULL, "是否退出游戏?", "退出游戏", MB_YESNO) == IDYES) break;
            else continue;
        }
    }
    cout << "游戏结束,Bye bye~" << endl;
    Sleep(1145.14);
    cout << "作者:fumanke" << endl;
    Sleep(1145.14);
    setColor(0);
    return 0;
}