【C语言】扫雷游戏完整代码实现

简介: 【C语言】扫雷游戏完整代码实现

1.game.h

#define _CRT_SECURE_NO_WARNINGS
 
#include <string.h>
#include <stdio.h>
#include <time.h>
#include<stdlib.h>
 
#define ROW 9
#define COL 9
#define ROWS 11
#define COLS 11
#define EASY_MODE 9
 
 void InitBoard(char board[ROWS][COLS],  int rows,  int cols, char ret);//初始化棋盘
 void DisplayBoard(char board[ROWS][COLS], int row, int col);//打印棋盘
 void  setmine(char board[ROWS][COLS], int row, int col);//布置雷
 void SaoLei(char board[ROWS][COLS], char Double[ROWS][COLS], int row, int col);

2.game.c

#include "game.h"
 
void InitBoard(char board[ROWS][COLS], int rows, int cols, char ret)//初始化棋盘
{
  int i = 0;
  int j = 0;
  
  for (i = 0; i < rows; i++)
  {
    
    for (j = 0; j < cols; j++)
    {
      board[i][j] = ret;
    }
  }
}
 
void DisplayBoard(char board[ROWS][COLS], int row, int col)//打印棋盘
{
  printf("\n");
  printf("-------扫雷--------\n");
  int i = 0;
  int j = 0;
  
  for (j = 0; j <= col; j++)
  {
    printf("%d ", j);
  }
  printf("\n");
  for (i = 0; i < row; i++)
  {
    printf("%d ", i+1);
    for (j = 1; j <= col; j++)
    {
      printf("%c ", board[i+1][j]);
 
    }
    printf("\n");
  }
  printf("-------扫雷--------\n");
 
}
 
void  setmine(char board[ROWS][COLS], int row, int col)//布置雷
{
  int count = EASY_MODE;
  int x = 0;
  int y = 0;
  while (count)
  {
    x = rand() % row + 1;
    y = rand() % col + 1;
    if (board[x][y] == '0')
    {
      board[x][y] = '1';
      count--;
    }
  }
}
 
void SaoLei(char board[ROWS][COLS],char Double[ROWS][COLS], int row, int col)
{
  int a = 0;
  int b = 0;
  int z = 0;
  int m = 0;
  int g = row*col - EASY_MODE;
  while (g)
  {
  
    printf("请输入>:");
    scanf("%d%d", &a, &b);
    if (a > 0 && a < 10 && b > 0 && b < 10)
    {
      char t = '0';
      if (board[a][b] == '0')
      {
        for (z = -1; z <= 1; z++)
        {
          for (m = -1; m <= 1; m++)
          {
            t = t + board[a + z][b + m];
          }
        }
        t = t - '0' * 10 + '0';
        Double[a][b] = t;
        g--;
        
        DisplayBoard(Double, ROW, COL);//打印棋盘
 
        
      }
      else
      {
        printf("你被炸死了,游戏结束.");
        break;
      }
 
    }
    else
    {
      printf("输入格式有误,请重新输入.");
      
    }
  }
  printf("游戏结束了,你赢了.");
}

3.progress.c

#include "game.h"
 
void menu()
{
  printf("---------------------\n");
  printf("*******1.play********\n");
  printf("*******0.exit********\n");
  printf("---------------------\n");
}
 
void game()
{
  
  char mine[ROWS][COLS] = { 0 };
  char show[ROWS][COLS] = { 0 };
  InitBoard(mine, ROWS, COLS, '0');//初始化棋盘
  InitBoard(show, ROWS, COLS, '*');//初始化棋盘
  DisplayBoard(show, ROW, COL);//打印棋盘
  //DisplayBoard(mine, ROW, COL);//打印棋盘
  setmine(mine, ROW, COL);//布置雷
  SaoLei(mine, show, ROW, COL);
  DisplayBoard(mine, ROW, COL);//打印棋盘
}
 
int main()
{
  srand((unsigned int)time(NULL));
  int input = 0;
  menu();
  do
  {
    printf("请选择>:");
    scanf("%d", &input);
    switch (input)
    {
    case 1:
      printf("开始游戏\n");
      game();
      break;
    case 0:
      printf("退出游戏\n");
      break;
    default :
      printf("输入格式有误,请重新输入.\n");
      break;
    }
  }
  while (input);
  return 0;
}

4.运行结果


相关文章
|
29天前
|
存储 编译器 C语言
【数据结构】C语言实现链队列(附完整运行代码)
【数据结构】C语言实现链队列(附完整运行代码)
36 0
|
29天前
|
存储 算法 程序员
【数据结构】C语言实现顺序表万字详解(附完整运行代码)
【数据结构】C语言实现顺序表万字详解(附完整运行代码)
39 0
|
3天前
|
存储 算法 C语言
C语言进阶:顺序表(数据结构基础) (以通讯录项目为代码练习)
C语言进阶:顺序表(数据结构基础) (以通讯录项目为代码练习)
|
17天前
|
C语言
爱上C语言:扫雷小游戏,展开一片功能实现
爱上C语言:扫雷小游戏,展开一片功能实现
爱上C语言:扫雷小游戏,展开一片功能实现
|
25天前
费马螺线在现实生活中的应用
费马螺线在现实生活中的应用
10 1
|
25天前
|
人工智能 机器人 测试技术
【C/C++】C语言 21点桌牌游戏 (源码) 【独一无二】
【C/C++】C语言 21点桌牌游戏 (源码) 【独一无二】
|
30天前
|
编译器 定位技术 C语言
【C语言实战项目】扫雷游戏
【C语言实战项目】扫雷游戏
26 0
|
18天前
|
程序员 C语言
C语言库函数 — 内存函数(含模拟实现内存函数)
C语言库函数 — 内存函数(含模拟实现内存函数)
29 0
|
3天前
|
C语言
C语言:内存函数(memcpy memmove memset memcmp使用)
C语言:内存函数(memcpy memmove memset memcmp使用)
|
2天前
|
C语言
【C语言】字符分类函数与字符转换函数
【C语言】字符分类函数与字符转换函数
7 1