【C语言小游戏】扫雷


前言

《扫雷》是一款大众类的益智小游戏,于1992年发行。游戏目标是在最短的时间内根据点击格子出现的数字找出所有非雷格子,同时避免踩雷,踩到一个雷即全盘皆输。
在这里插入图片描述

本文就借此尝试用C语言中的数组知识,编写一个简易的程序来达到此游戏的效果。


1、思路分析

要实现这个扫雷游戏,我们需要两个数组。
一个用来存放布置好的雷的信息char mine[9][9],初始化为‘0’;
另一个用来存放排查出的雷的信息char show[9][9],初始化为‘1’。

这里需要三个文件:
game.h —— 存放函数的声明
game.c —— 实现函数完整的定义
test.c —— main函数和函数的调用


2、游戏菜单

在test.c中:

#define _CRT_SEURE_NO_WARNINGS 1
#pragma warning(disable:4996)


#include<stdio.h>

void menu()
{
   
	printf("*********************\n");
	printf("*****  1.play   *****\n");
	printf("*****  0.exit   *****\n");
	printf("*********************\n");

}
int main()
{
   
	int input = 0;
	do
	{
   
		menu();
		printf("请选择:>");
		scanf("%d",&input);
		switch (input)
		{
   
		case 1:
			printf("扫雷\n");
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误\n");
			break;
		}
	} while (input);
	return 0;
}

测试:
在这里插入图片描述


3、游戏过程

在game()函数中实现:

3.1初始化和棋盘

test.c

char mine[ROWS][COLS] = {
    0 };//存放布置好的雷的信息
char show[ROWS][COLS] = {
    0 };//存放排查出的雷的信息
//初始化数组的内容为指定的内容
//mine数组在没有布置雷的时候,都是'0'
InitBoard(mine,ROWS,COLS,'0');
//show数组在没有排查雷的时候,都为'*'
InitBoard(show, ROWS, COLS,'*');

//DisplayBoard(mine,ROW,COL);
DisplayBoard(show, ROW, COL);

game.h

void InitBoard(char board[ROWS][COLS],int rows,int cols,char set);
void DisplayBoard(char board[ROWS][COLS], int row, int col);

game.c

void InitBoard(char board[ROWS][COLS], int rows, int cols,char set)
{
   
	int i = 0;
	int j = 0;
	for (i = 0;i < rows;i++)
	{
   
		for (j = 0;j < cols;j++)
		{
   
			board[i][j] = set;
		}
	}
}

void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
   
	int i = 0;
	int j = 0;
	printf("-----扫雷游戏------\n");
	for (j = 0;j <=col;j++)
	{
   
		printf("%d ", j
当然,我可以给你介绍一下C语言扫雷游戏代码。下面是一个简单的C语言扫雷游戏代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> #define SIZE 10 #define MINES 10 int board[SIZE][SIZE]; int revealed[SIZE][SIZE]; void initializeBoard() { int i, j; for (i = 0; i < SIZE; i++) { for (j = 0; j < SIZE; j++) { board[i][j] = 0; revealed[i][j] = 0; } } } void placeMines() { int count = 0; while (count < MINES) { int x = rand() % SIZE; int y = rand() % SIZE; if (board[x][y] != -1) { board[x][y] = -1; count++; } } } void printBoard() { int i, j; for (i = 0; i < SIZE; i++) { for (j = 0; j < SIZE; j++) { if (revealed[i][j]) { if (board[i][j] == -1) { printf("* "); } else { printf("%d ", board[i][j]); } } else { printf(". "); } } printf("\n"); } } int countAdjacentMines(int x, int y) { int count = 0; int i, j; for (i = -1; i <= 1; i++) { for (j = -1; j <= 1; j++) { if (x + i >= 0 && x + i < SIZE && y + j >= 0 && y + j < SIZE) { if (board[x + i][y + j] == -1) { count++; } } } } return count; } void revealCell(int x, int y) { if (x < 0 || x >= SIZE || y < 0 || y >= SIZE) { return; } if (revealed[x][y]) { return; } revealed[x][y] = 1; if (board[x][y] == 0) { int i, j; for (i = -1; i <= 1; i++) { for (j = -1; j <= 1; j++) { revealCell(x + i, y + j); } } } } int main() { srand(time(NULL)); initializeBoard(); placeMines(); int gameOver = 0; while (!gameOver) { printBoard(); int x, y; printf("Enter the coordinates of the cell to reveal (x y): "); scanf("%d %d", &x, &y); if (board[x][y] == -1) { printf("Game Over! You hit a mine.\n"); gameOver = 1; } else { revealCell(x, y); int revealedCount = 0; int i, j; for (i = 0; i < SIZE; i++) { for (j = 0; j < SIZE; j++) { if (revealed[i][j]) { revealedCount++; } } } if (revealedCount == SIZE * SIZE - MINES) { printf("Congratulations! You won the game.\n"); gameOver = 1; } } } return 0; } ``` 这是一个简单的命令行扫雷游戏游戏板大小为10x10,总共有10个地雷。你可以根据需要进行修改和扩展。希望对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值