2048小游戏(2.0)

1.0在这里

感觉1.0没啥好玩的,是吧?玩了一会儿就玩腻了。

所以要开发2.0!

在任意位置输出文字

这样就不用麻烦的排版了!

关键就是SetConsoleCursorPosition函数(<Windows.h>)。

两个参数,第一个是输出句柄,第二个是位置。

输出句柄可以用GetStdHandle(STD_OUTPUT_HANDLE)来获得,位置有一个要注意的点,就是位置第一个是列,第二个是行,并且左上角是0行,0列

比如想移到3行,4列,就要使用SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), COORD{ 4, 3 });

Round xxx

输出这是第几轮游戏,形如:

长度要和整个框的长度一样,并且Round xxx要居中(现在必须用while(true)了,不能直接在main结尾再调用main了)

空格数:

spaces_{left}=\lfloor \frac{1}{2}(25-rs-6)\rfloor

spaces_{right}=\lceil \frac{1}{2}(25-rs-6)\rceil

“随机生成” 2.0

就只改一点点,本来是生成2,变成生成2或4(随机)

计分

在合成的时候计,随便计,我游戏中,每次移动(合成)分数增加:

\lfloor a_1 \div 3 \rfloor \times \lfloor a_2 \div 3 \rfloor \times \lfloor a_3 \div 3 \rfloor \times \cdots \times \lfloor a_n \div 3 \rfloor

其中a[i]表示参与合成的数中,第i个合成后的数(只有合成的才算,如果只是移动就不算)。

还有最高分。。。

计分的时候顺便加上......

评语

超过10000:The Best In The World!
5000~9999:The Best In The Nation!
2000~4999:The Best In The Province!
1000~1999:Unbelievable!
500~999:Best!
200~499:Very Good!
100~199:Great!
50~99:Conbo!

第一次合成:First Kill!

新纪录:新纪录

技能(前方高能)

商城

买卡啦~买卡啦~打乱卡500分一张~消失卡100分一张~

打乱卡

打乱场上所有数字的顺序

消失卡

选择场上的一个数,使其消失

对了,还有一件事。。。

当你死亡的时候如果有卡,可以选择用卡(当然也可以不用)

代码(我才很多人都是奔着这玩意儿来的,如果是的话说一声)

哇!字数一下子变得好多!

#include <ctime>
#include <cstdio>
#include <string>
#include <conio.h>
#include <cstring>
#include <cstdlib>
#include <Windows.h>

using namespace std;

int arr[10][10];
HANDLE stdhand = GetStdHandle(STD_OUTPUT_HANDLE);

void turnright()
{
	int tmp[5][5];
	for (int i = 1; i <= 4; i++)
	{
		for (int j = 1; j <= 4; j++)
		{
			tmp[i][j] = arr[i][j];
		}
	}
	for (int i = 1; i <= 4; i++)
	{
		for (int j = 1; j <= 4; j++)
		{
			arr[j][4 - i + 1] = tmp[i][j];
		}
	}
}

string move(char ch, int &score, int &coins)
{
	int pscore = 1, cnt = 0;
	switch (ch)
	{
	case 'U': turnright(); break;
	case 'L': turnright(); turnright(); break;
	case 'D': turnright(); turnright(); turnright(); break;
	}
	for (int i = 1; i <= 4; i++)
	{
		for (int j = 4; j >= 1; j--)
		{
			if (arr[i][j])
			{
				if (arr[i][j + 1])
				{
					if (arr[i][j + 1] == arr[i][j])
					{
						arr[i][j + 1] *= 2;
						pscore *= arr[i][j + 1]/3;
						if (arr[i][j + 1] == 4) pscore++;
						arr[i][j] = 0;
					}
					continue;
				}
				for (int k = j + 1; k <= 4; k++)
				{
					if (arr[i][k]) break;
					arr[i][k] = arr[i][k - 1];
					arr[i][k - 1] = 0;
				}
			}
		}
	}
	switch (ch)
	{
	case 'U': turnright(); turnright(); turnright(); break;
	case 'L': turnright(); turnright(); break;
	case 'D': turnright(); break;
	}
	pscore--;
	coins += pscore;
	score += pscore;
	if (pscore == score && score) 
		return "First Kill!\n"s;
	if (pscore >= 10000) return "The Best In The World!\n"s;
	else if (pscore >= 5000) return "The Best In The Nation!\n"s;
	else if (pscore >= 2000) return "The Best In The Province!\n"s;
	else if (pscore >= 1000) return "Unbelievable!\n"s;
	else if (pscore >= 500) return "Best!\n"s;
	else if (pscore >= 200) return "Very Good!\n"s;
	else if (pscore >= 100) return "Great!\n"s;
	else if (pscore >= 50) return "Conbo!\n"s;
	return "";
}

