C语言实现扫雷

写一个简单的程序实现扫雷游戏

首先先创建头文件<game.h>定义常量,以及引入的库文件

#define _CRT_SECURE_NO_WARNINGS 1

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

#define ROWS 11
#define COLS 11
#define boom 10

void game();

完成菜单,以及实现菜单选项


#include "game.h"

void test(){
	int input = 0;
	srand((unsigned int)time(NULL));
	do {
		printf("*****************************\n");
		printf("**********1.play*************\n");
		printf("**********0.exit ************\n");
		printf("*****************************\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏成功!\n");
			break;
		}
	} while (input);
}

int main() {
	test();
	return 0;
}

我们要选择进入游戏,则进入game(),在game()中调用我们需要实现的功能,

void game() {

	char Boom[ROWS][COLS] = { 0 };

	char Show[ROWS][COLS] = { 0 };

	InitBoard(Boom, ROWS, COLS, '0');

	InitBoard(Show, ROWS, COLS, '*');

	DisplayShow(Show, ROWS, COLS);

	SetBoom(Boom, ROWS, COLS);
	
	Find(Boom, Show, ROWS, COLS);
}

要用2个棋盘,一个棋盘用来随机安置雷的位置,另一个棋盘用来展现给玩家。创建好两个char的二维数组后,首先要对两个棋盘初始化。

InitBoard();

void InitBoard(char Board[ROWS][COLS], int row, int col, char ch) {
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++) {
		for (j = 0; j < col; j++) {
			Board[i][j] = ch;
		}
	}
}

初始化完成后,要将我们初始化的棋盘展示给玩家

Display();

void DisplayShow(char Board[ROWS][COLS], int row, int col) {
	int i = 0;
	int j = 0;
	for (i = 0; i < row - 1; i++) {
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i < row - 1; i++) {
		printf("%d ", i);
		for (j = 1; j < col - 1; j++) {
			printf("%c ", Board[i][j]);
		}
		printf("\n");
	}
}

需要将放置雷的棋盘放好雷,我们用字符’1’来表示雷

SetBoom();

void SetBoom(char Board[ROWS][COLS], int row, int col) {
	int x = 0;
	int y = 0;
	int i = 0;
	int count1 = 0;
	while (count1 < boom) {
		x = (rand() % (row - 2)) + 1;
		y = (rand() % (col - 2)) + 1;
		if (Board[x][y] == '0') {
			Board[x][y] = '1';
			count1++;
		}
	}
}

最后一步就是让玩家输入坐标去躲避雷的位置;如果输入的坐标和雷的坐标一致游戏就结束,如果不一致就输入下一个坐标,直到所有的雷都被找到。

void Find(char Boom[ROWS][COLS], char Show[ROWS][COLS], int row, int col) {

	int x = 0;
	int y = 0;

	while (1) {
		printf("请输入坐标:>");
		scanf("%d%d", &x, &y);

		if ((x >= 1 && x <= 9) && (y >= 1 && y <= 9)) {
			if (Show[x][y] != '*') {
				printf("该位置被占用,请重新输入\n");
			}
			if (Boom[x][y] == '1') {
				printf("被炸死\n");
				break;
			}
			if (Boom[x][y] == '0') {
				open_board(Boom, Show, x, y);

				DisplayShow(Show, ROWS, COLS);
				
				if (is_win(Show, ROWS, COLS) == boom) {
					printf("扫雷成功\n");
					break;
				}
			}
		}
		else {
			printf("坐标输入错误请重新输入\n");
		}
	}
}

在这中间我们要实现点开一个坐标就能够返回它周围8个坐标的雷的数量,返回值传给这个坐标,所以返回值是char类型

SearchBoom();

char SearchBoom(char Boom[ROWS][COLS], int x, int y) {

	return Boom[x - 1][y - 1] + Boom[x][y - 1] + Boom[x + 1][y - 1]
		 + Boom[x - 1][y] + Boom[x][y] + Boom[x + 1][y]
		 + Boom[x - 1][y + 1] + Boom[x][y + 1] + Boom[x + 1][y + 1] - 8 * '0';
}

