按找我们的编程思路,先写主函数,步骤需要的函数,然后声明,封装。
扫雷,我用了两个数组,一个显示数组,一个布置雷的数组,然后依次创建,我同样用的memset函数。
打印显示函数。
主函数中菜单,选择,游戏开始,等等。
#include"game.h"
void game() //游戏
{
char mine[Rows][Cols];
char show[Rows][Cols];
Init(mine, Rows, Cols, '0');
Init(show, Rows, Cols, '*');
Display(show, Row, Col);
Set_Mine(mine, Row, Col);
//Display(mine, Row, Col);
PlayerGo(mine, show, Row, Col);
Display(mine, Row, Col);
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do {
menu();
printf("请输入您的选择:>");
scanf("%d", &input);
if (input == 1)
{
game(); break;
}
else if (input == 0) printf("\t退出游戏\n");
} while (input != 0);
return 0;
}
把需要的函数,头文件在game.h文件中声明。
#define _CRT_SECURE_NO_WARNINGS 1
#ifndef __GAME_H__
#define __GAME_H__
#define Row 6 //行
#define Col 6 //列
#define Rows Row+2
#define Cols Col+2
#define thunder 10
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<windows.h>
void menu();
void game();
int PlayerGo(char mine[Rows][Cols], char show[Rows][Cols], int row, int col);
void Display(char arr[Rows][Cols], int row, int col);
void Init(char arr[Rows][Cols], int rows, int cols, char f);
void Set_Mine(char arr[Rows][Cols], int row, int col);
#endif // __GAME_H__
#include"game.h"
void menu()//菜单
{
system("cls");
system("color F9");
printf("\n\n\t****************************\n");
printf("\t**** 1.开始游戏 *****\n");
printf("\t**** 0.退出 *****\n");
}
int Sum_Emloem(char arr[Rows][Cols], int row, int col)
{
int i = 0;
int j = 0;
int count=0;
for (i = 1; i <= row; i++)
{
for (j = 1; j <= col; j++)
if (arr[i][j] == '*')
count++;
}
return count;
}
void Init(char arr[Rows][Cols], int rows, int cols, char f)//数组初始化
{
memset(arr,f, rows*cols);
}
void Display(char arr[Rows][Cols], int row, int col)//显示
{
int i = 0;
printf("\n\n\n\t ");
for (i = 1; i <= col; i++)
{
printf(" %d", i );
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("\t%d ", i);
int j = 0;
for (j = 1; j <= col; j++)
printf("%c ", arr[i][j]);
printf("\n");
}
}
void Set_Mine(char mine[Rows][Cols], int row, int col) //放置雷
{
int x = 0;
int y = 0;
int count = thunder;
while (count)
{
x = rand() % row+1;
y = rand() % col+1;
if (1 <= x&&x <= row && 1 <= y&&y <= col )
{
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
}
//统计她周围雷的个数然后好显示打印。
int Get_Num(char mine[Rows][Cols], int r, int c)
{
int count = 0;
int i = 0;
int j = 0;
for (i = r - 1; i <= r + 1; i++)
{
for (j = c - 1; j <= c + 1; j++)
{
if (mine[i][j] == '1')
count++;
}
}
return count;
}
//通过玩家给出的位置判断是雷否,不是雷就要显示周围雷个数,如果周围没有雷,就递归显示,直到周围有雷为止。在这个递归函数中注意我用了一个标记数组在记录这个位置是否已经访问过了,防止死循环般的重复递归查找。
int Gets_Num(char mine[Rows][Cols], char show[Rows][Cols], char mark[Rows][Cols], int r, int c,int row,int col)//扩展
{
if(Get_Num(mine, r, c) == 0)
{
show[r][c] = '0';
mark[r][c] = 'Y';
int i = 0;
for (i = r - 1; i <= r + 1; i++)
{
int j = 0;
for (j = c - 1; j <= c + 1; j++)
{
if ((i >= 1&&i <= row) && (j >=1&&j <= col))
{
if (mark[i][j] == 'N')
{
Gets_Num(mine, show, mark, i, j, row, col);
}
}
}
}
return 0;
}
else
{
show[r][c] = '0' + Get_Num(mine, r, c);
mark[r][c] = 'Y';
return 0;
}
}
//在这里为了让游戏更好玩,我写了一段程序,当第一次即使玩家踩到雷也会自动把雷移走。避免了玩家一开始玩就输了的尴尬。当然,这样就影响我们不能只有一个非雷的位置,因为这样玩家无论怎么玩都是赢。
int PlayerGo(char mine[Rows][Cols], char show[Rows][Cols], int row, int col)
{
int r = 0;
int c = 0;
char mark[Rows][Cols];
Init(mark, Rows, Cols,'N');
while (Sum_Emloem(show, row, col)>thunder)
{
printf("\n\n\t\t请玩家输入扫雷的位置(如:1 1 ):>");
scanf("%d %d", &r, &c);
if (Sum_Emloem(show, row, col) == row*col&&mine[r][c]=='1')
{
mine[r][c] = '0';
int x = 0;
int y = 0;
int cou = 1;
while (cou)
{
x = rand() % row + 1;
y = rand() % col + 1;
if (1 <= x&&x <= row && 1 <= y&&y <= col&&(x!=r||y!=c))
{
if (mine[x][y] == '0')
{
mine[x][y] = '1';
cou--;
}
}
}
}
if (1 <= r&&r <= row && 1 <= c&&c <= col)
{
if (mine[r][c] == '1')
{
printf("哇 ,好可惜啊,你踩到雷了哦~游戏结束\n");
return 1;
}
else
if (mine[r][c] == '0')
{
Gets_Num(mine, show, mark, r, c, row, col);
system("cls");
Display(show, row, col);
}
}
else printf("输入错误\n");
}
system("cls");
system("color C");
printf("\n\n\t\t恭喜玩家,排雷成功。*★,°*:.☆( ̄▽ ̄)/$:*.°★* 。\n");
return 0;
}