欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

简单扫雷游戏

程序员文章站 2022-04-12 10:33:18
...

game.h

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define count 6
#define ROW 3
#define COL 3
#define ROWS ROW+2
#define COLS COL+2

void initgame(char board[ROWS][COLS], int row, int col, char set);
void display(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);

game.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

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

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

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

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

void findmine(char mine[ROWS][COLS] , char show[ROWS][COLS], int row, int col)
{
	int win = 0;
	int x;
	int y;
	while (win < col*row - count)
	{
		printf("  请选择扫雷点:  \n");
		scanf("%d%d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x][y] == '1')
			{
				display(mine, row, col);
				display(show, row, col);
				printf("  引爆,游戏结束  \n");
				break;
			}
			else
			{
				show[x][y] = getminecount(mine, x, y) + '0';
				display(mine, row, col);
				display(show, row, col);
				win++;
			}
		}
		else
		{
			printf("  非法输入  \n");
		}
	}
	if (win == col * row - count)
	{
		display(show, row, col);
		printf("  游戏成功  \n");
	}
}

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 };
	initgame(mine, ROWS, COLS, '0');
	initgame(show, ROWS, COLS, '*');
	setmine(mine, ROW, COL);
	display(mine, ROW, COL);
	display(show, ROW, COL);
	findmine(mine, show, ROW, COL);
}

int main()
{
	srand((unsigned int)time(NULL));
	int input;
	do
	{
		menu();
		printf("  选择是否游戏  1  或者  0  \n");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("  退出游戏  ");
			break;
		default:
			printf("  非法输入,请重新输入  ");
			break;
		}
	} while (input);
	return 0;
}