跳到正文

剪贴板

剪贴板 4y8xacxp

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include <iostream>
#include <vector>
#include <string>
#include <thread>
#include <chrono>
#include <random>
#include <algorithm>
#include <sstream>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif

using namespace std;

class MeowGame {
private:
    int n, m, k;
    vector<int> cardDeck;
    vector<vector<int>> stacks;
    int currentCardIndex;
    vector<vector<int>> operations;
    int currentOpIndex;

    // 可视化参数
    const int DISPLAY_HEIGHT = 10;
    const int CARD_WIDTH = 5;

public:
    void initialize(int numStacks, int numCards, int numTypes, const vector<int>& cards) {
        n = numStacks;
        m = numCards;
        k = numTypes;
        cardDeck = cards;
        currentCardIndex = 0;
        currentOpIndex = 0;

        stacks.resize(n + 1); // 栈编号从1开始
        for (int i = 1; i <= n; i++) {
            stacks[i] = vector<int>();
        }
    }

    // 生成随机测试数据
    void generateRandomData(int numStacks, int numCards, int numTypes) {
        n = numStacks;
        m = numCards;
        k = numTypes;

        // 确保m是偶数(每种图案出现偶数次)
        if (m % 2 != 0) m++;

        cardDeck.clear();
        vector<int> cardPool;

        for(int i=1;i<=min(m/2,k);i++){
        	cardPool.push_back(i);
        	cardPool.push_back(i);
		}

		int tmp=m-2*k;
		if(tmp>0){
			// 生成卡牌对
	        for (int i = 0; i < tmp/2; i++) {
	            int pattern = rand() % k + 1;
	            cardPool.push_back(pattern);
	            cardPool.push_back(pattern);
	        }
		}

        // 随机打乱卡牌顺序
        random_shuffle(cardPool.begin(), cardPool.end());
        cardDeck = cardPool;

        currentCardIndex = 0;
        currentOpIndex = 0;

        stacks.resize(n + 1);
        for (int i = 1; i <= n; i++) {
            stacks[i] = vector<int>();
        }

        operations.clear();
    }

    void loadOperations(const vector<vector<int>>& ops) {
        operations = ops;
    }

    void clearScreen() {
        #ifdef _WIN32
        system("cls");
        #else
        system("clear");
        #endif
    }

    void displayState() {
        clearScreen();

        cout << "=== 喵了个喵游戏模拟器 ===" << endl;
        cout << "栈数量: " << n << " | 剩余卡牌: " << (m - currentCardIndex) << " | 图案种类: " << k << endl;
        cout << "当前操作: " << currentOpIndex << "/" << operations.size() << endl;
        cout << endl;

        // 显示牌堆顶部
        cout << "牌堆顶部: ";
//        for (int i = currentCardIndex; i < min(currentCardIndex + 5, m); i++) {
//            cout << cardDeck[i] << " ";
//        }
		for (int i = currentCardIndex; i < m; i++) {
            cout << cardDeck[i] << " ";
        }
        //if (m - currentCardIndex > 5) cout << "...";
        cout << endl << endl;

        // 显示栈
        int maxHeight = 0;
        for (int i = 1; i <= n; i++) {
            if (static_cast<int>(stacks[i].size()) > maxHeight) {
                maxHeight = stacks[i].size();
            }
        }

        maxHeight = min(maxHeight, DISPLAY_HEIGHT);

        for (int level = maxHeight - 1; level >= 0; level--) {
            for (int i = 1; i <= n; i++) {
                if (level < static_cast<int>(stacks[i].size())) {
                    int card = stacks[i][level];
                    string cardStr = to_string(card);
                    while (cardStr.length() < CARD_WIDTH - 2) cardStr = " " + cardStr + " ";
                    cout << "[" << cardStr << "]";
                } else {
                    string emptyStr = "";
                    for (int j = 0; j < CARD_WIDTH; j++) emptyStr += " ";
                    cout << emptyStr;
                }
                cout << "  ";
            }
            cout << endl;
        }

        // 显示栈底
//        cout << "栈底:    ";
//        for (int i = 1; i <= n; i++) {
//            if (!stacks[i].empty()) {
//                string bottomStr = to_string(stacks[i][0]);
//                int padding = (CARD_WIDTH - bottomStr.length()) / 2;
//                for (int j = 0; j < padding; j++) cout << " ";
//                cout << bottomStr;
//                for (int j = 0; j < CARD_WIDTH - bottomStr.length() - padding; j++) cout << " ";
//            } else {
//                string emptyStr = "";
//                for (int j = 0; j < CARD_WIDTH; j++) emptyStr += " ";
//                cout << emptyStr;
//            }
//            cout << "  ";
//        }
//        cout << endl;

        // 显示栈编号
        for (int i = 1; i <= n; i++) {
            string stackLabel = "栈" + to_string(i);
            int padding = (CARD_WIDTH - stackLabel.length()) / 2;
            for (int j = 0; j < padding; j++) cout << " ";
            cout << stackLabel;
            for (int j = 0; j < CARD_WIDTH - stackLabel.length() - padding; j++) cout << " ";
            cout << "  ";
        }
        cout << endl << endl;

        // 显示操作历史
        cout << "操作历史:" << endl;
        int start = max(0, currentOpIndex - 3);
        for (int i = start; i < currentOpIndex && i < static_cast<int>(operations.size()); i++) {
            cout << "步骤 " << (i + 1) << ": ";
            if (operations[i][0] == 1) {
                cout << "操作1 - 将卡牌放入栈 " << operations[i][1];
            } else {
                cout << "操作2 - 消去栈 " << operations[i][1] << " 和栈 " << operations[i][2] << " 的栈底";
            }
            cout << endl;
        }
        cout << endl;
    }

