根据我前面总结的一些关于C语言的一些知识,我们此博客就带着大家来写一个扫雷游戏的项目。
注:熟能生巧,不要求立马就写出来。大家可以多花费几天甚至一周的时间来消化,将这些东西变成自己的一些东西。
一.扫雷游戏test.c-----游戏的整个运行逻辑(调用下面的游戏模块)
首先我们需要在该文件中通过封装一个函数来先实现游戏的菜单。
接下来,我们需要封装一个函数来实现游戏的整个过程,全过程中我们用扫雷游戏的初阶来分析
(为了防止越界,我们吧这两个数组都分别大一圈,即编程11*11列表)
>1.打印一个9*9列表,并且布置雷(随机)
>2.扫雷
>3.统计排查出周围雷的个数的信息
初始化棋盘(尽可能封装分成一个函数,避免代码显得杂乱):
mine 数组中当没有雷的时候用字符‘0’表示;布置雷的时候用字符‘1’表示。
show 数组中当没有被排查的时候用字符‘ * ’表示。
打印棋盘
布置雷
排查雷(统计雷)
(x-1,y-1) (x,y-1) (x+1,y-1) (x,y-1) (x,y) (x+1,y) (x+1,y+1) (x,y+1) (x+1,y+1) 给大家列出这个表格,方便大家理解统计雷这块代码。
#define _CRT_SECURE_NO_WARNINGS
//-游戏的整个运行逻辑
#include "game.h"
#include <stdio.h>
void menu()
{
printf("********************\n");
printf("*******1.play*******\n");
printf("*******0.exit*******\n");
printf("********************\n");
}
void game()
{
//完成游戏的整个过程
char mine[ROWS][COLS] = { 0 };//存放布置好雷的信息
char show[ROWS][COLS] = { 0 };//存放排查出雷的信息,用于显示
//初始化棋盘
InitBoard(mine, ROWS, COLS,'0'); //'0'
InitBoard(show, ROWS, COLS,'*'); //'*'
//打印棋盘
//DisplayBoard(show, ROW, COL);
//DisplayBoard(mine, ROW, COL);
//布置雷
SetMine(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
//排查雷
FindMine(mine, show, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("------退出游戏------\n");
break;
default:
printf("-选择错误,请重新选择-\n");
break;
}
} while (input);
return 0;
}
二.(用下面两个文件,即游戏模块)实现游戏的整个过程:
1.game.h----游戏相关的函数的声明
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//游戏相关函数的声明
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
//棋盘的初始化
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 boad[ROWS][COLS], int row, int col);
//排查雷
void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS], int row,int col);
2.game.c----函数的实现
#define _CRT_SECURE_NO_WARNINGS
//函数的实现
#include "game.h"
//棋盘的初始化
void InitBoard(char board[ROWS][COLS], int rows, int cols,char set)
{
int i = 0;
for (i = 0; i < rows; i++)//不能取等于号,小心越界!
{
int j = 0;
for (j = 0; j < cols; j++)
{
board[i][j] = set; //初始化
}
}
}
//打印棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
printf("--欢迎进入扫雷游戏--\n");
int i = 0;
//打印列号
for (int i = 0; i <= row; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
int j = 0;
//打印行号
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 count = EASY_COUNT;
while (count) //这里不能放count--
{
int x = rand() % row +1;
int y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--; //布置成功后就count--;
}
}
}
//排查雷
GetMineCount(char mine[ROWS][COLS], int x, int y)
{
return mine[x - 1][y - 1] +
mine[x][y - 1] +
mine[x + 1][y - 1] +
mine[x + 1][y] +
mine[x + 1][y + 1] +
mine[x][y + 1] +
mine[x - 1][y + 1] +
mine[x - 1][y] - 8 * '0';
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
while (win<row*col-EASY_COUNT)
{
printf("请输入要排查的坐标:");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
if (show[x][y] != '*')
{
printf("该坐标已经被排查过了,无需再排查\n");
}
else
{
//统计mine数组的x,y坐标周围8个坐标中有几个雷
int count = GetMineCount(mine, x, y);
show[x][y] = count + '0';
DisplayBoard(show, ROW, COL);
win++;
}
}
}
else
{
printf("输入的坐标非法,请重新输入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
DisplayBoard(mine, ROW, COL);
}
}
接下来,我们看一下界面的运行图: