yinda_peng 发表于 2023-9-2 16:10:04

C语言扫雷【简易】

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SIZE 9
#define MINES 10

char board;
char visible;

void initializeBoard() {
      int i,j;
    for (i = 0; i < SIZE; i++) {
      for (j = 0; j < SIZE; j++) {
            board = ' ';
            visible = '-';
      }
    }
}

void printBoard() {
    printf("1 2 3 4 5 6 7 8 9\n");
    int i,j;
    for (i = 0; i < SIZE; i++) {
      printf("%d ", i + 1);
      for (j = 0; j < SIZE; j++) {
            printf("%c ", visible);
      }
      printf("\n");
    }
}

void placeMines() {
    srand(time(NULL));
    int count = 0;
    while (count < MINES) {
      int x = rand() % SIZE;
      int y = rand() % SIZE;
      if (board != '*') {
            board = '*';
            count++;
      }
    }
}

int countAdjacentMines(int x, int y) {
    int count = 0,i,j;
    for (i = -1; i <= 1; i++) {
      for (j = -1; j <= 1; j++) {
            int newX = x + i;
            int newY = y + j;
            if (newX >= 0 && newX < SIZE && newY >= 0 && newY < SIZE && board == '*') {
                count++;
            }
      }
    }
    return count;
}

void reveal(int x, int y) {
    if (x < 0 || x >= SIZE || y < 0 || y >= SIZE || visible != '-') {
      return;
    }

    visible = countAdjacentMines(x, y) + '0';

    if (visible == '0') {
            int i,j;
      for (i = -1; i <= 1; i++) {
            for (j = -1; j <= 1; j++) {
                int newX = x + i;
                int newY = y + j;
                reveal(newX, newY);
            }
      }
    }
}

int main() {
    initializeBoard();
    placeMines();
    int gameOver = 0;

    while (!gameOver) {
      printBoard();
      printf("Enter row and column (e.g., 3 4): ");
      int row, col;
      scanf("%d %d", &row, &col);

      if (row < 1 || row > SIZE || col < 1 || col > SIZE) {
            printf("Invalid input. Please try again.\n");
            continue;
      }

      row--;
      col--;

      if (board == '*') {
            printf("Game over! You hit a mine!\n");
            gameOver = 1;
      } else {
            reveal(row, col);
            int remaining = SIZE * SIZE - MINES;
            int revealed = 0;
            int i,j;
            for (i = 0; i < SIZE; i++) {
                for (j = 0; j < SIZE; j++) {
                  if (visible != '-') {
                        revealed++;
                  }
                }
            }

            if (revealed == remaining) {
                printf("Congratulations! You win!\n");
                gameOver = 1;
            }
      }
    }

    return 0;
}

花了两三天空闲时间写写,对大家来说应该就是很普通的东西,没写注释了,太像八皇后了这东西


sfqxx 发表于 2023-9-2 16:19:32

大佬{:10_254:}

编程这么好{:10_254:}

liuhongrun2022 发表于 2023-9-2 16:20:28

看看

歌者文明清理员 发表于 2023-9-2 16:36:22

zc

陈尚涵 发表于 2023-9-2 16:43:34

{:10_256:}{:10_256:}厉害

tomok 发表于 2023-9-2 16:52:28

看看代码

yinda_peng 发表于 2023-9-2 16:52:51

sfqxx 发表于 2023-9-2 16:19
大佬

编程这么好

别捧啦{:10_266:}

zhangjinxuan 发表于 2023-9-2 16:53:24

做了一个我一直都实现不了的作品,厉害{:5_106:}

这个应该是要用广度优先搜索吧,请教一下

kaneki.ken 发表于 2023-9-2 17:01:10

学习

申陌小鑫 发表于 2023-9-2 17:02:00

看看{:5_106:}

yinda_peng 发表于 2023-9-2 17:02:32

zhangjinxuan 发表于 2023-9-2 16:53
做了一个我一直都实现不了的作品,厉害

这个应该是要用广度优先搜索吧,请教一下

我是用深度做的,reveal(int x, int y) 函数就是来揭示(reveal)相邻的空白单元格。如果一个空白单元格被揭示,就继续递归把边上的空白格都翻出来,直到达到包围有地雷的边界。然后countAdjacentMines 计算周围的相邻地雷数量。

广度,应该也可以做,但是感觉代码量上会麻烦一些

yinda_peng 发表于 2023-9-2 17:03:59

zhangjinxuan 发表于 2023-9-2 16:53
做了一个我一直都实现不了的作品,厉害

这个应该是要用广度优先搜索吧,请教一下

我感觉和八皇后非常像这个东西,思路上都差不多

zhangjinxuan 发表于 2023-9-2 17:12:05

yinda_peng 发表于 2023-9-2 17:02
我是用深度做的,reveal(int x, int y) 函数就是来揭示(reveal)相邻的空白单元格。如果一个空白单元格 ...

比较习惯用广搜,毕竟深搜的话要递归。

yinda_peng 发表于 2023-9-2 17:13:11

zhangjinxuan 发表于 2023-9-2 17:12
比较习惯用广搜,毕竟深搜的话要递归。

都可以实现,个人偏好罢了

zhangjinxuan 发表于 2023-9-2 17:17:02

yinda_peng 发表于 2023-9-2 17:13
都可以实现,个人偏好罢了

{:10_256:}

Wei-Yuanzhe 发表于 2023-9-2 19:22:51

{:10_257:}

hornwong 发表于 2023-9-2 20:47:28

感谢分享!

litengyue 发表于 2023-9-3 11:33:05

怎么标记雷???

yinda_peng 发表于 2023-9-3 15:31:53

litengyue 发表于 2023-9-3 11:33
怎么标记雷???

我没做这个功能,因为只是在终端玩,没有做图形,所以说是简易了

lin折腾 发表于 2023-9-6 13:46:46

我来试试看
页: [1] 2
查看完整版本: C语言扫雷【简易】