2048 系列之朝代版

$ 自编 2048 朝代版 $

#include <bits/stdc++.h>
#include <conio.h>
#include <windows.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
void chaodai (int n) {
	if (n == 2)
		printf ("商") ; 
	if (n == 4)
		printf ("周") ; 
	if (n == 8)
		printf ("秦") ; 
	if (n == 16)
		printf ("汉") ; 
	if (n == 32)
		printf ("唐") ; 
	if (n == 64)
		printf ("宋") ; 
	if (n == 128)
		printf ("元") ; 
	if (n == 256)
		printf ("明") ; 
	if (n == 512)
		printf ("清") ; 
}
inline int max1 (int x, int y) {
	return x > y ? x : y;
}
const int N = 4;
int jiemian[N][N], fangxiang1, M = 2048;
using namespace std;
void put (int n) {
	while (n --)
		puts ("") ;
}
void guangbiao() { 	
	CONSOLE_CURSOR_INFO cursor;
	cursor.bVisible = FALSE;
	cursor.dwSize = sizeof(cursor);
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorInfo(handle, &cursor);
	return ;
}
void yan () {
	int cnt = rand () % 6 + 1;
	if (cnt == 1)
		system ("color 2") ;
	if (cnt == 2)
		system ("color 3") ;
	if (cnt == 3)
		system ("color 6") ;
	if (cnt == 4)
		system ("color B") ;
	if (cnt == 5)
		system ("color C") ;
	if (cnt == 6)
		system ("color E") ;
}
void print (string str) {
    for (int i = 0; i < str.size() - 1; ++ i) {
        Sleep (10) ;
        cout << str[i];
    }
    cout << str [str.size() - 1];
	Sleep (100) ;
}
void ko (int n) {
	while (n --)
		putchar (' ');
}
void xianshi () {
	system ("cls") ;
	put (1) , ko (12) ;
	printf ("目标:") ;
	chaodai (M) ; 
	put (1) , ko (5) ;
	for (int i = 1; i < N + 1; ++ i) {
		ko (4) ;
		putchar ('_') ;
	}
	put (2) ;
	for (int i = 0; i < N; ++ i) {
		ko (4) ;
		putchar ('|') ;
		for (int j = 0; j < N; ++ j)
			if (jiemian[i][j] != 0)
				ko (3), chaodai (jiemian[i][j]) ;
			else
				ko (4) , putchar ('_') ;
		ko (4) ;
		putchar ('|') ;
		put (2) ;
	}
	ko (5) ;
	for (int i = 1; i < N + 1; ++ i)
		ko (4) , putchar ('_') ;
	put (1) ;
}
bool kongwei () {
	for (int i = 0; i < N; ++ i)
		for (int j = 0; j < N; ++ j)
			if (jiemian[i][j] == 0)
				return 1;
	return 0;
}
void suiji () {
	int i, j;
	do {
		i = rand () % N;
		j = rand () % N;
	} while (jiemian[i][j] != 0) ;
	jiemian[i][j] = rand() % 2 * 2 + 2;
}
int MAX () {
	int maxx = 0;
	for (int i = 0; i < N; ++ i)
		for (int j = 0; j < N; ++ j)
			maxx = max1 (jiemian[i][j], maxx) ;
	return maxx;
}
bool xiangjia () {
	int sum;
	bool ok, flag = 0;
	if (fangxiang1 == 1) {
		for (int i = 1; i < N; ++ i)
			for (int j = 0; j < N; ++ j) {
				sum = i, ok = 0;
				while (sum >= 1 && jiemian[sum][j] != 0 ) {
					if (jiemian[sum - 1][j] == 0) {
						jiemian[sum - 1][j] = jiemian[sum][j];
						jiemian[sum][j] = 0;
						flag = 1;
					} 
					else {
						if (jiemian[sum - 1][j] == jiemian[sum][j] && ok == 0) {
							jiemian[sum - 1][j] = jiemian[sum - 1][j] * 2;
							jiemian[sum][j] = 0;
							ok = 1, flag = 1;
						} 
						else
							break;
					}
					-- sum;
				}
			}
	}
	if (fangxiang1 == 2) {
		for (int i = N - 2; i >= 0; -- i)
			for (int j = 0; j < N; ++ j) {
				sum = i, ok = 0;
				while (sum <= N - 2 && jiemian[sum][j] != 0 ) {
					if(jiemian[sum + 1][j] == 0) {
						jiemian[sum + 1][j] = jiemian[sum][j];
						jiemian[sum][j]=0;
						flag = 1;
					} 
					else {
						if (jiemian[sum + 1][j] == jiemian[sum][j] && ok == 0) {
							jiemian[sum + 1][j] = jiemian[sum + 1][j] * 2;
							jiemian[sum][j] = 0;
							ok = 1, flag = 1;
						} 
						else
							break;
					}
					++ sum;
				}
			}
	}
	if (fangxiang1 == 3) {
		for (int i = 0; i < N; ++ i)
			for (int j = 1; j < N; ++ j) {
				sum = j, ok = 0;
				while (sum >= 1 && jiemian[i][sum] != 0 ) {
					if (jiemian[i][sum - 1] == 0) {
						jiemian[i][sum - 1] = jiemian[i][sum];
						jiemian[i][sum] = 0;
						flag = 1;
					} 
					else {
						if (jiemian[i][sum - 1] == jiemian[i][sum] && ok == 0) {
							jiemian[i][sum - 1] = jiemian[i][sum - 1] * 2;
							jiemian[i][sum] = 0;
							ok = 1, flag = 1;
						} 
						else
							break;
					}
					-- sum;
				}
			}
	}
	if (fangxiang1 == 4) {
		for(int i = 0; i < N; ++ i)
			for(int j = N - 2; j >= 0; -- j) {
				sum = j, ok = 0;
				while (sum <= N - 2 && jiemian[i][sum] != 0 ) {
					if (jiemian[i][sum + 1] == 0) {
						jiemian[i][sum + 1] = jiemian[i][sum];
						jiemian[i][sum] = 0;
						flag = 1;
					} 
					else {
						if (jiemian[i][sum + 1] == jiemian[i][sum] && ok == 0) {
							jiemian[i][sum + 1] = jiemian[i][sum + 1] * 2;
							jiemian[i][sum] = 0;
							ok = 1, flag = 1;
						} 
						else
							break;
					}
					++ sum;
				}
			}
	}
	return flag;
}
bool shuru () {
	char str = getch () ;
	if (str < 0) {
		str = getch () ;
		if(str == 72) {
			fangxiang1 = 1;
			return 1;
		}
		if(str == 80) {
			fangxiang1 = 2;
			return 1;
		}
		if(str == 75) {
			fangxiang1 = 3;
			return 1;
		}
		if(str == 77) {
			fangxiang1 = 4;
			return 1;
		}
	}
	return 0;
}
void hebing () {
	for (int i = 0; i < N; i ++)
		for (int j = 0; j < N - 1; ++ j)
			if (jiemian[i][j] == jiemian[i][j + 1])
				return ;
	for (int j = 0; j < N; j ++)
		for (int i = 0; i < N - 1; ++ i)
			if (jiemian[i][j] == jiemian[i + 1][j])
				return ;
}
void guize () {
	yan () , system ("cls") , put (2) , ko (9);
	print ("用方向键移动方块") ;
	put (2) , ko (3) ; 
	print ("当两个有相同朝代的方块接触时") ;
	put (2) , ko (7) ;
	print ("它们会合并为一个方块") ;
	put (2) , ko (7) ;
	print ("数字到达目标即为胜利") ;
	put (2) , ko (9) ;
	print ("按任意键观看结束") ;
	getch () , system ("cls") ;
}
void chongxing () {
	system ("cls") ;
	yan (), put (6), ko (12);
	print ("请重新输入!") ; 
}
int main () {
	guangbiao () , yan () ;
	system ("mode con cols=35 lines=15") ;
	srand (time (0) ) ;
	put (6) , ko (10) ;
	print ("作者:刘科杰") ;
	system ("cls") ;
	yan () , put (6) , ko (3) ;
	while (1) {
		system ("cls") , yan () , put (2) ;
		ko (15) ;
		print ("首页") ;
		put (2) ;
		ko (11) ;
		print ("1.开始游戏") ;
		put (2) ; 
		ko (11) ;
		print ("2.游戏规则") ;
		put (2) ; 
		ko (11) ;
		print ("3.结束游戏") ;
		put (2) ; 
		ko (11) ;
		print ("请选择:") ;
		string n;
		cin >> n;
		if (n[0] == '1' && n.size () == 1)
			break;
		else if (n[0] == '2' && n.size () == 1)
			guize () ;
		else if (n[0] == '3' && n.size () == 1)
			return 0;
		else chongxing () ;
		system ("cls") ;
	}
	while (1) {
		system ("cls") , yan () , put (2) ;
		ko (15) ;
		print ("难度") ;
		put (2) , ko (11);
		print ("1.目标:元 ") ;
		put (2) , ko (11);
		print ("2.目标:明 ") ;
		put (2) , ko (11);
		print ("3.目标:清 ") ;
		put (2) , ko (11);
		print ("请选择:") ;
		string n;
		cin >> n;
		if (n[0] == '1' && n.size() == 1) {
			M = 128;
			break;
		}
		else if (n[0] == '2' && n.size() == 1) {
			M = 256;
			break;
		}
		else if (n[0] == '3' && n.size() == 1) {
			M = 512;
			break;
		}
		else if (n[0] == '4' && n.size() == 1) {
			break;
			M = 1024;
		}
		else if (n[0] == '5' && n.size() == 1) {
			break;
			M = 2048;
		}
		else 
			chongxing () ;
	}
	memset (jiemian, 0, sizeof (jiemian) ) ;
	bool ok = 0, flag;
	int k, geshu;
	suiji () , suiji () ;
	xianshi () , yan () ;
	while(1) {
		k = shuru () ;
		if (k == 0)
			continue;
		flag = xiangjia () ;
		if (flag == 1)
			xianshi () ;
		if (MAX() == M) {
			ok = 1;
			break;
		}
		geshu = kongwei () ;
		if (geshu == 1 && flag == 1)
			suiji () , xianshi () ;
		if (geshu == 0) {
			hebing () ;
			break;
		}
	}
	if (ok == 1) {
		system ("cls") ;
		Sleep (100) ;
		yan (), put (6) ,ko (14) ;
		print ("你赢了!") ;
	}
	else {
		system ("cls") ;
		Sleep (100) ;
		yan () , put (6) , ko (14) ;
		print ("你输了!") ;
	}
	put (4) ;
	return 0;
}


$ 我的博客写的这么好,为什么不点个赞呢? $

posted @ 2023-07-01 11:25  liukejie  阅读(18)  评论(0编辑  收藏  举报