ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

c语言实现简单的扫雷小游戏

2021-10-27 22:02:32  阅读:174  来源: 互联网

标签:rand ROWS 语言 int COLS 小游戏 扫雷 str printf


a.输入坐标来排雷。

b.雷排完之后,胜利游戏结束。

c.踩中雷之后,失败游戏结束。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<math.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define MINE_SUM 10

void gameBegin() {
	printf("+---------------------------------------+\n");
	printf("|***************************************|\n");
	printf("|****** 1.开始游戏 *** 2.游戏结束 ******|\n");
	printf("|***************************************|\n");
	printf("+---------------------------------------+\n");
}

void initBoard(char str[ROWS][COLS], char temp_) {
	for (int i = 0; i < ROWS; i++) {
		for (int j = 0; j < COLS; j++) {
			str[i][j] = temp_;
		}
	}
}

void inputBoard(char str[ROWS][COLS]) {
	for (int i = 0; i < ROWS; i++) {
		for (int j = 0; j < COLS; j++) {
			printf("%c ", str[i][j]);
		}
		printf("\n");
	}
	printf("-----------------------------------------\n");
}

void setMine(char str[ROWS][COLS]) {
	int temp_1 = 0;
	while (1) {
		int rand_x_ = rand() % 9 + 1;
		int rand_y_ = rand() % 9 + 1;
		if(str[rand_x_][rand_y_]=='0'){
			str[rand_x_][rand_y_] = '1';
			temp_1++;
		}
		if (temp_1 == 10) {
			break;
		}
	}
}

void showPlayer(char str[ROWS][COLS]) {
	printf("+--+--+--+--+--+--+--+--+--+--+\n");
	printf("| ");
	for (int i = 0; i < 10; i++) {
		printf("%d| ", i);
	}
	printf("\n");
	printf("+--+--+--+--+--+--+--+--+--+--+\n");
	for (int j = 1; j <= 9; j++) {
		printf("| ");
		printf("%d| ", j);
		for (int i = 1; i <= 9; i++) {
			printf("%c| ", str[j][i]);
		}
		printf("\n");
		printf("+--+--+--+--+--+--+--+--+--+--+\n");
	}
}

// 11  12  13
// 21  22  23
// 31  32  33

char tempMine(char str1[ROWS][COLS], int x, int y) {
	int temp = 0;
	if (str1[x - 1][y - 1] == '1') {
		temp = temp + 1;
	}
	else
	{
		temp = temp + 0;
	}
	if (str1[x - 1][y] == '1') {
		temp = temp + 1;
	}
	else {
		temp = temp + 0;
	}
	if (str1[x - 1][y + 1] == '1') {
		temp = temp + 1;
	}
	else {
		temp = temp + 0;
	}
	if (str1[x][y + 1] == '1') {
		temp = temp + 1;
	}
	else {
		temp = temp + 0;
	}
	if (str1[x + 1][y + 1] == '1') {
		temp = temp + 1;
	}
	else {
		temp = temp + 0;
	}
	if (str1[x + 1][y] == '1') {
		temp = temp + 1;
	}
	else {
		temp = temp + 0;
	}
	if (str1[x + 1][y - 1] == '1') {
		temp = temp + 1;
	}
	else {
		temp = temp + 0;
	}
	if (str1[x][y - 1] == '1') {
		temp = temp + 1;
	}
	else {
		temp = temp + 0;
	}
	return temp;
}

void gameIng(char str1[ROWS][COLS], char str2[ROWS][COLS]) {
	int x, y;
	int temp = 0;
	int mine_temp = 0;
	setMine(str1);//设置雷
	while (1) {
		//inputBoard(str1);
		showPlayer(str2);
		printf("请玩家输入要排除的棋盘坐标(x,y)(两个数字之间用逗号隔开):\n");
		scanf("%d,%d", &x, &y);
		if (str1[x][y] == '1') {
			printf("你被炸死了,游戏结束!\n");
			break;
		}
		if (str1[x][y] == '0') {

			str2[x][y] = tempMine(str1, x, y) + '0';
			mine_temp++;
		}
		if (mine_temp == (ROW * COL - MINE_SUM)) {
			printf("你胜利了,游戏结束!\n");
			break;
		}
		//完成游戏
		system("cls");
	}
	inputBoard(str1);
}

int main() {
	int choose_ = 0;
	char temp_1 = '0';
	char temp_2 = '?';
	srand((unsigned int)time(0));//定义随机数
	//3.定义棋盘
	//a.存储棋盘
	char mine_board_1[ROWS][COLS];
	//b.展示棋盘
	char mine_board_2[ROWS][COLS];
	//a.初始化存储棋盘
	initBoard(mine_board_1, temp_1);
	//b.初始化展示棋盘
	initBoard(mine_board_2, temp_2);
	//设置雷
	//setMine(mine_board_1);
	//打印棋盘
	//inputBoard(mine_board_1);
	//inputBoard(mine_board_2);

	while (choose_ != 2) {

		//1.定义游戏界面
		gameBegin();
		//2.玩家选择
		printf("请输入你的选择:");
		scanf("%d", &choose_);
		system("cls");
		switch (choose_)
		{
		case 1://4.开始游戏
			printf("游戏开始!\n");
			initBoard(mine_board_1, temp_1);
			initBoard(mine_board_2, temp_2);
			gameIng(mine_board_1, mine_board_2, &choose_);
			break;
		case 2:
			printf("游戏结束!\n");
			break;
		default:
			printf("输入有误请重新输入!\n");
			break;
		}

	}

}

标签:rand,ROWS,语言,int,COLS,小游戏,扫雷,str,printf
来源: https://blog.csdn.net/weixin_49312527/article/details/121003185

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有