欢迎来到扫雷游戏1.0
游戏逻辑
使用二维数组,初始化面板,展示面板,实现埋雷,实现提示,实现游戏循环
游戏文件
主函数文件:main.c
头文件:game.h
源文件:game.c
游戏实现
main.c
#include "game.h"
int main() {
char a[11][11], b[11][11];
InitBoard(a, b);
//ShowBoard(a, b);
//MaiLei(a);
do {
printf("欢迎来到扫雷游戏:1.开始游戏,2.退出游戏\n");
int in = 0;
scanf("%d", &in);
if (in == 2) {//in == 0肯定不行
printf("两秒后退出游戏\n");
sleep(2);
break;
}
else if (in == 1) {
StartGame(a, b);
}
else {
printf("你输错了,请重新输入\n");
}
}
while (1);
return 0;
}
game.h
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#ifndef SAOLEIPRIMER_GAME_H
#define SAOLEIPRIMER_GAME_H
#endif //SAOLEIPRIMER_GAME_H
void InitBoard(char a[11][11], char b[11][11]);
void ShowBoard(char a[11][11], char b[11][11], int ch);
void MaiLei(char a[11][11]);
char tips(int x, int y, char a[11][11]);
void StartGame(char a[11][11], char b[11][11]);
game.c
#include "game.h"
void InitBoard(char a[11][11], char b[11][11]) {//初始面板函数
//外围也要初始化
for (int i = 0; i < 11; i ++) {
for (int j = 0; j < 11; j ++) {
a[i][j] = '0';
}
}
for (int i = 0; i < 11; i ++) {
for (int j = 0; j < 11; j ++) {
b[i][j] = '*';
}
}
}
void ShowBoard(char a[11][11], char b[11][11], int ch) {//显示面板函数
if (ch == 1) {
printf("答案面板\n");
for (int i = 0; i < 10; i ++) {//11行11列都不显示
for (int j = 0; j < 10; j ++) {
if (i == 0)
printf("%c ", j + 48);
else if (j == 0)
printf("%c ", i + 48);
else
printf("%c ", a[i][j]);
}
printf("\n");
}
}
if (ch == 2) {
printf("扫雷面板\n");
for (int i = 0; i < 10; i ++) {
for (int j = 0; j < 10; j ++) {
if (i == 0)
printf("%c ", j + 48);
else if (j == 0)
printf("%c ", i + 48);
else
printf("%c ", b[i][j]);
}
printf("\n");
}
}
}
void MaiLei(char a[11][11]) {//埋雷函数
int m = 0, n = 0;
srand((unsigned int)time(NULL));
for (int i = 0; i < 10;) {
m = rand() % 9 + 1;
n = rand() % 9 + 1;
if (a[m][n] == '0') {
a[m][n] = '1';
i ++;
}
}
}
char tips(int x, int y, char a[11][11]) {//提示函数
int num = 48;
for (int i = x - 1; i <= x + 1; i ++)
for (int j = y - 1; j < y + 1; j ++) {
if (a[i][j] == '1') num ++;//a[i][j] = '1'是赋值!!!
}
return (char)num;
//(a[x - 1][y - 1] + a[x][y - 1] + a[x + 1][y - 1] + a[x - 1][y] + a[x + 1][y]
//+ a[x - 1][y + 1] + a[x][y + 1] + a[x + 1][y + 1]);//ascll
}
void StartGame(char a[11][11], char b[11][11]) {
printf("游戏开始\n");
MaiLei(a);
int left = 81;
while (left > 10) {
ShowBoard(a, b, 2);
//游戏交互逻辑
printf("请输入坐标:\n");
int x = 0, y = 0;
scanf("%d%d", &x, &y);
//游戏显示逻辑
if ((x >= 1) && (x <= 9) && (y >= 1) && (y <= 9)) {//输入有效
if (a[x][y] == '0') {//安全
b[x][y] = tips(x, y, a);
left --;
}
else {//踩雷
printf("被炸死了\n");
ShowBoard(a, b, 1);
break;//跳出循环
}
}
else //输入无效
printf("语法无效,请重新输入坐标\n");
}
if (left == 10) {//全部排雷
printf("扫雷成功\n");
ShowBoard(a, b, 1);
}
}
测试用例
欢迎来到扫雷游戏:1.开始游戏,2.退出游戏
1
游戏开始
扫雷面板
0 1 2 3 4 5 6 7 8 9
1 * * * * * * * * *
2 * * * * * * * * *
3 * * * * * * * * *
4 * * * * * * * * *
5 * * * * * * * * *
6 * * * * * * * * *
7 * * * * * * * * *
8 * * * * * * * * *
9 * * * * * * * * *
请输入坐标:
1
1
扫雷面板
0 1 2 3 4 5 6 7 8 9
1 0 * * * * * * * *
2 * * * * * * * * *
3 * * * * * * * * *
4 * * * * * * * * *
5 * * * * * * * * *
6 * * * * * * * * *
7 * * * * * * * * *
8 * * * * * * * * *
9 * * * * * * * * *
请输入坐标:
4 4
扫雷面板
0 1 2 3 4 5 6 7 8 9
1 0 * * * * * * * *
2 * * * * * * * * *
3 * * * * * * * * *
4 * * * 0 * * * * *
5 * * * * * * * * *
6 * * * * * * * * *
7 * * * * * * * * *
8 * * * * * * * * *
9 * * * * * * * * *
请输入坐标:
3
7
扫雷面板
0 1 2 3 4 5 6 7 8 9
1 0 * * * * * * * *
2 * * * * * * * * *
3 * * * * * * 2 * *
4 * * * 0 * * * * *
5 * * * * * * * * *
6 * * * * * * * * *
7 * * * * * * * * *
8 * * * * * * * * *
9 * * * * * * * * *
请输入坐标:
1 9
扫雷面板
0 1 2 3 4 5 6 7 8 9
1 0 * * * * * * * 0
2 * * * * * * * * *
3 * * * * * * 2 * *
4 * * * 0 * * * * *
5 * * * * * * * * *
6 * * * * * * * * *
7 * * * * * * * * *
8 * * * * * * * * *
9 * * * * * * * * *
请输入坐标:
9 9
扫雷面板
0 1 2 3 4 5 6 7 8 9
1 0 * * * * * * * 0
2 * * * * * * * * *
3 * * * * * * 2 * *
4 * * * 0 * * * * *
5 * * * * * * * * *
6 * * * * * * * * *
7 * * * * * * * * *
8 * * * * * * * * *
9 * * * * * * * * 1
请输入坐标:
7 7
扫雷面板
0 1 2 3 4 5 6 7 8 9
1 0 * * * * * * * 0
2 * * * * * * * * *
3 * * * * * * 2 * *
4 * * * 0 * * * * *
5 * * * * * * * * *
6 * * * * * * * * *
7 * * * * * * 2 * *
8 * * * * * * * * *
9 * * * * * * * * 1
请输入坐标:
8 8
被炸死了
答案面板
0 1 2 3 4 5 6 7 8 9
1 0 0 0 0 0 0 0 0 0
2 0 0 1 0 0 1 0 0 0
3 0 0 0 0 1 0 0 0 0
4 0 0 0 0 0 1 0 0 1
5 0 0 0 0 0 0 0 0 0
6 0 0 1 0 0 1 1 0 0
7 0 0 0 1 0 0 0 0 0
8 0 0 0 0 0 0 0 1 0
9 0 0 0 0 0 0 0 0 0
欢迎来到扫雷游戏:1.开始游戏,2.退出游戏
2
两秒后退出游戏
进程已结束,退出代码为 0
完毕,告一段落(🐶)