C++2048游戏

2024-03-27 20:08
文章标签 c++ 游戏 2048

本文主要是介绍C++2048游戏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近听说2048游戏挺好玩就上网找个玩,结果没有一个完整的所以我只好自己写一个了。
源代码:

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#include<time.h>int null_count();//统计还有几个空格
void start_game();//开始游戏
void reset_game();//重置游戏,不清空记录
void move_left();//向左移动
void move_right();//向右移动
void move_up();//向上移动
void move_down();//向下移动
void refresh_show();//刷新显示屏
void add_rand_num();//添加随机数
void check_if_over();//检查游戏是否结束int board[4][4];
int score = 0;
int best = 0;
int if_need_add_num;
int if_game_over;
int i;
int j;
int k;int main() {start_game();
}void setColor(unsigned short ForeColor = 7, unsigned short BackGroundColor = 0)
{HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleTextAttribute(handle, ForeColor + BackGroundColor * 0x10);
}void start_game() {reset_game();char fr;while (1) {fr = _getch();//从键盘读数if (if_game_over) {if (fr == 'y' || fr == 'Y') {reset_game();continue;}else if (fr == 'n' || fr == 'N') {return;}else continue;}if_need_add_num = 0;switch (fr) {case 'a':case 'A':move_left();break;case 'd':case 'D':move_right();break;case 'w':case 'W':move_up();break;case 's':case 'S':move_down();break;}if (score > best) {best = score;}if (if_need_add_num) {add_rand_num();refresh_show();}}
}int null_count() {int n = 0;for (i = 0; i < 4; i++) {for (j = 0; j < 4; j++) {if (board[i][j] == 0) {n++;}}}return n;
}void reset_game() {score = 0;if_need_add_num = 1;if_game_over = 0;srand(time(0));int n = rand() % 16;for (i = 0; i < 4; i++) {for (j = 0; j < 4; j++) {if (n == 0) {int k = rand() % 3;if (k == 0 || k == 1) {board[i][j] = 2;}else {board[i][j] = 4;}n--;}else {board[i][j] = 0;n--;}}}add_rand_num();system("cls");refresh_show();
}void move_left() {for (i = 0; i < 4; i++) {for (j = 1, k = 0; j < 4; j++) {if (board[i][j] > 0) {if (board[i][k] == board[i][j]) {board[i][k] *= 2;k++;score = score + 2 * board[i][j];board[i][j] = 0;if_need_add_num = 1;}else if (board[i][k] == 0) {board[i][k] = board[i][j];board[i][j] = 0;if_need_add_num = 1;}else {board[i][k + 1] = board[i][j];if ((k + 1) != j) {board[i][j] = 0;if_need_add_num = 1;}k++;}}}}
}void move_right() {for (i = 0; i < 4; i++) {for (j = 2, k = 3; j >= 0; j--) {if (board[i][j] > 0) {if (board[i][k] == board[i][j]) {board[i][k] *= 2;k--;score = score + 2 * board[i][j];board[i][j] = 0;if_need_add_num = 1;}else if (board[i][k] == 0) {board[i][k] = board[i][j];board[i][j] = 0;if_need_add_num = 1;}else {board[i][k - 1] = board[i][j];if ((k - 1) != j) {board[i][j] = 0;if_need_add_num = 1;}k--;}}}}
}void move_up() {for (i = 0; i < 4; i++) {for (j = 1, k = 0; j < 4; j++) {if (board[j][i] > 0) {if (board[k][i] == board[j][i]) {board[k][i] *= 2;k++;score = score + 2 * board[j][i];board[j][i] = 0;if_need_add_num = 1;}else if (board[k][i] == 0) {board[k][i] = board[j][i];board[j][i] = 0;if_need_add_num = 1;}else {board[k + 1][i] = board[j][i];if ((k + 1) != j) {board[j][i] = 0;if_need_add_num = 1;}k++;}}}}
}void move_down() {for (i = 0; i < 4; i++) {for (j = 2, k = 3; j >= 0; j--) {if (board[j][i] > 0) {if (board[k][i] == board[j][i]) {board[k][i] *= 2;k--;score = score + 2 * board[j][i];board[j][i] = 0;if_need_add_num = 1;}else if (board[k][i] == 0) {board[k][i] = board[j][i];board[j][i] = 0;if_need_add_num = 1;}else {board[k - 1][i] = board[j][i];if ((k - 1) != j) {board[j][i] = 0;if_need_add_num = 1;}k--;}}}}
}void refresh_show() {COORD pos = { 0, 0 };SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);printf("GAME: 2048     SCORE: %6d    BEST: %6d\n", score, best);printf("*************************************************************\n\n");printf("       ┌───┬───┬───┬───┐\n");for (int i = 0; i < 4; i++){printf("       │");for (int j = 0; j < 4; j++) {if (board[i][j] != 0){if (board[i][j] < 10){if (board[i][j] == 2){setColor(14, 0);}else if (board[i][j] == 4){setColor(6, 0);}else if (board[i][j] == 8){setColor(12, 0);}printf("  %d", board[i][j]);setColor(7, 0);printf("   │");}else if (board[i][j] < 100){if (board[i][j] == 16){setColor(4, 0);}else if (board[i][j] == 32){setColor(10, 0);}else if (board[i][j] == 64){setColor(2, 0);}printf("  %d", board[i][j]);setColor(7, 0);printf("  │");}else if (board[i][j] < 1000){if (board[i][j] == 128){setColor(9, 0);}else if (board[i][j] == 256){setColor(1, 0);}else if (board[i][j] == 512){setColor(13, 0);}printf("  %d", board[i][j]);setColor(7, 0);printf(" │");}else if (board[i][j] < 10000){if (board[i][j] == 1024){setColor(5, 0);}else{setColor(15, 0);}printf(" %4d", board[i][j]);setColor(7, 0);printf(" │");}else{int n = board[i][j];for (int k = 1; k < 30; k++){n /= 2;if (n == 1){setColor(7, 0);printf("2^%2d |", k);break;}}}}else{setColor(0, 0);printf("     ");setColor(7, 0);printf(" │");}}setColor(7, 0);if (i <= 2){printf("\n       ├───┼───┼───┼───┤\n");}else printf("\n       └───┴───┴───┴───┘\n\n");}printf("*************************************************************\n\n");printf("w↑ ↓s  a←  →d    输入方向键后请按回车读入:  ");if (null_count() == 0) {check_if_over();if (if_game_over) {printf("\nGAME OVER,TRY AGAIN?[Y/N]:");}}
}void add_rand_num() {srand(time(0));int n = rand() % null_count();for (int i = 0; i < 4; i++) {for (int j = 0; j < 4; j++) {if (board[i][j] == 0) {if (n != 0) {n--;}else {int k = rand() % 3;if (k == 0 || k == 1) {board[i][j] = 2;return;}else {board[i][j] = 4;return;}}}}}
}void check_if_over() {for (int i = 0; i < 4; i++) {for (int j = 0; j < 3; j++) {if (board[i][j] == board[i][j + 1] || board[j][i] == board[j + 1][i]) {if_game_over = 0;return;}}}if_game_over = 1;
}

