C语言实现扫雷

 

test.c

 

 

#include<stdio.h> 
#include<stdlib.h> 
#include"game.h" 
void menu() 

    printf("***************************\n"); 
    printf("********  1.piay  *********\n"); 
    printf("********  0.exit  *********\n"); 
    printf("请选择:"); 

void game() 

     
    char mine[ROWS][COLS]; 
    char show[ROWS][COLS]; 
    int x,y; 
    int icount=0;          
    srand((unsigned int)time(NULL));  
    memset(mine,'0',ROWS*COLS*sizeof(mine[0][0])); 
    memset(show,'*',ROWS*COLS*sizeof(show[0][0])); 
    set_mine(mine); 
    display(mine,ROWS,COLS); 
    display(show,ROWS,COLS);                                  
   while(1)                                        
   { 
   printf("请输入坐标:");                  
   scanf("%d%d",&x,&y); 
     if(((x>=1)&&(x<=ROW))&&((y>=1)&&(y<=COL)))//chongfushurukongbai 
      {   icount++; 
          if(icount==1)                            
         { 
             if(mine[x][y]==1) 
             { 
             finput(mine,x,y);  
             } 
             show[x][y]=mine_count(mine,x,y)+'0';  
             search_road(mine,show,x,y); 
             display(show,ROWS,COLS); 
         } 
          else 
          {                                  
              if(mine[x][y]=='1')                      
              { 
               printf("很遗憾,你输了!!!\n");         
               display(mine,ROWS,COLS); 
               break; 
              } 
              else 
              { 
                  show[x][y]=mine_count(mine,x,y)+'0'; 
                  search_road(mine,show,x,y); 
                  display(show,ROWS,COLS); 
              } 
           }     
   } 
 
   else 
       printf("请重新输入\n"); 
  if((ROW*COL-MAX) == check_win(show)) 
  { 
      printf("恭喜你赢了\n"); 
          break; 
  } 
   } 

enum
   { 
    EXIT, 
    PLAY 
   }; 
int main() 

 
     int input; 
    do{ 
        menu(); 
        scanf("%d",&input); 
        switch(input) 
        { 
            case PLAY:      game();
    break; 
            case EXIT:     
    break; 
            default:printf("请重新选择");
    break; 
        } 
    }while(input!=0); 
        system("pause"); 
        return 0; 

 

 

 

 

 

game.h

#define COLS 11 
#define ROWS 11 
#define COL (COLS-2) 
#define ROW (ROWS-2) 
#define MAX 14 
 
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
#include <string.h> 
 
void init_board(char mine[ROWS][COLS],char set, int row, int col); 
void set_mine  (char mine[ROWS][COLS]); 
void display   (char mine[ROWS][COLS], int row, int col); 
void finput    (char mine[ROWS][COLS], int x, int y);            //first input 
void search_road(char mine[ROWS][COLS],char show[ROWS][COLS],int x, int y); 
int mine_count (char mine[ROWS][COLS], int x, int y); 
int check_win(char show[ROWS][COLS]);

 

 

 

game.c

#include"game.h" 
void  init_board(char mine[ROWS][COLS],char set, int row, int col)//chushihua 

  memset(mine,'0',row*col*sizeof(mine[0][0])); 

void set_mine(char mine[ROWS][COLS]) 

    int x,y; 
    int count=0; 
    do{ 
        x=rand()%9+1; 
        y=rand()%9+1; 
        if(mine[x][y]!='1') 
           { 
               mine[x][y]='1'; 
               count++; 
           } 
 
    }while(count<MAX); 
 

void display(char board[ROWS][COLS], int row, int col) 

    int i = 0; 
    int j = 0; 
    printf("    ");             
    for(i=1; i<=ROW; i++) 
    { 
        printf("%d ", i); 
    } 
    printf("\n"); 
    for(i=1; i<=ROW ; i++) 
    { 
        printf("---"); 
    } 
    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 finput(char mine[ROWS][COLS], int x, int y) 

    int a,b; 
    if(mine[x][y]=='1')
           { 
               mine[x][y]='0';   
             while(1) 
             { 
               a=rand()%9+1; 
               b=rand()%9+1; 
                 if( mine[a][b]!='1') 
                    { 
                    mine[a][b]='1';   
                    break ; 
                    } 
             } 
           } 

void search_road(char mine[ROWS][COLS],char show[ROWS][COLS],int x, int y) 
{   
    if(0 == mine_count(mine,x,y)) 
    {    
     show[x][y]=' '; 
    if((x-1)>0&&(y-1)>0&&show[x-1][y-1]=='*')                
        search_road (mine,show,x-1,y-1); 
         
    if((y-1)>0&&show[x][y-1]=='*')                          
        search_road (mine,show,x-1,y-1); 
                             
    if((x+1)<=ROW &&(y-1)>0&&show[x+1][y-1]=='*')             
     search_road (mine,show,x+1,y-1); 
 
    if((x+1)<=ROW&&show[x+1][y]=='*')                         
        search_road (mine,show,x+1,y); 
                          
    if((x+1)<=ROW&&(y+1)<=COL&&show[x+1][y+1]=='*')           
        search_road (mine,show,x+1,y+1); 
 
    if((y+1)<=COL&&show[x][y+1]=='*')                         
        search_road (mine,show,x,y+1); 
                  
    if((x-1)>0&&(y+1)<=COL&&show[x-1][y+1]=='*')             
        search_road (mine,show,x-1,y+1); 
                     
    if((x-1)>0&&show[x-1][y]=='*')                           
        search_road (mine,show,x-1,y); 
                       
    } 
    else  
        show[x][y]=mine_count(mine,x,y)+'0'; 

int mine_count(char mine[ROWS][COLS], int x, int y) 

       int rcount;    
       rcount=mine[x-1][y-1]+mine[x][y-1]+mine[x+1][y-1]   
             +mine[x+1][y+1]+mine[x][y+1]+mine[x-1][y+1] 
             +mine[x+1][y]+mine[x-1][y]-8*48;                          
            return rcount; 
    
  

int check_win(char show[ROWS][COLS]) 

  int i,j,c_count=0; 
  for(i=1;i<=ROW;i++) 
  { 
    for(j=1;j<=COL;j++) 
    { 
      if(show[i][j]!='*') 
          c_count++; 
    } 
   
  } 
  return c_count; 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值