跳到正文

休闲·娱乐

2048(AKIOI版)

tanruiqing约 32 分钟
原文

这是 2025年10月17日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2025年1月24日
收录
2025年12月12日
更新
2026年9月19日
篇幅
约 32 分钟

版本

  1. v2当前
  2. v1

C++代码:

C++
#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
#define int long long
using namespace std;
const int chang = 5,wide = 5;

int maxx;
bool yu[10][10];
string ch[10][10];
string name[20] = {" ","S E","C E","Ign","R E","TLE","MLE","OLE","ILE","SJU","DOJ","P C","P E","UKE","W A","A C","A K"};
int sum[20] = {0,2,4,8,12,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536};
int ans[10][10];
bool f_c = 0;
int maxch[10][10];

void zm(int i,int j){
	if(ch[i][j] == "S E")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
	else if(ch[i][j] == "C E")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
	else if(ch[i][j] == "Ign")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 8);
	else if(ch[i][j] == "R E")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
	else if(ch[i][j] == "TLE")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
	else if(ch[i][j] == "MLE")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
	else if(ch[i][j] == "OLE")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);
	else if(ch[i][j] == "ILE")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
	else if(ch[i][j] == "SJU")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
	else if(ch[i][j] == "DOJ")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
	else if(ch[i][j] == "PC")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 3);
	else if(ch[i][j] == "PE")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 1);
	else if(ch[i][j] == "UKE")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 13);
	else if(ch[i][j] == "W A")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 5);
	for(int k = 0 ; k < ch[i][j].length() ; k++){
		cout << ch[i][j][k];
		if((ch[i][j] == "A C" || ch[i][j] == "A K") && k == 1){
			int sum = rand() % 13;
			sum++;
			if(sum == 9)sum = 10;
			if(sum == 10)sum = 11;
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), sum);
			cout << ch[i][j][k];
		}else if((ch[i][j] == "A C" || ch[i][j] == "A K") && k == 3){
			int sum = rand() % 13;
			sum++;
			if(sum == 9)sum = 10;
			if(sum == 10)sum = 11;
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), sum);
			cout << ch[i][j][k];
		}
	}
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
	return ;
}

void zm2(int i,int j){
	if(name[maxch[i][j]] == "S E")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
	else if(name[maxch[i][j]] == "C E")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
	else if(name[maxch[i][j]] == "Ign")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 8);
	else if(name[maxch[i][j]] == "R E")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
	else if(name[maxch[i][j]] == "TLE")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
	else if(name[maxch[i][j]] == "MLE")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
	else if(name[maxch[i][j]] == "OLE")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);
	else if(name[maxch[i][j]] == "ILE")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
	else if(name[maxch[i][j]] == "SJU")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
	else if(name[maxch[i][j]] == "DOJ")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
	else if(name[maxch[i][j]] == "PC")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 3);
	else if(name[maxch[i][j]] == "PE")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 1);
	else if(name[maxch[i][j]] == "UKE")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 13);
	else if(name[maxch[i][j]] == "W A")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 5);
	for(int k = 0 ; k < name[maxch[i][j]].length() ; k++){
		cout << name[maxch[i][j]][k];
		if((name[maxch[i][j]] == "A C" || name[maxch[i][j]] == "A K") && k == 1){
			int sum = rand() % 13;
			sum++;
			if(sum == 9)sum = 10;
			if(sum == 10)sum = 11;
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), sum);
			cout << name[maxch[i][j]][k];
		}else if((name[maxch[i][j]] == "A C" || name[maxch[i][j]] == "A K") && k == 3){
			int sum = rand() % 13;
			sum++;
			if(sum == 9)sum = 10;
			if(sum == 10)sum = 11;
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), sum);
			cout << name[maxch[i][j]][k];
		}
	}
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
	return ;
}

void print(string s,int n){
	int l = s.length();
	for(int i = 0 ; i < l ; i++){
		cout << s[i];
		Sleep(n);
	}
	return ;
}

void gotoxy(int x, int y) {
   COORD pos = {static_cast<SHORT>(x), static_cast<SHORT>(y)};
   HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
   SetConsoleCursorPosition(hOut, pos);
}

