本文实例为大家分享了C语言实现扫雷小程序的具体代码,供大家参考,具体内容如下
首先创建一个项目,建立一个头文件game.h,两个源文件game.c和test.c
game.h代码片:
#ifndef __GAME_H__
#define __GAME_H__
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define ROWS 10//行数
#define COLS 10//列数
#define MINE 20//雷数
void Init_board(char arr[ROWS+2][COLS+2],int rows,int cols, char a);
void Is_show(char arr[ROWS+2][COLS+2], int rows, int cols);
void Set_mine(char arr[ROWS + 2][COLS + 2], int rows, int cols);
void Over_board(char arr[ROWS + 2][COLS + 2], int rows, int cols);
#endif
test.c代码片:
#include"game.h"
#include<time.h>
void menu() //打印菜单栏
{
int i = 0;
int j = 0;
for (i = 0; i < 5; i++)
{
if (i == 2)
{
printf(" 1.Play 0.Exit ");
}
else
{
for (j = 0; j < COLS * 3; j++)
{
printf("%c", 3);
}
}
printf("\n");
}
}
int get_mine(char arr[ROWS + 2][COLS + 2], int x, int y)//用于清空没有雷区的地方
{
return (arr[x - 1][y] - '0')
+ (arr[x - 1][y - 1] - '0')
+ (arr[x - 1][y + 1] - '0')
+ (arr[x][y - 1] - '0')
+ (arr[x][y + 1] - '0')
+ (arr[x + 1][y - 1] - '0')
+ (arr[x + 1][y + 1] - '0')
+ (arr[x + 1][y] - '0');
}
void game() //玩游戏函数
{
char mine[ROWS+2][COLS+2] = { 0 };
char show[ROWS+2][COLS+2] = { 0 };
int win = 1;
srand((unsigned int)time(NULL));
Init_board(mine, ROWS + 2, COLS + 2, '0');
Init_board(show, ROWS + 2, COLS + 2, '*');
//打印雷区棋盘 方便调试
Set_mine(mine, ROWS + 2, COLS + 2);
Is_show(show, ROWS + 2, COLS + 2);
while (win < ROWS*COLS - MINE)
{
int x = 0;
int y = 0;
printf("请选择>:");
scanf("%d%d", &x, &y);
if ((x >= 1) && (x <= 10) && (y >= 1) && (y <= 10))
{
if (mine[x][y] == '1')
{
printf("你输了!\n");
Over_board(mine, ROWS + 2, COLS + 2);
Is_show(mine, ROWS + 2, COLS + 2);
break;
}
else
{
int count = get_mine(mine, x, y);
if (count == 0)
{
win += 9;
show[x][y] = ' ';
show[x - 1][y - 1] = ' ';
show[x - 1][y] = ' ';
show[x - 1][y + 1] = ' ';
show[x][y - 1] = ' ';
show[x][y + 1] = ' ';
show[x + 1][y - 1] = ' ';
show[x + 1][y] = ' ';
show[x + 1][y + 1] = ' ';
Is_show(show, ROWS + 2, COLS + 2);
}
else
{
win++;
show[x][y] = count + '0';
Is_show(show, ROWS + 2, COLS + 2);
}
}
}
else
{
printf("输入有误,请重新输入%c", 1);
}
}
if (win >=ROWS*COLS - MINE)
{
Over_board(show, ROWS + 2, COLS + 2);
printf("你赢了!\n");
Is_show(show, ROWS + 2, COLS + 2);
}
}
int main()
{
int input = 0;
do
{
menu();
printf("请选择>;");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
break;
default:
printf("选择错误,请重新选择\n");
break;
}
} while (input);
return 0;
}
game.c代码片:
#include"game.h"
void Init_board(char arr[ROWS+2][COLS+2], int rows, int cols, char a)//初始化棋盘
{
memset(arr, a, rows*cols);
}
void Is_show(char arr[ROWS+2][COLS+2], int rows, int cols)//打印棋盘
{
int i = 0;
int j = 0;
printf(" ");
for (i = 0; i < rows - 2; i++)
{
printf("_%d__", i + 1);
}
printf("\n");
for (i = 0; i < rows - 2; i++)
{
printf("%2d ", i + 1);
for (j = 0; j < cols - 2; j++)
{
printf("|_%c_", arr[i + 1][j + 1]);
}
printf("|\n");
}
}
void Set_mine(char arr[ROWS + 2][COLS + 2], int rows, int cols)//设置雷区
{
int x = 0;
int y = 0;
int count = MINE;
while (count)
{
x = rand() % 10 + 1;
y = rand() % 10 + 1;
if (arr[x][y] != '1')
{
arr[x][y] = '1';
count--;
}
}
}
void Over_board(char arr[ROWS + 2][COLS + 2], int rows, int cols)//将所有不是雷区的位置清空
{
int i = 0;
int j = 0;
for (i = 0; i < rows - 2; i++)
{
for (j = 0; j < cols - 2; j++)
{
if (arr[i + 1][j + 1] == '0')
arr[i + 1][j + 1] = ' ';
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。