最后如果这个坐标周围没有雷的话,我们需要把这个坐标的周围展开,直到下个坐标周围有雷为止,所以用递归实现。

open_board();

void open_board(char Boom[ROWS][COLS], char Show[ROWS][COLS], int x, int y) {
	Show[x][y] = SearchBoom(Boom, x, y);
	if (Show[x][y] == '0' && x > 0 && x < ROWS && y>0 && y < COLS) {
		if (Show[x - 1][y - 1] == '*') {
			open_board(Boom, Show, x - 1, y - 1);
		}
		if (Show[x][y - 1] == '*') {
			open_board(Boom, Show, x, y - 1);
		}
		if (Show[x + 1][y - 1] == '*') {
			open_board(Boom, Show, x + 1, y - 1);
		}
		if (Show[x - 1][y] == '*') {
			open_board(Boom, Show, x - 1, y);
		}
		if (Show[x + 1][y] == '*') {
			open_board(Boom, Show, x + 1, y);
		}
		if (Show[x - 1][y + 1] == '*') {
			open_board(Boom, Show, x - 1, y + 1);
		}
		if (Show[x][y + 1] == '*') {
			open_board(Boom, Show, x, y + 1);
		}
		if (Show[x + 1][y + 1] == '*') {
			open_board(Boom, Show, x + 1, y + 1);
		}
	}
}

完成到这里所有的功能就可以实现了

最后放一下扫雷实现代码链接:扫雷的实现

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的扫雷程序的C语言代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> #define ROWS 10 #define COLS 10 #define MINES 10 void print_board(char board[ROWS][COLS], int reveal_all); void place_mines(char board[ROWS][COLS]); int count_adjacent_mines(char board[ROWS][COLS], int row, int col); void reveal_cell(char board[ROWS][COLS], int row, int col, int *remaining_cells); void play_game(char board[ROWS][COLS]); int main() { char board[ROWS][COLS]; srand(time(NULL)); place_mines(board); print_board(board, 0); play_game(board); return 0; } void print_board(char board[ROWS][COLS], int reveal_all) { int i, j; printf(" "); for (i = 0; i < COLS; i++) { printf("%d ", i + 1); } printf("\n"); for (i = 0; i < ROWS; i++) { printf("%c ", 'A' + i); for (j = 0; j < COLS; j++) { if (board[i][j] == '*' && !reveal_all) { printf(". "); } else { printf("%c ", board[i][j]); } } printf("\n"); } } void place_mines(char board[ROWS][COLS]) { int i, j, count = 0; for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { board[i][j] = '.'; } } while (count < MINES) { i = rand() % ROWS; j = rand() % COLS; if (board[i][j] == '.') { board[i][j] = '*'; count++; } } } int count_adjacent_mines(char board[ROWS][COLS], int row, int col) { int i, j, count = 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 && board[i][j] == '*') { count++; } } } return count; } void reveal_cell(char board[ROWS][COLS], int row, int col, int *remaining_cells) { int i, j; if (board[row][col] == '.') { (*remaining_cells)--; board[row][col] = '0' + count_adjacent_mines(board, row, col); if (board[row][col] == '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) { reveal_cell(board, i, j, remaining_cells); } } } } } } void play_game(char board[ROWS][COLS]) { int row, col, remaining_cells = ROWS * COLS - MINES; while (remaining_cells > 0) { printf("Enter row and column: "); scanf("%d %d", &row, &col); row--; col--; if (row >= 0 && row < ROWS && col >= 0 && col < COLS) { if (board[row][col] == '*') { printf("Game over!\n"); print_board(board, 1); return; } else { reveal_cell(board, row, col, &remaining_cells); print_board(board, 0); } } } printf("You won!\n"); } ``` 这个程序使用了一个二维字符数组来表示扫雷的棋盘,其中每个字符表示该位置的状态,'.'表示未揭开,'*'表示地雷,'0'-'8'表示相邻地雷的数量。在程序开始时,使用`place_mines`函数随机放置地雷。然后使用`print_board`函数打印当前的棋盘。在玩家选择一个位置后,使用`reveal_cell`函数揭开该位置,如果该位置是地雷则游戏结束,否则更新棋盘并继续游戏,直到所有非地雷位置都被揭开。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值