扫雷(C语言版本)附赠py java

JAVA版扫雷   Python版扫雷  C++见正文

扫雷这款益智游戏,最早由微软旗下的WINDOWS推出,我们使用python还原了他的图像,现在使用JAVA和C语言还原了他的文字版,大家可以为他加上贴图,增加更多功能,给大家展示的只是最基础的,金大家继续深挖捣鼓

原代码如下(c):

#include <stdio.h>

#include <stdlib.h>

#include <stdbool.h>

#include <time.h>

 

#define SIZE 5 // 游戏棋盘大小

#define MINES 5 // 地雷数量

 

char board[SIZE][SIZE]; // 棋盘格子显示

bool mines[SIZE][SIZE]; // 是否为地雷

bool revealed[SIZE][SIZE]; // 是否已揭开

int remainingCells; // 剩余未揭开格子数量

 

// 初始化游戏

void initializeGame() {

    for (int i = 0; i < SIZE; i++) {

        for (int j = 0; j < SIZE; j++) {

            board[i][j] = '-';

            mines[i][j] = false;

            revealed[i][j] = false;

        }

    }

    remainingCells = SIZE * SIZE - MINES;

}

 

// 随机放置地雷

void placeMines() {

    srand(time(NULL));

    int minesToPlace = MINES;

    while (minesToPlace > 0) {

        int row = rand() % SIZE;

        int col = rand() % SIZE;

        if (!mines[row][col]) {

            mines[row][col] = true;

            minesToPlace--;

        }

    }

}

 

// 计算每个非地雷格子周围的地雷数量

void calculateNumbers() {

    for (int i = 0; i < SIZE; i++) {

        for (int j = 0; j < SIZE; j++) {

            if (!mines[i][j]) {

                int count = 0;

                for (int di = -1; di <= 1; di++) {

                    for (int dj = -1; dj <= 1; dj++) {

                        int ni = i + di;

                        int nj = j + dj;

                        if (ni >= 0 && ni < SIZE && nj >= 0 && nj < SIZE && mines[ni][nj]) {

                            count++;

                        }

                    }

                }

                if (count > 0) {

                    board[i][j] = count + '0';

                }

            }

        }

    }

}

 

// 打印当前游戏棋盘状态

void printBoard() {

    printf("当前棋盘状态:\n");

    printf(" ");

    for (int col = 0; col < SIZE; col++) {

        printf("%d ", col);

    }

    printf("\n");

    for (int row = 0; row < SIZE; row++) {

        printf("%d ", row);

        for (int col = 0; col < SIZE; col++) {

            if (revealed[row][col]) {

                printf("%c ", board[row][col]);

            } else {

                printf("- ");

            }

        }

        printf("\n");

    }

    printf("\n");

}

 

// 揭开一个格子

void revealCell(int row, int col) {

    revealed[row][col] = true;

    remainingCells--;

    if (board[row][col] == '0') {

        for (int di = -1; di <= 1; di++) {

            for (int dj = -1; dj <= 1; dj++) {

                int ni = row + di;

                int nj = col + dj;

                if (ni >= 0 && ni < SIZE && nj >= 0 && nj < SIZE && !revealed[ni][nj]) {

                    revealCell(ni, nj);

                }

            }

        }

    }

}

 

// 主游戏逻辑

void playGame() {

    initializeGame();

    placeMines();

    calculateNumbers();

 

    while (remainingCells > 0) {

        printBoard();

        int row, col;

        printf("请输入要揭开的格子位置 (行 列): ");

        scanf("%d %d", &row, &col);

 

        if (row < 0 || row >= SIZE || col < 0 || col >= SIZE) {

            printf("无效的位置,请重新输入。\n");

            continue;

        }

 

        if (mines[row][col]) {

            printf("很抱歉,你揭开的是地雷!\n");

            printf("游戏结束!\n");

            break;

        } else if (revealed[row][col]) {

            printf("这个位置已经揭开过了,请选择其他位置。\n");

            continue;

        } else {

            revealCell(row, col);

            if (remainingCells == 0) {

                printf("恭喜!你成功揭开了所有非地雷格子!\n");

                break;

            }

        }

    }

}

 

// 主函数

int main() {

    playGame();

    return 0;

}

原理:通过printf()将文字打印在控制台上,运用函数,d%(可以根据自己的个性化增加)百分比随机生成地雷,初来乍到,请各位指点

-JAVA版扫雷    Python版扫雷​​​​​​​

游戏截图664861b0664645d7b3c8aa1fdf0fba84.jpg

882c73af00e746b3b9e0b09f0a2ef16b.jpg 

 根据自己的喜好,个性化添加功能,增加修改贴图,完善这款游戏

截止2024-7-1 22:05:46.13该源码仅在CSDN公布过,谢绝抄袭搬运

