C#教程
主页 > 软件编程 > C#教程 >

c++实现扫雷小游戏的代码

2022-02-22 | 秩名 | 点击:

分成两个源文件和一个头文件

注意:这串代码并不完整,不能够实现当所查坐标周围雷的数量为0时,直接展开周围坐标;

头文件:game.h

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

#include <stdio.h>

 

 

#define count 10 //雷的数量

 

//定义 行-ROW,列-COL

#define ROW 9

#define COL 9

#define ROWS ROW+2 //多加一些,方便代码

#define COLS COL+2

 

//初始化棋盘,声明的函数均在game.c中实现

void InitBoard(char board[ROWS][COLS],int rows,int cols);

 

//打印棋盘

void DisplayBoard(char board[ROW][COL],int row.int col);

 

//布置雷

void SetMine(char board[ROW][COLS],int row,int col);

 

//找雷

void FindMine(char mine[][COLS],char show[][COLS],int row,int col);

第一个源文件:saolei.c

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

#include "game.h"  //需要包含和声明的东西在game.h中完成

 

void menu()

  printf("                  \n");

  printf("       1.play     \n");

  printf("       0.exit     \n");

  printf("                  \n");

}

 

void game()

  // ROW and COL 在game.h中定义

  char mine[ROWS][COLS];  // 地雷埋藏的棋盘

  char show[ROWS][COLS];  // 展示出的棋盘

  //初始化棋盘

  //game()中的函数在game.h中声明,在game.c中实现

  InitBoard(mine,ROWS,COLS,'0');   //0代表无雷,1代表有雷

  InitBOard(show,ROWS,COLS,'*');

  //打印棋盘

  DisplayBoard(show,ROW,COL);

  //布置雷

  SetMine(mine,ROW,COL);

  //找雷

  FindMine(mine,show,ROW,COL);

   

}

 

 

int main()

{

  srand((unsigned int) time(NULL));//生成随机数

  int input = 0;

  do              //do-while循环

  {

    menu();    //(1--play   0--exit)也是do-while循环的条件

    printf("请选择:");

    scanf("%d",&input);

    switch(input)

    { 

    case 1:

        printf("开始游戏\n");

        game();

        break;

     case 0:

        printf("退出游戏");

        break;

     default:   

        printf("选择错误,请重新输入");

        break;

    }

  }while(input)

   

  return 0;

}

第二个源文件:game.c

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

#include "game.h"

 

void InitBoard(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 DisplayBoard(char board[ROWS][COLS],int row,int col)

{

  int i = 0;

  int j = 0;

  for(i = 0;i <= row;i++)

  {

    printf("%d",i);

  }

  printf("\n");

  for(i = 1;i <= row;i++)

  {

    printf("%d"i)

    for(j = 1;j <= col;j++)

    {

      printf(" %c ",board[i][j]);

    }

    printf("\n");

  }

   

}

 

void SetMine(char board[ROW][COL],int row,int col)

{

  int x = 0;

  int y = 0;

  while(count) //count-雷的数量

  {

    x = rand()%row + 1;//因为在1-row中布置雷,边缘一排不用,最后为了记录

    y = rand()%col + 1;//周围雷的数量方便,同时代码方便

    if( board[x][y] == '0')

    {

      board[x][y] = '1';

      count--;

    }

  }

}

 

//算出周围雷的个数

int get_mine_count(mine[ROWS][COLS],int row,int col)

{

  int i = 0;

  int j = 0;

  int mine_count = 0;

  for( i = -1;i <= 1; i++ )

  {

    for( j = -1;j <= 1; j++ )

    {

      if( mine[i][j] == '1')

      {

        mine_count++;

      }

    }

  }

  return mine_count;

}

 

void FindMine(char mine[][COLS],char show[][COLS],int row,int col)

{

  int x = 0;

  int y = 0;

  int ret = 0;//已经查找过的位置的数量

  while(ret < row * col - count)//当还剩下count的数量时,赢得游戏

  { 

    printf("请输入查找位置下标:");

    scanf("%d%d",&x,&y);

    if(x >= 1 && x <= row && y >= 1 && y <= col)

    {

      if(mine[x][y] == '0')

      {

        //当所查坐标的位置不是雷时,将该坐标展示成周围雷的个数

        int c = get_mine_count(mine,ROW,COL);

        mine[x][y] = c + '0';//返回值为int型,数组为char型,因此+'0'

        ret++;

        DisplayBoard(show,ROW,COL);

      }

      else

      {

        printf("YOU LOSE\n");

        break;

      }

    }

    else

    {

      printf("输入非法,请重新输入\n");

    }

  }

  printf("VICTORY\n");  //获得胜利

}

原文链接:https://blog.51cto.com/u_15500997/5026371
相关文章
最新更新