简单小游戏程序(小白)
前言
简单的C语言小游戏优化:
1.玩家第一次排查不会踩雷
2.玩家每排查一个坐标,若非雷则会自动扩展至雷区
一、文件
用到的文件:game.h game.c test.c
二、具体代码
1.game.h
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define Mine_Count 10//自定义雷个数
#define ROW 9
#define COL 9
#define ROWS ROW + 2
#define COLS COL + 2
//初始化
void InitBoard(char board[ROWS][COLS], int row, int col,char set);
//打印面板
void DisplayBoard(char board[ROWS][COLS],int row, int col);
//布置雷
void SetMine(char mine[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 1
#include "game.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;//初始化两个棋盘分别为'0'和'*'
}
}
}
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 mine[ROWS][COLS], int row, int col)
{
int count = Mine_Count;
while (count)
{
int x = rand()%row+1;//1-9随机数
int y = rand()%col+1;//1-9随机数
if (mine[x][y] == '0')
{
mine[x][y] = '1';//雷为1
count--;
}
}
}
static int GetMine(char mine[ROWS][COLS],int x,int y)
{
return mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] +
mine[x][y - 1] + mine[x][y + 1] +
mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] - 8 * '0';//棋盘为字符型,需要把字符型转化为整型
}
void ExpandBoard(char mine[ROWS][COLS],char show[ROWS][COLS], int x, int y, int *p)//递归实现展开非雷数
{
int i = 0;
int j = 0;
for (i = -1; i < 2; i++)
{
for (j = -1; j < 2; j++)
{
if (i != 0 || j != 0)
{
if (x + i >= 1 && x + i <= ROW && y + j >= 1 && y + j <= COL)
{
if (show[x + i][y + j] == '*' && mine[x + i][y + j] != '1')
{
int count = GetMine(mine, x+i, y+j);
if (count != 0)
{
show[x + i][y + j] = count + '0';
(*p)++;
}
else
{
show[x + i][y + j] = '0';
(*p)++;
ExpandBoard(mine, show, x + i, y + j, p);
}
}
}
}
}
}
}
void ReSetMine(char mine[ROWS][COLS], int row, int col, int x, int y, int count)//第一次踩雷重新布置雷
{
mine[x][y] = '0';
while (count)
{
int i = rand() % row + 1;//1-9随机数
int j = rand() % col + 1;//1-9随机数
if ((mine[i][j] != '1') && i != x && j != y)
{
mine[i][j] = '1';//雷为1
count--;
}
}
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
int count = 0;
while (win < row * col - Mine_Count)
{
printf("请输入坐标: ");
scanf("%d %d", &x, &y);
if (show[x][y] != '*') {
printf("该位置已被扫过,请重新输入!\n");
continue;
}
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
if (win == 0)//第一次踩雷
{
ReSetMine(mine, row, col, x, y, 1);//第一次踩雷重新布置雷
//DisplayBoard(mine, row, col);
count = GetMine(mine, x, y);//重新布置雷后该坐标必然不是雷,所以显示的是周围雷的个数
if (count == 0)
{
show[x][y] = '0';
win++;
ExpandBoard(mine, show, x, y, &win);
DisplayBoard(show, row, col);
}
else
{
show[x][y] = count + '0';//棋盘为字符型,需要把整型转化为字符型
win++;
DisplayBoard(show, row, col);
}
}
else
{
printf("很遗憾,您被炸的毛都不剩一根!\n");
DisplayBoard(mine, row, col);
break;
}
}
else
{
count = GetMine(mine, x, y);
if (count == 0)
{
show[x][y] = '0';
}
else
{
show[x][y] = count + '0';
}
win++;
ExpandBoard(mine, show, x, y, &win);
DisplayBoard(show, row, col);
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
if (win == row * col - Mine_Count)
{
printf("恭喜您,排雷成功!\n");
}
}
3.test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
printf("*********************************\n");
printf("********** 1.play ************\n");
printf("********** 0.else ************\n");
printf("*********************************\n");
}
void game()
{
printf("规则:在以下格子中有10个雷\n");
printf("请输入正确的坐标格式完成游戏\n");
printf("例如:1 1,2 2,3 3等等\n");
//存储数据
char mine[ROWS][COLS] = { 0 };//存储雷
char show[ROWS][COLS] = { 0 };//展示给玩家
//初始化
InitBoard(mine,ROWS,COLS,'0');
InitBoard(show, ROWS, COLS,'*');
//打印数组
//DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
//布置雷
SetMine(mine,ROW,COL);
//DisplayBoard(mine, 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 0:
printf("游戏结束\n");
break;
case 1:
game();
break;
default:
printf("请输入正确选项!\n");
break;
}
} while (input);
return 0;
}
三、效果图
成功啦
以上为该游戏的全部代码