C语言扫雷

电脑杂谈  发布时间:2020-06-10 16:07:25  来源:网络整理

扫雷游戏的c语言程序设计报告_高质量程序设计指南:c /c语言_扫雷c语言程序设计

1. 首先是编写游戏的测试模块. 有一些菜单功能和选择功能会提示玩家进入,而需要编写的各种功能都带有一个整体框架.

扫雷游戏的c语言程序设计报告_高质量程序设计指南:c /c语言_扫雷c语言程序设计

//测试模块test。c 
#include "game.h"
void Menu()
{
	printf("***********************************************************************\n");
	printf("************************1.PLAY          0.EXIT*************************\n");
	printf("***********************************************************************\n");
}
int Game()
{
	int x = 0;
	int y = 0;
	int i = 0;
	int j = 0;
	int ret = 0;
	int num = count;
	char real[ROWS][COLS] = {'0'};
	char show[ROWS][COLS] = {'*'};
	srand((unsigned)time(NULL));
	Init(real,ROWS, COLS, '0');
	Init(show, ROWS, COLS, '*');
	SetMine(real, ROWS, COLS, count);
	Display(show, ROW, COL);
	printf("Pelase Input x and y :\n");
	scanf_s("%d%d", &x, &y);
	if (real[x][y] == '1')//保证第一次不踩到雷
	{
		do
		{
			SetMine(real, ROWS, COLS, count);
		} while (real[x][y] == '1');
	}
	show[x][y] = SearchMind(real, &x, &y, ROWS, COLS) + '0';
	for (i = 0; i <ROWS; i++)//管理员监视器模块
	{
		//printf("%3d", i); 
		for (j = 0; j < COLS; j++)
		{
			printf("%3c", real[i][j]);
		}
		printf("\n");
	}
	printf("\n");
	Display(show, ROW, COL);
	while ((ROW*COL) > (ROW*COL - num))
	{
		printf("Pelase Input x and y :\n");
		scanf_s("%d%d", &x, &y);
		ret = SearchMind(real, &x, &y, ROWS, COLS) + 1;
		if (ret!=0)//不是踩到雷的情况进入
		{
			show[x][y] = SearchMind(real, &x, &y, ROWS, COLS) + '0';//转化成字符型
            Display(show, ROW, COL);
		}
		if (ret == 0)//踩到雷的情况进入
		{
			return 0;
		}
		num--;
	}
	return 1;
}
int main()
{
	Menu();
	while (1)
	{
		int m = 0;
		scanf_s("%d", &m);
		switch (m)
		{
		case 1:
		{
			if (Game())
			{
				printf("you win\n");
			}
			else
			{
				printf("you failed\n");
				Menu();
			}
			break; 
		}
		case 2:
			return 0;
			break;
		default:
			printf("请重新输入\n");
			break;
		}
	}
}

扫雷游戏的c语言程序设计报告_高质量程序设计指南:c /c语言_扫雷c语言程序设计

在测试函数中使用头文件#include“ game.h”而不是#include ...的原因是因为我们在后续头文件game.h中包含了需要声明的内容头文件

扫雷游戏的c语言程序设计报告_高质量程序设计指南:c /c语言_扫雷c语言程序设计

接下来是头文件game.h

扫雷c语言程序设计_扫雷游戏的c语言程序设计报告_高质量程序设计指南:c /c语言

#ifndef __GAME_H__
#define __GAME_H__
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define ROWS 11
#define COLS 11
#define ROW 9
#define COL 9
#define count 10
void Init(char arr[ROWS][COLS],int rows, int cols, const char ch);
void SetMine(char str[ROWS][COLS], int rows, int cols, int cot);
void Display(char str[ROWS][COLS], int rows, int cols);
int SearchMind(char str[ROWS][COLS], int* x, int* y, int rows, int cols);
#endif

接下来是游戏中最重要的部分和核心部分. 游戏功能文件game.c


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-240765-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      热点图片
      拼命载入中...