C语言写扫雷

用C语言单纯写扫雷不是很难,但是要想第一次输入坐标不被炸死,并且当周围没有雷时自动展开,还是有点难度的:
一共需要九个函数来完成这个代码,分别是:初始化数组,选择难度,打印扫雷棋盘,确定雷的个数,布雷,扫雷,确保第一次不被炸死,没有雷时可以展开。

代码如下:
test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

int  game()
{
    int ret = 0;
    char arr[ROWS][COLS] = {0};
    char show[ROWS][COLS] = {0};
    int i = 0;
    int j = 0;
    int n = 0;
    int s = 0;
    int q = 0;
   Initialize( arr, ROWS, COLS, '0');
   printf("请选择难度:> 1  2  3");
   printf("\n");
   scanf("%d",&n);
   s = select_level(n);
   set_mine( arr,ROW, COL,s) ; 
   Initialize( show, ROWS, COLS, '*');
   display( show, ROW, COL);
   safe_mine( arr,show,ROW,COL);
    display( show, ROW, COL);
    while(1)
    { 
        printf("请输入坐标\n");
        scanf("%d %d",&i,&j);
        Sweep(arr, show, ROW, COL, i, j);
        q = count_show(show, ROW, COL);
        if(s == q )
        {  
            printf("恭喜你,你赢了\n");
            display( show, ROW, COL);
             break;
        }
        if( Sweep(arr, show, ROW, COL, i, j) == 0 )
        {
            printf("你被炸死了\n");
            display( show, ROW, COL);
            printf("布雷图\n");
            display( arr, ROW, COL); 
              break;
        }    
        display( show, ROW, COL);

    }
return 0;
 void menu()
{
    printf("*******************************************\n");
    printf("********       1、play           **********\n");
    printf("********       0、exit           **********\n");
    printf("*******************************************\n");
}
void test()
{
    int i = 0;
    do{
     menu();
     printf ("请选择:");
     scanf("%d", &i);
     switch(i)
      {
     case 1:
       game();
       break;
     case 0:
       printf("退出游戏\n");
       break;
     default:
         printf("输入有误,请重新输入\n");
             break;
     }
    }while(i);
}

int main()
{
    srand((unsigned)time(NULL)); 
    test();
    return 0;
}
game.c:


#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void Initialize(char a[ROWS][COLS], int rows, int cols, char c)
{
    int i = 0;
    int j = 0;
    for(i = 0; i < rows; i++)
    {
        for(j = 0; j < cols; j++)
        {
            a[i][j] = c;
        }
    }
}
int select_level(int n)
{
    int count = 0;
    switch (n){
    case 1:
        count = 10;
        break;
    case 2:
        count = 20;
        break;
    case 3:
        count = 30;
        break;
    default:
        printf("你输入的有误,请重新输入!\n\n");
        break;
    }
     return count;
}
void display(char a[ROWS][COLS], int row, int col)   

    int i = 0; 
    int j = 0; 
    printf(" ");
    for (i = 1; i <=col; i++) 
    { 
        printf(" %d ", i); 
    } 
    printf("\n"); 
    for (i = 1; i <= row; i++) 
    { 
        printf("%d", i); 
        for (j = 1; j <= col; j++) 
        { 
            printf(" %c ", a[i][j]); 
        } 
        printf("\n"); 
    } 
void set_mine(char a[ROWS][COLS],int  row,int col,int n)
{
    int i = 0;
    int j = 0;
    int count = n;
    while(count)
    {
        i = rand()%ROW + 1;
        j = rand()%COL + 1;
        if(a[i][j] == '0'&&i > 0&&i <= row&&j > 0&&j <= row)
        {
            a[i][j] = '1';
            count--;
        }
    }
}
int get_num(char a[ROWS][COLS], int row, int col) 

    int count = 0; 
    if (a[row - 1][col - 1] == '1')
    { 
        count++; 
    } 
    if (a[row - 1][col] == '1') 
    { 
        count++; 
    } 
    if (a[row - 1][col + 1] == '1') 
    { 
        count++; 
    } 
    if (a[row][col - 1] == '1')
    { 
        count++; 
    } 
    if (a[row][col + 1] == '1') 
    { 
        count++; 
    } 
    if (a[row + 1][col - 1] == '1') 
    { 
        count++; 
    } 
    if (a[row + 1][col] == '1') 
    { 
        count++; 
    } 
    if (a[row + 1][col + 1] == '1')
    { 
        count++; 
    } 
    return  count; 
}
int Sweep(char a[ROWS][COLS], char show[ROWS][COLS],int row,int col,int x, int y) 
        int ret = 0;
        if(x > 0&&x <= row&&y > 0&&y <= row)
        {  
        if(a[x][y]=='1')
     {  
         show[x][y] = 'X';
           return 0;
        }
        else
        {
           ret = get_num(a,x,y);
           show[x][y] = ret + '0';
           check_round(a,show, x, y);
        }
        }
    return 1; 
}
void safe_mine(char a[ROWS][COLS],char show[ROWS][COLS],int row, int col)
{
    int x = 0;
    int y = 0;
    char ch = 0;
    int count = 0;
    int ret = 1;
    printf("输入坐标扫雷\n");
    while (1)
    {
        scanf("%d%d", &x, &y);
        if ((x >= 1 && x <= row) && (y >= 1 && y <= col))
        {
            if (a[x][y] == '1')
            {
                a[x][y] = '0';
                ch = get_num(a,x,y);
                show[x][y] = ch + '0';
                 check_round(a,show, x, y);
                while (ret)
                {
                    int x = rand() % ROW + 1;
                    int y = rand() % COL + 1;
                    if (a[x][y] == '0')
                    {
                        a[x][y] = '1';
                        ret--;
                        break;
                    }
                }break;
            }
            if (a[x][y] == '0')
            {
               ch = get_num(a,x,y);
                show[x][y] = ch + '0';
                check_round(a,show, x, y);
                break;
            }
        }
        else
        {
            printf("输入错误重新输入\n");
        }
    }
}
void check_round(char a[ROWS][COLS],char show[ROWS][COLS],int x,int y)

    int ret; 
    ret=get_num(a,x,y); 
    if (ret==0) 
    { 
        show[x][y]='0'; 
        if ((x-1)>0&&(y-1)>0&&(show[x-1][y-1]=='*')) 
            check_round(a,show,x-1,y-1); 
        if ((x-1)>0&&(y)>0&&(show[x-1][y]=='*')) 
            check_round(a,show,x-1,y); 
        if ((x-1)>0&&(y+1)>0&&(show[x-1][y+1]=='*')) 
            check_round(a,show,x-1,y+1); 
        if ((x)>0&&(y-1)>0&&(show[x][y-1]=='*')) 
            check_round(a,show,x,y-1); 
        if ((x)>0&&(y+1)>0&&(show[x][y+1]=='*')) 
            check_round(a,show,x,y+1); 
        if ((x+1)>0&&(y-1)>0&&(show[x+1][y-1]=='*')) 
            check_round(a,show,x+1,y-1); 
        if ((x+1)>0&&(y)>0&&(show[x+1][y]=='*')) 
            check_round(a,show,x+1,y); 
        if ((x+1)>0&&(y+1)>0&&(show[x+1][y+1]=='*')) 
            check_round(a,show,x+1,y+1); 
    }else 
        show[x][y]=ret+'0'; 

