数组小练习(2)——扫雷
一、游戏介绍
扫雷,一款经典的小游戏,我们用c语言来实现一下。
二、编程思路
用两个二维数组来分别存储布置好的地雷数据和排查出来的地雷数据;
在存储布置地雷数据的二维数组中,用“0”表示没有地雷,用“1”表示布置了地雷;
在存储排查地雷数据的二维数组中,用“*”表示还没有排查的位置,用“0”-“9”表示排查位置周围地雷的数量;
布置地雷用rand函数模拟;
三、代码
Minesweeper.h(函数声明)
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#define ROW 9
#define ROWS ROW+2
#define COL 9
#define COLS COL+2
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void InitBoard(char board[ROWS][COLS], int rows, int cols,char set);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void SetMine(char board[ROWS][COLS], int row, int col,int count);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col,int num);
Minesweeper.cpp(游戏的函数具体实现)
#include"Minesweeper.h"
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 (i = 0; i <= col; i++) {
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++) {
printf("%d ", i);
for (j = 1; j <= col; j++) {
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("-----------------------------------------\n");
}
void SetMine(char board[ROWS][COLS], int row, int col,int count) {
while (count) {
int x = rand() % row+1;
int y = rand() % col+1;
if (board[x][y] == '0') {
board[x][y] = '1';
count--;
}
}
}
int GetMineCount(char mine[ROWS][COLS], int x, int y) {
int sum = 0;
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
sum += mine[i][j];
}
}
return sum - 9 * '0';
}
void ShowMine(char mine[ROWS][COLS],char show[ROWS][COLS],int x, int y) {
int count = GetMineCount(mine, x, y);
if (count == 0) {
show[x][y] = count + '0';
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (i >= 1 && i <= ROW && j >= 1 && j <= COL && show[i][j] == '*') {
ShowMine(mine, show, i, j);
}
}
}
}
else {
show[x][y] = count + '0';
}
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col,int num) {
int x = 0;
int y = 0;
int win = 0;
while (win<row*col-num) {
printf("请输入要排查的坐标:>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col) {
if (mine[x][y] == '1') {
printf("\n很遗憾,你被炸死了!\n");
DisplayBoard(mine, row, col);
break;
}
else {
ShowMine(mine, show, x, y);
DisplayBoard(show, row, col);
win++;
}
}
else {
printf("坐标非法!\n");
}
}
if (win == row * col - num) {
printf("恭喜你,排雷成功\n");
DisplayBoard(mine, row, col);
}
}
test.cpp(测试)
#include"Minesweeper.h"
void menu() {
printf("****************************************\n");
printf("********** 1. play **********\n");
printf("********** 0. exit **********\n");
printf("****************************************\n");
}
void Minesweeper() {
char mine[ROWS][COLS] = {0};
char show[ROWS][COLS] = {0};
InitBoard(mine, ROWS, COLS,'0');
InitBoard(show, ROWS, COLS,'*');
SetMine(mine, ROW, COL, 10);
DisplayBoard(show, ROW, COL);
FindMine(mine, show, ROW, COL,10);
}
int main() {
int input = 0;
srand((unsigned int)time(NULL));
do {
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input) {
case 1:
Minesweeper();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误\n");
}
} while (input);
return 0;
}
四、改进思路
1.增加传统扫雷游戏里的插红旗功能。
2.增加可视化。
3.增加游戏难度选择,增加游戏趣味性。
|