用C++实现小游戏2048

本文介绍了如何使用C++编程实现经典小游戏2048,玩家通过方向键操作,目标是达到2048作为游戏结束条件。
摘要由CSDN通过智能技术生成

用C++写了一个简单2048小游戏,最大数字达到2048即为结束

方向键控制移动

2048小游戏

#include <iostream>
#include <Windows.h>
#include <ctime>
#include <conio.h>
#include <algorithm>

using namespace std;

int nums[4][4];
int emp = 16, score = 0; // 空格子数, 分数
int maxv = 2; // 场上最大数,到达2048时,结束游戏
int dirc_st = 0b0000; // 状态变量,表示可操作的方向

// 绘制地图函数
void drawmap()
{
   
    // 先清屏幕
    system("cls");
    puts("          ---- 2 0 4 8 ----");
    puts("               by DNA\n");
    cout << "              得分: " << score  << '\n' << endl;
  
    for (int col = 1; col <= 17; col++)
    {
   
        cout << "  ";
        if (col % 4 == 1)   puts(" -------------------------------- ");
        else if (col % 4 == 2 || col % 4 == 0)   puts("|       |       |       |       |");
        else
        {
   
            cout << '|';
            for (int row = 0; row <= 3; row++)
                if (nums[col / 4][row] != 0)
                    printf("%5d  |", nums[col / 4][row]);
                else
                    printf("       |");
            puts("");
        }
    }



    // 测试
    /*
    puts("");
    cout << "empty = " << emt << endl;
    cout << "max :" << maxv << endl;
    cout << "方向状态" << endl;
    cout << "上 : " << (dirc_st >> 3 & 1) << endl;
    cout << "下 : " << (dirc_st >> 2 & 1) << endl;
    cout << "左 : " << (dirc_st >> 1 & 1) << endl;
    cout << "右 : " << (dirc_st >> 0 & 1) << endl;
    */
}

// 产生新数字
void newnums()
{
   
    // 产生2:67% 产生4:33%
    int newnumber = (rand() % 3) ? 2 : 4;

    int x, y;
    do
    {
   
        x = rand() % 4, y = rand() % 4;
    } while (nums[x][y] != 0);

    nums[x][y] = newnumber;
    score += newnumber;
    emp --;
}

