扫雷

game.h


#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 a[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__ 

game.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void Initialize(char a[ROWS][COLS], int rows, int cols, char b)
{
    int i = 0;
    int j = 0;
    for(i = 0; i < rows; i++)
    {
        for(j = 0; j < cols; j++)
        {
            a[i][j] = b;
        }
    }
}
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';

                while (1)
                {
                    int x = rand() % ROW + 1;
                    int y = rand() % COL + 1;
                    if (a[x][y] == '0')
                    {
                        a[x][y] = '1';                    
                        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;
}

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值