c语言简单实现扫雷游戏

对于扫雷游戏,我相信每个人都玩过,大概了解它应有的操作和大概的原理。那我们应该怎样着手去实现扫雷游戏呢……

  1. 首先对于雷阵这个界面,就想到可以定义的一个二维数组,我们不可能把把雷的位置也显示出来,所有需要定义2个二维数组:mine用来布雷,show用来显示扫雷情况,并对其初始化,mine开始初始化为0,show初始化为’*’;
  2. 接下来就是主要逻辑功能的实现,宏定义雷的个数DEFAULT,利用循环给mine中补雷为1。然后就开始扫雷,输入你要扫的位置,如果有雷,提示你踩雷了,本轮游戏结束,如果没踩到雷,玩家继续扫,直到扫完所有没有雷的地方。另外,每扫一次,没有雷的话,该位置会显示其周围是否有雷,有几个雷。因为统计的四周,位置在边缘位置,就是另外一种情况不好实现,所以把二维数组的行和列都加2,雷阵从数组的第2行第2列开始,到倒数第二行倒数第二列,这样无论是否在不在边缘,都可以当作一种情况来实现统计的雷的个数。

声明部分

//mine.h
#define _CRT_SECURE_NO_WARNINGS 1
#ifndef __MINE_H__
#define __MINE_H__
#define ROWS 11
#define CLOS 11
#define DEFAULT 10
enum OP
{
    EXIT,
    PLAY
};
void play_game();
//玩游戏
void init_mine(char mine[ROWS][CLOS], char show[ROWS][CLOS]);
//初始化两个数组
void set_mine(char mine[ROWS][CLOS]);
//布雷
void mine_sweep(char mine[ROWS][CLOS], char show[ROWS][CLOS]);
//扫雷
int get_mine(char mine[ROWS][CLOS], int x, int y);
//统计该位置周围的雷的个数
#endif

实现部分

//mine.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "mine.h"
#include <stdio.h>
#include <stdlib.h>
void init_mine(char mine[ROWS][CLOS], char show[ROWS][CLOS])
{
    int i = 0;
    int j = 0;
    for (i = 1; i < ROWS-1; i++)
    {
        for (j = 1; j < CLOS-1; j++)
        {
            mine[i][j] = '0';
        }
    }
    for (i = 1; i < ROWS-1; i++)
    {
        for (j = 1; j < CLOS-1; j++)
        {
            show[i][j] = '*';
        }
    }
}
void display(char show[ROWS][CLOS])
{
    int i = 0;
    int j = 0;
    printf("  ");
    for (i = 1; i < CLOS - 1; i++)
    {
        printf("%d ", i);
    }
    printf("\n");
    for (i = 1; i < ROWS - 1; i++)
    {
        printf("%d ", i);
        for (j = 1; j < CLOS-1; j++)
        {
            printf("%c ", show[i][j]);
        }
        printf("\n");
    }
}
void set_mine(char mine[ROWS][CLOS])
{
    int i = 0;
    int x = 0;
    int y = 0;
    int count = DEFAULT;
    while(count)
    {
        x = rand() % 9 + 1;//产生1-9的随机数
        y = rand() % 9 + 1;
        if (mine[x][y] == '0')
        {
            mine[x][y] = '1';
            count--;
        }
    }
}
int get_mine(char mine[ROWS][CLOS], int x, int y)
{
    int count = 0;
    if (mine[x - 1][y - 1] == '1')
        count++;
    if (mine[x][y-1] == '1')
        count++;
    if (mine[x+1][y-1] == '1')
        count++;
    if (mine[x - 1][y] == '1')
        count++;
    if (mine[x+1][y] == '1')
        count++;
    if (mine[x-1][y+1] == '1')
        count++;
    if (mine[x][y+1] == '1')
        count++;
    if (mine[x+1][y + 1] == '1')
        count++;
    return count;
}
void mine_sweep(char mine[ROWS][CLOS], char show[ROWS][CLOS])
{
    int x = 0;
    int y = 0;
    int count = 0;
    while (count != (ROWS-2)*(CLOS-2) - DEFAULT)
    {
        printf("请输入位置>");
        scanf("%d%d", &x, &y);
        if (mine[x][y] == '1')
        {
            printf("踩雷了!\n");
            return;
        }
        else
        {
            count++;
            int ret = get_mine(mine, x, y);
            show[x][y] = ret + '0';
            display(show);
        }
    }
    printf("扫雷成功\n");
}

void play_game()
{
    char mine[ROWS][CLOS];
    char show[ROWS][CLOS];
    init_mine(mine, show);
    display(show);
    set_mine(mine);
    //display(mine);
    mine_sweep(mine, show);
}

测试部分

//test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "mine.h"
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <time.h>
void menu()
{
    printf("***************************\n");
    printf("***********1.play**********\n");
    printf("***********0.exit**********\n");
    printf("***************************\n");
}
void game()
{
    int input = 1;
    srand((unsigned)time(NULL));
    do
    {
        menu();
        printf("请选择>");
        scanf("%d", &input);
        switch (input)
        {
          case PLAY:
              play_game();
              break;
          case EXIT:
              break;
        }

    } while (input);

}
int main()
{
    game();
    system("pause");
    return 0;

}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下面是一个简单扫雷游戏的 C 语言代码实现: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 #define EASY_COUNT 10 void InitBoard(char board[ROWS][COLS], int rows, int cols, char set); void DisplayBoard(char board[ROWS][COLS], int row, int col); void SetMine(char board[ROWS][COLS], int row, int col); int GetMineCount(char board[ROWS][COLS], int x, int y); int main() { char board[ROWS][COLS] = {0}; int win = 0; int x, y; int count = 0; // 用来记录玩家已经排除了多少非雷格子 InitBoard(board, ROWS, COLS, '*'); SetMine(board, ROW, COL); DisplayBoard(board, ROW, COL); while (1) { printf("请输入排除非雷格子的坐标(x,y): "); scanf("%d,%d", &x, &y); if (x < 1 || x > ROW || y < 1 || y > COL) { printf("坐标非法,请重新输入!\n"); continue; } if (board[x][y] != '*') { printf("该位置已经排除,请重新输入!\n"); continue; } if (board[x][y] == '1' || board[x][y] == '2' || board[x][y] == '3' || board[x][y] == '4' || board[x][y] == '5' || board[x][y] == '6' || board[x][y] == '7' || board[x][y] == '8') { printf("该位置周围已经有 %c 个雷,请重新输入!\n", board[x][y]); continue; } if (board[x][y] == '#') { printf("你踩到雷了,游戏结束!\n"); break; } int mine_count = GetMineCount(board, x, y); count++; if (count == ROW * COL - EASY_COUNT) { printf("你赢了,游戏结束!\n"); break; } board[x][y] = mine_count + '0'; DisplayBoard(board, ROW, COL); } return 0; } void InitBoard(char board[ROWS][COLS], int rows, int cols, char set) { int i, j; 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, j; for (i = 1; i <= row; i++) { for (j = 1; j <= col; j++) { printf("%c ", board[i][j]); } printf("\n"); } } void SetMine(char board[ROWS][COLS], int row, int col) { int count = EASY_COUNT; while (count) { int x = rand() % row + 1; int y = rand() % col + 1; if (board[x][y] == '*') { board[x][y] = '#'; count--; } } } int GetMineCount(char board[ROWS][COLS], int x, int y) { return board[x - 1][y - 1] + board[x - 1][y] + board[x - 1][y + 1] + board[x][y - 1] + board[x][y + 1] + board[x + 1][y - 1] + board[x + 1][y] + board[x + 1][y + 1] - 8 * '*'; } ``` 该代码实现扫雷游戏是一个简单的版本,只支持输入坐标进行排雷操作,不支持扫雷标记、双击打开周围格子等高级功能。如果需要实现这些高级功能,可以在该代码的基础上进行扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值