C语言编程题,实现一个简易的扫雷游戏,扫雷游戏在C语言中是一个经典的编程挑战。为了简化实现,假设棋盘是固定大小的,我们用一个简单的文本界面来表示扫雷的棋盘,玩家可以选择“揭开”某个格子,若该格子是地雷,则游戏失败;若格子周围没有地雷,则显示周围的地雷数。

1、初始化棋盘

initializeBoard() 函数创建一个大小为 ROWS x COLS 的棋盘,初始状态下所有格子都未揭开,且无地雷。

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

#define ROWS 10
#define COLS 10
#define MINES 10

typedef struct {
    int revealed;
    int mine;
    int adjacentMines;
} Cell;

Cell board[ROWS][COLS];

// 初始化棋盘
void initializeBoard() {
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            board[i][j].revealed = 0;
            board[i][j].mine = 0;
            board[i][j].adjacentMines = 0;
        }
    }
}

int main() {
  printf("Hello, World!");
  return 0;
}

2、布置地雷

placeMines() 随机在棋盘上放置 MINES 个地雷。

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

#define ROWS 10
#define COLS 10
#define MINES 10

typedef struct {
    int revealed;
    int mine;
    int adjacentMines;
} Cell;

Cell board[ROWS][COLS];


// 随机布置地雷
void placeMines() {
    srand(time(NULL));
    int minesPlaced = 0;
    while (minesPlaced < MINES) {
        int row = rand() % ROWS;
        int col = rand() % COLS;
        if (board[row][col].mine == 0) {
            board[row][col].mine = 1;
            minesPlaced++;
        }
    }
}

int main() {
  printf("Hello, World!");
  return 0;
}

3、计算周围地雷数

calculateAdjacentMines() 计算每个格子周围的地雷数,用于提示玩家。

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

#define ROWS 10
#define COLS 10
#define MINES 10

typedef struct {
    int revealed;
    int mine;
    int adjacentMines;
} Cell;

Cell board[ROWS][COLS];

// 计算每个格子周围的地雷数
void calculateAdjacentMines() {
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            if (board[i][j].mine == 1) continue;
            int count = 0;
            for (int x = -1; x <= 1; x++) {
                for (int y = -1; y <= 1; y++) {
                    int ni = i + x;
                    int nj = j + y;
                    if (ni >= 0 && ni < ROWS && nj >= 0 && nj < COLS && board[ni][nj].mine) {
                        count++;
                    }
                }
            }
            board[i][j].adjacentMines = count;
        }
    }
}

int main() {
  printf("Hello, World!");
  return 0;
}

4、游戏循环

playGame() 是游戏的主循环,每次玩家输入想揭开的格子坐标,并调用 revealCell() 函数揭开格子。若玩家揭开地雷,游戏结束;若揭开所有非雷区格子,则游戏胜利。

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

#define ROWS 10
#define COLS 10
#define MINES 10

typedef struct {
    int revealed;
    int mine;
    int adjacentMines;
} Cell;

Cell board[ROWS][COLS];

// 显示棋盘
void displayBoard() {
    printf("   ");
    for (int j = 0; j < COLS; j++) {
        printf("%2d ", j);
    }
    printf("\n");
    
    for (int i = 0; i < ROWS; i++) {
        printf("%2d ", i);
        for (int j = 0; j < COLS; j++) {
            if (board[i][j].revealed) {
                if (board[i][j].mine) {
                    printf(" * ");
                } else {
                    printf(" %d ", board[i][j].adjacentMines);
                }
            } else {
                printf(" . ");
            }
        }
        printf("\n");
    }
}

// 揭开一个格子
int revealCell(int row, int col) {
    if (row < 0 || row >= ROWS || col < 0 || col >= COLS || board[row][col].revealed) {
        return 0;
    }
    
    board[row][col].revealed = 1;
    
    if (board[row][col].mine) {
        return 1;  // 踩到雷
    }
    
    // 如果周围没有地雷,自动揭开周围格子
    if (board[row][col].adjacentMines == 0) {
        for (int x = -1; x <= 1; x++) {
            for (int y = -1; y <= 1; y++) {
                revealCell(row + x, col + y);
            }
        }
    }
    
    return 0;
}