以上则是代码,制作不易请勿乱喷。

这篇关于C++2048游戏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/853247

相关文章

C++ 中的 if-constexpr语法和作用

《C++中的if-constexpr语法和作用》if-constexpr语法是C++17引入的新语法特性,也被称为常量if表达式或静态if(staticif),:本文主要介绍C++中的if-c... 目录1 if-constexpr 语法1.1 基本语法1.2 扩展说明1.2.1 条件表达式1.2.2 fa

C++中::SHCreateDirectoryEx函数使用方法

《C++中::SHCreateDirectoryEx函数使用方法》::SHCreateDirectoryEx用于创建多级目录,类似于mkdir-p命令,本文主要介绍了C++中::SHCreateDir... 目录1. 函数原型与依赖项2. 基本使用示例示例 1:创建单层目录示例 2:创建多级目录3. 关键注

C++从序列容器中删除元素的四种方法

《C++从序列容器中删除元素的四种方法》删除元素的方法在序列容器和关联容器之间是非常不同的,在序列容器中,vector和string是最常用的,但这里也会介绍deque和list以供全面了解,尽管在一... 目录一、简介二、移除给定位置的元素三、移除与某个值相等的元素3.1、序列容器vector、deque

C++常见容器获取头元素的方法大全

《C++常见容器获取头元素的方法大全》在C++编程中,容器是存储和管理数据集合的重要工具,不同的容器提供了不同的接口来访问和操作其中的元素,获取容器的头元素(即第一个元素)是常见的操作之一,本文将详细... 目录一、std::vector二、std::list三、std::deque四、std::forwa

C++字符串提取和分割的多种方法

《C++字符串提取和分割的多种方法》在C++编程中,字符串处理是一个常见的任务,尤其是在需要从字符串中提取特定数据时,本文将详细探讨如何使用C++标准库中的工具来提取和分割字符串,并分析不同方法的适用... 目录1. 字符串提取的基本方法1.1 使用 std::istringstream 和 >> 操作符示

C++原地删除有序数组重复项的N种方法

《C++原地删除有序数组重复项的N种方法》给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度,不要使用额外的数组空间,你必须在原地修改输入数组并在使用O(... 目录一、问题二、问题分析三、算法实现四、问题变体:最多保留两次五、分析和代码实现5.1、问题分析5.

C++ 各种map特点对比分析

《C++各种map特点对比分析》文章比较了C++中不同类型的map(如std::map,std::unordered_map,std::multimap,std::unordered_multima... 目录特点比较C++ 示例代码 ​​​​​​代码解释特点比较1. std::map底层实现:基于红黑

C++中函数模板与类模板的简单使用及区别介绍

《C++中函数模板与类模板的简单使用及区别介绍》这篇文章介绍了C++中的模板机制,包括函数模板和类模板的概念、语法和实际应用,函数模板通过类型参数实现泛型操作,而类模板允许创建可处理多种数据类型的类,... 目录一、函数模板定义语法真实示例二、类模板三、关键区别四、注意事项 ‌在C++中,模板是实现泛型编程

利用Python和C++解析gltf文件的示例详解

《利用Python和C++解析gltf文件的示例详解》gltf,全称是GLTransmissionFormat,是一种开放的3D文件格式,Python和C++是两个非常强大的工具,下面我们就来看看如何... 目录什么是gltf文件选择语言的原因安装必要的库解析gltf文件的步骤1. 读取gltf文件2. 提

C++快速排序超详细讲解

《C++快速排序超详细讲解》快速排序是一种高效的排序算法,通过分治法将数组划分为两部分,递归排序,直到整个数组有序,通过代码解析和示例,详细解释了快速排序的工作原理和实现过程,需要的朋友可以参考下... 目录一、快速排序原理二、快速排序标准代码三、代码解析四、使用while循环的快速排序1.代码代码1.由快