当前位置: 首页 > news >正文

新手练习项目 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矩阵// 在游戏板上随机生成两个数字2board[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)
点赞加关注,收藏不迷路!本篇文章对你有帮助的话,还请多多点赞支持!

http://www.lryc.cn/news/276031.html

相关文章:

  • 2023年度总结:技术沉淀、持续学习
  • Unity 利用UGUI之Slider制作进度条
  • OCS2 入门教程(四)- 机器人示例
  • FreeRTOS学习第6篇–任务状态挂起恢复删除等操作
  • BLE Mesh蓝牙组网技术详细解析之Access Layer访问层(六)
  • Netlink 通信机制
  • 2024.1.8每日一题
  • 看了致远OA的表单设计后的思考
  • mmdetection训练自己的数据集
  • MySQL取出N列里最大or最小的一个数据
  • 编写.NET的Dockerfile文件构建镜像
  • 【C语言】浙大版C语言程序设计(第三版) 练习7-4 找出不是两个数组共有的元素
  • 7.27 SpringBoot项目实战 之 整合Swagger
  • 创建第一个SpringMVC项目,入手必看!
  • go 切片长度与容量的区别
  • 回归和分类区别
  • docker nginx滚动日志配置
  • 大数据分析案例-基于LinearRegression回归算法构建房屋价格预测模型
  • React-hook-form-mui(一):基本使用
  • python总结-生成器与迭代器
  • MySQL如何从数据中截取所需要的字符串
  • 动态加载和动态链接的区别
  • js数组循环,当前循环完成后执行下次循环
  • 决策树(Decision Trees)
  • 湖南大学-计算机网路-2023期末考试【部分原题回忆】
  • LCD—液晶显示
  • 论正确初始化深度学习模型参数的重要性
  • ALSA学习(5)——ASoC架构中的Machine
  • LeetCode 0447.回旋镖的数量:哈希表
  • 容器相关笔记