扫雷的简单实现

      //  test.h
#ifndef __GAME_H__
#define __GAME_H__
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define DEFAULT_COUNT 10
#define ROW 10
#define COL 10
#define ROWS (ROW+2)
#define COLS (COL+2)
void InitBoard(char board[ROWS][COLS],int row,int col,char set);
void Set_mine(char board[ROWS][COLS]);
void Display(char board[ROWS][COLS],int row,int col);
int Get_Mine(char board[ROWS][COLS],int x,int y);
void First(char board[ROWS][COLS],int x,int y);

#endif//__GAME_H__

      // game.c

#include"test.h"
void InitBoard(char board[ROWS][COLS],int row,int col,char set)//初始化
{
 int i = 0;
 int j = 0;
 for(i = 0;i < row;i++)
 {
     for(j = 0;j < col;j++)
{
   board[i][j] = set;
}
 }
}


int get_rand_num()//随机数函数
{
    return rand()%ROW+1;
}
void Set_mine(char board[ROWS][COLS])//布雷
{
   int count = DEFAULT_COUNT;
   while(count)
   {
     int x = get_rand_num();
     int y = get_rand_num();
if(board[x][y] == '0')
{
   board[x][y] = '1';
count--;
}
   }
}
void Display(char board[ROWS][COLS],int row,int col)//打印雷盘
{
int k=0;
int i=0,j=0;printf(" ");
for(k=1;k<row-1;k++)
{   
printf(" %2d ",k);
}
printf("\n");
for(i=1 ;i<row-1; i++)
{   
printf("  ————————————————————\n");
printf("%d ",i);
for(j=1 ;j<col-1; j++)
{   printf("|");
printf(" %c ",board[i][j]);
}

printf("|");
printf("\n");

}
   printf("  ————————————————————\n");
}


int Get_Mine(char board[ROWS][COLS],int x,int y)//得到周围雷数
{
   return (board[x-1][y]
  +board[x-1][y-1]
  +board[x-1][y+1]
  +board[x][y-1]
  +board[x][y+1]
  +board[x+1][y]
  +board[x+1][y-1]
  +board[x+1][y+1] - 8*'0');
  }
void First(char board[ROWS][COLS],int x,int y)//第一步判断为雷时,雷的位置移位
{
if(board[x][y]=='1')
{
board[x][y]='0';

while(1)
{
int x = get_rand_num();
        int y = get_rand_num();
if(board[x][y]=='0')
{
board[x][y]='1';
break;
}
}
}

           // test.c
#include<stdio.h>
#include<windows.h>
#include"test.h"
void menu()
{
   printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
   printf("$$$$$         1.play         $$$$$\n");
   printf("$$$$$         2.exit         $$$$$\n");
   printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
}
void game()
{
char mine[ROWS][COLS] = {0};
char show[ROWS][COLS] = {0};
int x = 0;
int y = 0;
int win = 0;
int step = 0;
    srand((unsigned int)time(NULL));
InitBoard(mine,ROWS,COLS,'0');
InitBoard(show,ROWS,COLS,'*');


    Set_mine(mine);
    Display(mine,ROWS,COLS);
printf("\n");
    Display(show,ROWS,COLS);
while(win<(ROW*COL-DEFAULT_COUNT))
{
 printf("请输入坐标:>");
 scanf("%d%d",&x,&y);
 if((x>=1)&&(x<=ROW)&&(y>=1)&&(y<=COL))
 {
 if((step == 0)&&(mine[x][y] == '1') == 1)//判断第一步是否为雷
 First(mine,x,y);
 if(mine[x][y] == '1')
  {
    printf("oh,you dead!\n");
return;
  }
 else
 {
if(show[x][y] == '*')
win++;//*的个数
show[x][y] =  Get_Mine(mine,x,y)+'0';
         Display(show,ROWS,COLS);
  }
 step++;
 }
 else
  {
    printf("输入下标错误,请重新输入!\n");
  }
}
      printf("beautiful,you win\n");
}
int main()
{
int input = 0;
do
{
menu();
printf("请选择:>");
scanf("%d",&input);
switch(input)
{
case 1:
game();
break;
case 2:
exit(1);
break;
default:
printf("选择错误,请重新选择:\n");
break;
}
}while(input);
system("pause");
   return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值