//C_day09.h
#ifndef C_day_h
#define C_day_h
#include "C_day09.h"
#include <stdio.h>
#include<string.h>
#include<time.h>
#include<stdlib.h>
#define ROW 9
#define COL 9
#define ROWS (ROW+2)
#define COLS (COL+2)
#define EASY_COUNT 10
void game();
void InitBoard(char mine[ROWS][COLS], char set);
void DispBoard(char mine[ROWS][COLS]);
void SetMine(char board[ROWS][COLS]);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS]);
int ExpandBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int win);
#endif //C_day_h
//C_day09.c
#include "C_day09.h"
void game(){
char mine[ROWS][COLS] = {0};
char show[ROWS][COLS] = {0};
InitBoard(mine, '0');
InitBoard(show, '*');
SetMine(mine);
DispBoard(mine);
DispBoard(show);
FindMine(mine, show);
}
void InitBoard(char board[ROWS][COLS], char set){
memset(board, set, ROWS*COLS*sizeof(board[0][0]));
}
void DispBoard(char board[ROWS][COLS]){
int i, j;
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 board[ROWS][COLS]){
int x, y;
int count = EASY_COUNT;
while(count){
x = rand()%9+1;
y = rand()%9+1;
if(board[x][y] == '0'){
board[x][y] = '1';
count--;
}
}
}
int GetMineCount(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 win = 0;
int count;
while(win < ROW*COL-EASY_COUNT){
int x, y;
printf("请输入坐标->:");
scanf("%d%d", &x, &y);
if(win == 0 && mine[x][y] == '1'){
count = 1;
mine[x][y] = '0';
while(count){
int i = rand()%9+1;
int j = rand()%9+1;
if(mine[i][j] == '0' && (i!=x + j!=y) >= 1){
mine[i][j] = '1';
count--;
}
}
}
if(x>=1 && x<=ROW && y>=1 && y<=COL){
if(mine[x][y] == '1'){
printf("游戏结束\n");
DispBoard(mine);
break;
}
else{
win = ExpandBoard(mine, show, x, y, win);
DispBoard(show);
}
}
else
printf("坐标非法\n");
}
if(win >= ROW*COL-EASY_COUNT)
printf("恭喜你游戏胜利\n");
}
int ExpandBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int win){
int count;
if(x>=1 && x<=ROW && y>=1 && y<=COL){
count = GetMineCount(mine, x, y);
if(count == 0 && show[x][y] == '*'){
show[x][y] = '0';
win++;
win = ExpandBoard(mine, show, x-1, y+1, win);
win = ExpandBoard(mine, show, x+1, y-1, win);
win = ExpandBoard(mine, show, x-1, y, win);
win = ExpandBoard(mine, show, x+1, y, win);
win = ExpandBoard(mine, show, x-1, y-1, win);
win = ExpandBoard(mine, show, x+1, y+1, win);
win = ExpandBoard(mine, show, x, y-1, win);
win = ExpandBoard(mine, show, x, y+1, win);
}
else if(show[x][y] == '*'){
show[x][y] = count + '0';
win++;
}
}
return win;
}
//main.c
#include "C_day09.h"
void menu(){
printf("*************************\n");
printf("*** 1.play 0.exit ***\n");
printf("*************************\n");
}
void test(){
int input;
srand((unsigned int)time(NULL));
do{
menu();
printf("请输入->:");
scanf("%d", &input);
switch(input){
case 1:
game();
break;
case 0:
break;
default:
printf("输入错误,请重新输入.\n");
break;
}
}while(input);
}
int main() {
test();
return 0;
}
直接上代码了,总体分为三个文件。
main.c
C_day09.h
C_day09.c
两个源文件和一个头文件;
使用前须知:
1. 游戏为展开式扫雷,默认雷个数EASY_COUNT 10可自行适当更改。游戏预定第一次不会炸死,当第一次恰好选择了雷的地方,程序将会自动把此处设置为无雷,并重新开辟一个新的雷点。
2. 自带横纵轴,第一张为内置答案,1代表此处有雷,0代表没有。第二张为玩家玩的图,*代表未知,数字代表周围雷的个数。
3. 输入坐标方式为: X Y(中间有一个空格)。
还有不清楚的欢迎留言询问,我将在看见第一时间回答相关问题。