    bool executeNextOperation() {
        if (currentOpIndex >= static_cast<int>(operations.size())) {
            return false;
        }

        vector<int> op = operations[currentOpIndex];
        currentOpIndex++;

        if (op[0] == 1) {
            // 操作1: 将牌堆顶卡牌放入栈
            if (currentCardIndex >= m) {
                cout << "错误: 牌堆已空!" << endl;
                return false;
            }

            int stackNum = op[1];
            if (stackNum < 1 || stackNum > n) {
                cout << "错误: 栈编号 " << stackNum << " 无效!" << endl;
                return false;
            }

            int card = cardDeck[currentCardIndex];
            currentCardIndex++;

            stacks[stackNum].push_back(card);

            // 检查栈顶消去
            if (stacks[stackNum].size() >= 2) {
                int top = stacks[stackNum].size() - 1;
                if (stacks[stackNum][top] == stacks[stackNum][top - 1]) {
                    // 自动消去栈顶相同卡牌
                    stacks[stackNum].pop_back();
                    stacks[stackNum].pop_back();
                }
            }

        } else if (op[0] == 2) {
            // 操作2: 消去两个栈的栈底
            int stack1 = op[1];
            int stack2 = op[2];

            if (stack1 < 1 || stack1 > n || stack2 < 1 || stack2 > n) {
                cout << "错误: 栈编号无效!" << endl;
                return false;
            }

            if (stack1 == stack2) {
                cout << "错误: 不能选择相同的栈!" << endl;
                return false;
            }

            if (stacks[stack1].empty() || stacks[stack2].empty()) {
                cout << "错误: 栈 " << (stacks[stack1].empty() ? stack1 : stack2) << " 为空!" << endl;
                return false;
            }

            if (stacks[stack1][0] != stacks[stack2][0]) {
                cout << "错误: 栈底卡牌不相同!" << endl;
                return false;
            }

            // 消去栈底
            stacks[stack1].erase(stacks[stack1].begin());
            stacks[stack2].erase(stacks[stack2].begin());
        }

        return true;
    }

    bool isGameCompleted() {
        // 检查是否所有卡牌都已处理且所有栈为空
        if (currentCardIndex < m) return false;

        for (int i = 1; i <= n; i++) {
            if (!stacks[i].empty()) return false;
        }

        return true;
    }

    // 交互式操作输入模式
    void interactiveInputMode() {
        string input;

        cout << "=== 交互式操作输入模式 ===" << endl;
        cout << "在此模式下,您可以手动输入每一步操作" << endl;
        cout << "操作格式:" << endl;
        cout << "  1. 放入卡牌: 1 [栈编号]" << endl;
        cout << "  2. 消去栈底: 2 [栈1编号] [栈2编号]" << endl;
        cout << "  输入 'q' 退出,输入 'r' 重新开始" << endl << endl;

        // 显示初始状态
        displayState();

        while (true) {
            cout << "请输入操作: ";
            getline(cin, input);

            if (input == "q" || input == "Q") {
                break;
            }

            if (input == "r" || input == "R") {
                // 重新开始
                currentCardIndex = 0;
                currentOpIndex = 0;
                for (int i = 1; i <= n; i++) {
                    stacks[i].clear();
                }
                operations.clear();
                displayState();
                continue;
            }

            // 解析输入
            vector<int> op;
            stringstream ss(input);  // 现在应该可以正常工作了
            string token;

            while (ss >> token) {
                try {
                    op.push_back(stoi(token));
                } catch (...) {
                    cout << "输入格式错误! 请重新输入." << endl;
                    op.clear();
                    break;
                }
            }

            if (op.empty()) continue;

            if (op[0] == 1 && op.size() == 2) {
                // 操作1
                operations.push_back({1, op[1]});
            } else if (op[0] == 2 && op.size() == 3) {
                // 操作2
                operations.push_back({2, op[1], op[2]});
            } else {
                cout << "操作格式错误! 正确格式: '1 s' 或 '2 s1 s2'" << endl;
                continue;
            }

            // 执行操作
            if (!executeNextOperation()) {
                cout << "操作执行失败! 请检查操作是否合法." << endl;
                operations.pop_back(); // 移除无效操作
                currentOpIndex--;
            }

            displayState();

            if (isGameCompleted()) {
                cout << "?? 游戏完成! 所有卡牌已成功消去!" << endl;
                cout << "总共使用了 " << operations.size() << " 步操作" << endl;
                break;
            }

            if (currentCardIndex >= m && !isGameCompleted()) {
                cout << "??  牌堆已空但游戏未完成! 输入 'r' 重新开始." << endl;
            }
        }
    }

