欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

用C语言实现扫雷游戏

程序员文章站 2022-04-07 15:37:48
...

用C语言实现扫雷游戏

—扫雷游戏的基本思路
1.打印游戏的菜单
2.为了便于后面排查周围雷的个数,初始化两个二维数组,一个作为向玩家显示的面板,一个作为布雷的面板。两个数组嵌套在一起。如下图所示:用C语言实现扫雷游戏
3. 将初始化的两个数组打印出来
4. 在布雷的数组里面随机放入雷
5. 玩家扫雷
.输入要排查的区域的坐标
.如果第一次遇到雷,将雷移走,继续游戏
.如果再次遇到雷,炸死,游戏结束,并且显示布雷的面板
.如果不是雷,统计周围类的个数,并且显示在输 入的坐标所对应的区域里
.如果周围没有雷的话,所要排查的区域四周展开
.显示玩家面板
.统计没有布雷的区域的个数,用来判断游戏是否结束

现在按照设计的思路写程序来实现我们的想法
新建一个项目:扫雷.h

#ifndef _SAOLEI__H__
#define _SAOLEI__H__
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 10
#define COL 10
#define COUNT 20
//初始化数组
void init(char mine_board[ROW][COL], char player_board[ROW + 2][COL + 2], int row, int col);

//打印埋有雷的面板
void display_mine(char mine_board[ROW][COL], int row, int col);

//打印玩家看到的面板
void display_player(char player_board[ROW+2][COL+2], int row, int col);

//布雷
void put_mine(char mine_board[ROW][COL], int row, int col);

//玩家扫雷
void find_mine(char mine_board[ROW][COL], char player_board[ROW + 2][COL + 2], int row, int col);

//避免第一次遇到雷被炸死
void safe_mine(char mine_board[ROW][COL], int x, int y, int row, int col);

//统计周围类的个数并显示
int count_board(char mine_board[ROW][COL], int row, int col);


#endif // _SAOLEI__H__de
---扫雷.c

#include"扫雷.h"
void init(char mine_board[ROW][COL],char player_board[ROW+2][COL+2], int row, int col)//初始化数组
{
    int i = 0;
    int j = 0;
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            mine_board[i][j] = '0';//埋有雷的数组
        }
    }
    for (i = 0; i < row+2; i++)
    {
        for (j = 0; j < col+2; j++)
        {
            player_board[i][j] = '*';//向玩家显示的数组
        }
    }
}
void display_mine(char mine_board[ROW][COL], int row, int col)//打印埋有雷的数组
{
    int i = 0;
    int j = 0;
    for (i =0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            printf("%3c", mine_board[i][j]);
        }
        printf("\n");
    }
}
void display_player(char player_board[ROW+2][COL+2], int row, int col)//打印玩家能看到的数组面板
{
    int i = 0;
    int j = 0;
    printf("  ");
    for (i = 1; i <col+1; i++)
    {
        printf("  %d", i);//打印列号
    }
    printf("\n");
    printf("    ------------------------------\n");

    for (i = 1; i <row+1; i++)
    {
        printf("%3d", i);//打印行号
        printf("|");
        for (j =1; j <col+1; j++)
        {
            printf("%3c",player_board[i][j]);
        }
        printf(" \n");
    }
}
void put_mine(char mine_board[ROW][COL], int row, int col)//布雷
{
    srand((unsigned int)time(NULL));
    int count =COUNT;//count为总共要布的雷的个数
    while (count)
    {
        int i = rand() % row+1;
        int j = rand() % col+1;
        if (mine_board[i][j] == '0')
        {
            mine_board[i][j] = '1';
            count--;
        }
    }
}

