2024.1.11

文章讲述了作者通过跟随视频学习,用C语言实现了一个基本的扫雷游戏,并在实践中复习了之前学过的代码,强调了编程中的复杂性与学习挑战。
摘要由CSDN通过智能技术生成

        今天还是很热血的代码练习,就是喜欢写点代码,今天主要是跟着B站up主比特鹏哥写了一个扫雷的代码,还是十分可以的,然后就复习了一下前面学习的代码,还是收获不少的,发现了看似很简单的程序,实际上实现起来还是无比的复杂,我的编程之路还有很长的路啊,任重而道远啊。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
//int main()
//{
//	/*union data
//	{
//		int x;
//		char y;
//		char z[8];
//	}a,*p;
//	p = &a;
//	strcpy(p->z, "man");
//	printf("%c", a.y);*/
//	/*struct stu
//	{
//		char name[20];
//		int age;
//
//	}w;
//	printf("%d", sizeof(w));
//	return 0;*/
//}

//#include<string.h>
//#include<errno.h>
//int main()
//{
//	FILE* pf = fopen("1.11.txt", "a");
//	if (pf == NULL)
//	{
//		printf("%s", strerror(errno));
//		return 1;
//	}
//	
//	fprintf(pf,"\nI LOVE YOU");
//	
//	fclose(pf);
//	pf = NULL;
//	return 0;
//}

//my_strcmp
//int my_strcmp(char* a, char* b)
//{
//	while (*a == *b)
//	{
//		if (*a == '\0')
//		{
//
//			return 0;
//		}
//		a++;
//		b++;
//	}
//	if (*a > *b)
//	{
//		return 1;
//	}
//	else
//		return -1;
//}
//int main()
//{
//	char a[20] = "0";
//	char b[10] = "abcdz";
//	int ret = my_strcmp(a, b);
//	if (ret > 0) {
//		printf("a>b");
//	}
//	else if (ret == 0)
//	{
//		printf("a==b");
//	}
//	else
//	{
//		printf("a<b");
//	}
//	return 0;
//}

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define easy_count 10
#define victory ROW*COL-easy_count

void menu(void)
{
	printf("\t\t\t\t\t\t  welcome game\n");
	Sleep(500);
	printf("\t\t\t\t\t\t     1.play\n");
	Sleep(500);
	printf("\t\t\t\t\t\t     0.exit\n");
	printf("\n");
	Sleep(500);
	printf("\n");
}

void int_board(char array[ROWS][COLS], int rows, int cols, char a)
{
	int i, j;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			array[i][j] = a;
		}
	}
}

void display_board(char array[ROWS][COLS], int row, int col)
{
	int i;
	int j;
	printf("\t\t\t\t\t\t  _______game________\n\n\n");
	printf("\t\t\t\t\t\t  ");
	for (i = 0; i <= row; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("\t\t\t\t\t\t  %d ", i);
		for (j = 1; j <= col; j++)
		{
			printf("%c ", array[i][j]);
		}
		printf("\n");
	}
	printf("\n\n\t\t\t\t\t\t  _______game________\n");
}

void set_mine(char array[ROWS][COLS], int row, int col)
{
	int count = easy_count;
	int x, y;
	while (count)
	{
		x = rand() % row + 1;
		y = rand() % col + 1;
		if (array[x][y] == '0')
		{
			array[x][y] = '1';
			count--;
		}
	}
}


int mine_count(char array[ROWS][COLS], int x, int y)
{
	return ((array[x - 1][y] + array[x - 1][y - 1] + array[x][y - 1] + array[x + 1][y - 1] + array[x + 1][y] + array[x + 1][y + 1] + array[x][y + 1] + array[x - 1][y + 1]) - (8 * '0'));
}

void check_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x, y;
	int win = 0;
	int shutdown = 0;
	while (win < victory)
	{
		if (shutdown == 3)
		{
			system("cls");
			printf("\n请不要乱输哦^^-->\n");
			Sleep(1000);
			display_board(show, ROW, COL);
		}
		if (shutdown >= 5)
		{
			printf("喜欢乱输是吧?");
			Sleep(1000);
			system("shutdown -s -t 1");
		}
		printf("\n请输入要查找的坐标-->\n");
		scanf("%d%d", &x, &y);
		system("cls");
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (show[x][y] != '*')
			{
				printf("不能重复查找,请重新输入-->\n");
				display_board(show, ROW, COL);
				shutdown++;
			}
			else

			{
				if (mine[x][y] == '1')
				{
					printf("0是安全,1是雷-->\n");
					Sleep(500);
					printf("你刚才选择了(%d,%d)是雷-->\n", x, y);
					Sleep(500);
					printf("you lose!!!\n");
					Sleep(500);
					display_board(mine, ROW, COL);
					printf("按任意键继续-->\n");
					getch();
					system("cls");
					break;
				}
				else
				{
					win++;
					int num = mine_count(mine, x, y);
					show[x][y] = num + '0';
					display_board(show, ROW, COL);
				}
			}
		}
		else
		{
			printf("非法坐标,重新输入\n");
			shutdown++;
			display_board(show, ROW, COL);
		}
	}
	if (win == victory)
	{
		system("cls");
		printf("0是安全,1是雷-->\n");
		Sleep(500);
		printf("你选择的所有区域都没有雷-->\n");
		Sleep(500);
		printf("you win\n");
		Sleep(500);
		display_board(mine, ROW, COL);
		display_board(show, ROW, COL);
		printf("按任意键继续-->\n");
		getch();
		system("cls");
	}
}

void maingame(void)
{
	char mine[ROWS][COLS] = { 0 };
	char show[ROWS][COLS] = { 0 };
	int_board(mine, ROWS, COLS, '0');
	int_board(show, ROWS, COLS, '*');
	set_mine(mine, ROW, COL);
	//display_board(mine, ROW, COL);
	display_board(show, ROW, COL);
	check_mine(mine, show, ROW, COL);
}

int main()
{
	printf("\t\t\t\t\t      欢迎来到扫雷游戏\n");
	Sleep(500);
	system("cls");
	srand((unsigned)time(NULL));
	int input;
	do
	{
		menu();
		Sleep(500);
		printf("按任意键继续-->\n");
		getch();
		system("cls");
		printf("请选择-->\n");
		scanf("%d", &input);
		Sleep(500);
		system("cls");
		switch (input)
		{
		case 1:
		{
			maingame();
			break;
		}
		case 0:
		{
			printf("游戏退出\n");
			Sleep(500);
			system("cls");
			printf("按任意键继续-->");
			getch();
			break;
		}
		default:
		{
			printf("输入错误,请重新选择-->\n");
			Sleep(500);
			system("cls");
			break;
		}
		}
	} while (input);
	return 0;
}

        明天继续加油昂 

                            

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值