牛刀小试 - C++ 实现2048(可存档)

2024-01-10 13:52

本文主要是介绍牛刀小试 - C++ 实现2048(可存档),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

参考文档

借助了这位大佬的开发思路, 开发过程中学到了很多
C语言实现《2048游戏》

技术点:

system调整控制台大小的问题

unsigned and 符号位

C++对齐输出(左对齐和右对齐)

C++ goto语句详解

完整代码

/*********************************************************************
程序名: 贪吃蛇
*********************************************************************/
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_NONSTDC_NO_DEPRECATE
#include <iostream> // 对应c中的#include <stdio.h>
#include <cstdlib>  // 对应C中的#include <stdlib.h>
#include <ctime>	// 对应c中的#include <time.h>
#include <conio.h> // 使用getch()函数
#include <windows.h> //使用Sleep()函数
#include <iomanip> // C++ 输出对齐函数
#include <fstream> // 文件操作头函数using namespace std;void Menu();//菜单
void Rule_of_game();//游戏规则 
void Begin(); //开始 
void Table();//打印4×4方格 
int Random_number1();//产生2或4的随机数 
int Random_number2();//产生0、1、2、3的随机数 
int Get(int *p_cnt, int score);//输入指令 
void Move();//保留上一次的棋盘布局 
int If_move();//判断是否移动 
int Over();//判断是否结束  
void Write_max(int score);//向2048游戏-最高记录写入最高分 
int Read_max();//读出最高分记录的文件
void Write_file(int *p_cnt, int scort);//存档 
int Read_file(int *p_cnt);//读档 
void again();//是否继续游戏
int color(int c);//更改颜色
void box_color(int x);//不同数值对应不同颜色 int a[4][4] = { 0 };// 定义全局的二维数组 
int A[4][4]; //保留上一步的棋局 char *name_max = "2048游戏-最高记录.txt";
char *name_file = "[山海]-2048游戏.txt";int main()
{system("mode con cols=90 lines=30");  //调整控制台大小system("title 2048超级大大型游戏"); //程序标题 system("color F0");//F 表示背景颜色,0表示前景颜色Menu();system("PAUSE");return 0;
}void Menu() //菜单
{cout << "\t\t║   欢迎使用由[山海]制作的2048超级大大型游戏   ║\n";cout << "\t\t╟──────────────────────────────────────────────╢\n";cout << "\t\t║请输入选项:                                  ║\n";cout << "\t\t║              ┌───┐                           ║\n";cout << "\t\t║              │ 1 │ 游戏规则                  ║\n";cout << "\t\t║              └───┘                           ║\n";cout << "\t\t║              ┌───┐                           ║\n";cout << "\t\t║              │ 2 │ 开始游戏                  ║\n";cout << "\t\t║              └───┘                           ║\n";cout << "\t\t║              ┌───┐                           ║\n";cout << "\t\t║              │ 3 │ 继续游戏                  ║\n";cout << "\t\t║              └───┘                           ║\n";cout << "\t\t║              ┌───┐                           ║\n";cout << "\t\t║              │ 4 │ 退出游戏                  ║\n";cout << "\t\t║              └───┘                           ║\n";cout << "\t\t╚══════════════════════════════════════════════╝\n";int x = 1, max = 0;while (x){switch (getch())		// getch()函数检测按键。{case '1':x = 0;Rule_of_game();//游戏规则Menu();break;case '2':x = 0;system("cls");//清屏 int h, l;for (h = 0; h < 4; h++){for (l = 0; l < 4; l++){a[h][l] = 0;A[h][l] = 0;}}Begin(); //开始游戏 break;case '3':x = 0;system("cls");//清屏max = Read_max();//读取之前的最高记录 Begin(); //开始游戏 break;case '4':exit(0);default:cout << "输入了非法选项,请重新选择!" << endl;}}
}void Rule_of_game()
{system("cls");//清屏 cout << "╔══════════════════════════════════════════════════════════════════════════════════╗" << endl;cout << "║本游戏通过按键W、S、A、D(不区分大小写)四个键分别控制滑块上移、下移、左移和右移。 ║" << endl;cout << "║滑块移动的过程中,如有相邻且相等数字的滑块将会相加,并获得相应的分数。            ║" << endl;cout << "║当棋盘上的所有位置均有数字,且不能合成,则游戏结束。本游戏未设置游戏成功。         ║"<< endl;cout << "║游戏过程中输入I即为存档并退出游戏,输入O即为读档,输入P则退出游戏。               ║"<< endl;cout << "║“开始游戏”则清除最高记录及存档、“继续游戏”则保存之前的最高记录,且可以读档。       ║" << endl;cout << "╚══════════════════════════════════════════════════════════════════════════════════╝" << endl;system("pause");//暂停 system("cls");//清屏 
}void Begin()
{int score = 0;int sign = 1;int h, l, cnt = 0;int *p_cnt = &cnt;while (1){cout << "\t\t╔══════════════════════════════════════════════╗" << endl;cout << "\t\t║   欢迎使用由[山海]制作的2048超级大大型游戏  ║" << endl;cout << "\t\t╟──────────────────────────────────────────────╢" << endl;cout << "\t\t║移动:   ┌───┐        存档: 读档: 退出:  ║" << endl;cout << "\t\t║         │ W │                                ║" << endl;cout << "\t\t║     ┌───┼───┼───┐    ┌───┐ ┌───┐ ┌───┐       ║" << endl;cout << "\t\t║     │ A │ S │ D │    │ I │ │ O │ │ P │       ║" << endl;cout << "\t\t║     └───┴───┴───┘    └───┘ └───┘ └───┘       ║" << endl;cout << "\t\t╚══════════════════════════════════════════════╝" << endl;while (1){if (sign == 1){h = Random_number2();l = Random_number2();if (a[h][l] == 0) {a[h][l] = Random_number1();cnt++;break;}}else{break;}}Table(); // 打印棋盘int max = Read_max();printf("\t\t移动的步数:%d\t当前得分:%d\n\t\t最高记录:%d\n", cnt - 1, score, max);Move(); //保留棋盘score += Get(p_cnt, score);//得分if (score > max){Write_max(score);//保存此次记录}sign = If_move();//判断棋盘是否有变动 if (Over() == 0){if (max < score){//打破记录 Write_max(score);//保存此次记录cout << "\t\t恭喜您已打破记录!" << endl;}break;}system("cls"); //清屏}
}int Get(int *p_cnt,int score)
{int h, l, t, sum = 0;char ch;ch = getch();//输入字符 switch (ch){/*滑块向上移动*/case 'w':case 'W':/*向上聚集数字*/for (l = 0; l < 4; l++){for (t = 0; t < 4; t++){for (h = 3; h > 0; h--){if (a[h - 1][l] == 0){a[h - 1][l] = a[h][l];a[h][l] = 0;}}}}/*竖直方向上,相邻且相同的数字相加*/for (l = 0; l < 4; l++){for (h = 0; h < 3; h++){if (a[h][l] == a[h + 1][l]){a[h][l] += a[h + 1][l];a[h + 1][l] = 0;sum += a[h][l];}}}/*向上聚集数字*/for (l = 0; l < 4; l++){for (t = 0; t < 4; t++){for (h = 3; h > 0; h--){if (a[h - 1][l] == 0){a[h - 1][l] = a[h][l];a[h][l] = 0;}}}}break;case 's':case 'S':for (l = 0; l < 4; l++){for (t = 0; t < 4; t++){for (h = 0; h < 3; h++){if (a[h + 1][l] == 0){a[h + 1][l] = a[h][l];a[h][l] = 0;}}}}for (l = 0; l < 4; l++){for (h = 3; h > 0; h--){if (a[h][l] == a[h - 1][l]){a[h][l] += a[h - 1][l];a[h - 1][l] = 0;sum += a[h][l];}}}for (l = 0; l < 4; l++){for (t = 0; t < 4; t++){for (h = 0; h < 3; h++){if (a[h + 1][l] == 0){a[h + 1][l] = a[h][l];a[h][l] = 0;}}}}break;case 'a':case 'A':for (h = 0; h < 4; h++){for (t = 0; t < 4; t++){for (l = 3; l > 0; l--){if (a[h][l - 1] == 0){a[h][l - 1] = a[h][l];a[h][l] = 0;}}}}for (h = 0; h < 4; h++){for (l = 0; l < 3; l++){if (a[h][l] == a[h][l + 1]){a[h][l] += a[h][l + 1];a[h][l + 1] = 0;sum += a[h][l];}}}for (h = 0; h < 4; h++){for (t = 0; t < 4; t++){for (l = 3; l > 0; l--){if (a[h][l - 1] == 0){a[h][l - 1] = a[h][l];a[h][l] = 0;}}}}break;case 'd':case 'D':for (h = 0; h < 4; h++){for (t = 0; t < 4; t++){for (l = 0; l < 3; l++){if (a[h][l + 1] == 0){a[h][l + 1] = a[h][l];a[h][l] = 0;}}}}for (h = 0; h < 4; h++){for (l = 3; l > 0; l--){if (a[h][l] == a[h][l - 1]){a[h][l] += a[h][l - 1];a[h][l - 1] = 0;sum += a[h][l];}}}for (h = 0; h < 4; h++){for (t = 0; t < 4; t++){for (l = 0; l < 3; l++){if (a[h][l + 1] == 0){a[h][l + 1] = a[h][l];a[h][l] = 0;}}}}break;case 'i':case 'I':       //存档当前游戏格局 Write_file(p_cnt, score);//调用存档函数 break;case 'o':case 'O':sum = Read_file(p_cnt);//读档break;case 'p':case 'P':system("cls"); //清屏Menu();//回到菜单 default:break;}return sum;
}void Table()
{int h, l;for (h = 0; h < 4; h++){if (h == 0){printf("\t\t\t┏━━━━━━┳━━━━━━┳━━━━━━┳━━━━━━┓\n");}else {printf("\t\t\t┣━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━┫\n");}printf("\t\t\t┃      ┃      ┃      ┃      ┃\n");for (l = 0; l < 4; l++){if (a[h][l] == 0){//如果二维数组与棋盘对应的位置为零,则不放入数据 if (l == 0){printf("\t\t\t┃      ");}else {printf("┃      ");}}else{//否则放入与二维数组对应的数据 if (l == 0){printf("\t\t\t┃");box_color(a[h][l]);//判断滑块的数值,给对应颜色 printf("%5d ", a[h][l]);color(0);//恢复白色背景 }else {printf("┃");box_color(a[h][l]);//判断滑块的数值,给对应颜色 printf("%5d ", a[h][l]);color(0);//恢复白色背景 }}}printf("┃\n");printf("\t\t\t┃      ┃      ┃      ┃      ┃\n");}printf("\t\t\t┗━━━━━━┻━━━━━━┻━━━━━━┻━━━━━━┛\n");
}int Random_number1() //随机生成2 or 4
{int s;srand(time(0));s = ((unsigned)rand() % 2);s = s == 0 ? 2 : 4;return s;
}int Random_number2() //随机生成二维数组的下标
{int x;x = ((unsigned)rand() % 4);return x;
}void Move()
{int h, l;for (h = 0; h < 4; h++){for (l = 0; l < 4; l++){A[h][l] = a[h][l];}}
}int If_move()
{int h, l, sign = 0;for (h = 0; h < 4; h++){for (l = 0; l < 4; l++){if (a[h][l] != A[h][l]){//未进入条件框,说明滑块没有发生移动 sign = 1;goto out;}}}out:return sign;
}int Over()
{int over = 0;int h, l;for (h = 0; h < 4; h++){for (l = 0; l < 4; l++){if (a[h][l] == 0){//有空格 over = 1;//游戏继续 return 1;}}}if (over == 0){//没有空格 for (h = 0; h < 3; h++){for (l = 0; l < 3; l++){if (a[h][l] == a[h][l + 1]){over = 1;//游戏继续 break;}else if (a[h][l] == a[h + 1][l]){over = 1;//游戏继续 break;}}if (over == 1){break;}}}return over;//游戏结束 
}void Write_max(int score)//写入文件 
{ofstream fout(name_max);        //读写操作,对打开的文件可进行读写操作 fout << score;fout.close();
}int Read_max() // 读取文件
{int max_score = 0;ifstream fin(name_max, ios::in);if (!fin){cout << "\t\t读取失败!程序即将自动退出 . . .";Sleep(3000);//睡眠3000毫秒,即停留3秒钟 exit(0); //退出程序}else{fin >> max_score;fin.close();}return max_score;//返回最高记录的值 
}void Write_file(int *p_cnt, int score)
{int h, l;ofstream fout(name_file);   if (!fout){cout << "\t\t存档失败!程序即将自动退出 . . ." << endl;Sleep(3000);//睡眠3000毫秒,即停留3秒钟 exit(0);}else{for (h = 0; h < 4; h++){for (l = 0; l < 4; l++){fout << a[h][l] << " ";}}}fout << score << " ";fout << *p_cnt - 1;	cout << "\t\t存档成功!游戏即将自动退出 . . ." << endl;Sleep(3000); //睡眠3000毫秒,即停留3秒钟 exit(0);fout.close();
}int Read_file(int *p_cnt)
{int h, l, score = 0;ifstream fin(name_file , ios::in);if (!fin){cout << "\t\t读档失败!程序即将自动退出 . . ." << endl;Sleep(3000); //睡眠3000毫秒,即停留3秒钟 exit(0);}else{for (h = 0; h < 4; h++){for (l = 0; l < 4; l++){fin >> a[h][l];}}}fin >> score;fin >> *p_cnt;fin.close();Table(); // 打印棋盘return score;
}void again()
{cout << "\t\t╔══════════════════════════════════════════════╗" << endl;cout << "\t\t║                   您已失败                    ║" << endl;cout << "\t\t╟──────────────────────────────────────────────╢" << endl;cout << "\t\t║是否继续:                                     ║" << endl;cout << "\t\t║          ┌───┐                               ║" << endl;cout << "\t\t║          │ 1 │ 扶我起来,我还能再来一局!      ║" << endl;cout << "\t\t║          └───┘                               ║" << endl;cout << "\t\t║          ┌───┐                               ║" << endl;cout << "\t\t║          │ 2 │ 不了不了,我要学习!            ║" << endl;cout << "\t\t║          └───┘                               ║" << endl;cout << "\t\t╚══════════════════════════════════════════════╝" << endl;if (getch() == '1'){system("cls"); // 清屏int h, l;for (h = 0; h < 4; h++){for (l = 0; l < 4; l++){a[h][l] = 0;A[h][l] = 0;}}Menu();//回到菜单 }else {cout << "\t\t游戏结束!" << endl;}
}
int color(int c)
{SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY |BACKGROUND_BLUE |BACKGROUND_RED |BACKGROUND_GREEN |c);return 0;
}void box_color(int x)
{switch (x){case 2:color(8);break;case 4:color(0);break;case 8:color(2);break;case 16:color(6);break;case 32:color(10);break;case 64:color(3);break;case 128:color(9);break;case 256:color(11);break;case 512:color(5);break;case 1024:color(13);break;case 2048:color(12);break;default:color(4);break;}
}

运行示例

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

这篇关于牛刀小试 - C++ 实现2048(可存档)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Flutter实现文字镂空效果的详细步骤

《Flutter实现文字镂空效果的详细步骤》:本文主要介绍如何使用Flutter实现文字镂空效果,包括创建基础应用结构、实现自定义绘制器、构建UI界面以及实现颜色选择按钮等步骤,并详细解析了混合模... 目录引言实现原理开始实现步骤1:创建基础应用结构步骤2:创建主屏幕步骤3:实现自定义绘制器步骤4:构建U

SpringBoot中四种AOP实战应用场景及代码实现

《SpringBoot中四种AOP实战应用场景及代码实现》面向切面编程(AOP)是Spring框架的核心功能之一,它通过预编译和运行期动态代理实现程序功能的统一维护,在SpringBoot应用中,AO... 目录引言场景一:日志记录与性能监控业务需求实现方案使用示例扩展:MDC实现请求跟踪场景二:权限控制与

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

C#如何调用C++库

《C#如何调用C++库》:本文主要介绍C#如何调用C++库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录方法一:使用P/Invoke1. 导出C++函数2. 定义P/Invoke签名3. 调用C++函数方法二:使用C++/CLI作为桥接1. 创建C++/CL

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环