跳到正文

个人记录

2048

Composite_Function约 13 分钟
原文
文章信息与版本

文章信息

发布
2022年5月6日
收录
2025年12月12日
更新
2026年11月10日
篇幅
约 13 分钟

版本

  1. v2当前
  2. v1

2048,重温经典

FGX第一款游戏

经典2048,唤回美好记忆

无需换行,合成更爽

C++
/*
	Section: 2
	Update: 无需换行
	Name: 2048小游戏
	Copyright: No
	Author: FGX
*/
#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
#include<string>
#include<fstream>
#include<time.h>
#include<ctime>
using namespace std;
long long mp[5][5],score=0,skin=1;
char choose;
long long Rand(long long l,long long r){
	return l+rand()%(r-l+1);
}
void new_mp(){
	long long x,y;
	do x=Rand(1,4),y=Rand(1,4);
	while(mp[x][y]!=0);
	mp[x][y]=1;
	return ;
}
bool check(){
	for(long long i=1;i<=4;i++)
		for(long long j=1;j<=4;j++)
			if(mp[i][j]==mp[i][j+1]||mp[i][j]==mp[i+1][j]||mp[i][j]==0) return true;
	return false;
}
long long cal(long long x){
	long long num=0;
	do num++,x/=10;
	while(x>0);
	return num;
}
void SetFont(double size) {
	CONSOLE_FONT_INFOEX cfi;
	cfi.cbSize=sizeof cfi;
	cfi.nFont=0;
	cfi.dwFontSize.X=0;
	cfi.dwFontSize.Y=size;  //设置字体大小
	cfi.FontFamily=FF_DONTCARE;
	cfi.FontWeight=FW_NORMAL; //字体粗细 FW_BOLD
	SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
	HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_FONT_INFO consoleCurrentFont;
	GetCurrentConsoleFont(handle,FALSE,&consoleCurrentFont);
}
void color(int Forg,int Back){
	WORD wColor=((Back & 0x0F)<<4)+(Forg & 0x0f);
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),wColor);
}
string picture(long long x){
	if(skin==1){
		if(x==0) return "     ";
		if(x==1) return "  1  ";
		if(x==2) return "  2  ";
		if(x==4) return "  4  ";
		if(x==8) return "  8  ";
		if(x==16) return " 1 6 ";
		if(x==32) return " 3 2 ";
		if(x==64) return " 6 4 ";
		if(x==128) return "1 2 8";
		if(x==256) return "2 5 6";
		if(x==512) return "5 1 2";
		if(x==1024) return "10 24";
		if(x==2048) return "20 48";
		if(x==4096) return "40 96";
		if(x==8192) return "81 92";
		if(x==16384) return "16384";
		if(x==32768) return "32768";
		if(x==65536) return "65536";
	}
	if(skin==2){
		if(x==0) return "     ";
		if(x==1) return " C E ";
		if(x==2) return " W J ";
		if(x==4) return " R E ";
		if(x==8) return "T L E";
		if(x==16) return "M L E";
		if(x==32) return "I L E";
		if(x==64) return "O L E";
		if(x==128) return "U K E";
		if(x==256) return " W A ";
		if(x==512) return " P C ";
		if(x==1024) return " A C ";
		if(x==2048) return " P E ";
		if(x==4096) return "D O J";
		if(x==8192) return "A J E";
		if(x==16384) return " A U ";
		if(x==32768) return " A K ";
		if(x==65536) return "I O I";
	}
}
void print(){
	cout<<"分数"<<score<<endl;
	cout<<"—————————————"<<endl;
	for(long long i=1;i<=4;i++){
		cout<<"|     |     |     |     |"<<endl;
		for(long long j=1;j<=4;j++){
			color(15,16),cout<<"|";
			if(mp[i][j]<=16) color(15,16);
			else if(mp[i][j]<=256) color(11,16);
			else if(mp[i][j]<=4096) color(5,16);
			else if(mp[i][j]<=65536) color(6,16);
			cout<<picture(mp[i][j]);
			color(15,16);
		}
		cout<<"|"<<endl;
		cout<<"|     |     |     |     |"<<endl;
		cout<<"—————————————"<<endl;
	}
}
int main(){
	SetFont(30);
	cout<<"Name: 2048小游戏 "<<endl;
	cout<<"Copyright: No "<<endl;
	cout<<"Author: FGX"<<endl;
	cout<<"—————开始游戏—————"<<endl ;
	cout<<"欢迎来到2048小游戏"<<endl;
	cout<<"请勿修改数据"<<endl;
	cout<<"祝您玩得愉快"<<endl;
	Sleep(1000);
	while(true){
		system("cls");
		cout<<"1.开始游戏  2.更改皮肤"<<endl;
		choose=getch();
		system("cls");
		if(choose=='1'){
			for(int i=1;i<=4;++i)
				for(int j=1;j<=4;++j)
					mp[i][j]=0;
			score=0;
			new_mp(),new_mp();
			while(check()){
				bool flag=false;
				print();
				cout<<"你的操作(上,下,左,右):"<<endl;
				do choose=getch();
				while((int)choose!=72&&(int)choose!=80&&(int)choose!=75&&(int)choose!=77);
				if((int)choose==72){
					for(int i=1;i<=4;i++)
						for(int j=1;j<=4;j++)
							for(int x=i;x>1;x--)
								if(mp[x-1][j]==0){
									swap(mp[x][j],mp[x-1][j]);
									flag=true;
								}
					for(int i=2;i<=4;i++)
						for(int j=1;j<=4;j++)
							if(mp[i][j]==mp[i-1][j]&&mp[i][j]!=0){
								mp[i-1][j]*=2;
								mp[i][j]=0;
								score+=mp[i-1][j];
								flag=true;
							}
					for(int i=1;i<=4;i++)
						for(int j=1;j<=4;j++)
							for(int x=i;x>1;x--)
								if(mp[x-1][j]==0){
									swap(mp[x][j],mp[x-1][j]);
									flag=true;
								}
				}
				else if((int)choose==80){
					for(int i=4;i>=1;i--)
						for(int j=1;j<=4;j++)
							for(int x=i;x<4;x++)
								if(mp[x+1][j]==0){
									swap(mp[x][j],mp[x+1][j]);
									flag=true;
								}
					for(int i=3;i>=1;i--)
						for(int j=1;j<=4;j++)
							if(mp[i][j]==mp[i+1][j]&&mp[i][j]!=0){
								mp[i+1][j]*=2;
								mp[i][j]=0;
								score+=mp[i+1][j];
								flag=true;
							}
					for(int i=4;i>=1;i--)
						for(int j=1;j<=4;j++)
							for(int x=i;x<4;x++)
								if(mp[x+1][j]==0){
									swap(mp[x][j],mp[x+1][j]);
									flag=true;
								}
				}
				else if((int)choose==75){
					for(int i=1;i<=4;i++)
						for(int j=1;j<=4;j++)
							for(int y=j;y>1;y--)
								if(mp[i][y-1]==0){
									swap(mp[i][y],mp[i][y-1]);
									flag=true;
								}
					for(int i=1;i<=4;i++)
						for(int j=2;j<=4;j++)
							if(mp[i][j]==mp[i][j-1]&&mp[i][j]!=0){
								mp[i][j-1]*=2;
								mp[i][j]=0;
								score+=mp[i][j-1];
								flag=true;
							}
					for(int i=1;i<=4;i++)
						for(int j=1;j<=4;j++)
							for(int y=j;y>1;y--)
								if(mp[i][y-1]==0){
									swap(mp[i][y],mp[i][y-1]);
									flag=true;
								}
				}
				else if((int)choose==77){
					for(int i=1;i<=4;i++)
						for(int j=4;j>=1;j--)
							for(int y=j;y<4;y++)
								if(mp[i][y+1]==0){
									swap(mp[i][y],mp[i][y+1]);
									flag=true;
								}
					for(int i=1;i<=4;i++)
						for(int j=3;j>=1;j--)
							if(mp[i][j]==mp[i][j+1]&&mp[i][j]!=0){
								mp[i][j+1]*=2;
								mp[i][j]=0;
								score+=mp[i][j+1];
								flag=true;
							}
					for(int i=1;i<=4;i++)
						for(int j=4;j>=1;j--)
							for(int y=j;y<4;y++)
								if(mp[i][y+1]==0){
									swap(mp[i][y],mp[i][y+1]);
									flag=true;
								}
				}
				if(flag) new_mp();
				system("cls");
			}
			if(skin==1) cout<<"游戏结束"<<endl;
			else if(skin==2) cout<<"AFO"<<endl;
			cout<<"你得了"<<score<<"分"<<endl;
			Sleep(1000);
		}
		else if(choose=='2'){
			cout<<"1.传统数字(1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536)"<<endl;
			cout<<"2.程序员(CE WJ RE TLE MLE ILE OLE UKE WA PC AC PE DOJ SJE AU AK)"<<endl;
			do choose=getch();
			while(choose!='1'&&choose!='2');
			if(choose=='1') skin=1;
			else if(choose=='2') skin=2;
		}
	}
}

如果喜欢点个赞吧,顺便留言你玩得多好

要更新或有bug下方说⬇️哦

原文
存档评论8 条抓取于