linux简易扫雷c语言代码,C语言实现简易版扫雷的完整过程
时间: 2023-12-03 08:29:08 浏览: 150
以下是一个简单的基于Linux的扫雷游戏C语言代码。代码实现了一个简单的扫雷游戏,玩家可以选择不同的难度级别并尝试找到所有地雷。
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 10
#define COL 10
#define EASY 10
#define MEDIUM 20
#define HARD 30
int main()
{
int board[ROW][COL], num_mines, num_flags;
int i, j, r, c, count, gameover = 0, win = 0, choice;
char action;
srand(time(NULL));
printf("Welcome to Minesweeper\n");
printf("----------------------\n");
printf("Choose a difficulty level:\n");
printf("1. Easy\n2. Medium\n3. Hard\n");
scanf("%d", &choice);
switch (choice) {
case 1:
num_mines = EASY;
break;
case 2:
num_mines = MEDIUM;
break;
case 3:
num_mines = HARD;
break;
default:
printf("Invalid choice. Defaulting to Easy difficulty.\n");
num_mines = EASY;
break;
}
//initialize board with 0's
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
board[i][j] = 0;
}
}
//place mines randomly
for (i = 0; i < num_mines; i++) {
r = rand() % ROW;
c = rand() % COL;
if (board[r][c] == -1) { //already a mine here
i--;
continue;
}
board[r][c] = -1;
}
//fill in numbers
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
if (board[i][j] == -1) { //skip mines
continue;
}
count = 0;
if (i-1 >= 0 && j-1 >= 0 && board[i-1][j-1] == -1) count++;
if (i-1 >= 0 && board[i-1][j] == -1) count++;
if (i-1 >= 0 && j+1 < COL && board[i-1][j+1] == -1) count++;
if (j-1 >= 0 && board[i][j-1] == -1) count++;
if (j+1 < COL && board[i][j+1] == -1) count++;
if (i+1 < ROW && j-1 >= 0 && board[i+1][j-1] == -1) count++;
if (i+1 < ROW && board[i+1][j] == -1) count++;
if (i+1 < ROW && j+1 < COL && board[i+1][j+1] == -1) count++;
board[i][j] = count;
}
}
//game loop
while (!gameover && !win) {
num_flags = 0;
printf("\n\n");
for (i = 0; i < ROW; i++) {
printf("%d ", i);
for (j = 0; j < COL; j++) {
if (board[i][j] == -2) { //flag
printf("F ");
num_flags++;
}
else if (board[i][j] >= 0 && board[i][j] <= 8) {
printf("%d ", board[i][j]);
}
else {
printf("X ");
}
}
printf("\n");
}
printf(" ");
for (i = 0; i < COL; i++) {
printf("%d ", i);
}
printf("\n\n");
printf("Number of flags: %d\n", num_flags);
printf("Enter coordinate and action (f = flag, u = unflag, r = reveal): ");
scanf("%d %d %c", &r, &c, &action);
if (r < 0 || r >= ROW || c < 0 || c >= COL) {
printf("Invalid coordinate.\n");
continue;
}
if (action == 'f' || action == 'F') { //flag
if (board[r][c] != -2) {
board[r][c] = -2;
}
else {
board[r][c] = 0;
}
}
else if (action == 'r' || action == 'R') { //reveal
if (board[r][c] == -1) { //mine
gameover = 1;
}
else {
board[r][c] += 10; //reveal
if (board[r][c] == 9) { //win
win = 1;
}
else if (board[r][c] == 10) { //empty square
//reveal adjacent squares
if (r-1 >= 0 && c-1 >= 0 && board[r-1][c-1] >= 0 && board[r-1][c-1] <= 8) board[r-1][c-1] += 10;
if (r-1 >= 0 && board[r-1][c] >= 0 && board[r-1][c] <= 8) board[r-1][c] += 10;
if (r-1 >= 0 && c+1 < COL && board[r-1][c+1] >= 0 && board[r-1][c+1] <= 8) board[r-1][c+1] += 10;
if (c-1 >= 0 && board[r][c-1] >= 0 && board[r][c-1] <= 8) board[r][c-1] += 10;
if (c+1 < COL && board[r][c+1] >= 0 && board[r][c+1] <= 8) board[r][c+1] += 10;
if (r+1 < ROW && c-1 >= 0 && board[r+1][c-1] >= 0 && board[r+1][c-1] <= 8) board[r+1][c-1] += 10;
if (r+1 < ROW && board[r+1][c] >= 0 && board[r+1][c] <= 8) board[r+1][c] += 10;
if (r+1 < ROW && c+1 < COL && board[r+1][c+1] >= 0 && board[r+1][c+1] <= 8) board[r+1][c+1] += 10;
}
}
}
else if (action == 'u' || action == 'U') { //unflag
if (board[r][c] == -2) {
board[r][c] = 0;
}
}
else {
printf("Invalid action.\n");
}
}
//game over, reveal all squares
printf("\n\n");
for (i = 0; i < ROW; i++) {
printf("%d ", i);
for (j = 0; j < COL; j++) {
if (board[i][j] == -2) { //flag
printf("F ");
}
else if (board[i][j] >= 0 && board[i][j] <= 8) {
printf("%d ", board[i][j]);
}
else if (board[i][j] == -1) { //mine
printf("M ");
}
else {
printf("X ");
}
}
printf("\n");
}
printf(" ");
for (i = 0; i < COL; i++) {
printf("%d ", i);
}
printf("\n\n");
if (gameover) {
printf("Game over! You hit a mine.\n");
}
else {
printf("Congratulations, you win!\n");
}
return 0;
}
```
该代码包括以下功能:
- 难度选择
- 地雷随机放置
- 数字填充
- 可以插旗
- 可以取消插旗
- 可以揭开方块
- 只有揭开所有非地雷方块才能获胜
这个代码还有可以改进的地方,比如增加计时器和玩家得分等等。但是,希望这个代码可以给你提供一个基础的思路和参考。
阅读全文