int count_mine(char mine_board[ROW][COL], int x, int y)//统计周围雷的个数
{
    int count = 0;
    if (mine_board[x - 1][y - 1] == '1')
    {
        count++;
    }
    if (mine_board[x - 1][y ] == '1')
    {
        count++;
    }
    if (mine_board[x - 1][y +1] =='1')
    {
        count++;
    }
    if (mine_board[x][y +1] == '1')
    {
        count++;
    }
    if (mine_board[x+1][y+1] == '1')
    {
        count++;
    }
    if (mine_board[x +1][y] == '1')
    {
        count++;
    }
    if (mine_board[x+1][y - 1] == '1')
    {
        count++;
    }
    if (mine_board[x][y - 1] == '1')
    {
        count++;
    }
    return count;
}
void safe_mine(char mine_board[ROW][COL], int x, int y, int row, int col)
{
       int i = 0;
       int j = 0;
        mine_board[x][y] = '0';
        i = x;
        j = y;
        while (1)
        {
            x = rand() % row  + 1;
            y = rand() % col  + 1;
            if (x != i&&y != j&&mine_board[x][y] == '1')
            {
                ;
            }
         if (x != i&&y != j&&mine_board[x][y] != '1')
            {
                mine_board[x][y] = '1';
                break;
            }

        }
}

void find_mine(char mine_board[ROW][COL], char player_board[ROW + 2][COL + 2], int row, int col)//扫雷
{

    int x = 0;
    int y = 0;
    int choice = 0;
    int count = 0;//count为排查过的区域的个数
    printf("请输入要排查的区域的坐标:\n");
    scanf("%d%d", &x, &y);
    if ((1 <= x&&x <= row) && (1 <= y&&y <= col))
    {
        while (1)//如果第一次踩到雷游戏不结束
        {
            if (mine_board[x][y] == '1')
            {
                safe_mine(mine_board, x, y, ROW, COL);
                goto flag;
            }
            else
            {
                break;
            }
        }

    }
flag:while (1)
{
         printf("请输入要排查的区域的坐标:\n");
         scanf("%d%d", &x, &y);
         if ((1 <= x&&x <= row) && (1 <= y&&y <= col))
         {
             if (mine_board[x][y] == '1')
             {
                 printf("你踩到雷了!游戏结束!\n");
                 display_mine(mine_board, ROW, COL);
             }
             else
             {
                 int ret = count_mine(mine_board, x, y);
                 player_board[x][y] = ret + '0';
                 if (ret == 0)//如果周围没有雷的话展开
                 {
                     player_board[x - 1][y - 1] = player_board[x - 1][y] = player_board[x - 1][y + 1] = player_board[x][y + 1] = player_board[x + 1][y + 1] = player_board[x + 1][y] = player_board[x + 1][y - 1] = player_board[x][y - 1] = ' ';
                 }
                 display_player(player_board, ROW, COL);
                 count++;
             }

             if (count == ROW*COL - COUNT)//判断游戏是否结束
             {
                 printf("恭喜你,扫雷成功!\n");
                 display_player(player_board, ROW, COL);//打印玩家可以看到的显示面板
                 break;
             }
         }
             else
                 printf("输入错误请重新输入!\n"); 
   }
}
---test.c

#include"扫雷.h"
void menu()
{
    printf("######################\n");
    printf("####### 1.play #######\n");
    printf("####### 0.exit #######\n");
    printf("######################\n");
}
void game()
{
    int ret = 0;
    char mine_board[ROW][COL] = { 0 };
    char player_board[ROW + 2][COL + 2] = { 0 };
    init(mine_board, player_board, ROW, COL);//初始化数组
    display_player(player_board, ROW, COL);//打印玩家可以看到的显示面板
    put_mine(mine_board, ROW, COL);//布雷
    find_mine(mine_board, player_board, ROW, COL);//扫雷
}
int main()
{
    menu();
    printf("请选择:>\n");
    int choose = 0;
    scanf("%d", &choose);
    switch (choose)
    {
    case 1:
        game();
        break;
    case 0:
        printf("退出游戏\n");
        break;
    default:
        printf("选择错误请重新输入!\n");
        break;
    }while (choose);
    system("pause");
    return 0;
}

用C语言实现扫雷游戏
—简单的扫雷游戏就实现了!

相关标签: c语言 扫雷