头文件
#ifndef __MINE_H__
#define __MINE_H__
#define LINE 10
#define LIST 10
#define ROWS 6
#define COWS 6
int game(char UserBoard[LINE+2][LIST+2], char PlayerBoard[LINE][LIST]);
void PrintBoard(char Playerboard[LINE][LIST]);
void MineLay(char UserBoard[LINE + 2][LIST + 2]);
void PrintUser(char UserBoard[LINE + 2][LIST + 2]);
int MineClear(char UserBoard[LINE + 2][LIST + 2], char PlayerBoard[LINE][LIST]);
void Blast(char UserBoard[LINE + 2][LIST + 2], char PlayerBoard[LINE][LIST]);
int Counter(char UserBoard[LINE + 2][LIST + 2], char PlayerBoard[LINE][LIST], int x, int y);
#endif //mine.h
函数文件
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#include"mine.h"
void MineLay(char UserBoard[LINE + 2][LIST + 2])
{
int x = 0, y = 0,i = 0,j = 0;
char m = '0';
printf("**************************\n");
printf("******* a: 十个雷 ********\n");
printf("******* b: 二十雷 ********\n");
printf("**************************\n");
printf("请选择难度>:");
fflush(stdin);
scanf("%c",&m);
printf("\n");
if ('a' == m)
{
j = 10;
}
else
j = 20;
while (i < j)
{
x = rand() % 10 + 1;
y = rand() % 10 + 1;
if ('0' == UserBoard[x][y])
{
UserBoard[x][y] = '1';
i++;
}
}
}
void Blast(char UserBoard[LINE + 2][LIST + 2], char PlayerBoard[LINE][LIST])//全局输了
{
int i = 0, j = 0;
for (i = 1; i <=LINE; i++)
{
for (j = 1; j <= LIST; j++)
{
if ('1' == UserBoard[i][j])
{
PlayerBoard[i - 1][j - 1] = '#';
}
if ('*' == PlayerBoard[i - 1][j - 1]) //
{ //
PlayerBoard[i - 1][j - 1] = ' '; //
} //
}
}
}
void PrintBoard(char Playerboard[LINE][LIST])
{
int i = 0, j = 0;
printf(" 0 1 2 3 4 5 6 7 8 9 10\n");
printf("------------------------\n");
for (i = 0; i < LINE; i++)
{
printf("%2d| ",i+1);
for (j = 0; j < LIST; j++)
{
printf("%c ",Playerboard[i][j]);
}
printf("\n");
}
printf("\n");
}
int MineClear(char UserBoard[LINE + 2][LIST + 2], char PlayerBoard[LINE][LIST])
{
int x = 0, y = 0,num = 0,win = 0,i =0,j = 0,m = 0,n = 0,p = 0,q = 0,a = 0,b = 0;
while (1)
{
printf("请输入扫雷坐标>:");
scanf("%d%d", &x, &y);
a = x;
b = y;
i = x;
j = y;
m = x;
n = y;
p = x;
q = y;
if ((x > 0 && x <= 10) && (y > 0 && y <= 10))
{
if ('1' == UserBoard[x][y])
{
PlayerBoard[x-1][y-1] = '#';
Blast(UserBoard, PlayerBoard);
PrintBoard(PlayerBoard);
printf("啊!!!!你被炸死啦!!!!\n");
return 0;
}
if ('0' == UserBoard[x][y])
{
for (a = x; a >= 1; a--,b = y)//向上向左
{
if (0 != Counter(UserBoard, PlayerBoard, a, b))
break;
for (b = y; b >= 1; b--) 自加自减和for循环的初值一定要注意哦,我一开始的代码是 for (a = x; a >= 1 && '0' == UserBoard[a][b]; a--)for(; b >= 1 && '0' == UserBoard[a][b]; b--)此时
{ //此时,当a--时,b的值已经是上次的0了,所以下面的每次都不执行,因为判断条件是b>=1
if (0 != Counter(UserBoard, PlayerBoard, a, b))
break;
}
}
for (i = x; i >= 1; i--,j = y)//向上向右数
{
if (0 != Counter(UserBoard, PlayerBoard, i, j))
break;
for (j = y; j <= LIST; j++) //for循环里面尽量不要既执行又判断
{
if (0 != Counter(UserBoard, PlayerBoard, i, j))
break;
}
}
for (m = x; m <= LINE; m++,n = y)//向下向左
{
if (0 != Counter(UserBoard, PlayerBoard, m, n))
break;
for (n = y; n >= 1; n--)
{
if (0 != Counter(UserBoard, PlayerBoard, m, n))
break;
}
}
for (p = x; p <= LINE; p++,q = y)//向下向右
{
if (0 != Counter(UserBoard, PlayerBoard, p, q))
break;
for (q = y; q <= 4; q++)
{
if (0 != Counter(UserBoard, PlayerBoard, p, q))
break;
}
}
PrintBoard(PlayerBoard);
//PrintUser(UserBoard);
win = 0; //一开始的时候这个地方没有写win = 0;这样就出现了一个问题,就是,上次输入一个坐标循环的时候win已经有大于0的值,导致循环完的最终结果不能小于3
for (x = 0; x < LINE; x++)
{
for (y = 0; y < LIST;y++)
if ('*' == PlayerBoard[x][y])
{
win++;
}
}
if (win <= 3)
{
for (x = 0; x < LINE; x++)
{
for (y = 0; y < LIST; y++)
if ('*' == PlayerBoard[x][y])
{
PlayerBoard[x][y] = '#';
}
}
printf("扫雷成功,你赢了!!!\n");
return 0;
}
}
}
}
}
int Counter(char UserBoard[LINE + 2][LIST + 2], char PlayerBoard[LINE][LIST], int x, int y)
{
int num = 0;
num = (UserBoard[x - 1][y - 1] - '0') +
(UserBoard[x - 1][y] - '0') +
(UserBoard[x - 1][y + 1] - '0') +
(UserBoard[x][y - 1] - '0') +
(UserBoard[x][y + 1] - '0') +
(UserBoard[x + 1][y - 1] - '0') +
(UserBoard[x + 1][y] - '0') +
(UserBoard[x + 1][y + 1] - '0');
if (0 == num)
{
PlayerBoard[x - 1][y - 1] = '0';
}
else
{
PlayerBoard[x - 1][y - 1] = num + '0';
}
return num;
}
int game(char UserBoard[LINE+2][LIST+2], char PlayerBoard[LINE][LIST])
{
/*int x = 0, y = 0;
int i = 0, j = 0;*/
//PrintBoard(PlayerBoard);
MineLay(UserBoard);
PrintBoard(PlayerBoard);
//PrintUser(UserBoard); ///
return MineClear(UserBoard, PlayerBoard);
return 0;
}
void PrintUser(char UserBoard[LINE + 2][LIST + 2])
{
int i = 0, j = 0;
printf(" 0 1 2 3 4 5 6 7 8 9 10\n");
for (i = 0; i < LINE + 2; i++)
{
printf("%2d ", i );
for (j = 0; j < LIST + 2; j++)
{
printf("%c ", UserBoard[i][j]);
}
printf("\n");
}
printf("\n");
}
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<windows.h>
#include"mine.h"
//1.第一次扫雷,扫一大片2.玩家选择难度3.插标记
void menu()
{
printf("***********************\n");
printf("****** 1.play *******\n");
printf("****** 0.exit *******\n");
printf("***********************\n");
printf("请选择相应的数字>:");
}
int main()
{
char PlayerBoard[LINE][LIST];
char UserBoard[LINE + 2][LIST + 2];
int input = 1, a = 0;
srand((unsigned int)time(NULL));
while (input)
{
memset(PlayerBoard, '*', sizeof(char)*LINE*LIST);
memset(UserBoard, '0', sizeof(char)*(LINE+2)*(LIST+2));
menu();
scanf("%d", &input);
printf("\n");
switch (input)
{
case 1:a = game(UserBoard, PlayerBoard);
break;
case 0:break; //此处break的作用是结束switch
default:printf("你的输入不符合要求,请重新输入>:"); break;
}
if (0 == input)
{
break; //此处break的作用是跳出while循环,即结束游戏
}
}
printf("游戏结束,欢迎再次使用!!!\n");
system("pause");
return 0;
}