[c语言]------扫雷游戏

目录

1,游戏实现

        1,初始化

        2,打印

        3,创建雷

        4,扫雷

2,完整代码

        1,game.h

        2,game.c

        3,执行文件

扫描二维码关注公众号,回复: 17140318 查看本文章

1,游戏实现

        1,初始化

                因为需要初始化雷,同时扫雷,所以创建了2个二维数组,本来棋盘应该是9*9,但是为了避免溢出,所以创建了2个11*11的数组。第一个数组初始为'0',第二个初始为'*'.

void Initboard(char board[ROWS][COLS], int row, int col, char set)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		int j = 0;
		for (j = 0; j < col; j++)
		{
			board[i][j] = set;
		}
	}
}

        2,打印

                打印出2个二维数组,观察是否初始化成功。

void Displayboard(char board[ROWS][COLS], int row, int col)
{
	printf("----------扫雷游戏----------\n");
	int i = 0;
	for (i = 0; i <= col; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);
		int j = 0;
		for (j = 1; j <= col; j++)
		{
			
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}

        3,创建雷

                因为雷需要在9*9的地方创建,所以使用随机数,随机生成对应的位置,创建出10个雷。

void SetMine(char board[ROWS][COLS], int row, int col)
{
	int count = MAX;
	while (count > 0)
	{
		int x = rand() % 9 + 1;
		int y = rand() % 9 + 1;
		if (board[x][y] == '0')
		{
			board[x][y] = '1';
			count--;
		}
	}
}

        4,扫雷

                在mine数组中选择一个点,如果该点为'1',则游戏结束,玩家被炸死,如果不为'1',则开始判断是否是'0',是'0'则开始判断周围是否是'0',进行递归。

void win(char mine[ROWS][COLS], char show[ROWS][COLS],int x, int y, int* count)
{
	if (show[x][y] != '*')
	{
		return;
	}
	if (Getmine(mine, x, y) != 0)
	{
		(*count)++;
		int ret = Getmine(mine, x, y);
		show[x][y] = ret+ '0';
		return;
	}
	else if(Getmine(mine, x, y) == 0)
	{
		(*count)++;
		show[x][y] = ' ';
		win(mine,show, x - 1, y - 1, count);
		win(mine, show, x , y - 1, count);
		win(mine, show, x + 1, y - 1, count);
		win(mine, show, x + 1, y , count);
		win(mine, show, x + 1, y + 1, count);
		win(mine, show, x , y+1 , count);
		win(mine, show, x - 1, y + 1, count);
		win(mine, show, x - 1, y , count);
	}
}
void Findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int count = 0;
	while (count < ROW * COL - MAX)
	{
		printf("请玩家输入位置,用空格隔开:>");
		scanf("%d %d", &x, &y);
		if (1 <= x && x <= row && 1 <= y && y <= col)
		{
			if (mine[x][y] == '1')
			{
				printf("很遗憾,你被炸死了\n");
				Displayboard(mine, ROW, COL);
				break;
			}
			else
			{
				win(mine, show, x, y,&count);
				
				Displayboard(show, ROW, COL);

			}

		}
		else
		{
			printf("非法输入,请重新输入\n");
		}


	}
	if (ROW * COL-MAX == count)
	{
		printf("玩家胜利\n");
	}

}
void SetMine(char board[ROWS][COLS], int row, int col)
{
	int count = MAX;
	while (count > 0)
	{
		int x = rand() % 9 + 1;
		int y = rand() % 9 + 1;
		if (board[x][y] == '0')
		{
			board[x][y] = '1';
			count--;
		}
	}
}
int Getmine(char mine[ROWS][COLS], int row, int col)
{
	return (mine[col - 1][row - 1] + mine[row][col - 1] + mine[row + 1][col - 1] + mine[row + 1][col] + mine[row + 1][col + 1]+ mine[row][col+ 1] + mine[row - 1][col + 1] + mine[row - 1][col - 1] - 8 * '0');


}

2,完整代码

        1,game.h