bool lose()
{
	for (int i = 1; i <= 4; i++)
	{
		for (int j = 1; j <= 4; j++)
		{
			if (arr[i][j] == 0) return false;
		}
	}
	for (int i = 1; i <= 4; i++)
	{
		for (int j = 1; j <= 4; j++)
		{
			if (arr[i][j] == arr[i - 1][j] ||
				arr[i][j] == arr[i][j + 1] ||
				arr[i][j] == arr[i + 1][j] ||
				arr[i][j] == arr[i][j-1]) return false;
		}
	}
	return true;
}

void out(int markx = -1, int marky = -1)
{
	for (int i = 1; i <= 4; i++)
	{
		printf(R"(|-----+-----+-----+-----|
|     |     |     |     |
|)");
		for (int j = 1; j <= 4; j++)
		{
			string oh = to_string(arr[i][j]);
			if (arr[i][j] == 0) oh = ' ';
			int len = (5 - oh.size()) / 2, rlen = 5-oh.size()-len;
			printf("%s", string(len, ' ').c_str());
			if(i == markx && j == marky) SetConsoleTextAttribute(stdhand, 15 | BACKGROUND_RED);
			printf("%s", oh.c_str());
			SetConsoleTextAttribute(stdhand, 15);
			printf("%s|", string(rlen, ' ').c_str());
		}
		printf(R"(
|     |     |     |     |
)");
	}
	printf("|-----+-----+-----+-----|\n");
}

bool ups(int &upset)
{
	if (upset <= 0)
	{
		printf("没有打乱卡!\n");
		return false;
	}
	printf("确认?[Y]es,[N]o");
	char ch = getch();
	if (ch != 'y' && ch != 'Y')
	{
		printf("\n取消成功!\n");
		return false;
	}
	upset--;
	for (int i = 1; i <= 4; i++)
	{
		for (int j = 1; j <= 4; j++)
		{
			int x = rand() % 4 + 1, y = rand() % 4 + 1;
			swap(arr[i][j], arr[x][y]);
		}
	}
	printf("\n打乱成功!\n");
	return true;
}

