按照教程打完了。好几个bug都是自己打出来的。比如统计周围8个格子时,有一个各自加号填成了减号。我还以为平移了,一会显示是0一会显示是2。结果单纯的打错了。debug的时候断点放在scanf后面会顺畅一些。中间多放一些变量名方便监视。以及mine要多显示,在测试的时候。虽然成品是show,但是拿成品测试累死个人
test.c
cpp
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void menu()
{
printf("------------------------------------\n");
printf("---------- 1.play ----------------\n");
printf("---------- 2.exit ----------------\n");
printf("------------------------------------\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(show, ROW, COL);
findmine(mine, show, ROW, COL);
/*Displayboard(mine, ROW, COL);
Displayboard(show, ROW, COL);*/
}
int main()
{
//初始化
int input= 0;
srand((unsigned int)time(NULL));
//生成随机数的最简步骤有哪些?
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch(input)
{
case 1:
game();
//printf("扫雷\n");
break;
case 2:
printf("退出游戏\n");
break;
default:
printf("错误选择\n");
break;
}
} while (input == 2 ? 0 : input);
return 0;
}
game.c
cpp
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void Initboard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i, j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
void Displayboard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("---------- 扫雷游戏 -----------\n");
//记得换行符
for (j = 0; j <= col; j++)
{
printf("%d ", j);
}
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 board[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
int get_mine_count(char board[ROWS][COLS], int x, int y)
{
int i;
i=(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');
return i;
}
void findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int win = 0;
while (win<row*col-EASY_COUNT)
{
printf("请输入要排查的坐标:>");
int x = 0;
int y = 0;
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] != '*')
{
printf("排查过了,换一个\n");
}
else
{
if (mine[x][y] == '1')
{
printf("你被炸死了\n");
Displayboard(mine, ROW, COL);
break;
}
else
{
win++;
//周围几个雷
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';//转换为数字字符
//Displayboard(mine, ROW, COL);
Displayboard(show, ROW, COL);
}
}
}
else
{
printf("请重新输入坐标\n");
}
}
//注意这里条件不要写成赋值了
if (win == row * col - EASY_COUNT)
{
printf("排雷成功\n");
Displayboard(mine, ROW, COL);
}
}
game.h
cpp
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
//这几个头文件对应的英文意思和内容是什么
#define EASY_COUNT 10
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
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 board[ROWS][COLS], int row, int col);
void findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
输出结果:
cpp
------------------------------------
---------- 1.play ----------------
---------- 2.exit ----------------
------------------------------------
------------------------------------
请选择:>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 1 * * * * * * * *
2 * * * * * * * * *
3 * * * * * * * * *
4 * * * * * * * * *
5 * * * * * * * * *
6 * * * * * * * * *
7 * * * * * * * * *
8 * * * * * * * * *
9 * * * * * * * * *
---------- 扫雷游戏 -----------
请输入要排查的坐标:>1 2
---------- 扫雷游戏 -----------
0 1 2 3 4 5 6 7 8 9
1 1 1 * * * * * * *
2 * * * * * * * * *
3 * * * * * * * * *
4 * * * * * * * * *
5 * * * * * * * * *
6 * * * * * * * * *
7 * * * * * * * * *
8 * * * * * * * * *
9 * * * * * * * * *
---------- 扫雷游戏 -----------
请输入要排查的坐标:>2 1
你被炸死了
---------- 扫雷游戏 -----------
0 1 2 3 4 5 6 7 8 9
1 0 0 0 1 0 0 0 0 0
2 1 0 0 0 0 0 0 0 0
3 0 0 0 0 1 0 0 0 1
4 0 0 0 0 0 0 1 0 0
5 0 0 0 0 0 0 0 0 1
6 0 0 0 0 0 0 0 0 0
7 0 1 0 0 0 0 0 0 0
8 1 0 0 0 0 0 0 0 0
9 0 0 0 0 0 0 0 1 1
---------- 扫雷游戏 -----------
------------------------------------
---------- 1.play ----------------
---------- 2.exit ----------------
------------------------------------
------------------------------------
请选择:>2
退出游戏