#pragma once
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define MAX 10
#include<stdlib.h>
#include<time.h>
#include <stdio.h>

void Initboard(char board[ROWS][COLS], int row, int col, char set);
void Displayboard(char board[ROWS][COLS], int row, int col);
void SetMine(char board[ROWS][COLS], int row, int col);
void Findmine(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col);

        2,game.c

#include "game.h"

void Initboard(char board[ROWS][COLS], int row, int col, char set)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		int j = 0;
		for (j = 0; j < col; j++)
		{
			board[i][j] = set;
		}
	}
}
void Displayboard(char board[ROWS][COLS], int row, int col)
{
	printf("----------扫雷游戏----------\n");
	int i = 0;
	for (i = 0; i <= col; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);
		int j = 0;
		for (j = 1; j <= col; j++)
		{
			
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}

void SetMine(char board[ROWS][COLS], int row, int col)
{
	int count = MAX;
	while (count > 0)
	{
		int x = rand() % 9 + 1;
		int y = rand() % 9 + 1;
		if (board[x][y] == '0')
		{
			board[x][y] = '1';
			count--;
		}
	}
}
int Getmine(char mine[ROWS][COLS], int row, int col)
{
	return (mine[col - 1][row - 1] + mine[row][col - 1] + mine[row + 1][col - 1] + mine[row + 1][col] + mine[row + 1][col + 1]+ mine[row][col+ 1] + mine[row - 1][col + 1] + mine[row - 1][col - 1] - 8 * '0');


}


void win(char mine[ROWS][COLS], char show[ROWS][COLS],int x, int y, int* count)
{
	if (show[x][y] != '*')
	{
		return;
	}
	if (Getmine(mine, x, y) != 0)
	{
		(*count)++;
		int ret = Getmine(mine, x, y);
		show[x][y] = ret+ '0';
		return;
	}
	else if(Getmine(mine, x, y) == 0)
	{
		(*count)++;
		show[x][y] = ' ';
		win(mine,show, x - 1, y - 1, count);
		win(mine, show, x , y - 1, count);
		win(mine, show, x + 1, y - 1, count);
		win(mine, show, x + 1, y , count);
		win(mine, show, x + 1, y + 1, count);
		win(mine, show, x , y+1 , count);
		win(mine, show, x - 1, y + 1, count);
		win(mine, show, x - 1, y , count);
	}
}
void Findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int count = 0;
	while (count < ROW * COL - MAX)
	{
		printf("请玩家输入位置,用空格隔开:>");
		scanf("%d %d", &x, &y);
		if (1 <= x && x <= row && 1 <= y && y <= col)
		{
			if (mine[x][y] == '1')
			{
				printf("很遗憾,你被炸死了\n");
				Displayboard(mine, ROW, COL);
				break;
			}
			else
			{
				win(mine, show, x, y,&count);
				
				Displayboard(show, ROW, COL);

			}

		}
		else
		{
			printf("非法输入,请重新输入\n");
		}


	}
	if (ROW * COL-MAX == count)
	{
		printf("玩家胜利\n");
	}

}

        3,执行文件

#include "game.h"


void menu()
{
	printf("******************************\n");
	printf("********    1.play     *******\n");
	printf("********    0.exit     *******\n");
	printf("******************************\n");
}

void game(char mine[ROWS][COLS], char show[ROWS][COLS])
{
	srand((unsigned int)time(NULL));
	Initboard(mine,  ROWS, COLS,'0');
	Initboard(show, ROWS, COLS, '*');
	Displayboard(show, ROW, COL);
	SetMine(mine, ROW, COL);
	Findmine(mine, show, ROW, COL);
}
int main()
{
	char mine[ROWS][COLS] = { 0 };
	char show[ROWS][COLS] = { 0 };
	int input = 0;
	do
	{
		menu();
		printf("请输入选项:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game(mine,show);
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("非法输入,请重新输入\n");
			break;
		}
	} while (input);
	return 0;
	
}

猜你喜欢

转载自blog.csdn.net/2201_75443644/article/details/131413302