为提升游戏的体验性,不会让玩家第一次就踩到雷
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"game.h"
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 < rows-1; i++)
{
printf(" %d ", i);
for (j = 1; j < cols-1; j++)
{
printf(" %c ", show[i][j]);
}
printf("\n");
}
}
void set_mine(char mine[rows][cols])//设置雷的位置
{
int count = Count;
int x = 0;
int y = 0;
srand((unsigned)time(NULL));
while (count)
{
x = rand() % 9 + 1;
y = rand() % 9 + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
int Sweep(char mine[rows][cols], char show[rows][cols])//开始扫雷
{
int count = 0;
int x = 0;
int y = 0;
int clear = 0;
while (count != (rows - 2)*(cols - 2) - Count)
{
printf("请输入坐标:");
scanf("%d %d", &x, &y);
if (mine[x][y] == '1')
{
if (clear == 0)
{
move_mine(mine, x, y);
show[x][y] = '0';
display(show);
clear++;
}
else
{
printf("你挂了\n");
display(mine);
break;
}
}
else
{
int ret = get_num(mine, x, y);
show[x][y] = ret + '0';
if (ret==0)
{
show[x][y] = '0';
show[x][y-1] = '0';
show[x][y+1] = '0';
show[x-1][y-1] = '0';
show[x-1][y] = '0';
show[x-1][y+1] = '0';
show[x+1][y-1] = '0';
show[x+1][y] = '0';
show[x+1][y+1] = '0';
}
display(show);
count++;
}
}
printf("你赢了\n");
display(mine);
return 0;
}
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 ][y - 1] == '1')
{
count++;
}
if (mine[x - 1][ y ] == '1')
{
count++;
}
if (mine[x + 1 ][y + 1] == '1')
{
count++;
}
if (mine[ x ][y + 1] == '1')
{
count++;
}
if (mine[ x + 1 ][ y ] == '1')
{
count++;
}
if (mine[ x + 1 ][y - 1] == '1')
{
count++;
}
if (mine[x - 1][ y + 1] == '1')
{
count++;
}
return count;
}
int Game(char mine[rows][cols], char show[rows][cols])//游戏
{
set_mine(mine);
display(show);
display(mine);
Sweep(mine, show);
return 0;
}
void move_mine(char mine[rows][cols],int x,int y)
{
int tmp;
int i = 0;
int j = 0;
srand((unsigned)time(NULL));
i = rand() % 9 + 1;
j = rand() % 9 + 1;
if (mine[i][j] = '0')
{
tmp = mine[i][j];
mine[i][j] = mine[x][y];
mine[x][y] = tmp;
}
}
game.h
#define _CRT_SECURE_NO_WARNINGS 1
#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 Count 10
void display(char show[rows][cols]);
int Game(char mine[rows][cols], char show[rows][cols]);//游戏
void set_mine(char mine[rows][cols]);//设置雷的位置
int Sweep(char mine[rows][cols], char show[rows][cols]);//开始扫雷
int get_num(char mine[rows][cols], int x, int y);//计算雷的个数
void move_mine(char mine[rows][cols],int x,int y);//避免第一次踩
#endif
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
int menu()
{
printf("************** welcome to saolei ****************\n");
printf("**************** 1.play ********************\n");
printf("**************** 0.exit ********************\n");
printf("*************************************************\n");
return 0;
}
int main()
{
int input = 0;
char mine[rows][cols] = { 0 };
char show[rows][cols] = { 0 };
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
mine[i][j] = '0';
show[i][j] = '*';
}
}
menu();
printf("请选择-->");
scanf("%d", &input);
switch (input)
{
case 1:
Game(mine, show);
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入有误\n");
break;
}
return 0;
}