跳到正文

剪贴板

剪贴板 gw8pj5sw

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<conio.h>
#include<ctime>
#include<cmath>
#include<windows.h>
using namespace std;
int score=0,best=0;
int premove[5][5];
void SCAB(int ForgC, int BackC){
	WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
}
char instruction[36][35]={
	"Play as 2048!",
	"get AK to win!",
	" ",
	"Use arrow keys or",
	"WASD to move block",
	"Use R to restart game",
	"Use Q to quit game",
	" ",
	"BLOCKS",
	"CE=2 compile error",
	"@=4 judging",
	"RE=8 runtime error",
	"TLE=16 time limit exceeded",
	"MLE=32 memory limit exceeded",
	"ILE=64 idleness limit exceeded",
	"OLE=128 output limit exceeded",
	"UKE=256 unknown error",
	"WA=512 wrong answer",
	"PC=1024 partly correct",
	"AC=2048 accepted",
	"PE=4096 presentation error",
	"DOJ=8192 denial of judging",
	"SJE=16384 special judge error",
	"AU=32768",
	"AK=65536 (win)",
	"#=*1 pragma...",
	"O2=*2",
	"O3=*4",
	" ",
	"made by jiangpeiyuan",
	"original 2048:",
	"https://2048game.com/",
	"original AK-IOI:",
	"https://ak-ioi.com/apps/oi-2048/",
	" ",
	"Hope You Like It!"
};
char endskin[14][61]={
	"+----------------------------------------------------------+",
	"|                  A    FFFFF   OOO     !                  |",
	"|   #   #         A A   F      O   O    !          #   #   |",
	"|                A   A  FFFFF  O   O    !                  |",
	"|   /---\\        AAAAA  F      O   O               /---\\   |",
	"|  /     \\       A   A  F       OOO     !         /     \\  |",
	"+----------------------------------------------------------+",
	"+-+-+-+-+-+-+-+-+-+-+-+-+-+-+--+-+-+-+-+-+-+-+-+-+-+-+-+-+-+",
	"|.........................A....K..KK.......................|",
	"+...#...#................A.A...K.K.................#...#...+",
	"|................-----..A...A..KK.....-----................|",
	"+..\\...../..............AAAAA..K.K................\\...../..+",
	"|...\\---/...............A...A..K..KK...............\\---/...|",
	"+-+-+-+-+-+-+-+-+-+-+-+-+-+-+--+-+-+-+-+-+-+-+-+-+-+-+-+-+-+"
};
int color_type[20]={0,6,1,5,13,13,13,13,0,4,3,2,12,10,8,14,9,15,14,9};
char scorenum[9][41]={
	"----------------------------------------",
	"                                        ",
	"###  #  ##  ##  # # ###  ## ### ###  ## ",
	"# # ##    #   # # # #   #     # # # # # ",
	"# #  #   #  ##  ### ##  ##    #  #   ## ",
	"# #  #  #     #   #   # # #  #  # #   # ",
	"### ### ### ##    # ##  ##  #   ###   # ",
	"                                        ",
	"----------------------------------------"
};
char intro[9][30]={
	"@.@.@.@.@.@.@.@.@.@.@.@.@.@.@",
	".                           .",
	"@   #   ]  ]]   III OOO III @",
	".  # #  ] ]      I  O O  I  .",
	"@ #   # ]]    @  I  O O  I  @",
	". ##### ] ]      I  O O  I  .",
	"@ #   # ]  ]]   III OOO III @",
	".                           .",
	"@.@.@.@.@.@.@.@.@.@.@.@.@.@.@"
};
char endd[5][14]={
	" BBBB    JJJ ",
	" B   B    J  ",
	" BBBB     J  ",
	" B   B J  J  ",
	" BBBB   JJ   "
};
char better[36][16]={
	"~~~~~~~~~~~~~~~","~~~~~~~~~~~~~~~","~~~~~~~~~~~~~~~","~~~~~~~~~~~~~~~","~~~~~~~~~~~~~~~","~~~~~~~~~~~~~~~","~~~~~~~~~~~~~~~","~~~~~~~~~~~~~~~","~~~~~~~~~~~~~~~",
	"~~~~~~~~~~~~~~~","~~~~~~~~~~~~~~~","~~~~~~#~#~~~~~~","~~~~~#####~~~~~","~~~~~~#~#~~~~~~","~~~~~#####~~~~~","~~~~~~#~#~~~~~~","~~~~~~~~~~~~~~~","~~~~~~~~~~~~~~~",
	"~~~~~~~~~~~~~~~","~~~~~~~~~~~~~~~","~~&&&&&~&&&&&~~","~~&~~~&~~~~~&~~","~~&~~~&~&&&&&~~","~~&~~~&~&~~~~~~","~~&&&&&~&&&&&~~","~~~~~~~~~~~~~~~","~~~~~~~~~~~~~~~",
	"~~~~~~~~~~~~~~~","~~~~~~~~~~~~~~~","~~@#@#@~@#@#@~~","~~#~~~#~~~~~#~~","~~@~~~@~@#@#@~~","~~#~~~#~~~~~#~~","~~@#@#@~@#@#@~~","~~~~~~~~~~~~~~~","~~~~~~~~~~~~~~~"
};
char tiles[160][16]={
	"~~~~~~~~~~~~~~~","~~~~~~~~~~~~~~~","~~~~~~~~~~~~~~~","~~~~~~~~~~~~~~~","~~~~~~~~~~~~~~~","~~~~~~~~~~~~~~~","~~~~~~~~~~~~~~~","~~~~~~~~~~~~~~~","~~~~~~~~~~~~~~~",
	"@@@@@@@@@@@@@@@","@             @","@  |||| ||||| @","@ |     |     @","@ |     ||||| @","@ |     |     @","@  |||| ||||| @","@             @","@@@@@@@@@@@@@@@",
	"@@@@@@@@@@@@@@@","@        *    @","@   $$$$$**   @","@  $     *    @","@             @","@    *     $  @","@   **$$$$$   @","@    *        @","@@@@@@@@@@@@@@@",
	"&&&&&&&&&&&&&&&","&             &","& !!!!  !!!!! &","& !   ! !     &","& !!!!  !!!!! &","& ! !   !     &","& !  !! !!!!! &","&             &","&&&&&&&&&&&&&&&",
	"&&&&&&&&&&&&&&&","&             &","& ### #   ### &","&  #  #   #   &","&  #  #   ### &","&  #  #   #   &","&  #  ### ### &","&             &","&&&&&&&&&&&&&&&",
	"###############","#.............#","#.%.%.%...%%%.#","#.%%%.%...%...#","#.%%%.%...%%%.#","#.%.%.%...%...#","#.%.%.%%%.%%%.#","#.............#","###############",
	"###############","#.............#","#.@@@.@...@@@.#","#..@..@...@...#","#..@..@...@@@.#","#..@..@...@...#","#.@@@.@@@.@@@.#","#.............#","###############",
	"]]]]]]]]]]]]]]]","].............]","].OOO.O...OOO.]","].O.O.O...O...]","].O.O.O...OOO.]","].O.O.O...O...]","].OOO.OOO.OOO.]","].............]","]]]]]]]]]]]]]]]",
	"]]]]]]]]]]]]]]]","].............]","].$.$.$.$.$$$.]","].$.$.$.$.$...]","].$.$.$$..$$$.]","].$.$.$.$.$...]","].$$$.$.$.$$$.]","].............]","]]]]]]]]]]]]]]]",
	"XXXXXXXXXXXXXXX","X-------------X","X-W-W-W---W---X","X-W-W-W--W-W--X","X-W-W-W-W---W-X","X-W-W-W-WWWWW-X","X--W-W--W---W-X","X-------------X","XXXXXXXXXXXXXXX",
	"XXXXXXXXXXXXXXX","X-------------X","X-@@@@---@@@@-X","X-@---@-@-----X","X-@@@@--@-----X","X-@-----@-----X","X-@------@@@@-X","X-------------X","XXXXXXXXXXXXXXX",
	"$$$$$$$$$$$$$$$","$-------------$","$---%----%%%%-$","$--%-%--%-----$","$-%---%-%-----$","$-%%%%%-%-----$","$-%---%--%%%%-$","$-------------$","$$$$$$$$$$$$$$$",
	"$$$$$$$$$$$$$$$","$-------------$","$-PPPP--PPPPP-$","$-P---P-P-----$","$-PPPP--PPPPP-$","$-P-----P-----$","$-P-----PPPPP-$","$-------------$","$$$$$$$$$$$$$$$",
	"///////////////","/&&&&&&&&&&&&&/","/&..&&...&&&.&/","/&.&.&.&.&&&.&/","/&.&.&.&.&&&.&/","/&.&.&.&.&.&.&/","/&..&&...&&.&&/","/&&&&&&&&&&&&&/","///////////////",
	"///////////////","/&&&&&&&&&&&&&/","/&&  &&& &   &/","/& &&&&& & &&&/","/&& &&&& &   &/","/&&& & & & &&&/","/&  &&& &&   &/","/&&&&&&&&&&&&&/","///////////////",
	"|-|-|-|-|-|-|-|","-@@@@@@@@@@@@@-","|@@@`@@@`@@@`@|","-@@`@`@@.@@@.@-","|@`@@@`@`@@@`@|","-@.`.`.@.@@@.@-","|@`@@@`@@.`.@@|","-@@@@@@@@@@@@@-","|-|-|-|-|-|-|-|",
	"---.---.---.---",".@#@#@#@#@#@#@.","-#@# #@# #@  #-",".@# # #@ @ @#@.","-# #@# #  @#@#-",".@     @ @ @#@.","-# #@# # #@  #-",".@#@#@#@#@#@#@.","---.---.---.---",
};
int a[5][5]={
	{1,2,3,4},
	{4,3,16,1},
	{6,5,3,5},
	{0,3,5,0}
};
char pressing;
void checkmovement(){
	int key;
	while(true){
		key=0;
		if(_kbhit) key=getch();
		if(key==72||key==119){
			pressing='u';
			break;
		}
		if(key==75||key==97){
			pressing='l';
			break;
		}
		if(key==115||key==80){
			pressing='d';
			break;
		}
		if(key==100||key==77){
			pressing='r';
			break;
		}
		if(key==113){
			pressing='Q';
			break;
		}
		if(key==114){
			pressing='R';
			break;
		}
	}
}
void print(){
	for(int i=0;i<9;i++){
		SCAB(color_type[i+9],0);
		printf("%s       ",intro[i]);
		int pf=100000;
		while(pf){
			for(int j=score/pf%10*4;j<=score/pf%10*4+3;j++){
				if(score<pf) SCAB(8,0);
				else SCAB(7,0);
				printf("%c",scorenum[i][j]);
			}
			pf/=10;
		}
		printf("\n");
	}
	printf("\n");
	for(int i=0;i<4;i++){
		for(int j=0;j<9;j++){
			for(int k=0;k<4;k++){
				if(a[i][k]>=0){
					SCAB(7,color_type[a[i][k]]);
					printf("%s",tiles[a[i][k]*9+j]);
				}
				else{
					SCAB(color_type[16+a[i][k]*-1],0);
					printf("%s",better[-a[i][k]*9+j]);
				}
			}
			SCAB(7,0);
			printf(" ");
			if(i*9+j>=9&&i*9+j<=24)
				SCAB(color_type[i*9+j-8],0);
			if(i*9+j==16) SCAB(7,0);
			printf("%s",instruction[i*9+j]);
			printf("\n");
		}
	}
	printf("\n");
	for(int i=0;i<5;i++){
		SCAB(0,2);
		printf("%s",endd[i]);
		SCAB(0,0);
		printf("                       ");
		int pf=100000;
		SCAB(0xe,0);
		while(pf){
			for(int j=best/pf%10*4;j<=best/pf%10*4+3;j++){
				printf("%c",scorenum[i+2][j]);
			}
			pf/=10;
		}
		printf("\n");
	}
}
int gameover;
void createrandomblock(){
	int emptyx[18],emptyy[18];
	int cnt=0;
	for(int i=0;i<4;i++){
		for(int j=0;j<4;j++){
			if(a[i][j]==0){
				cnt++;
				emptyx[cnt]=i;
				emptyy[cnt]=j;
			}
		}
	}
	srand(time(NULL));
	int num=rand()%cnt+1;
	int ran[16]={1,1,1,1,1,1,1,1,1,1,1,2,2,-1,-1,-2};
	a[emptyx[num]][emptyy[num]]=ran[rand()%16];

}
void startgame(){
	score=0;
	gameover=0;
	for(int i=0;i<4;i++){
		for(int j=0;j<4;j++)
			a[i][j]=0;
	}
	createrandomblock();
	createrandomblock();
}
void moveblock(){
	if(pressing=='l'){
		for(int i=0;i<4;i++){
			int putpoint=0;
			for(int j=0;j<4;j++){
				if(a[i][j]!=0){
					a[i][putpoint]=a[i][j];
					if(putpoint!=j) a[i][j]=0;
					putpoint++;
				}
			}
			for(int j=0;j<4;j++){
				if(a[i][j]>0&&a[i][j]<16&&a[i][j]==a[i][j+1]){
					a[i][j]++;
					a[i][j+1]=0;
					score+=pow(2,a[i][j]);
				}
				else if(a[i][j]==-2&&a[i][j]==a[i][j+1]){
					a[i][j]=-3;
					a[i][j+1]=0;
					score+=pow(2,a[i][j]);
				}
				else if(a[i][j]<=-1&&a[i][j+1]>0&&a[i][j+1]<16){
					if(premove[i][j]!=0&&premove[i][j+1]!=0){
						a[i][j]=a[i][j+1]-a[i][j]-1;
						a[i][j+1]=0;
						score+=pow(2,a[i][j]);
					}
				}
				else if(a[i][j+1]<=-1&&a[i][j]>0&&a[i][j]<16){
					if(premove[i][j]!=0&&premove[i][j+1]!=0){
						a[i][j]=a[i][j]-a[i][j+1]-1;
						a[i][j+1]=0;
						score+=pow(2,a[i][j]);
					}
				}
			}
			putpoint=0;
			for(int j=0;j<4;j++){
				if(a[i][j]!=0){
					a[i][putpoint]=a[i][j];
					if(putpoint!=j) a[i][j]=0;
					putpoint++;
				}
			}
		}
	}
	/////////////////////////////////
	else if(pressing=='r'){
		for(int i=0;i<4;i++){
			int putpoint=3;
			for(int j=3;j>=0;j--){
				if(a[i][j]!=0){
					a[i][putpoint]=a[i][j];
					if(putpoint!=j) a[i][j]=0;
					putpoint--;
				}
			}
			for(int j=3;j>=0;j--){
				if(a[i][j]>0&&a[i][j]==a[i][j-1]){
					a[i][j]++;
					a[i][j-1]=0;
					score+=pow(2,a[i][j]);
				}
				else if(a[i][j]==-2&&a[i][j]==a[i][j-1]){
					a[i][j]=-3;
					a[i][j-1]=0;
					score+=pow(2,a[i][j]);
				}
				else if(a[i][j]<=-1&&a[i][j-1]>0&&a[i][j-1]<16){
					if(premove[i][j]!=0&&premove[i][j-1]!=0){
						a[i][j]=a[i][j-1]-a[i][j]-1;
						a[i][j+1]=0;
						score+=pow(2,a[i][j]);
					}
				}
				else if(a[i][j-1]<=-1&&a[i][j]>0&&a[i][j]<16){
					if(premove[i][j]!=0&&premove[i][j-1]!=0){
						a[i][j]=a[i][j]-a[i][j-1]-1;
						a[i][j-1]=0;
						score+=pow(2,a[i][j]);
					}
				}
			}
			putpoint=3;
			for(int j=3;j>=0;j--){
				if(a[i][j]!=0){
					a[i][putpoint]=a[i][j];
					if(putpoint!=j) a[i][j]=0;
					putpoint--;
				}
			}
		}
	}
	////////////////////////
	else if(pressing=='u'){
		for(int i=0;i<4;i++){
			int putpoint=0;
			for(int j=0;j<4;j++){
				if(a[j][i]!=0){
					a[putpoint][i]=a[j][i];
					if(putpoint!=j) a[j][i]=0;
					putpoint++;
				}
			}
			for(int j=0;j<4;j++){
				if(a[j][i]>0&&a[j][i]==a[j+1][i]){
					a[j][i]++;
					a[j+1][i]=0;
					score+=pow(2,a[j][i]);
				}
				else if(a[j][i]==-2&&a[j][i]==a[j+1][i]){
					a[j][i]=-3;
					a[j+1][i]=0;
					score+=pow(2,a[j][i]);
				}
				else if(a[j][i]<=-1&&a[j+1][i]>0&&a[j+1][i]<16){
					if(premove[j][i]!=0&&premove[j+1][i]!=0){
						a[j][i]=a[j+1][i]-a[j][i]-1;
						a[j+1][i]=0;
						score+=pow(2,a[j][i]);
					}
				}
				else if(a[j+1][i]<=-1&&a[j][i]>0&&a[j][i]<16){
					if(premove[j][i]!=0&&premove[j+1][i]!=0){
						a[j][i]=a[j][i]-a[j+1][i]-1;
						a[j+1][i]=0;
						score+=pow(2,a[j][i]);
					}
				}
			}
			putpoint=0;
			for(int j=0;j<=3;j++){
				if(a[j][i]!=0){
					a[putpoint][i]=a[j][i];
					if(putpoint!=j) a[j][i]=0;
					putpoint++;
				}
			}
		}
	}
	////////////////////////////
	else if(pressing=='d'){
		for(int i=0;i<4;i++){
			int putpoint=3;
			for(int j=3;j>=0;j--){
				if(a[j][i]!=0){
					a[putpoint][i]=a[j][i];
					if(putpoint!=j) a[j][i]=0;
					putpoint--;
				}
			}
			for(int j=3;j>=0;j--){
				if(a[j][i]>0&&a[j][i]==a[j-1][i]){
					a[j][i]++;
					a[j-1][i]=0;
					score+=pow(2,a[j][i]);
				}
				else if(a[j][i]==-2&&a[j][i]==a[j-1][i]){
					a[j][i]=-3;
					a[j-1][i]=0;
					score+=pow(2,a[j][i]);
				}
				else if(a[j][i]<=-1&&a[j-1][i]>0&&a[j-1][i]<16){
					if(premove[j][i]!=0&&premove[j-1][i]!=0){
						a[j][i]=a[j-1][i]-a[j][i]-1;
						a[j-1][i]=0;
						score+=pow(2,a[j][i]);
					}
				}
				else if(a[j-1][i]<=-1&&a[j][i]>0&&a[j][i]<16){
					if(premove[j][i]!=0&&premove[j-1][i]!=0){
						a[j][i]=a[j][i]-a[j-1][i]-1;
						a[j-1][i]=0;
						score+=pow(2,a[j][i]);
					}
				}
			}
			putpoint=3;
			for(int j=3;j>=0;j--){
				if(a[j][i]!=0){
					a[putpoint][i]=a[j][i];
					if(putpoint!=j) a[j][i]=0;
					putpoint--;
				}
			}
		}
	}
}
void checkover(){
	bool flag1=true,flag2=false;
	for(int i=0;i<4;i++){
		for(int j=0;j<4;j++)
		{
			if(a[i][j]<=0) flag1=false;
			if(a[i][j]==16) flag2=true;
		}
	}
	for(int i=0;i<3;i++){
		for(int j=0;j<3;j++){
			if(a[i][j]==a[i+1][j]&&a[i][j]!=0) flag1=false;
			if(a[i][j]==a[i][j+1]&&a[i][j]!=0) flag1=false;
		}
	}
	if(flag1) gameover=1;
	if(flag2) gameover=2;
}
void over(){
system("cls");
for(int i=0;i<9;i++){
		SCAB(color_type[i+9],0);
		printf("%s       ",intro[i]);
		int pf=100000;
		while(pf){
			for(int j=score/pf%10*4;j<=score/pf%10*4+3;j++){
				if(score<pf) SCAB(8,0);
				else SCAB(7,0);
				printf("%c",scorenum[i][j]);
			}
			pf/=10;
		}
		printf("\n");
	}
	printf("\n");
	for(int i=0;i<4;i++){
		for(int j=0;j<9;j++){
			if(9*i+j>=15&&9*i+j<=21){
				if(gameover==1){
					SCAB(0,12);
					printf("%s",endskin[9*i+j-15]);
				}
				else{
					SCAB(0,11);
					printf("%s",endskin[9*i+j-15+7]);
				}
			}
			else{
				for(int k=0;k<4;k++){
					if(a[i][k]>=0){
						SCAB(7,color_type[a[i][k]]);
						printf("%s",tiles[a[i][k]*9+j]);
					}
					else{
						SCAB(color_type[16+a[i][k]*-1],0);
						printf("%s",better[-a[i][k]*9+j]);
					}
				}
			}
			SCAB(7,0);
			printf(" ");
			if(i*9+j>=9&&i*9+j<=24)
				SCAB(color_type[i*9+j-8],0);
			if(i*9+j==16) SCAB(7,0);
			printf("%s",instruction[i*9+j]);
			printf("\n");
		}
	}
	printf("\n");
	for(int i=0;i<5;i++){
		SCAB(0,2);
		printf("%s",endd[i]);
		SCAB(0,0);
		printf("                       ");
		int pf=100000;
		SCAB(0xe,0);
		while(pf){
			for(int j=best/pf%10*4;j<=best/pf%10*4+3;j++){
				printf("%c",scorenum[i+2][j]);
			}
			pf/=10;
		}
		printf("\n");
	}
}

int main(){
	while(true){
		startgame();
		system("cls");
		print();
		while(!gameover){
			checkmovement();
			if(pressing=='Q') exit(0);
			if(pressing=='R') break;
			for(int i=0;i<4;i++){
				for(int j=0;j<4;j++)
					premove[i][j]=a[i][j];
			}
			moveblock();
			system("cls");
			print();
			for(int i=1;i<=30000000;i++){
				i*=2;
				i/=2;
				i+=2;
				i-=2;
			}

			bool flag=false;
			for(int i=0;i<4;i++){
				for(int j=0;j<4;j++){
					if(a[i][j]!=premove[i][j]){
						flag=true;
					}
				}
			}
			best=max(best,score);
			if(flag){
				score++;
				createrandomblock();
				best=max(best,score);
				system("cls");
				print();
			}
			checkover();
		}
		if(gameover!=0){
			over();
			checkmovement();
			if(pressing=='Q') exit(0);
		}
	}
	return 0;
}