bool dsp(int &disappear)
{
	if (disappear <= 0)
	{
		printf("没有消失卡!\n");
		return false;
	}
	disappear--;
	int mx = 1, my = 1;
	while (true)
	{
		char ch;
		system("cls");
		out(mx, my);
		printf("↑↓←→移动,空格选中。\n");
		printf("若要取消,可以选中一个空格子然后使用消失卡,也可以把位置移到地图之外。\n");
		while ((ch = getch()) != -32 && ch != ' ');
		if (ch == ' ')
		{
			if (arr[mx][my] == 0)
			{
				printf("取消成功!\n");
				return false;
			}
			arr[mx][my] = 0;
			printf("成功!\n");
			return true;
		}
		ch = getch();
		switch (ch)
		{
		case 'H': mx--; break;
		case 'P': mx++; break;
		case 'K': my--; break;
		case 'M': my++; break;
		}
		if (mx <= 0 || mx >= 5 || my <= 0 || my >= 5)
		{
			printf("取消成功!\n");
			return false;
		}
	}
}
int main()
{
	int score = 0, upset = 0, disappear = 0, rd = 0, maxscore = 0, coins = 0;
	while (++rd)
	{
		iakioi:
		memset(arr, 0, sizeof arr);
		srand(time(NULL));
		score = 0;
		string said;
		while (true)
		{
			while (true)
			{
				bool flag = false;
				for (int i = 1; i <= 4; i++)
				{
					for (int j = 1; j <= 4; j++)
					{
						if (!arr[i][j]) flag = true;
						if (rand() % 16 || arr[i][j]) continue;
						arr[i][j] = rand() % 2 * 2 + 2;
						goto et;
					}
				}
				if (!flag) break;
			}
		et:
			system("cls");
			int rs = to_string(rd).size(), ln = (25-rs-6)/2, rn = (26-rs-6)/2;
			printf("%sRound %d%s\n", string(ln, '-').c_str(), rd, string(rn, '-').c_str());
			out();
			SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), COORD{ 30, 13 });
			printf("分数:%d分\n", score);
			SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), COORD{ 30, 5 });
			printf("%s\n", said.c_str());
			if (score > maxscore)
			{
				maxscore = score;
				SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), COORD{ 60, 9 });
				printf("新纪录!\n");
			}
			SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), COORD{30, 9});
			printf("最高分:%d分!\n", maxscore);
			SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), COORD{ 0, 18 });
			printf("打乱卡:%d张\t消失卡:%d张\n", upset, disappear);
			printf("↑↓←→移动,s进入商城,r重来,a使用打乱卡,b使用消失卡。\n");
			if (lose()) break;
			char ch = 0;
			while ((ch = getch()) != -32 && ch != 's' && ch != 'a' && ch != 'b' && ch != 'r');
			if (ch == 'r')
			{
				printf("确定吗?[Y]es/[N]o");
				ch = getch();
				if (ch == 'Y' || ch == 'y') goto iakioi;
			}
			if (ch == 's')
			{
				while (true)
				{
					system("cls");
					printf("要进入商店吗?(输入Y或N,[Y]es, [N]o)");
					ch = getch();
					if (ch == 'y' || ch == 'Y')
					{
						while (true)
						{
							system("cls");
							printf(R"(剩余%d积分
要买什么呢?
输入1:打乱卡,随机打乱场上的所有数字,500积分一张。
输入2:消失卡,可以选择一个数字,使其消失,100积分一张。
输入其他:退出
)", coins);
							ch = getch();
							if (ch == '1')
							{
								if (coins < 500)
								{
									printf("积分不足!\n");
									getch();
									continue;
								}
								coins -= 500;
								upset++;
							}
							else if (ch == '2')
							{
								if (coins < 100)
								{
									printf("积分不足!\n");
									getch();
									continue;
								}
								coins -= 100;
								disappear++;
							}
							else break;
						}
					}
					if (ch == 'n' || ch == 'N') goto et;
				}
			}
			if (ch == 'a')
			{
				ups(upset);
				getch();
				goto et;
			}
			if (ch == 'b')
			{
				dsp(disappear);
				//getch();
				goto et;
			}
			ch = getch();
			said = "";
			switch (ch)
			{
			case 'H': said = move('U', score, coins); break;
			case 'P': said = move('D', score, coins); break;
			case 'K': said = move('L', score, coins); break;
			case 'M': said = move('R', score, coins); break;
			}
		}
		SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), COORD{ 0, 22 });
		printf("Lose!\n");
		Sleep(1000);
		while (_kbhit()) getch();
		if (upset || disappear)
		{
			printf(R"(要不要使用打乱/消失卡呢?
1.打乱卡
2.消失卡
其他.不用)");
			char ch = getch();
			if (ch == '1')
			{
				system("cls");
				if (ups(upset)) goto et;
			}
			if (ch == '2')
			{
				if (dsp(disappear)) goto et;
			}
		}
		getch();
	}
	return 0;
}

  • 19
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值