目录
测试情况
文章目录
主体
主体包括菜单,游戏规则简绍,选择进行与否
int main()
{
int input;
srand((unsigned int)time(NULL));
do
{ menu();
scanf("%d", &input);
switch (input)
{
case 2:rule(); break;
case 1:game(); break;
case 0:printf("退出游戏\n"); break;
default:printf("输入非法,请重新输入\n"); break;
}
} while (input);
return 0;
}
整体架构流程
进行游戏概括,首先要创建两个字符数组,一个进行操作另一个进行展示。扫雷选择x*x格式,例如9*9格式,需要先将两个数组初始化,初始化完成后需要进行雷的随机布置
,(在这里编程时尽量先完成棋盘的打印方便调试观察,等到全部任务完成后再调换位置)
布置完雷,后进行棋盘的打印和寻找
主要内容:
将所有头文件置于一个文件中:
技术名词解释
雷的随机布置需要用到随机数,可以使用rand函数,但是制造出来的随机数是伪随机数,这里就可以借助time函数返回时间戳,弄到srand(),将种子数改变,制造出真正的随机数,使用时也要包括头文件time.h ,stdlib.h
技术细节
1初始化:,a,b分别代表横纵坐标,c代表被初始化的字符('0'/'*')
2打印棋盘:
3,布置雷
4寻找雷
5*:细节添加
这里的细节照应规则
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
//初始化棋盘
void Init_arr(char board[ROW][COL], int a, int b,char c)
{
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
{
board[i][j] = c;
}
}
}
void Display(char board[ROW][COL], int a, int b)
{
printf("-------------扫雷游戏-----------------\n");
for (int i = 0; i <= row; i++)
{
printf(" %d ", i);
}
printf("\n");
printf("\n");
for (int i = 1; i <=a; i++)
{
printf(" %d ", i);
for (int j = 1; j <= b; j++)
{
printf("'%c' ", board[i][j]);
}
printf("\n");
printf("\n");
}
}
void SetMine(char board[ROW][COL], int a, int b)
{
int count = EASY_MINE;
while (count)
{
int x = rand() % a + 1;
int y = rand() % b + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
int get(char board[ROW][COL], 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');
}
int GETCOUNT(char board[ROW][COL],char show[ROW][COL],int x, int y)
{
for (int i = x - 1; i <= x + 1; i++)
{
for (int j = y - 1; j <= y + 1; j++)
{
int q = rand() % 100;
if (board[i][j] != '1'&&q>=50)
{
show[i][j] = get(board, x, y)+'0';
}
}
}
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 board[ROW][COL], char show[ROW][COL], int a, int b)
{
int x, y ,win=0;
while(win>=0)
{
printf("请选择所要查找的坐标:");
scanf("%d %d", &x, &y);
if (x > 0 && x <= a && y > 0 && y <= b)
{
if (board[x][y] == '1')
{
printf("很遗憾,游戏失败\n");
Display(board, row, col);
break;
}
else
{
show[x][y] = GETCOUNT(board,show, x, y)+'0';
Display(show, row, col);
Display(board, row, col);
}
}
else
printf("坐标非法,请重新输入\n");
int tmp = 0;
for (int i = 1; i <= a; i++)
{
for (int j = 1; j <= b; j++)
{
if (show[i][j] == '*')
{
tmp++;
}
}
}
win = tmp;
if (win == EASY_MINE)
{
printf("恭喜你,所有的雷已被您找到!\n");
show[x][y] = GETCOUNT(board, show,&win,x, y);
break;
}
}
}
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
printf("----------------\n");
printf("-----0.exit-----\n");
printf("-----1.play-----\n");
printf("-----2.rule-----\n");
printf("----------------\n");
}
void rule()
{
printf("总共有%d个雷\n", EASY_MINE);
printf("输入所要查找的坐标,\n如若不是雷,则在该处显示周围雷的\n个数,并随机爆出一圈某几个不是雷的\n位置,并也显示周围雷的个数\n");
printf("直到所有被找到\n");
}
void game()
{
char board[ROW][COL] = { 0 };
char show[ROW][COL] = { 0 };
//初始化棋盘
Init_arr(board, ROW, COL,'0');
Init_arr(show, ROW, COL,'*');
打印棋盘
Display(show, row, col);
//布置棋盘
SetMine(board, row, col);
FindMine(board, show, row, col);
//寻找
}
int main()
{
int input;
srand((unsigned int)time(NULL));
do
{
menu();
scanf("%d", &input);
switch (input)
{
case 2:rule(); break;
case 1:game(); break;
case 0:printf("退出游戏\n"); break;
default:printf("输入非法,请重新输入\n"); break;
}
} while (input);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define ROW 11
#define COL 11
#define row 9
#define col 9
#define EASY_MINE 10
void Init_arr(char board[ROW][COL], int a, int b, char c);
void Display(char board[ROW][COL], int a, int b);
void SetMine(char board[ROW][COL], int a, int b);
void FindMine(char board[ROW][COL], char show[ROW][COL], int a, int b);
测试情况