// 检查游戏是否胜利
int checkWin() {
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            if (!board[i][j].revealed && !board[i][j].mine) {
                return 0;
            }
        }
    }
    return 1;
}

// 主游戏循环
void playGame() {
    int gameOver = 0;
    int win = 0;
    
    while (!gameOver && !win) {
        displayBoard();
        
        int row, col;
        printf("请输入要揭开的格子 (行 列): ");
        scanf("%d %d", &row, &col);
        
        if (revealCell(row, col)) {
            gameOver = 1;
            printf("你踩到了雷,游戏结束!\n");
        } else if (checkWin()) {
            win = 1;
            printf("恭喜你,成功避开了所有地雷!\n");
        }
    }
    
    displayBoard();
}

int main() {
  printf("Hello, World!");
  return 0;
}

int main() {
  printf("Hello, World!");
  return 0;
}

5、完整代码

一个简易的扫雷游戏,可以根据自己的需求进行扩展和完善。

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

#define ROWS 10
#define COLS 10
#define MINES 10

typedef struct {
    int revealed;
    int mine;
    int adjacentMines;
} Cell;

Cell board[ROWS][COLS];

// 初始化棋盘
void initializeBoard() {
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            board[i][j].revealed = 0;
            board[i][j].mine = 0;
            board[i][j].adjacentMines = 0;
        }
    }
}

// 随机布置地雷
void placeMines() {
    srand(time(NULL));
    int minesPlaced = 0;
    while (minesPlaced < MINES) {
        int row = rand() % ROWS;
        int col = rand() % COLS;
        if (board[row][col].mine == 0) {
            board[row][col].mine = 1;
            minesPlaced++;
        }
    }
}

// 计算每个格子周围的地雷数
void calculateAdjacentMines() {
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            if (board[i][j].mine == 1) continue;
            int count = 0;
            for (int x = -1; x <= 1; x++) {
                for (int y = -1; y <= 1; y++) {
                    int ni = i + x;
                    int nj = j + y;
                    if (ni >= 0 && ni < ROWS && nj >= 0 && nj < COLS && board[ni][nj].mine) {
                        count++;
                    }
                }
            }
            board[i][j].adjacentMines = count;
        }
    }
}

// 显示棋盘
void displayBoard() {
    printf("   ");
    for (int j = 0; j < COLS; j++) {
        printf("%2d ", j);
    }
    printf("\n");
    
    for (int i = 0; i < ROWS; i++) {
        printf("%2d ", i);
        for (int j = 0; j < COLS; j++) {
            if (board[i][j].revealed) {
                if (board[i][j].mine) {
                    printf(" * ");
                } else {
                    printf(" %d ", board[i][j].adjacentMines);
                }
            } else {
                printf(" . ");
            }
        }
        printf("\n");
    }
}

// 揭开一个格子
int revealCell(int row, int col) {
    if (row < 0 || row >= ROWS || col < 0 || col >= COLS || board[row][col].revealed) {
        return 0;
    }
    
    board[row][col].revealed = 1;
    
    if (board[row][col].mine) {
        return 1;  // 踩到雷
    }
    
    // 如果周围没有地雷,自动揭开周围格子
    if (board[row][col].adjacentMines == 0) {
        for (int x = -1; x <= 1; x++) {
            for (int y = -1; y <= 1; y++) {
                revealCell(row + x, col + y);
            }
        }
    }
    
    return 0;
}

// 检查游戏是否胜利
int checkWin() {
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            if (!board[i][j].revealed && !board[i][j].mine) {
                return 0;
            }
        }
    }
    return 1;
}

// 主游戏循环
void playGame() {
    int gameOver = 0;
    int win = 0;
    
    while (!gameOver && !win) {
        displayBoard();
        
        int row, col;
        printf("请输入要揭开的格子 (行 列): ");
        scanf("%d %d", &row, &col);
        
        if (revealCell(row, col)) {
            gameOver = 1;
            printf("你踩到了雷,游戏结束!\n");
        } else if (checkWin()) {
            win = 1;
            printf("恭喜你,成功避开了所有地雷!\n");
        }
    }
    
    displayBoard();
}

int main() {
    initializeBoard();
    placeMines();
    calculateAdjacentMines();
    playGame();
    
    return 0;
}

推荐文档

相关文档

大家感兴趣的内容

随机列表