目录
前言
实现c语言对一个简单游戏的设计,我发现他们都有很多相似之处,代码上比如游戏菜单,逻辑上都是以先搭建环境(让游戏在一定的范围内实现),然后是具体功能的实现(让游戏动起来)。
设计思路
建立游戏菜单-->建立棋盘-->初始化棋盘-->布置雷区-->排查雷-->判断是否踩雷-->(1.踩雷,退出游戏,2.没有继续游戏,若排完退出游戏宣布获胜)
代码的实现
test.c文件代码
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void test()
{
printf("---------------------------------\n");
printf("------1.play 0.exit----------\n");
printf("---------------------------------\n");
}
void game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
//设置雷
SetMine(mine, ROW, COL);
DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
//排查雷
FindMine(mine, show, ROW, COL);
}
int main()
{
int input = 0;
//设置随机数
srand((unsigned int)time(NULL));
do
{
test();
printf("请输入一个数:\n");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 2:
printf("退出游戏!");
break;
default:
printf("输入错误,请重新输入!");
break;
}
} while (input);
return 0;
}
game.c文件代码
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void InitBoard(char arr[ROWS][COLS], int rows, int cols, char set)
{
int i, j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
arr[i][j] = set;
}
}
}
void DisplayBoard(char arr[ROWS][COLS], int row, int col)
{
int i, j;
printf("---------------扫雷游戏-----------------\n");
for (j = 0; j <= col; j++)
{
printf("%d ", j);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 0; j < col; j++)
{
printf("%c ", arr[i][j]);
}
printf("\n");
}
printf("-----------------------------------------\n");
}
void SetMine(char mine[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int i = rand() % row + 1;
int j = rand() % col + 1;
if (mine[i][j] == '0')
{
mine[i][j] = '1';
}
count--;
}
}
int get_mine_count(char board[ROWS][COLS], int x, int y)
{
return (board[x - 1][y] +
board[x - 1][y - 1] +
board[x][y - 1] +
board[x + 1][y - 1] +
board[x + 1][y] +
board[x + 1][y + 1] +
board[x][y + 1] +
board[x - 1][y + 1] - 8 * '0');
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int i, j;
int win = 0;
while (1)
{
printf("请输入坐标:");
scanf("%d %d", &i, &j);
if (i >= 1 && i <= row && j >= 1 && j <= col)
{
if (show[i][j] != '*')
{
printf("该坐标被排查过了,不能重复排查\n");
}
else
{
if (mine[i][j] == '1')
{
printf("你踩中雷了,游戏结束!");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
win++;
int count = get_mine_count(mine, i, j);
show[i][j] = count + '0';//转换成数字字符
DisplayBoard(show, ROW, COL);
}
}
}
else
{
printf("输入错误,请重新输入!");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
DisplayBoard(mine, ROW, COL);
}
}
game.h文件代码
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
//初始化数组
void InitBoard(char arr[ROWS][COLS],int rows ,int cols,char set);
//打印数组
void DisplayBoard(char arr[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);
总结
游戏的实现,主要涉及了C语言中的一些基本知识,比如循环与分支,函数,数组等。