目前扫雷已完成Python,java,c++语言编写

  • 27
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
扫雷是一种经典的游戏,可以使用C语言来实现。以下是一个简单的扫雷程序的实现示例: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> #define ROWS 10 // 行数 #define COLS 10 // 列数 #define MINES 10 // 雷的数量 void init_game(char board[][COLS], int rows, int cols); // 初始化游戏面板 void print_board(char board[][COLS], int rows, int cols); // 打印游戏面板 void set_mines(char board[][COLS], int rows, int cols, int mines); // 布雷 void get_neighbors(int row, int col, int rows, int cols, int neighbors[]); // 获取邻居格子 int count_mines(char board[][COLS], int row, int col, int rows, int cols); // 计算某个格子周围的雷数 void reveal(char board[][COLS], int row, int col, int rows, int cols); // 翻开某个格子 int game_over(char board[][COLS], int rows, int cols); // 判断游戏是否结束 int main() { char board[ROWS][COLS]; int row, col; srand((unsigned)time(NULL)); // 设置随机数种子 init_game(board, ROWS, COLS); // 初始化游戏面板 set_mines(board, ROWS, COLS, MINES); // 布雷 print_board(board, ROWS, COLS); // 打印游戏面板 while (1) { printf("请输入坐标(行 列): "); scanf("%d %d", &row, &col); if (row < 0 || row >= ROWS || col < 0 || col >= COLS) { printf("输入错误,请重新输入\n"); continue; } if (board[row][col] == '*') { // 点到雷了 printf("游戏结束!你输了\n"); board[row][col] = 'X'; // 标记点到的雷 print_board(board, ROWS, COLS); break; } else { reveal(board, row, col, ROWS, COLS); // 翻开格子 print_board(board, ROWS, COLS); } if (game_over(board, ROWS, COLS)) { // 判断游戏是否结束 printf("游戏结束!你赢了\n"); break; } } return 0; } // 初始化游戏面板 void init_game(char board[][COLS], int rows, int cols) { int i, j; for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { board[i][j] = '-'; } } } // 打印游戏面板 void print_board(char board[][COLS], int rows, int cols) { int i, j; printf(" "); for (j = 0; j < cols; j++) { printf(" %d", j); } printf("\n"); for (i = 0; i < rows; i++) { printf("%d ", i); for (j = 0; j < cols; j++) { printf("%c ", board[i][j]); } printf("\n"); } printf("\n"); } // 布雷 void set_mines(char board[][COLS], int rows, int cols, int mines) { int count = 0, i, j; while (count < mines) { i = rand() % rows; j = rand() % cols; if (board[i][j] == '-') { board[i][j] = '*'; count++; } } } // 获取邻居格子 void get_neighbors(int row, int col, int rows, int cols, int neighbors[]) { int i, j, k = 0; for (i = row - 1; i <= row + 1; i++) { for (j = col - 1; j <= col + 1; j++) { if (i >= 0 && i < rows && j >= 0 && j < cols && (i != row || j != col)) { neighbors[k++] = i * cols + j; } } } } // 计算某个格子周围的雷数 int count_mines(char board[][COLS], int row, int col, int rows, int cols) { int neighbors[8], count = 0, i; get_neighbors(row, col, rows, cols, neighbors); for (i = 0; i < 8; i++) { if (board[neighbors[i] / cols][neighbors[i] % cols] == '*') { count++; } } return count; } // 翻开某个格子 void reveal(char board[][COLS], int row, int col, int rows, int cols) { int count; if (board[row][col] != '-') { // 已经翻开了 return; } count = count_mines(board, row, col, rows, cols); if (count > 0) { // 周围有雷 board[row][col] = '0' + count; } else { // 周围没有雷 board[row][col] = ' '; reveal(board, row - 1, col, rows, cols); reveal(board, row + 1, col, rows, cols); reveal(board, row, col - 1, rows, cols); reveal(board, row, col + 1, rows, cols); } } // 判断游戏是否结束 int game_over(char board[][COLS], int rows, int cols) { int i, j, count = 0; for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { if (board[i][j] == '-' || board[i][j] == '*') { count++; } } } return count == MINES; } ``` 在这个示例程序中,我们使用二维字符数组来表示游戏面板。其中,'-'表示未翻开的格子,'*'表示雷,' '表示周围没有雷的格子,'1'-'8'表示周围有1-8个雷的格子,'X'表示点到的雷。我们使用了递归算法来实现翻开格子的功能,当某个格子周围没有雷时,我们会递归地翻开它周围的格子,直到所有周围有雷的格子都被标记为'1'-'8'。 这只是一个简单的示例程序,还有很多可以改进的地方。比如,可以添加计时器和计分系统,以及更加友好的界面等等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值