void Allmemset(){
	memset(ans , 0 , sizeof ans);
	for(int i = 1 ; i <= wide ; i++){
		for(int j = 1 ; j <= chang ; j++){
			ch[i][j] = "";
		}
	}
	return ;
}

void Read(){
    ifstream in("2048.in");
    in >> maxx;
    for(int i = 1 ; i <= wide ; i++){
    	for(int j = 1 ; j <= chang ; j++){
    		in >> maxch[i][j];
		}
	}
	in.close();
}
void Save(){
    ofstream out("2048.in");
    out << maxx << '\n';
    for(int i = 1 ; i <= wide ; i++){
    	for(int j = 1 ; j <= chang ; j++){
    		out << maxch[i][j] << ' ';
		}
	}
	out.close();
}

int X(){
	int x = rand() % chang;
	x++;
	return x;
}

int Y(){
	int y = rand() % wide;
	y++;
	return y;
}

void ts(){
	if(f_c == 1)print("按任意键继续...\n",10);
	else print("press any key to continue\n",10);
	char c = getchar();
	c = getchar();
	return ;
}

bool AK(){
	for(int i = 1 ; i <= wide ; i++){
		for(int j = 1 ; j <= chang ; j++){
			if(ch[i][j] == "AK"){
				system("cls");
				if(f_c == 0)cout << "you are AK!\n";
				else cout << "你AK了!\n"; 
				ts();
				return 1;
			}
		}
	}
	return 0;
}

bool AFO(){
	bool f = 0;
	for(int i = 1 ; i <= wide ; i++){
		for(int j = 1 ; j <= chang ; j++){
			if(ch[i][j] == ""){
				f = 1;
				return 1;
			}
		}
	}
	cout << "AFO!\n";
	ts();
	return 0;
}

void fig(){
	for(int i = 1 ; i <= chang ; i++){
		cout << "=====================\n";
		cout << "|";
		for(int j = 1 ; j <= wide ;j++){
			if(ch[i][j] != ""){
				zm(i,j);
				cout << "|";
			}else{
				cout << "   |";
			}
		}
		cout << '\n';
	}
	cout << "=====================\n";
	return ;
}

void w(){
	bool f = 0;
	for(int i = 1 ; i <= chang ; i++){
		for(int j = 1 ; j <= wide ; j++){
			f = 0;
			if(ch[i][j] != ""){
				int x = i;
				string s = ch[i][j];
				while(true){
					x--;
					if((ch[x][j] != s && ch[x][j] != "") || x <= 0){
						break;
					}else if(ch[x][j] == s){
						f = 1;
						ch[x + 1][j] = "";
						ans[x][j]++;
						ch[x][j] = name[ans[x][j]];
						ans[x + 1][j] = 0;
						break;
					}else if(ch[x][j] == ""){
						f = 1;
						ch[x][j] = ch[x + 1][j];
						ch[x + 1][j] = "";
						ans[x][j] = ans[x + 1][j];
						ans[x + 1][j] = 0;
					}
				}
				if(f == 0)yu[i][j] = 1;
			}
		}
	}
	for(int i = 1 ; i <= chang ; i++){
		for(int j = 1 ; j <= wide ; j++){
			if(ch[i][j] != "" && yu[i][j] == 0){
				int x = i;
				string s = ch[i][j];
				while(true){
					x--;
					if((ch[x][j] != s && ch[x][j] != "") || x <= 0){
						break;
					}else if(ch[x][j] == s){
						ch[x + 1][j] = "";
						ans[x][j]++;
						ch[x][j] = name[ans[x][j]];
						ans[x + 1][j] = 0;
						break;
					}else if(ch[x][j] == ""){
						ch[x][j] = ch[x + 1][j];
						ch[x + 1][j] = "";
						ans[x][j] = ans[x + 1][j];
						ans[x + 1][j] = 0;
					}
				}
				yu[i][j] = 1;
			}
		}
	}
}

