C语言实现简易扫雷
首先,写代码之前要将整体思路写出来:
扫雷游戏:1.需要两个二维数组,一个用来展示,一个用来放雷;
2.整体骨架在代码中都有注释说明;
3.游戏难度比较简单,适合初学者观看,如果有大佬看明白,可以指点一二.
//使用二维数组来表示地图,此处需要2个二维数组,第一个二维数组表示地雷的雷阵,第二个二维数组表示用户看到的地图
//扫雷地图大小9*9;但是二维数组11*11
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#define MINE_COUNT 10
#define ROW 9
#define COL 9
char show_map[ROW + 2][COL + 2];
char mine_map[ROW + 2][COL + 2];
int Menu(){
int choice = -1;
printf("************************\n");
printf("* 欢迎来到扫雷游戏 *\n");
printf("* 请您选择 *\n");
printf("* 1.开始游戏 *\n");
printf("* 2.离开游戏 *\n");
printf("************************\n");
while (1){
scanf("%d", &choice);
if (choice == 1)
{
return 1;
break;
}
else if (choice == 2)
{
exit(2);
}
else
{
printf("输入非法,请重新输入!\n");
continue;
}
}
}
int choice = -1;
printf("************************\n");
printf("* 欢迎来到扫雷游戏 *\n");
printf("* 请您选择 *\n");
printf("* 1.开始游戏 *\n");
printf("* 2.离开游戏 *\n");
printf("************************\n");
while (1){
scanf("%d", &choice);
if (choice == 1)
{
return 1;
break;
}
else if (choice == 2)
{
exit(2);
}
else
{
printf("输入非法,请重新输入!\n");
continue;
}
}
}
void Init(){ //初始化 布雷
srand(time(0));
memset(mine_map, '0', (ROW + 2)*(COL + 2));
memset(show_map, '*', (ROW + 2)*(COL + 2));
int count = MINE_COUNT;
int row = -1;
int col = -1;
while (count>=0){
row = rand() % ROW + 1;
col = rand() % COL + 1;
if (show_map[row][col] == '*')
{
mine_map[row][col] = '1';
count--;
continue;
}
}
}
void Print(){ // 1 2 3 4 5 6 7 8 9
printf(" "); // -----------------
for (int col = 1; col <= COL; col++) // 01 | | | | | | | | |
{ // -----------------
printf(" %d ", col);
}
printf("\n ---------------------------\n");
for (int row = 1; row <= ROW; row++)
{
printf("%02d ", row);
printf(" |%c |%c |%c |%c |%c |%c |%c |%c |%c |\n", show_map[row][1], show_map[row][2], show_map[row][3], show_map[row][4], show_map[row][5],
show_map[row][6], show_map[row][7], show_map[row][8], show_map[row][9]);
printf(" ---------------------------\n");
}
srand(time(0));
memset(mine_map, '0', (ROW + 2)*(COL + 2));
memset(show_map, '*', (ROW + 2)*(COL + 2));
int count = MINE_COUNT;
int row = -1;
int col = -1;
while (count>=0){
row = rand() % ROW + 1;
col = rand() % COL + 1;
if (show_map[row][col] == '*')
{
mine_map[row][col] = '1';
count--;
continue;
}
}
}
void Print(){ // 1 2 3 4 5 6 7 8 9
printf(" "); // -----------------
for (int col = 1; col <= COL; col++) // 01 | | | | | | | | |
{ // -----------------
printf(" %d ", col);
}
printf("\n ---------------------------\n");
for (int row = 1; row <= ROW; row++)
{
printf("%02d ", row);
printf(" |%c |%c |%c |%c |%c |%c |%c |%c |%c |\n", show_map[row][1], show_map[row][2], show_map[row][3], show_map[row][4], show_map[row][5],
show_map[row][6], show_map[row][7], show_map[row][8], show_map[row][9]);
printf(" ---------------------------\n");
}
}
char MineBoom(){
printf(" ");
for (int col = 1; col <= COL; col++)
{
printf(" %d ", col);
}
printf("\n ---------------------------\n");
for (int row = 1; row <= ROW; row++)
{
printf("%02d ", row);
printf(" |%c |%c |%c |%c |%c |%c |%c |%c |%c |\n", mine_map[row][1], mine_map[row][2], mine_map[row][3], mine_map[row][4], mine_map[row][5],
mine_map[row][6], mine_map[row][7], mine_map[row][8], mine_map[row][9]);
printf(" ---------------------------\n");
}
}
char MineBoom(){
printf(" ");
for (int col = 1; col <= COL; col++)
{
printf(" %d ", col);
}
printf("\n ---------------------------\n");
for (int row = 1; row <= ROW; row++)
{
printf("%02d ", row);
printf(" |%c |%c |%c |%c |%c |%c |%c |%c |%c |\n", mine_map[row][1], mine_map[row][2], mine_map[row][3], mine_map[row][4], mine_map[row][5],
mine_map[row][6], mine_map[row][7], mine_map[row][8], mine_map[row][9]);
printf(" ---------------------------\n");
}
}
char IsFull(){
int ful = COL*ROW - MINE_COUNT;
for (int row = 1; row <= ROW; row++){
for (int col = 1; col <= COL; col++){
{
if (show_map[row][col] == '1' || show_map[row][col] == '0')
{
ful--;
}
}
int ful = COL*ROW - MINE_COUNT;
for (int row = 1; row <= ROW; row++){
for (int col = 1; col <= COL; col++){
{
if (show_map[row][col] == '1' || show_map[row][col] == '0')
{
ful--;
}
}
}
}
if (ful == 0)
{
return 'p';
}
}
char PlayerMove(char mine_map[ROW + 2][COL + 2], char show_map[ROW + 2][COL + 2]){
int row = -1;
int col = -1;
while (1)
{
printf("请玩家选的位置(输入格式:坐标 坐标):");
scanf("%d %d", &row, &col);
if (row < 1 || row>ROW || col>COL || col < 1)
{
printf("输入越界,请重新输入!\n");
continue;
}
if (row > 0 && row<10 && col>0 && col < 10)
{
if (show_map[row][col] == '*')
{
//1.有雷 显示雷区,结束游戏
if (mine_map[row][col] == '1')
{
MineBoom();
return 'n';
break;
}
//3.显示此位的周围一圈 是否有雷
else if (mine_map[row][col] == '0')
{
int count = '0';
if (mine_map[row - 1][col - 1] == '1')
{
count++;
}
if (mine_map[row - 1][col ] == '1')
{
count++;
}
if (mine_map[row - 1][col + 1] == '1')
{
count++;
}
if (mine_map[row ][col - 1] == '1')
{
count++;
}
if (mine_map[row][col + 1] == '1')
{
count++;
}
if (mine_map[row + 1][col - 1] == '1')
{
count++;
}
if (mine_map[row + 1][col ] == '1')
{
count++;
}
if (mine_map[row + 1][col + 1] == '1')
{
count++;
}
{
show_map[row][col] = count;
Print();
return 'k';
}
}
else{
printf("已经选过,请重新输入!\n");
continue;
}
}
}
if (ful == 0)
{
return 'p';
}
}
char PlayerMove(char mine_map[ROW + 2][COL + 2], char show_map[ROW + 2][COL + 2]){
int row = -1;
int col = -1;
while (1)
{
printf("请玩家选的位置(输入格式:坐标 坐标):");
scanf("%d %d", &row, &col);
if (row < 1 || row>ROW || col>COL || col < 1)
{
printf("输入越界,请重新输入!\n");
continue;
}
if (row > 0 && row<10 && col>0 && col < 10)
{
if (show_map[row][col] == '*')
{
//1.有雷 显示雷区,结束游戏
if (mine_map[row][col] == '1')
{
MineBoom();
return 'n';
break;
}
//3.显示此位的周围一圈 是否有雷
else if (mine_map[row][col] == '0')
{
int count = '0';
if (mine_map[row - 1][col - 1] == '1')
{
count++;
}
if (mine_map[row - 1][col ] == '1')
{
count++;
}
if (mine_map[row - 1][col + 1] == '1')
{
count++;
}
if (mine_map[row ][col - 1] == '1')
{
count++;
}
if (mine_map[row][col + 1] == '1')
{
count++;
}
if (mine_map[row + 1][col - 1] == '1')
{
count++;
}
if (mine_map[row + 1][col ] == '1')
{
count++;
}
if (mine_map[row + 1][col + 1] == '1')
{
count++;
}
{
show_map[row][col] = count;
Print();
return 'k';
}
}
else{
printf("已经选过,请重新输入!\n");
continue;
}
}
else{
printf("输入非法,请重新输入!\n");
continue;
}
}
}
}
printf("输入非法,请重新输入!\n");
continue;
}
}
}
}
void Game(){
if (Menu() == 1)//1.选择菜单
{
Init();//2.初始化,布雷
Print(); //3.打印棋盘
while (1){
if (PlayerMove(mine_map,show_map) == 'n'){
printf("踩到雷啦,游戏结束!\n");
break;
}
else if (IsFull() == 'p'){
printf("恭喜玩家胜利!\n");
break;
}
else {
continue;
}
}
}
system("pause");
}
int main(){
Game();
return 0;
}
if (Menu() == 1)//1.选择菜单
{
Init();//2.初始化,布雷
Print(); //3.打印棋盘
while (1){
if (PlayerMove(mine_map,show_map) == 'n'){
printf("踩到雷啦,游戏结束!\n");
break;
}
else if (IsFull() == 'p'){
printf("恭喜玩家胜利!\n");
break;
}
else {
continue;
}
}
}
system("pause");
}
int main(){
Game();
return 0;
}