    // 自动模拟模式(使用预设操作序列)
    void autoSimulationMode() {
        string input;

        cout << "开始自动模拟..." << endl;
        cout << "按回车键执行下一步操作,输入 'q' 退出,输入 'a' 自动执行剩余操作" << endl;
        #ifdef _WIN32
        system("pause");
        #else
        cout << "按回车继续...";
        getline(cin, input);
        #endif

        while (currentOpIndex < static_cast<int>(operations.size())) {
            displayState();

            cout << "下一步操作: ";
            if (operations[currentOpIndex][0] == 1) {
                cout << "操作1 - 将卡牌放入栈 " << operations[currentOpIndex][1];
            } else {
                cout << "操作2 - 消去栈 " << operations[currentOpIndex][1]
                     << " 和栈 " << operations[currentOpIndex][2] << " 的栈底";
            }
            cout << endl;

            cout << "请输入命令 (回车=下一步, q=退出, a=自动执行): ";
            getline(cin, input);

            if (input == "q" || input == "Q") {
                break;
            } else if (input == "a" || input == "A") {
                // 自动执行剩余操作
                while (currentOpIndex < static_cast<int>(operations.size())) {
                    if (!executeNextOperation()) {
                        cout << "执行操作时发生错误!" << endl;
                        break;
                    }
                    displayState();
                    this_thread::sleep_for(chrono::milliseconds(500));
                }
                break;
            } else {
                // 执行下一步操作
                if (!executeNextOperation()) {
                    cout << "执行操作时发生错误! 按回车继续..." << endl;
                    getline(cin, input);
                }
            }
        }

        displayState();

        if (isGameCompleted()) {
            cout << "?? 游戏完成! 所有卡牌已成功消去!" << endl;
        } else {
            cout << "游戏未完成。" << endl;
        }

        cout << "按回车键退出..." << endl;
        getline(cin, input);
    }

    // 显示当前卡牌序列(用于参考)
    void displayCardSequence() {
        cout << "生成的卡牌序列: ";
        for (int card : cardDeck) {
            cout << card << " ";
        }
        cout << endl;
    }
};

// 显示主菜单
int showMainMenu() {
    cout << "=== 喵了个喵游戏模拟器 ===" << endl;
    cout << "1. 使用预设样例" << endl;
    cout << "2. 生成随机数据并手动操作" << endl;
    cout << "3. 生成随机数据并使用预设操作序列" << endl;
    cout << "4. 退出" << endl;
    cout << "请选择模式: ";

    int choice;
    cin >> choice;
    cin.ignore(); // 清除输入缓冲区
    return choice;
}

// 获取用户输入参数
void getParameters(int &n, int &m, int &k) {
    cout << "请输入参数:" << endl;
    cout << "栈数量 n (1-10): ";
    cin >> n;
    cout << "卡牌数量 m (偶数, 建议4-50): ";
    cin >> m;
    cout << "图案种类 k 就固定为 2n-1:"<<2*n-1<<endl;
    k = 2 * n - 1;
    cin.ignore(); // 清除输入缓冲区
}

int main() {
    srand(time(0)); // 初始化随机种子

    while (true) {
        int choice = showMainMenu();
        MeowGame game;

        if (choice == 1) {
            // 使用预设样例
            int n = 2, m = 4, k = 2;
            vector<int> cards = {1, 2, 1, 2};
            vector<vector<int>> operations = {
                {1, 1}, {1, 1}, {1, 2}, {2, 1, 2}, {1, 1}
            };

            game.initialize(n, m, k, cards);
            game.loadOperations(operations);
            game.autoSimulationMode();

        } else if (choice == 2) {
            // 生成随机数据并手动操作
            int n, m, k;
            getParameters(n, m, k);

            game.generateRandomData(n, m, k);
            game.displayCardSequence();
            cout << "按回车开始手动操作..." << endl;
            string tmp;
            getline(cin, tmp);

            game.interactiveInputMode();

        } else if (choice == 3) {
            // 生成随机数据并使用预设操作序列
            int n, m, k;
            getParameters(n, m, k);

            game.generateRandomData(n, m, k);
            game.displayCardSequence();

            // 这里可以加载预设的操作序列
            // 目前先使用空序列,实际使用时需要提供有效的操作序列
            vector<vector<int>> operations;
            game.loadOperations(operations);

            cout << "注意: 此模式需要有效的操作序列,目前使用空序列。" << endl;
            cout << "按回车继续..." << endl;
            string tmp;
            getline(cin, tmp);

            game.autoSimulationMode();

        } else if (choice == 4) {
            break;
        } else {
            cout << "无效选择,请重新输入!" << endl;
        }
    }

    return 0;
}