C++程序设计 课程设计

本蒟蒻第一次写小程序,还望大佬批评指正。

写一个连连看小游戏:

初始化

const int ROWS = 10;
const int COLS = 10;
const int TIME_LIMIT = 60; // 游戏时间限制,单位:秒
const int SCORE_PER_PAIR = 10; // 每成功配对一对方块的得分
const int MAX_REFRESHES = 3; // 最大刷新次数

 用到的函数

void initializeBoard(int board[ROWS][COLS]);
void displayBoard(int board[ROWS][COLS], int score, int timeLeft, int refreshesLeft);
bool canConnect(int board[ROWS][COLS], int x1, int y1, int x2, int y2);
void removeBlock(int board[ROWS][COLS], int x, int y);
bool isBoardEmpty(int board[ROWS][COLS]);
bool hasMoreMoves(int board[ROWS][COLS]);
void refreshBoard(int board[ROWS][COLS]);

保证可玩性

游戏初始时输出提示

 std::cout << "Welcome to the Matching Game!" << std::endl;
 std::cout << "Rules:" << std::endl;
 std::cout << "1. Connect two matching blocks that are adjacent horizontally or vertically." << std::endl;
 std::cout << "2. You have " << TIME_LIMIT << " seconds to clear the board." << std::endl;
 std::cout << "3. You can refresh the board up to " << MAX_REFRESHES << " times by entering '1' when prompted." << std::endl;
 std::cout << "4. Enter coordinates as (x y) where x and y start from 1." << std::endl;
 std::cout << "Good luck and have fun!" << std::endl;
 std::cout << "==========================================" << std::endl;

首先产生一个数字矩阵,以当前时间为随机变量

void initializeBoard(int board[ROWS][COLS]) {
    std::srand(std::time(0));
    for (int i = 0; i < ROWS; ++i) {
        for (int j = 0; j < COLS; ++j) {
            board[i][j] = std::rand() % 100 + 1; // 随机生成1到5之间的数字
        }
    }
}

然后将该矩阵输出,并将已经配对的点用“."代替

void displayBoard(int board[ROWS][COLS], int score, int timeLeft, int refreshesLeft) {
    std::cout << "Score: " << score << " | Time left: " << timeLeft << "s" << " | Refreshes left: " << refreshesLeft << std::endl;
    for (int i = 0; i < ROWS; ++i) {
        for (int j = 0; j < COLS; ++j) {
            if (board[i][j] == 0) {
                std::cout << ". "; 
            }
            else {
                std::cout << board[i][j] << " ";
            }
        }
        std::cout << std::endl;
        std::cout << std::endl;
    }
}

游玩者可以选择刷新(共3次机会),或者直接进入游戏

int choice;
std::cout << "Enter 1 to refresh the board, 0 to continue: ";
std::cin >> choice;

if (choice == 1 && refreshesLeft > 0) {
    refreshBoard(board);
    refreshesLeft--;
    continue; 
}

输入

 int x1, y1, x2, y2;
 std::cout << "Enter coordinates of first block (x1 y1): ";
 std::cin >> x1 >> y1;
 std::cout << "Enter coordinates of second block (x2 y2): ";
 std::cin >> x2 >> y2;

 x1 -= 1;
 y1 -= 1;
 x2 -= 1;
 y2 -= 1;

并将输入的自然坐标(从1 开始)转为矩阵坐标(从0开始)

判断是否能配对

bool canConnect(int board[ROWS][COLS], int x1, int y1, int x2, int y2) {
    if (board[x1][y1] == board[x2][y2]) {
        if ((abs(x1 - x2) == 1 && y1 == y2) || (abs(y1 - y2) == 1 && x1 == x2)) {
            return true;
        }
    }
    return false;
}

将已经配对的数变为”.“

void removeBlock(int board[ROWS][COLS], int x, int y) {
    board[x][y] = 0; 
}

判断输入的点坐标是否超出了矩阵的范围

bool isBoardEmpty(int board[ROWS][COLS]) {
    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(int board[ROWS][COLS]) {
    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(board, i, j, k, l)) {
                            return true;
                        }
                    }
                }
            }
        }
    }
    return false;
}

 配对并输出提示

        if (canConnect(board, x1, y1, x2, y2)) {
            removeBlock(board, x1, y1);
            removeBlock(board, x2, y2);
            score += SCORE_PER_PAIR;

            if (isBoardEmpty(board)) {
                std::cout << "Congratulations! You've cleared the board!" << std::endl;
                break;
            }
        }
        else {
            std::cout << "Cannot connect these blocks. Try again." << std::endl;
        }

        if (!hasMoreMoves(board)) {
            std::cout << "No more moves available. Game Over!" << std::endl;
            break;
        }
    }

    std::cout << "Your final score: " << score << std::endl;
    return 0;
}