void s(){
	bool f = 0;
	for(int i = chang ; i >= 1 ; i--){
		for(int j = 1 ; j <= wide ; j++){
			f = 0;
			if(ch[i][j] != ""){
				int x = i;
				string s = ch[i][j];
				while(true){
					x++;
					if((ch[x][j] != s && ch[x][j] != "") || x > chang){
						break;
					}else if(ch[x][j] == s){
						f = 1;
						ch[x - 1][j] = "";
						ch[x][j] = name[ans[x - 1][j] + 1];
						ans[x - 1][j] = 0;
						ans[x][j]++;
						s = name[ans[x][j]];
						break;
					}else if(ch[x][j] == ""){
						f = 1;
						ch[x][j] = ch[x - 1][j];
						ch[x - 1][j] = "";
						ans[x][j] = ans[x - 1][j];
						ans[x - 1][j] = 0;
					}
				}
				if(f == 0)yu[i][j] = 1;
			}
		}
	}
	for(int i = chang ; i >= 1 ; i--){
		for(int j = 1 ; j <= wide ; j++){
			if(ch[i][j] != "" && yu[i][j] == 0){
				int x = i;
				string s = ch[i][j];
				while(true){
					x++;
					if((ch[x][j] != s && ch[x][j] != "") || x > chang){
						break;
					}else if(ch[x][j] == s){
						ch[x - 1][j] = "";
						ch[x][j] = name[ans[x - 1][j] + 1];
						ans[x - 1][j] = 0;
						ans[x][j]++;
						s = name[ans[x][j]];
						break;
					}else if(ch[x][j] == ""){
						ch[x][j] = ch[x - 1][j];
						ch[x - 1][j] = "";
						ans[x][j] = ans[x - 1][j];
						ans[x - 1][j] = 0;
					}
				}
				yu[i][j] = 1;
			}
		}
	}
}

void d(){
	bool f = 0;
	for(int i = 1 ; i <= chang ; i++){
		for(int j = 1 ; j <= wide ; j++){
			f = 0;
			if(ch[i][j] != ""){
				int y = j;
				string s = ch[i][j];
				while(true){
					y++;
					if((ch[i][y] != s && ch[i][y] != "") || y > wide){
						break;
					}else if(ch[i][y] == s){
						f = 1;
						ch[i][y - 1] = "";
						ch[i][y] = name[ans[i][y - 1] + 1];
						ans[i][y - 1] = 0;
						ans[i][y]++;
						s = name[ans[i][y]];
						break;
					}else if(ch[i][y] == ""){
						f = 1;
						ch[i][y] = ch[i][y - 1];
						ch[i][y - 1] = "";
						ans[i][y] = ans[i][y - 1];
						ans[i][y - 1] = 0;
					}
				}
				if(f == 0)yu[i][j] = 1;
			}
		}
	}
	for(int i = 1 ; i <= chang ; i++){
		for(int j = 1 ; j <= wide ; j++){
			if(ch[i][j] != "" && yu[i][j] == 0){
				int y = j;
				string s = ch[i][j];
				while(true){
					y++;
					if((ch[i][y] != s && ch[i][y] != "") || y > wide){
						break;
					}else if(ch[i][y] == s){
						ch[i][y - 1] = "";
						ch[i][y] = name[ans[i][y - 1] + 1];
						ans[i][y - 1] = 0;
						ans[i][y]++;
						s = name[ans[i][y]];
						break;
					}else if(ch[i][y] == ""){
						ch[i][y] = ch[i][y - 1];
						ch[i][y - 1] = "";
						ans[i][y] = ans[i][y - 1];
						ans[i][y - 1] = 0;
					}
				}
				yu[i][j] = 1;
			}
		}
	}
}

