一、简易扫雷
本文第一篇简易扫雷我借鉴了https://blog.csdn.net/LX18792732127/article/details/52794268里的文章,做出了一些修改
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define rows 11
#define cols 11
#define Count 10
void set_time(char mine[rows][cols]){//设置雷的位置
int count = Count;
int a = 0;
int b = 0;
srand((unsigned)time(0));
while (count){
a = rand() % 9 + 1;
b = rand() % 9 + 1;
if (mine[a][b] == '0'){
mine[a][b] = '1';
count--;
}
}
}
void display(char show[rows][cols]){
int i = 0;
int j = 0;
printf(" ");
for (i = 1; i < cols - 1; ++i){
printf("%d", i);
}
printf("\n");
for (i = 1; i < cols - 1; ++i){
printf("%d", i);
for (j = 1; j < cols - 1; j++){
printf("%c", show[i][j]);
}
printf("\n");
}
}
int get_num(char mine[rows][cols], int x, int y){
int count = 0;
if (mine[x - 1][y - 1] == '1'){
count++;
}
if (mine[x - 1][y + 1] == '1'){
count++;
}
if (mine[x][y - 1] == '1'){
count++;
}
if (mine[x][y + 1] == '1'){
count++;
}
if (mine[x + 1][y - 1] == '1'){
count++;
}
if (mine[x + 1][y] == '1'){
count++;
}
if (mine[x + 1][y + 1] == '1'){
count++;
}
return count;
}
int Sweep(char mine[rows][cols], char show[rows][cols]){
int count = 0;
int x = 0;
int y = 0;
while (count != ((rows - 2)*(cols - 2) - Count)){
printf("请输入坐标:\n");
scanf("%d%d", &x, &y);
if (mine[x][y] == '1'){
printf("你踩到雷了!\n");
return 0;
}
else
{
int ret = get_num(mine, x, y);
show[x][y] = ret + '0';
display(show);
count++;
}
}
printf("恭喜你赢了!\n");
display(mine);
return 0;
}
int Game(char mine[rows][cols], char show[rows][cols]){
set_time(mine);
display(show);
//display可以将雷的位置显示出来
Sweep(mine, show);
return 0;
}
int main(){
char mine[rows][cols];
char show[rows][cols];
int m = 0;
int n = 0;
printf("**********************\n");
printf("******1 开始游戏*****\n");
printf("******0 退出游戏*****\n");
printf("**********************\n");
for (m = 0; m < rows - 1; ++m){
for (n = 0; n < cols - 1; n++){
mine[m][n] = '0';
show[m][n] = '*';
}
}
while (1){
printf("请输入您的选项: \n");
int choice;
scanf("%d", &choice);
if (choice == 1){
Game(mine, show);
break;
}
else if (choice == 0){
printf("退出游戏!\n");
return 0;
}
else
printf("输入有误,请重新输入!");
}
system("pause");
return 0;
}