以下是全部代码

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

const int ROWS = 10;
const int COLS = 10;
const int TIME_LIMIT = 60; 
const int SCORE_PER_PAIR = 10; 
const int MAX_REFRESHES = 3; 

void initializeBoard(int board[ROWS][COLS]);
void displayBoard(int board[ROWS][COLS], int score, int timeLeft, int refreshesLeft);
bool canConnect(int board[ROWS][COLS], int x1, int y1, int x2, int y2);
void removeBlock(int board[ROWS][COLS], int x, int y);
bool isBoardEmpty(int board[ROWS][COLS]);
bool hasMoreMoves(int board[ROWS][COLS]);
void refreshBoard(int board[ROWS][COLS]);

int main() {
    std::cout << "Welcome to the Matching Game!" << std::endl;
    std::cout << "Rules:" << std::endl;
    std::cout << "1. Connect two matching blocks that are adjacent horizontally or vertically." << std::endl;
    std::cout << "2. You have " << TIME_LIMIT << " seconds to clear the board." << std::endl;
    std::cout << "3. You can refresh the board up to " << MAX_REFRESHES << " times by entering '1' when prompted." << std::endl;
    std::cout << "4. Enter coordinates as (x y) where x and y start from 1." << std::endl;
    std::cout << "Good luck and have fun!" << std::endl;
    std::cout << "==========================================" << std::endl;

    int board[ROWS][COLS];
    initializeBoard(board);

    int score = 0;
    int refreshesLeft = MAX_REFRESHES;
    auto startTime = std::chrono::steady_clock::now();

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

        displayBoard(board, score, timeLeft, refreshesLeft);

        if (timeLeft <= 0) {
            std::cout << "Time's up! Game Over!" << std::endl;
            break;
        }

        int choice;
        std::cout << "Enter 1 to refresh the board, 0 to continue: ";
        std::cin >> choice;

        if (choice == 1 && refreshesLeft > 0) {
            refreshBoard(board);
            refreshesLeft--;
            continue;
        }

        int x1, y1, x2, y2;
        std::cout << "Enter coordinates of first block (x1 y1): ";
        std::cin >> x1 >> y1;
        std::cout << "Enter coordinates of second block (x2 y2): ";
        std::cin >> x2 >> y2;

        
        x1 -= 1;
        y1 -= 1;
        x2 -= 1;
        y2 -= 1;

        if (canConnect(board, x1, y1, x2, y2)) {
            removeBlock(board, x1, y1);
            removeBlock(board, x2, y2);
            score += SCORE_PER_PAIR;

            if (isBoardEmpty(board)) {
                std::cout << "Congratulations! You've cleared the board!" << std::endl;
                break;
            }
        }
        else {
            std::cout << "Cannot connect these blocks. Try again." << std::endl;
        }

        if (!hasMoreMoves(board)) {
            std::cout << "No more moves available. Game Over!" << std::endl;
            break;
        }
    }

    std::cout << "Your final score: " << score << std::endl;
    return 0;
}

void initializeBoard(int board[ROWS][COLS]) {
    std::srand(std::time(0));
    for (int i = 0; i < ROWS; ++i) {
        for (int j = 0; j < COLS; ++j) {
            board[i][j] = std::rand() % 100 + 1; // 随机生成1到5之间的数字
        }
    }
}

void displayBoard(int board[ROWS][COLS], int score, int timeLeft, int refreshesLeft) {
    std::cout << "Score: " << score << " | Time left: " << timeLeft << "s" << " | Refreshes left: " << refreshesLeft << std::endl;
    for (int i = 0; i < ROWS; ++i) {
        for (int j = 0; j < COLS; ++j) {
            if (board[i][j] == 0) {
                std::cout << ". "; 
            }
            else {
                std::cout << board[i][j] << " ";
            }
        }
        std::cout << std::endl;
        std::cout << std::endl;
    }
}

bool canConnect(int board[ROWS][COLS], int x1, int y1, int x2, int y2) {
    if (board[x1][y1] == board[x2][y2]) {
        if ((abs(x1 - x2) == 1 && y1 == y2) || (abs(y1 - y2) == 1 && x1 == x2)) {
            return true;
        }
    }
    return false;
}

void removeBlock(int board[ROWS][COLS], int x, int y) {
    board[x][y] = 0; 
}

bool isBoardEmpty(int board[ROWS][COLS]) {
    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(int board[ROWS][COLS]) {
    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(board, i, j, k, l)) {
                            return true;
                        }
                    }
                }
            }
        }
    }
    return false;
}

void refreshBoard(int board[ROWS][COLS]) {
    initializeBoard(board);
    std::cout << "The board has been refreshed." << std::endl;
}

游戏界面如下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值