C语言实现【扫雷小游戏】
程序员文章站
2022-04-07 14:32:03
...
扫雷小游戏我们大家都玩过,这次,自己写了一个简单版本的扫雷游戏。代码如下:
头文件game.h
#ifndef __GAME_H__
#define __GAME_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
//#define MIDDLE_COUNT 10 中等难度
//#define HARD_COUNT 10 高级难度
void InitBoard(char a[ROWS][COLS],int rows, int cols,char set);
void DisplayBoard(char a[ROWS][COLS], int row, int col);
void SetMine(char a[ROW][COL],int row,int col);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS],int row,int col);
int GetMineCount(char mine[ROWS][COLS], int x, int y);
#endif //__GAME_H__
源文件game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void InitBoard(char a[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++)
{
a[i][j] = set;
}
}
}
void DisplayBoard(char a[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
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 ", a[i][j]);
}
printf("\n");
}
printf("\n");
printf("---------------------------\n");
printf("\n");
}
void SetMine(char a[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % ROW+ 1;
int y = rand() % COL+ 1;
if (a[x][y] == '0')
{
a[x][y] = '1';
count--;
}
}
}
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
return mine[x - 1][y] +
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] - 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
{
int count =GetMineCount(mine,x,y);
win++;
show [x][y] = count + '0';
DisplayBoard(show, ROW, COL);
}
}
else
{
printf("坐标非法!请重新输入:\n");
}
}
if (win==row*col - EASY_COUNT)
{
printf("恭喜你,排雷成功!\n");
DisplayBoard(mine, ROW, COL);
}
}
源文件test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
printf("~~~~~~~~~~~~~~~~~~~~~~\n");
printf("**********************\n");
printf("**** 1.Play ****\n");
printf("**** 0.Exit ****\n");
printf("**********************\n");
printf("~~~~~~~~~~~~~~~~~~~~~~\n");
}
void game()
{
//1.布置雷
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);
//2.扫雷
FindMine(mine,show,ROW,COL);
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("\n");
printf("请选择---->");
scanf("%d", &input);
printf("\n");
switch (input)
{
case 1:
game();
break;
case 2:
printf("退出游戏\n");
break;
default:
printf("选择错误,请重新输入\n");
break;
}
} while (input);
}
int main()
{
test();
system("pause");
return 0;
}
写好后来玩一下试试。
游戏界面如下:
这样一步步的玩就ok了!
上一篇: 涨姿势!写给网页设计师的网页设计简史
下一篇: 两个SEO优化手段让你的网站走向灭亡