int count_show(char show[ROWS][COLS],int row, int col)
{
    int count = 0;
    int i = 0;
    int j = 0;
    for (i = 1; i <= row; i++)
    {
        for (j = 1; j <= col; j++)
        {
            if (show[i][j] == '*')
            {
                count++;
            }
        }

    }
    return count;
}
game.h:

#define _CRT_SECURE_NO_WARNINGS 1
#ifndef __GAME_H__
#define __GAME_H__
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define ROWS 11
#define COLS 11
#define ROW   9
#define COL   9
void Initialize(char a[ROWS][COLS], int rows, int cols, char b);
void display(char a[ROWS][COLS], int row, int col);
int select_level(int n);
void set_mine(char a[ROWS][COLS],int row,int col,int n);
int get_num(char a[ROWS][COLS], int row, int col); 
int Sweep(char a[ROWS][COLS], char show[ROWS][COLS],int row, int col,int x,int y);
void check_round(char mine[ROWS][COLS],char show[ROWS][COLS],int x,int y);
void safe_mine(char a[ROWS][COLS],char show[ROWS][COLS],int row, int col);
int count_show(char show[ROWS][COLS],int row, int col);
 #endif //__GAME_H__

猜你喜欢

转载自blog.csdn.net/R_T_P_A_D/article/details/79859663