// 状态判断
void statecheck()
{
   
    dirc_st = 0b0000; // 分别代表当前状态是否可以上下左右移动
    // 上
    for (int i = 1; i <= 3; i ++ )
        for (int j = 0; j <= 3; j++)
        {
   
            if (nums[i][j] != 0 && nums[i - 1][j] == 0 || 
                nums[i][j] != 0 && nums[i - 1][j] == nums[i][j]
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的C++实现2048小游戏的示例代码: ```c++ #include <iostream> #include <ctime> #include <conio.h> #include <Windows.h> using namespace std; const int ROW = 4, COL = 4; // 定义方格行数和列数 int game[ROW][COL]; // 存储游戏数据的二维数组 // 游戏初始化函数 void initGame() { // 初始化游戏数据,将所有元素置为0 for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { game[i][j] = 0; } } // 随机生成两个初始值为2的方块 srand(time(NULL)); // 随机数种子 int count = 0; while (count < 2) { int x = rand() % ROW; int y = rand() % COL; if (game[x][y] == 0) { game[x][y] = 2; count++; } } } // 显示游戏界面函数 void showGame() { system("cls"); // 清屏 // 显示游戏界面 cout << "-----------------------------" << endl; for (int i = 0; i < ROW; i++) { cout << "|"; for (int j = 0; j < COL; j++) { if (game[i][j] == 0) { cout << " |"; } else { cout << " " << game[i][j] << " |"; } } cout << endl; cout << "-----------------------------" << endl; } } // 判断游戏是否结束函数 bool isGameOver() { // 判断是否还有空位 for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { if (game[i][j] == 0) { return false; } } } // 判断相邻的方块是否相等 for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL - 1; j++) { if (game[i][j] == game[i][j + 1]) { return false; } } } for (int i = 0; i < ROW - 1; i++) { for (int j = 0; j < COL; j++) { if (game[i][j] == game[i + 1][j]) { return false; } } } return true; } // 向左移动函数 void moveLeft() { for (int i = 0; i < ROW; i++) { // 先将非零元素移到左边 int k = 0; for (int j = 0; j < COL; j++) { if (game[i][j] != 0) { game[i][k++] = game[i][j]; } } // 将左边空白处置为0 for (; k < COL; k++) { game[i][k] = 0; } // 合并相邻的相同元素 for (int j = 0; j < COL - 1; j++) { if (game[i][j] == game[i][j + 1]) { game[i][j] *= 2; game[i][j + 1] = 0; } } // 再次移动非零元素 k = 0; for (int j = 0; j < COL; j++) { if (game[i][j] != 0) { game[i][k++] = game[i][j]; } } // 将左边空白处置为0 for (; k < COL; k++) { game[i][k] = 0; } } } // 向右移动函数 void moveRight() { for (int i = 0; i < ROW; i++) { // 先将非零元素移到右边 int k = COL - 1; for (int j = COL - 1; j >= 0; j--) { if (game[i][j] != 0) { game[i][k--] = game[i][j]; } } // 将右边空白处置为0 for (; k >= 0; k--) { game[i][k] = 0; } // 合并相邻的相同元素 for (int j = COL - 1; j > 0; j--) { if (game[i][j] == game[i][j - 1]) { game[i][j] *= 2; game[i][j - 1] = 0; } } // 再次移动非零元素 k = COL - 1; for (int j = COL - 1; j >= 0; j--) { if (game[i][j] != 0) { game[i][k--] = game[i][j]; } } // 将右边空白处置为0 for (; k >= 0; k--) { game[i][k] = 0; } } } // 向上移动函数 void moveUp() { for (int j = 0; j < COL; j++) { // 先将非零元素移到上面 int k = 0; for (int i = 0; i < ROW; i++) { if (game[i][j] != 0) { game[k++][j] = game[i][j]; } } // 将上面空白处置为0 for (; k < ROW; k++) { game[k][j] = 0; } // 合并相邻的相同元素 for (int i = 0; i < ROW - 1; i++) { if (game[i][j] == game[i + 1][j]) { game[i][j] *= 2; game[i + 1][j] = 0; } } // 再次移动非零元素 k = 0; for (int i = 0; i < ROW; i++) { if (game[i][j] != 0) { game[k++][j] = game[i][j]; } } // 将上面空白处置为0 for (; k < ROW; k++) { game[k][j] = 0; } } } // 向下移动函数 void moveDown() { for (int j = 0; j < COL; j++) { // 先将非零元素移到下面 int k = ROW - 1; for (int i = ROW - 1; i >= 0; i--) { if (game[i][j] != 0) { game[k--][j] = game[i][j]; } } // 将下面空白处置为0 for (; k >= 0; k--) { game[k][j] = 0; } // 合并相邻的相同元素 for (int i = ROW - 1; i > 0; i--) { if (game[i][j] == game[i - 1][j]) { game[i][j] *= 2; game[i - 1][j] = 0; } } // 再次移动非零元素 k = ROW - 1; for (int i = ROW - 1; i >= 0; i--) { if (game[i][j] != 0) { game[k--][j] = game[i][j]; } } // 将下面空白处置为0 for (; k >= 0; k--) { game[k][j] = 0; } } } int main() { initGame(); // 初始化游戏 showGame(); // 显示游戏界面 while (!isGameOver()) { char ch = getch(); // 获取键盘输入 switch (ch) { case 'a': moveLeft(); break; case 'd': moveRight(); break; case 'w': moveUp(); break; case 's': moveDown(); break; } // 随机生成一个新的方块 int x, y; do { x = rand() % ROW; y = rand() % COL; } while (game[x][y] != 0); game[x][y] = 2; showGame(); // 显示游戏界面 } cout << "Game Over!" << endl; return 0; } ``` 该示例代码使用了Windows API中的`system()`函数和`getch()`函数,因此只能在Windows环境下编译运行。如果需要在其他环境下运行,需要使用相应的替代函数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值