扫雷的代码实现

本文介绍了如何实现扫雷游戏的代码逻辑,包括首次点击安全、周围无雷时的自动展开等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要求:

(1)第一次下子,不炸死。

(2)坐标周围没雷时,可以实现展开。

game.h  
#ifndef __GAME_H__  
#define __GAME_H__  
  
#include <stdio.h>  
#include <stdlib.h>  
#include <time.h>  
#define _CRT_SECURE_NO_WARNINGS  
#define ROW 9  
#define COL 9  
  
#define ROWS ROW+2  
#define COLS COL+2  
  
#define COUNT 50  
  
  
void Mine(char mine[ROWS][COLS],int row,int col);  
void Show(char show[ROWS][COLS],int row,int col);  
void Board(char board[ROWS][COLS],int rows,int cols, char set);  
void Display(char board[ROWS][COLS], int row, int col);  
void SetMine(char mine[ROWS][COLS], int row, int col);  
char GetMineCount(char mine[ROWS][COLS], int x, int y);  
void Sweep(char mine[ROWS][COLS],char show[ROWS][COLS]);  
void Rmove(char mine[ROWS][COLS],int x,int y);  
void Expand(char mine[ROWS][COLS],char show[ROWS][COLS],int x,int y);  
  
#endif//__GAME_H__  
game.c  
#include "game.h"  
void Mine(char mine[ROWS][COLS],int row,int col){  
  int i = 0;  
  int j = 0;  
  char set;  
  scanf("%c",&set);*/  
  for(i=0;i<row;i++){  
      for(j=0;j<col;j++){  
         mine[i][j]='0';  
    }  
 }  
}  
void Show(char show[ROWS][COLS],int row,int col){  
  int i = 0;  
  int j = 0;  
  for(i=0;i<row;i++){  
      for(j=0;j<col;j++){  
          show[i][j]='*';  
      }  
  }  
}  
void Board(char board[ROWS][COLS],int rows,int cols, char set){  
        int i = 0;  
        int j = 0;  
        for(i=0;i<rows;i++){  
            for(j=0;j<cols;j++){  
                board[i][j]=set;  
            }  
        }  
}  
void Display(char board[ROWS][COLS], int row, int col){  
    int i = 0;  
    int j = 0;  
    printf("  ");  
    for(i=1;i<row-1;i++){  
        printf(" %d",i);  
    }  
    printf("\n");  
    for(i=1;i<row-1;i++){  
        printf("%2d ",i);  
        for(j=1;j<col-1;j++){  
            printf("%c ",board[i][j]);  
        }  
        printf("\n");  
    }  
}  
void SetMine(char mine[ROWS][COLS], int row, int col){  
    int x = 0;  
    int y = 0;  
    int minecount = COUNT;  
    srand((unsigned)time(NULL));  
    while(minecount){  
        x = rand()%(ROW)+1;  
        y = rand()%(COL)+1;  
        if(mine[x][y] == '0'){  
            mine[x][y] = '1';  
            minecount--;  
        }  
    }  
}  
char GetMineCount(char mine[ROWS][COLS], int x, int y){  
    int minecount = 0;  
    if(mine[x-1][y] == '1')  
        minecount++;  
    if(mine[x-1][y+1] =='1')  
        minecount++;  
    if(mine[x-1][y+1] == '1')  
        minecount++;  
    if(mine[x][y+1] == '1')  
        minecount++;  
    if(mine[x+1][y+1] == '1')  
        minecount++;  
    if(mine[x+1][y] == '1')  
        minecount++;  
    if(mine[x+1][y-1] == '1')  
        minecount++;  
    if(mine[x][y-1] == '1')  
        minecount++;  
    if(mine[x-1][y-1] == '1')  
        minecount++;  
    return minecount;  
}  
void Sweep(char mine[ROWS][COLS],char show[ROWS][COLS]){  
    int x = 0;  
    int y = 0;  
    int clear = 0;    
    int minecount = 0;    
    while(clear < (ROW)*(COL)-EASY_COUNT){  
        printf("开始排雷,请输入坐标:\n");  
        scanf("%d%d",&x,&y);  
        if(mine[x][y] == '1'){  
            if(clear == 0){  
                Rmove(mine,x,y);  
                Display(mine,ROWS,COLS);  
                printf("\n");  
                minecount = GetMineCount(mine,x,y);  
                if(minecount == 0){  
                    show[x][y] = ' ';  
                    clear++;  
                    Expand(mine,show,x,y);  
                    Display(show,ROWS,COLS);  
                }  
                else{  
                    show[x][y] = minecount + '0';  
                    Display(show,ROWS,COLS);  
                }  
            }  
           else{  
                printf("很遗憾,你被炸死了!\n");  
                Display(mine,ROWS,COLS);     
                break;  
            }  
        }  
        else if(mine[x][y] != '1'){  
            minecount = GetMineCount(mine,x,y);  
            if(minecount == 0){  
                show[x][y] = ' ';  
            }  
            else{  
                show[x][y] = minecount + '0';  
            }  
            clear++;  
            Expand(mine,show,x,y);  
            Display(show,ROWS,COLS);  
        }  
        if(clear == (ROW)*(COL)-COUNT){  
             printf("扫雷成功\n");  
         }  
     }  
}  
void Rmove(char mine[ROWS][COLS],int x,int y)  
{  
        mine[x][y] = '0';  
        GetMineCount(mine,x,y);  
        while(1){  
            int x1 = rand()%(ROW)+1;  
            int y1 = rand()%(COL)+1;   
            if(mine[x1][y1] != '1' && ((x1!=x) && (y1!=y))){  
                mine[x1][y1] = '1';  
                break;  
            }  
        }  
}  
void Expand(char mine[ROWS][COLS],char show[ROWS][COLS],int x,int y){  
    if((x>=1)&&(y>=1)&&(x<=ROW)&&(y<=COL)){  
        if(GetMineCount(mine,x,y) == 0){  
            show[x][y] = ' ';   
          if(show[x-1][y-1] == '*'){  
              Expand(mine,show,x-1,y-1);  
          }  
          if(show[x-1][y] == '*'){  
              Expand(mine,show,x-1,y);  
          }  
          if(show[x-1][y+1] == '*'){  
              Expand(mine,show,x-1,y+1);  
          }  
          if(show[x][y+1] == '*'){  
              Expand(mine,show,x,y+1);  
          }  
          if(show[x+1][y+1] == '*'){  
              Expand(mine,show,x+1,y+1);  
          }  
          if(show[x+1][y] == '*'){  
              Expand(mine,show,x+1,y);  
          }  
          if(show[x+1][y-1] == '*'){  
              Expand(mine,show,x+1,y-1);  
          }  
          if(show[x][y-1] == '*') {  
              Expand(mine,show,x,y-1);  
          }  
        }  
    }  
}  
test.c  
#include "game.h"  
void menu(){  
    printf("*****************************\n");  
    printf("****   欢迎进入扫雷界面  ****\n");  
    printf("*****************************\n");  
    printf("****  1 play     0 exit  ****\n");  
    printf("*****************************\n");  
}  
void game(){  
    int x = 0;  
    int y = 0;  
    int count = 0;  
    char mine[ROWS][COLS]={0};  
    char show[ROWS][COLS]={0};  
    Board(show,ROWS,COLS,'*');  
    Display(show,ROWS,COLS);  
    printf("\n");  
    Board(mine,ROWS,COLS,'0');  
    Display(mine,ROWS,COLS);  
    SetMine(mine,ROW,COL);  
    DisplayBoard(mine,ROWS,COLS); 
    printf("\n");  
    Sweep(mine,show);  
}  
void test(){  
    int input = 0;  
    do{  
        menu();  
        printf("请选择>\n");  
        scanf("%d",&input);  
        switch(input)  
        {  
        case 1:  
            game();  
            break;  
        case 0:  
            printf("退出游戏\n");  
            break;  
        default:  
            printf("输入不正确 ,请重新选择!\n");  
            break;  
        }  
    }while(input);  
}  
int main()  
{  
    test();  
    system("pause");  
    return 0;  
}  


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值