实现用C语言编写一个扫雷游戏
大家想必都玩过扫雷,无论那个版本都有难度供已选择,下面来分享一个自己用C语言编写的扫雷游戏吧!
编写语言:C语言
编写软件:visual studio 2017
1.首先是将游戏的测试模块写好,要有提示玩家进入的菜单函数以及选择函数等各种需要编写的函数想出一个整体框架来
//测试模块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;
}
}
}
测试函数中的头文件 #include "game.h",而不是#include
接下来是头文件game.h
#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
#include "game.h"
void Init(char arr[ROWS][COLS], int rows, int cols, const char ch)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
arr[i][j] = ch;
}
}
}
//随机埋雷 SetMine()
void SetMine(char str[ROWS][COLS], int rows, int cols, int cot)
{
int x = 0;
int y = 0;
while (cot > 0)
{
x = rand() % 9 + 1;
y = rand() % 9 + 1;
if (str[x][y]==‘0‘)
{
str[x][y] = ‘1‘;
cot--;
}
}
}
//显示棋盘 Display()
void Display(char str[ROWS][COLS], int rows, int cols)
{
int i = 0;
int j = 0;
for (i = -1; i < rows; i++)
{
printf("%3d", i+1);
}
printf("\n");
for (i = 1; i <=rows; i++)
{
printf("%3d", i);
for (j = 1; j <= cols; j++)
{
printf("%3c", str[i][j]);
}
printf("\n");
}
}
//输入坐标,判断周围的雷数,并回馈数字,为0时递归SearchMine()
int SearchMind(char str[ROWS][COLS], int* x, int* y, int rows,int cols)
{
int number = 0;
//判断是否踩到雷
if (str[*x][*y] == ‘1‘)
{
return -1;
}
number = str[*x - 1][*y - 1] + str[*x - 1][*y] + str[*x - 1][*y + 1]
+ str[*x][*y - 1] + str[*x][*y + 1]
+ str[*x + 1][*y - 1] + str[*x + 1][*y] + str[*x + 1][*y + 1] - 8 * ‘0‘;
//if (number == 0)
//{
// //改变x,y的值实现爆炸效果
// number = SearchMind(str, &x, &y, rows, cols);
//}//递归
return number;
}
//判断输赢 Distinguish()
原文地址:https://www.cnblogs.com/liuzhengkai/p/9096968.html
时间: 2024-10-22 18:36:59