编辑代码

#include <iostream>
#include <string.h>
// #include "levels.h"  // 包含关卡定义头文件
using namespace std;

// 定义关卡结构
struct Level {
    int width;
    int height;
    char wi_ch[20];
    char he_ch[20];
    char map_ch[20][20];
    char target_ch[20][20];
};

// 定义关卡数组
Level levels[] = {

    // 关卡 1
    {
        4, 3,
        {' ', 'B','B','B','B'},
        {' ', 'A','A','A'},
        {
            {},
            {' ', 'O','O','O','O'},
            {' ', 'O','O','O','O'},
            {' ', 'O','O','O','O'}
        },
        {
            {},
            {' ', 'A','A','A','A'},
            {' ', 'A','B','B','A'},
            {' ', 'A','A','A','A'}
        }
    },

    // 关卡 2
    {
        5, 4,
        {' ', 'C','C','C','C','C'},
        {' ', 'D','D','D','D'},
        {
            {},
            {' ', 'O','O','O','O','O'},
            {' ', 'O','O','O','O','O'},
            {' ', 'O','O','O','O','O'},
            {' ', 'O','O','O','O','O'}
        },
        {
            {},
            {' ', 'D','D','D','D','D'},
            {' ', 'D','C','C','C','D'},
            {' ', 'D','C','C','C','D'},
            {' ', 'D','D','D','D','D'}
        }
    },

    // 关卡 3
    {
        3, 3,
        {' ', 'X','X','X'},
        {' ', 'Y','Y','Y'},
        {
            {},
            {' ', 'O','O','O'},
            {' ', 'O','O','O'},
            {' ', 'O','O','O'}
        },
        {
            {},
            {' ', 'Y','Y','Y'},
            {' ', 'Y','X','Y'},
            {' ', 'Y','Y','Y'}
        }
    }

};

int width, height;
char wi_ch[20], he_ch[20], target_ch[20][20], map_ch[20][20];

bool check_win();
bool check_string(string s, int low, int upp);
void print_target();
void print_map();
void copy_arr(char a[], char b[], int l);

int main() {
    string c;
    int n, step = 0, level = 0; // level从0开始
    bool wrong_inp = 0, is_win = 0;
    // 获取当前关卡信息
    width = levels[level].width;
    height = levels[level].height;
    copy_arr(wi_ch,levels[level].wi_ch, width);
    copy_arr(he_ch,levels[level].he_ch, height);
    for (int i = 1;i <= height;i++) {
        for (int j = 1;j <= width;j++) {
            map_ch[i][j] = levels[level].map_ch[i][j];
        }
    }
    for (int i = 1;i <= height;i++) {
        for (int j = 1;j <= width;j++) {
            target_ch[i][j] = levels[level].target_ch[i][j];
        }
    }
    while (step <= 1000) {
        if (level+1 >= sizeof(levels)/sizeof(Level)) { // 如果完成所有关卡
            is_win = 1;
            break;
        }
        system("clear");
        if (wrong_inp == 1) {
            cout << "请输入图中对应标出的行列哦" << endl;
            wrong_inp = 0;
        }

        print_target(); // 更新print_target函数参数
        print_map(); // 更新print_map函数参数
        cout << "第 " << level + 1 << " 幅画作" << endl; // 显示关卡号(从1开始)
        
        if (check_win()) { // 更新check_win函数参数
            cout << "恭喜您用"<<step<<"步完成了画作"<<endl;
            cout << "任意字符加回车键开启下一幅画作...";
            cin >> c;
            level++;
            // 获取当前关卡信息
            width = levels[level].width;
            height = levels[level].height;
            copy_arr(wi_ch,levels[level].wi_ch, width);
            copy_arr(he_ch,levels[level].he_ch, height);
            for (int i = 1;i <= height;i++) {
                for (int j = 1;j <= width;j++) {
                    map_ch[i][j] = levels[level].map_ch[i][j];
                }
            }
            for (int i = 1;i <= height;i++) {
                for (int j = 1;j <= width;j++) {
                    target_ch[i][j] = levels[level].target_ch[i][j];
                }
            }
            step = 0;
            continue;
        }
        cout << "输入你要涂刷的行列值(输入0退出):";
        cin >> c;
        if (c == "0") {
            break;
        }
        if (!check_string(c, height+width)) {
            wrong_inp = 1;
            continue;
        }
        step++;
        n = c - '0';
        if (1 <= n && n <= height) {
            for (int i = 1; i <= width; i++) {
                map_ch[n][i] = he_ch[n];
            }
        } else {
            for (int i = 1; i <= height; i++) {
                map_ch[i][n - height] = wi_ch[n - height];
            }
        }
    }
    if (is_win) {
        cout << "恭喜您完成所有画作" << endl;
    }
    else {
        cout << "再接再厉,你已经完成了" <<level<<"幅画作"<< endl;
    }
    cout << "任意字符加回车键结束...";
    cin >> c;
    return 0;
}

// 更新函数以接受参数
bool check_win() {
    for (int h = 1; h <= height; h++) {
        for (int w = 1; w <= width; w++) {
            if (map_ch[h][w] != target_ch[h][w]) {
                return false;
            }
        }
    }
    return true;
}

#include <string>
#include <sstream>
#include <limits>

using namespace std;

bool check_string(const string& s, int low, int upp) {
    // 创建一个字符串流对象并初始化为输入字符串
    istringstream iss(s);
    int num;
    // 从字符串流中读取整数
    iss >> num;

    // 检查是否转换成功以及转换后的数字是否在指定范围内
    if (iss.fail() || !iss.eof()) {
        // 转换失败或字符串中包含非数字字符
        return false;
    }

    return (num >= low && num <= upp);
}

void print_target() {
    cout << "目标画作:" << endl;
    for (int h = 1; h <= height; h++) {
        for (int w = 1; w <= width; w++) {
            cout << target_ch[h][w];
        }
        cout << endl;
    }
}

void print_map() {
    cout << "您的画作:" << endl;
    for (int h = 1; h <= height; h++) {
        for (int w = 1; w <= width; w++) {
            if (w == 1)
                cout << he_ch[h] << h << " ";
            cout << map_ch[h][w];
        }
        cout << endl;
    }
    cout << "   ";
    for (int i = 1; i <= width; i++) {
        cout << i + height;
    }
    cout << endl << "   ";
    for (int i = 1; i <= width; i++) {
        cout << wi_ch[i];
    }
    cout << endl;
}

void copy_arr(char a[], char b[], int l) {
    for (int i = 1;i <= l;i++) {
        a[i] = b[i];
    }
}