1. Question & Analysis
-
Question: 整个扫雷小游戏,能扫就行,之后再完善(边学边记录);
-
Analysis: 1.打印菜单 ;2.进入游戏通道与指令;3. 不限次数玩游戏(含退出指令);
4. 得设置两个棋盘数组, 雷区数组mine,显示数组show,9×9规格,但是为了方便排雷的时候不会溢出数组,外扩一圈,即行列的首尾多加1;5.初始化雷区和显示区; 6. 打印展示一下这两个数组;7. 随机数布置雷区(打印出来检查一下); 8.开始扫雷(获取坐标,判断合法性,判断是否为雷--炸死退出;否则统计坐标周围的雷数量并在show中展示出来),然后继续扫雷;9. 设置胜利判断条件,并退出本次游戏。
2. Code Modules
2.1 头文件
#pragma once
//常量定义:
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define MINES 10
//头文件包含:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//函数声明:
// 1.初始化数组:
void InitialBoard(char board[ROWS][COLS], int rows, int cols, char set);
// 2. 打印一下数组:
void DisplayBoard(char board[ROWS][COLS], int row, int col);
//3. 布置雷区:
void SetMine(char board[ROWS][COLS], int row, int col);
//4. 开始扫雷:
void ClearMine(char board1[ROWS][COLS], char board2[ROWS][COLS], int row, int col);
2.2 游戏实现
#define _CRT_SECURE_NO_WARNINGS 1
#pragma warning(disable:6031)
#include "mine.h"
//Author: wanlin_z
void InitialBoard(char board[ROWS][COLS], int rows, int cols, char set) {
int i, j;
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, j;
printf("*^-^* Please Enjoy The Game *^-^*\n");
for (i = 0; i <= row; 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");
}
}
void SetMine(char board[ROWS][COLS], int row, int col) {
int total = MINES;
while (total) {
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] == '0') {
board[x][y] = '1';
total--;
}
}
}
char CountMines(char board1[ROWS][COLS], int x, int y) {
return board1[x - 1][y - 1] + board1[x - 1][y] + board1[x - 1][y + 1] + board1[x][y - 1] + board1[x][y + 1] +
board1[x + 1][y - 1] + board1[x + 1][y] + board1[x + 1][y + 1] - 7 * '0' ;
}
void ClearMine(char board1[ROWS][COLS], char board2[ROWS][COLS], int row, int col) {
int x, y;
int totals = row * col - MINES;
while (totals) {
printf("Enter Your Coordinate>:");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col) {
if (board1[x][y] == '1') {
printf("Boom! You've been blown up!\n");
break;
}
else {
//判断周围的雷数;
board2[x][y] = CountMines(board1, x, y);
DisplayBoard(board2, row, col);
totals--;
}
}
else {
printf("Illegal input, try again!\n");
}
}
if (totals == 0) {
printf("You win the game!\n");
}
}
2.3 游戏测试
#define _CRT_SECURE_NO_WARNINGS 1
#pragma warning(disable:6031)
#include "mine.h"
void menu() {
printf("********************\n");
printf("***** 1-play *****\n");
printf("***** 0-exit *****\n");
printf("********************\n");
}
void game() {
//定义mine&show数组:
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
//初始化mine--'0'&show--'*'数组:
InitialBoard(mine, ROWS, COLS, '0');
InitialBoard(show, ROWS, COLS, '*');
//展示一下初始化后的mine&show:
//DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
//布置雷区:
SetMine(mine, ROW, COL);
//DisplayBoard(mine, ROW, COL);
//开始扫雷:
ClearMine(mine, show, ROW, COL);
}
int main() {
int input = 0;
menu();
srand((unsigned int)time(NULL));
do {
printf("Enter the game instructions<1/0>:");
scanf("%d", &input);
switch (input) {
case 1:
game();
break;
case 0:
printf("Exit games!\n");
break;
default :
printf("Illegal input, try again!\n");
break;
}
} while (input);
return 0;
}
3. Summary
勉强敲出这个弱鸡版的扫雷,之后还有很多待完善的。之后再抽空完善加个递归,然后就可以自动扩散没有雷的区域。哈哈哈,期待各位大佬提建议,和思路,哈哈哈