【C语言】实现小游戏扫雷

用C语言实现扫雷小游戏,一共有三部分的代码

game.h

#ifndef __GAME_H_
#define __GAME_H_

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define EASY_COUNT 10
#define ROW 9
#define COL 9

#define ROWS ROW+2
#define COLS COL+2

void InitBoard(char arr[ROWS][COLS], int row, int col, char set);
void DisplayBoard(char arr[ROWS][COLS], int row, int col);
void SetMine(char arr[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
char GetMineCount(char mine[ROWS][COLS], int x, int y);

#endif //__GAME_H_

game.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

void InitBoard(char arr[ROWS][COLS], int rows, int cols, char set)
{
	int i = 0;
	int j = 0;
	for(i=0; i<rows; i++)
	{
		for(j=0; j<cols; j++)
		{
			arr[i][j]=set;
		}
	}
}

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

void SetMine(char arr[ROWS][COLS], int row, int col, int x, int y)
{
	int count = EASY_COUNT;
	while(count)
	{
		int a = rand()%row+1;
		int b = rand()%row+1;
		if((arr[a][b] != '1')&&((x != a)||(y != b)))
		{
			arr[a][b]='1';
			count --;
		}
	}
}

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
    int num = 0;
	char count = 0;
    printf("请输入坐标:");
    scanf("%d%d", &x, &y);
	while(show[x][y] != '*')
	{
		printf("请重新输入:");
		scanf("%d%d", &x, &y);
	}
	SetMine(mine,ROW,COL,x,y);
	while(1)
	{
        if(x>=1 && x<=row && y>=0 && y<=col)
		{
			num++;
			if(mine[x][y]=='1')
			{
				printf("很遗憾,你输了\n");
				/*DisplayBoard(mine, ROW, COL);*/
				break;
			}
			else
			{
				if(num == row*col - EASY_COUNT)
				{
					printf("恭喜你,排雷成功\n");
					break;
				}
				count = GetMineCount(mine,x,y);
				show[x][y] = count;
				DisplayBoard(show, ROW, COL);
			}
			printf("请继续输入:");
		}
		else
		{
			printf("坐标非法,请重新输入:\n");
		}
		scanf("%d%d", &x, &y);
		while(show[x][y] != '*')
		{
			printf("请重新输入:");
			scanf("%d%d", &x, &y);
		}
	}
}

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

test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

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

void game()
{
	char mine[ROWS][COLS]={0};
	char show[ROWS][COLS]={0};
	InitBoard(mine, ROWS, COLS, '0');
	InitBoard(show, ROWS, COLS, '*');
	DisplayBoard(show, ROW, COL);
	FindMine(mine, show, ROW, COL);
	/*DisplayBoard(mine, ROW, COL);*/	
}

void test()
{
	int input = 0;
	do
	{
		printf("请选择:");
	    scanf("%d", &input);
	    switch(input)
		{
		case 1:
			game();
			break;
		case 0:
			exit(1);
			break;
		default:
			printf("输入错误,请重新输入:");
			break;
		}
	}while(input);
}

int main()
{
	srand((unsigned int)time(NULL));
	menu();
	test();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/wxj_enchanted/article/details/80231415