C++程序设计 课程设计

连连看小游戏改进

增加一个倒计时提示

还剩30s时提示

 if (timeLeft == 30) {
     cout << "还有30s" << endl;
 }

 改进了部分变量的表示

完整程序如下

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <chrono>
#include <vector>
#include <algorithm>

using namespace std;
int k = 0;
int re_rows = 4;
int re_cols = 4;
int time_limit = 120; // 游戏时间限制,单位:秒
const int score_point = 10; // 每成功配对一对方块的得分
const int maxreflsh = 3; // 最大刷新次数

class Game {
private:
    int num;
    int rows;
    int cols;
    vector<vector<int>> board;
    int score;
    int refreshesLeft;
    chrono::steady_clock::time_point startTime;
    int level;
    vector<vector<bool>> justic;
public:
    Game() : rows(re_rows), cols(re_cols), level(1) {
        score = 0;
        refreshesLeft = maxreflsh;
        initializeBoard();
        startTime = chrono::steady_clock::now();
    }

    void initializeBoard() {
        srand(time(0));
        board = vector<vector<int>>(rows, vector<int>(cols));
        vector<int> numbers;
        for (int i = 0; i < (rows * cols) / 2; ++i) {
            int num = rand() % 5 + 1; // 随机生成1到10之间的数字
            numbers.push_back(num);
            numbers.push_back(num);
        }

        random_shuffle(numbers.begin(), numbers.end());

        int k = 0;
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                board[i][j] = numbers[k++];
            }
        }
    }

    void displayTimeLeft(int timeLeft) {
        cout << "剩余时间: " << timeLeft << "s" << endl;
    }

    void displayBoard() {
        auto currentTime = chrono::steady_clock::now();
        int elapsedTime = chrono::duration_cast<chrono::seconds>(currentTime - startTime).count();
        int timeLeft = time_limit - elapsedTime;

        cout << "得分  " << score << "  剩余时间  " << timeLeft << "s" << " 等级 " << level << endl;
        cout << endl;
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                if (board[i][j] == 0) {
                    cout << "·   "; // 已消除的块用"."表示
                }
                else {
                    // 使用ANSI转义码添加颜色
                    switch (board[i][j]) {
                    case 1:
                        cout << "\033[31m" << board[i][j] << "\033[0m   "; // 红色
                        break;
                    case 2:
                        cout << "\033[32m" << board[i][j] << "\033[0m   "; // 绿色
                        break;
                    case 3:
                        cout << "\033[33m" << board[i][j] << "\033[0m   "; // 黄色
                        break;
                    case 4:
                        cout << "\033[34m" << board[i][j] << "\033[0m   "; // 蓝色
                        break;
                    case 5:
                        cout << "\033[35m" << board[i][j] << "\033[0m   "; // 紫色
                        break;
                    default:
                        cout << board[i][j] << "   "; // 默认颜色
                        break;
                    }
                }
            }
            cout << endl;
            cout << endl;
        }
    }

    bool judge(int x, int y) {
        return x >= 1 && x <= rows && y >= 1 && y <= cols;
    }

    bool have_path(int x1, int x2, int y1, int y2) {
        if (x1 == x2 && y1 == y2)
            return true;
        if (x1 < 0 || x1 >= rows || x2 < 0 || x2 >= rows || y1 < 0 || y1 >= cols || y2 < 0 || y2 >= cols || justic[x1][y1] || (num++ != 0 && board[x1][y1] != 0))
            return false;
        justic[x1][y1] = true;
        bool mapi = have_path(x1 + 1, x2, y1, y2) || have_path(x1 - 1, x2, y1, y2) || have_path(x1, x2, y1 + 1, y2) || have_path(x1, x2, y1 - 1, y2);
        return mapi;
    }
    bool canConnect(int x1, int y1, int x2, int y2) {
        num = 0;
        //中间两个块子之间必须要有空快经过
        int x_num = abs(x1 - x2);
        int y_num = abs(y1 - y2);
        int x1_ = x1, x2_ = x2, y1_ = y1, y2_ = y2;
        justic = vector<vector<bool>>(rows, vector<bool>(cols));
        for (int i = 0; i < rows; i++)
            for (int j = 0; j < cols; j++)
                justic[i][j] = false;
        if (board[x1][y1] != board[x2][y2])
            return false;
        if ((x_num == 1 && y_num == 0) || (x_num == 0 && y_num == 1))
            return true;
        else {
            if (!have_path(x1_, x2_, y1_, y2_))
                return false;
            else
                return true;
        }
    }

    void removeBlock(int x, int y) {
        board[x][y] = 0; // 使用0表示空块
    }

    bool isBoardEmpty() {
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                if (board[i][j] != 0) {
                    return false;
                }
            }
        }
        return true;
    }

    bool hasMoreMoves() {
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                if (board[i][j] != 0) {
                    for (int k = i; k < rows; ++k) {
                        for (int l = (k == i ? j + 1 : 0); l < cols; ++l) {
                            if (board[k][l] != 0 && canConnect(i, j, k, l)) {
                                return true;
                            }
                        }
                    }
                }
            }
        }
        return false;
    }

    void refreshBoard() {
        initializeBoard();
        cout << "这个棋盘刷新了哦" << endl;
    }

    void increaseDifficulty() {
        ++level;
        re_rows += k;
        re_cols += k;
        rows = re_rows;
        cols = re_cols;
        refreshesLeft = maxreflsh;
        board.clear(); // 清空游戏板

        // 重新初始化游戏板
        srand(time(0));
        board = vector<vector<int>>(rows, vector<int>(cols));
        vector<int> numbers;
        for (int i = 0; i < (rows * cols) / 2; ++i) {
            int num = rand() % 5 + 1; // 随机生成1到10之间的数字
            numbers.push_back(num);
            numbers.push_back(num);
        }
        random_shuffle(numbers.begin(), numbers.end());

        int k = 0;
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                board[i][j] = numbers[k++];
            }
        }
        time_limit += 20;
        startTime = chrono::steady_clock::now();
        cout << "难度提升,等级 " << level << "!" << endl;
        play();
    }



    void play() {
        bool justPaired = false;

        while (1) {
            auto currentTime = chrono::steady_clock::now();
            int elapsedTime = chrono::duration_cast<chrono::seconds>(currentTime - startTime).count();
            int timeLeft = time_limit - elapsedTime;

            if (timeLeft == 30) {
                cout << "还有30s" << endl;
            }

            displayBoard();

            int choice;
            if (!justPaired && refreshesLeft > 0) {
                cout << "你还有" << refreshesLeft << "次刷新机会,按1刷新游戏,按0进入游戏: ";
                cin >> choice;
                if (choice == 1 && refreshesLeft > 0 && !justPaired) {
                    refreshBoard();
                    refreshesLeft--;
                    continue;
                }
            }
            else {

            }


            if (timeLeft <= 0) {
                cout << "时间到了兄弟" << endl;
                break;
            }
            int x1, y1, x2, y2;
            cout << "请输入第一个坐标(x y): ";
            cin >> x1 >> y1;
            cout << "请输入第二个坐标(x y): ";
            cin >> x2 >> y2;
            if (x1 == x2 && y1 == y2) {
                cout << "请连接两个点" << endl;
                justPaired = true;
                continue;
            }
            if (!judge(x1, y1) || !judge(x2, y2)) {
                cout << "无效坐标,请重新输入" << endl;
                justPaired = true;
                continue; // 回到循环开始处重新获取用户输入
            }

            // 调整输入,使之从1开始计数转换为从0开始计数
            x1 -= 1;
            y1 -= 1;
            x2 -= 1;
            y2 -= 1;

            if (canConnect(x1, y1, x2, y2)) {
                justPaired = true;
                removeBlock(x1, y1);
                removeBlock(x2, y2);
                score += score_point;
                if (isBoardEmpty()) {
                    cout << "祝贺你,你做到了" << endl;
                    k = k + 2;
                    increaseDifficulty();
                }
            }
            else {
                score -= 5;
                justPaired = true;
                cout << "失败了,兄弟,再试试" << endl;
            }
        }

        cout << "你的最终得分是: " << score << endl;
    }
};

int main() {
    cout << "欢迎来到胡管的地狱游戏!!!" << endl;
    cout << "规则如下:" << endl;
    cout << "1. 连接两个水平或垂直相邻的匹配块,或者是中间存在空块的相同数字的方块。" << endl;
    cout << "2. 连接正确得分+10,连接错误得分-5。" << endl;
    cout << "3. 你现在有 " << time_limit << " 秒去清除块。" << endl;
    cout << "4. 你目前有 " << maxreflsh << " 次机会刷新游戏,记住按1刷新游戏。" << endl;
    cout << "5. 输入坐标为(x y),其中x和y从1开始。" << endl;
    cout << "6. 通关当前关卡后难度升级。" << endl;
    cout << "==========================================" << endl;

    Game game;
    game.play();

    return 0;
}

 游戏初始化界面如下

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值