新手练习项目 4:简易2048游戏的实现(C++)

名人说:莫听穿林打叶声,何妨吟啸且徐行。—— 苏轼《定风波·莫听穿林打叶声》
Code_流苏(CSDN)(一个喜欢古诗词和编程的Coder)

一、效果图

在这里插入图片描述

二、代码(带注释)

//创作者:Code_流苏(CSDN)
//未经允许,禁止转载发布,可自己学习使用
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

using namespace std;

const int SIZE = 4; // 定义游戏板的大小为4x4

// 初始化游戏板
void initializeBoard(vector<vector<int>>& board) {
    
    
    board.assign(SIZE, vector<int>(SIZE, 0)); // 将游戏板初始化为SIZE x SIZE的0矩阵
    // 在游戏板上随机生成两个数字2
    board[rand() % SIZE][rand() % SIZE] = 2;
    board[rand() % SIZE][rand() % SIZE] = 2;
}

// 打印游戏板
void printBoard(const vector<vector<int>>& board) {
    
    
    for (int i = 0; i < SIZE; ++i) {
    
    
        for (int j = 0; j < SIZE; ++j) {
    
    
            if(board[i][j] == 0) cout << ".";
            else cout << board[i][j];
            cout << "\t";
        }
        cout << endl;
    }
}

// 检查是否还有可移动的格子
bool canMove(const vector<vector<int>>& board) {
    
    
    for (int i = 0; i < SIZE; ++i) {
    
    
        for (int j = 0; j < SIZE; ++j) {
    
    
            // 如果有空格或者有相邻的相同数字,则可以移动
            if (board[i][j] == 0) return true;
            if (i < SIZE - 1 && board[i][j] == board[i + 1][j]) return true;
            if (j < SIZE - 1 && board[i][j] == board[i][j + 1]) return true;
        }
    }
    return false;
}

// 在随机位置添加一个数字2或4
void addNumber(vector<vector<int>>& board) {
    
    
    int i, j;
    do {
    
    
        i = rand() % SIZE;
        j = rand() % SIZE;
    } while (board[i][j] != 0); // 选择一个空的格子

    board[i][j] = (rand() % 10 == 0) ? 4 : 2; // 有10%的概率生成4,90%的概率生成2
}

// 旋转游戏板
void rotateBoard(vector<vector<int>>& board) {
    
    
    vector<vector<int>> temp(SIZE, vector<int>(SIZE));
    for (int i = 0; i < SIZE; ++i) {
    
    
        for (int j = 0; j < SIZE; ++j) {
    
    
            temp[j][SIZE - 1 - i] = board[i][j]; // 旋转90度
        }
    }
    board = temp;
}

// 向左移动格子并合并
void moveTiles(vector<vector<int>>& board) {
    
    
    for (int i = 0; i < SIZE; ++i) {
    
    
        int lastMergePosition = -1; 
        for (int j = 1; j < SIZE; ++j) {
    
    
            if (board[i][j] == 0) continue; // 如果当前格子为空,则跳过

            int previousPosition = j - 1;
            // 寻找可以合并或移动的位置
            while (previousPosition > lastMergePosition && board[i][previousPosition] == 0) {
    
    
                previousPosition--;
            }

            if (previousPosition == j) continue; // 如果没有可移动或合并的位置,继续下一个格子

            // 根据情况移动或合并格子
            if (board[i][previousPosition] == 0) {
    
    
                board[i][previousPosition] = board[i][j];
                board[i][j] = 0;
            } else if (board[i][previousPosition] == board[i][j]) {
    
    
                board[i][previousPosition] *= 2;
                board[i][j] = 0;
                lastMergePosition = previousPosition;
            } else if (previousPosition + 1 != j) {
    
    
                board[i][previousPosition + 1] = board[i][j];
                board[i][j] = 0;
            }
        }
    }
}

// 定义不同方向的移动
void moveLeft(vector<vector<int>>& board) {
    
    
    moveTiles(board);
}

void moveRight(vector<vector<int>>& board) {
    
    
    rotateBoard(board);
    rotateBoard(board);
    moveTiles(board);
    rotateBoard(board);
    rotateBoard(board);
}

void moveUp(vector<vector<int>>& board) {
    
    
    rotateBoard(board);
    rotateBoard(board);
    rotateBoard(board);
    moveTiles(board);
    rotateBoard(board);
}

void moveDown(vector<vector<int>>& board) {
    
    
    rotateBoard(board);
    moveTiles(board);
    rotateBoard(board);
    rotateBoard(board);
    rotateBoard(board);
}

// 主函数
int main() {
    
    
    srand(time(NULL)); // 设置随机种子
    vector<vector<int>> board;
    initializeBoard(board); // 初始化游戏板
    printBoard(board); // 打印游戏板

    while (true) {
    
    
        if (!canMove(board)) {
    
    
            cout << "游戏结束!" << endl;
            break;
        }

        char input;
        cout << "选择方向 (w/a/s/d): ";
        cin >> input; // 获取用户输入
        switch (input) {
    
    
            case 'a':
                moveLeft(board);
                break;
            case 'd':
                moveRight(board);
                break;
            case 'w':
                moveUp(board);
                break;
            case 's':
                moveDown(board);
                break;
            default:
                cout << "无效输入! 请使用 w/a/s/d." << endl;
                continue;
        }

        if (canMove(board)) {
    
    
            addNumber(board); // 在合适位置添加新的数字
        }

        printBoard(board); // 打印更新后的游戏板
    }

    return 0;
}

三、说明

上述代码实现了一个简单的2048游戏,主要由以下几个部分组成:

  1. 初始化游戏板 (initializeBoard函数):用于初始化一个SIZE x SIZE(在这个例子中是4x4)的游戏板,并随机在两个位置放置数字2。

  2. 打印游戏板 (printBoard函数):该函数用于遍历游戏板并打印每个元素,其中0被替换为.以便于观看。

  3. 检查是否可以移动 (canMove函数):这个函数用来检查游戏板上是否还有可合并的元素或者空位,以决定游戏是否结束。

  4. 添加数字 (addNumber函数):在玩家移动之后,在一个随机的空位置上添加一个新的数字(90%的概率是2,10%的概率是4)。

  5. 旋转游戏板 (rotateBoard函数):为了简化移动逻辑,此函数用来将游戏板顺时针旋转90度。

  6. 移动方块 (moveTiles函数):该函数用于处理实际的方块移动和合并逻辑。

  7. 移动方向 (moveLeft, moveRight, moveUp, moveDown函数):这些函数使用moveTilesrotateBoard来处理不同方向的移动。

  8. 主函数 (main函数):设置游戏的初始状态,然后进入一个循环,等待玩家输入来移动方块,直到没有移动可做时结束游戏。

补充说明:

  • 游戏板的大小是通过const int SIZE = 4预设的,即方格大小为4x4。
  • 游戏开始时,游戏板上有两个数字2。
  • 玩家可以通过输入’w’, ‘a’, ‘s’, 'd’来控制方块向上、左、下、右移动。
  • 当游戏板上没有空位或者没有可合并的相邻方块时,游戏结束。
  • 这个程序没有实现计分功能,可自己扩充实现。

Code_流苏(CSDN)(一个喜欢古诗词和编程的Coder)
点赞加关注,收藏不迷路!本篇文章对你有帮助的话,还请多多点赞支持!

猜你喜欢

转载自blog.csdn.net/qq_51646682/article/details/135404418
今日推荐