void a(){
	bool f = 0; 
	for(int i = 1 ; i <= chang ; i++){
		for(int j = wide ; j >= 1 ; j--){
			f = 0;
			if(ch[i][j] != ""){
				int y = j;
				string s = ch[i][j];
				while(true){
					y--;
					if(y < 0)break;
					if((ch[i][y] != s && ch[i][y] != "") || y <= 0){
						break;
					}else if(ch[i][y] == s){
						if(y < 0)break;
						f = 1;
						ch[i][y + 1] = "";
						ch[i][y] = name[ans[i][y + 1] + 1];
						ans[i][y + 1] = 0;
						ans[i][y]++;
						s = ch[i][y];
						break;
					}else if(ch[i][y] == ""){
						if(y < 0)break;
						f = 1;
						ch[i][y] = ch[i][y + 1];
						ch[i][y + 1] = "";
						ans[i][y] = ans[i][y + 1];
						ans[i][y + 1] = 0;
					}
				}
				if(f == 0)yu[i][j] = 1;
			}
		}
	}
	for(int i = 1 ; i <= chang ; i++){
		for(int j = wide ; j >= 1 ; j--){
			if(ch[i][j] != "" && yu[i][j] == 0){
				int y = j;
				string s = ch[i][j];
				while(true){
					y--;
					if(y < 0)break;
					if((ch[i][y] != s && ch[i][y] != "") || y <= 0){
						break;
					}else if(ch[i][y] == s){
						if(y < 0)break;
						ch[i][y + 1] = "";
						ch[i][y] = name[ans[i][y + 1] + 1];
						ans[i][y + 1] = 0;
						ans[i][y]++;
						s = ch[i][y];
						break;
					}else if(ch[i][y] == ""){
						if(y < 0)break;
						ch[i][y] = ch[i][y + 1];
						ch[i][y + 1] = "";
						ans[i][y] = ans[i][y + 1];
						ans[i][y + 1] = 0;
					}
				}
				yu[i][j] = 1;
			}
		}
	}
}

void zhi(int &a){
	for(int i = 1 ; i <= wide ; i++){
		for(int j = 1 ; j <= chang ; j++){
			a += sum[ans[i][j]];
		}
	}
	return ;
}

bool zao(){
	int x,y;
	if(AFO() == 0){
		return 0;
	}
	while(true){
		x = X(),y = Y();
		if(ch[x][y] == ""){
			int id = rand() % 2;
			id++;
			ch[x][y] = name[id];
			if(ch[x][y] == "S E")ans[x][y] = 1;
			else ans[x][y] = 2;
			break;
		}
	}
	return 1;
}

bool f = 0;

void go(){
	srand(static_cast<unsigned int>(time(0)));
	int x = X(),y = Y();
	ch[x][y] = name[1];
	ans[x][y] = 1;
	int xx = x , yy = y;
	while(xx == x && yy == y){xx = X(),yy = Y();}
	x = xx,y = yy;
	ch[x][y] = name[1];
	ans[x][y] = 1;
	int max_ = 4;
	while(true){
		gotoxy(0,0);
		fig();
		if(f_c == 0)cout << "Enter 'e' to exit,The current value:" << max_ << ",The max is " << maxx << "." << '\n';
		else cout << "输入‘e’可以退出,现在的值为" << max_ << ",最大值为" << maxx << "." << '\n';
		char c = _getch();
		if(c == 'e')return ;
		if(c == 'w'){
			w();
		}else if(c == 's'){
			s();
		}else if(c == 'a'){
			a();
		}else if(c == 'd'){
			d();
		}
		max_ = 0;
		if(zao() == 0){
			return ;
		}
		zhi(max_);
		if(maxx < max_){
			maxx = max_;
			for(int i = 1 ; i <= wide ; i++){
				for(int j = 1 ; j <= chang ; j++){
					maxch[i][j] = ans[i][j];
				}
			}
		}
		if(AK() == 1){
			return ;
		}
	}
	return ;
}

void See_the_highest_scores(){
	if(f_c == 0)cout << "the max score is:" << maxx << "!\n";
	else cout << "最大值为" << maxx << "!\n";
	for(int i = 1 ; i <= chang ; i++){
		cout << "=====================\n";
		cout << "|";
		for(int j = 1 ; j <= wide ;j++){
			if(name[maxch[i][j]] != " "){
				zm2(i,j);
				cout << "|";
			}else{
				cout << "   |";
			}
		}
		cout << '\n';
	}
	cout << "=====================\n";
	ts();
	return ; 
}

void huan(){
	f = !f;
	for(int i = 1 ; i <= 16 ; i++){
		for(int j = 0 ; j < name[i].length() ; j++){
			if(name[i][j] != ' '){
				if(f == 0)name[i][j] -= ' ';
				else name[i][j] += ' ';
			}
		}
	}
	return ;
}

