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

C语言实战:2048数字合并游戏

游戏简介

这是一个经典的2048游戏,使用C语言编写,运行在命令行终端。玩家需要通过滑动数字方块(上、下、左、右),让相同数字的方块合并,最终尝试合成2048(或更高)的方块!

游戏特点

✅ 简洁的终端界面
✅ 实时计分系统
✅ 随机生成数字(2或4)
✅ 游戏结束判定
✅ 支持键盘控制(WASD或方向键)

如何运行?

  1. 复制代码到C编译器(如GCC、Clang)

  2. 编译运行(例如:gcc 2048.c -o 2048,然后 ./2048

  3. 开始挑战你的数字合并技巧!

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h> // For getch() on Windows
#include <stdbool.h>#define SIZE 4int board[SIZE][SIZE] = {0};
int score = 0;// Function prototypes
void initBoard();
void printBoard();
void addRandomTile();
bool moveTiles(int direction);
bool isGameOver();
void rotateBoard();
bool mergeTiles();
void clearScreen();int main() {srand(time(0)); // Seed the random number generatorinitBoard();addRandomTile();addRandomTile();while (true) {clearScreen();printf("2048 Game - Score: %d\n\n", score);printBoard();if (isGameOver()) {printf("\nGame Over! Final Score: %d\n", score);break;}printf("\nUse arrow keys to move (w=up, s=down, a=left, d=right, q=quit): ");char input = getch();int direction = -1;switch (input) {case 'w': direction = 0; break; // Upcase 's': direction = 1; break; // Downcase 'a': direction = 2; break; // Leftcase 'd': direction = 3; break; // Rightcase 'q': printf("\nQuitting game...\n");return 0;default: continue;}if (moveTiles(direction)) {addRandomTile();}}return 0;
}void initBoard() {for (int i = 0; i < SIZE; i++) {for (int j = 0; j < SIZE; j++) {board[i][j] = 0;}}score = 0;
}void printBoard() {for (int i = 0; i < SIZE; i++) {printf("+------+------+------+------+\n");for (int j = 0; j < SIZE; j++) {printf("|");if (board[i][j] != 0) {printf("%5d ", board[i][j]);} else {printf("      ");}}printf("|\n");}printf("+------+------+------+------+\n");
}void addRandomTile() {int emptyCells[SIZE * SIZE][2];int count = 0;// Find all empty cellsfor (int i = 0; i < SIZE; i++) {for (int j = 0; j < SIZE; j++) {if (board[i][j] == 0) {emptyCells[count][0] = i;emptyCells[count][1] = j;count++;}}}if (count > 0) {// Choose a random empty cellint index = rand() % count;int x = emptyCells[index][0];int y = emptyCells[index][1];// 90% chance for 2, 10% chance for 4board[x][y] = (rand() % 10 == 0) ? 4 : 2;}
}bool moveTiles(int direction) {bool moved = false;// Rotate board to simplify movement logicfor (int i = 0; i < direction; i++) {rotateBoard();}// Move and merge tilesmoved = mergeTiles();// Rotate backfor (int i = 0; i < (4 - direction) % 4; i++) {rotateBoard();}return moved;
}void rotateBoard() {int temp[SIZE][SIZE];for (int i = 0; i < SIZE; i++) {for (int j = 0; j < SIZE; j++) {temp[i][j] = board[SIZE - j - 1][i];}}for (int i = 0; i < SIZE; i++) {for (int j = 0; j < SIZE; j++) {board[i][j] = temp[i][j];}}
}bool mergeTiles() {bool moved = false;for (int i = 0; i < SIZE; i++) {// First, move all tiles to the left (after rotation)int pos = 0;for (int j = 0; j < SIZE; j++) {if (board[i][j] != 0) {if (j != pos) {board[i][pos] = board[i][j];board[i][j] = 0;moved = true;}pos++;}}// Then merge adjacent tiles with the same valuefor (int j = 0; j < SIZE - 1; j++) {if (board[i][j] != 0 && board[i][j] == board[i][j + 1]) {board[i][j] *= 2;score += board[i][j];board[i][j + 1] = 0;moved = true;// Shift remaining tilesfor (int k = j + 1; k < SIZE - 1; k++) {board[i][k] = board[i][k + 1];}board[i][SIZE - 1] = 0;}}}return moved;
}bool isGameOver() {// Check for empty cellsfor (int i = 0; i < SIZE; i++) {for (int j = 0; j < SIZE; j++) {if (board[i][j] == 0) {return false;}}}// Check for possible mergesfor (int i = 0; i < SIZE; i++) {for (int j = 0; j < SIZE - 1; j++) {if (board[i][j] == board[i][j + 1]) {return false;}}}for (int j = 0; j < SIZE; j++) {for (int i = 0; i < SIZE - 1; i++) {if (board[i][j] == board[i + 1][j]) {return false;}}}return true;
}void clearScreen() {#ifdef _WIN32system("cls");#elsesystem("clear");#endif
}

http://www.dtcms.com/a/264382.html

相关文章:

  • 【C++】头文件的能力与禁忌
  • [Python 基础课程]数字
  • wrap+aria2c提高下载速度
  • 创宇智脑 MCP 赋能 AiPy,IP 风险调查效率实现 10 倍飞跃,威胁分析一键生成
  • c语言中的函数I
  • NV103NV105美光固态闪存NV107NV108
  • Python OrderedDict 用法详解
  • 【1.7 漫画Java核心并发编程】
  • 【硬核拆解】英伟达Blackwell芯片架构如何重构AI算力边界?
  • 第六章 OpenCV篇—傅里叶变换与直方图
  • 学习字符串
  • Flask+LayUI开发手记(十):构建统一的选项集合服务
  • Rust 定义与实例化结构体
  • php数据导出pdf文件
  • 目标检测系列(五)已标注数据集(yolo格式)导入labelstudio继续标注
  • 浏览器工作原理32 [#]同源策略:为什么XMLHttpRequst不能跨域请求资源
  • Android11 添加自定义物理按键事件监听回调
  • Nginx重定向协议冲突解决方案:The plain HTTP request was sent to HTTPS port
  • uniapp选择相册
  • CAD文件处理控件Aspose.CAD教程:使用 Python 将绘图转换为 Photoshop
  • 【基础】Golang 执行命令shell命令 + Start和Run方法详解
  • ES6数组的`flat()`和`flatMap()`函数用法
  • 黑马python(二十三)
  • vue2 el-select下拉选择框 点击其他位置或者弹窗关闭下拉框/点击取消时,下拉框变成之前的值
  • 2025年跨端云真机测试平台深度测评:XR与折叠屏时代的兼容性之战
  • 《量化开发》系列 第 1 篇:金融知识基础入门指南(附 GitHub 学习项目)
  • 什么是 BigKey?
  • 定时器的设计
  • 电源芯片之DCDC初探索ING
  • 用lines_gauss的width属性提取缺陷