void help(){
	system("cls");
	if(f_c == 0){
		cout << "===Complete list of evaluation results:===\n";
		cout << "fail:AFO:Away From OI\n";
		cout << "1.CE:Compile Error,fraction:2\n";
		cout << "2.SE:System Error,fraction:4\n";
		cout << "3.Ign:Ignored,fraction:8\n";
		cout << "4.RE:Runtime Error,fraction:16\n";
		cout << "5.TLE:Time Limit Exceeded,fraction:32\n";
		cout << "6.MLE:Memory Limit Exceeded,fraction:64\n";
		cout << "7.OLE:Output Limit Exceeded,fraction:128\n";
		cout << "8.ILE:Idleness Limit Exceeded,fraction:256\n";
		cout << "9.SJU:Special Judge Error,fraction:512\n";
		cout << "10.DOJ:Denial of Judging,fraction:1024\n";
		cout << "11.PC:Partially Correct,fraction:2048\n";
		cout << "12.UKE:Unknown Error,fraction:4096\n";
		cout << "13.WA:Wrong Answer,fraction:8192\n";
		cout << "14.AC:Accepted,fraction:16384\n";
		cout << "15.AK:All Killed,fraction:65536\n";
		cout << "Win:AK\n";
		cout << "Game Methods:Two identical blocks can be combined to form blocks of the next level.\n";
	}else{
		cout << "===评测结果大全:===\n";
		cout << "失败:AFO:远离oi\n";
		cout << "1.CE:编译错误,分数:2\n";
		cout << "2.SE:系统错误,:4\n";
		cout << "3.Ign:评测被忽视,分数:8\n";
		cout << "4.RE:运行时错误,分数:16\n";
		cout << "5.TLE:超过时间限制,分数:32\n";
		cout << "6.MLE:超过空间限制,分数:64\n";
		cout << "7.OLE:超出输出限制,分数:128\n";
		cout << "8.ILE:超出输入限制,分数:256\n";
		cout << "9.SJU:特殊判断错误,分数:512\n";
		cout << "10.DOJ:拒绝评判,分数:1024\n";
		cout << "11.PC:部分正确,分数:2048\n";
		cout << "12.UKE:未知错误,分数:4096\n";
		cout << "13.WA:答案错误,分数:8192\n";
		cout << "14.AC:答案正确,分数:16384\n";
		cout << "15.AK:全部正确!,分数:65536\n";
		cout << "胜利:AK\n";
		cout << "游玩方法:两个同样的方块可以合成下一个等级的方块\n";
	}
	if(f_c == 1)print("按任意键继续...\n",10);
	else print("press any key to continue\n",10);
	char c = getchar(); 
	c = getchar(); 
	return ;
} 

signed main(){
	Read();
	//2048 mini-games
	while(true){
		system("cls");
		Allmemset();
		if(f_c == 0)cout << "=====2048 mini-games=====\n";
		else cout << "=====2048 小游戏=====\n";
		if(f_c == 0)cout << "1.go!\n";
		else cout << "1.开始!\n";
		if(f_c == 0)cout << "2.quit\n"; 
		else cout << "2.退出\n";
		if(f_c == 0)cout << "3.See the highest scores\n";
		else cout << "3.看一看最大得分\n";
		if(f == 1){if(f_c == 0)cout << "4.Turn on lowercase mode.\n";else cout << "4.开启小写模式\n";}
		else{if(f_c == 0)cout << "4.Turn on caps mode.\n";else cout << "4.开启大写模式\n";}
		if(f_c == 0)cout << "5.Chinese mode\n";
		else cout << "5.英文模式\n";
		if(f_c == 0)cout << "6.Help!\n";
		else cout << "6.帮助\n";
		int a;
		cin >> a;
		if(a == 2){
			Save();
			break;
		}else if(a == 1){
			system("cls");
			go();
		}else if(a == 3){
			system("cls");
			See_the_highest_scores();
		}else if(a == 4){
			huan();
		}else if(a == 5){
			f_c = !f_c;
		}else if(a == 6){
			help();
		}
	}
	return 0;
}