我们先创建game.h头文件来声明我们要用到的所有函数和定义的宏
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<Windows.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
void menu();//菜单
void game();//开始游戏
void Init(char arr[ROWS][COLS], int row, int col, char set);//初始化棋盘
void DisplayBoard(char arr[ROWS][COLS], int row, int col);
void SetMine(char arr[ROWS][COLS], int row, int col);
void SetShow(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
void FindMine(char arr[ROWS][COLS], char show[ROWS][COLS], int row, int col);
void FindMine(char arr[ROWS][COLS], char show[ROWS][COLS], char help[ROWS][COLS], int row, int col);
在game.c文件中对game.h中的函数做具体实现
#pragma once
#include"game.h"
void menu(){
printf("*******************\n");
printf("**** 1、开始游戏 *\n");
printf("**** 0、退出游戏 *\n");
printf("*******************\n");
}
void game(){
char mine[ROWS][COLS];
char show[ROWS][COLS];
char help[ROWS][COLS];
Init(mine,ROW,COL,'0');
Init(show,ROW,COL,'*');
Init(help, ROW, COL, '#');
//设置雷
SetMine(mine, ROW, COL);
//更新show内容
SetShow(mine, show, ROW, COL);
//查找点
FindMine(mine, show, help , ROW, COL);
system("pause");
}
void Init(char arr[ROWS][COLS], int row, int col, char set){
for (int i = 1; i <= row; i++){
for (int j = 1; j <= col; j++){
arr[i][j] = set;
}
}
}
void DisplayBoard(char arr[ROWS][COLS],int row,int col){
for (int i = 0; i <= row; i++){
printf("%d ", i);
}
printf("\n");
for (int i = 1; i <= row; i++){
printf("%d ", i);
for (int j = 1; j <= col; j++){
printf("%c ", arr[i][j]);
}
printf("\n");
}
}
void SetMine(char arr[ROWS][COLS], int row, int col){
int count = EASY_COUNT;
while (count){
int x = rand() % row + 1;
int y = rand() % col + 1;
if (arr[x][y] != '1'){
arr[x][y] = '1';
count--;
}
}
}
void FindMine(char arr[ROWS][COLS],char show[ROWS][COLS],char help[ROWS][COLS], int row, int col){
int x=0;
int y=0;
int count=0;
while (1){
system("cls");
DisplayBoard(help, row, col);
printf("输入要查找的坐标:\n");
scanf_s("%d%d", &x, &y);
if (x >= 1 && x <= row&&y >= 1 && y <= col){
if (arr[x][y] == '1'){
printf("很不幸你被炸死了\n");
}
else{
help[x][y] = show[x][y];
count++;
if (count == row*col - EASY_COUNT){
printf("恭喜你赢了\n");
return;
}
}
}
else{
printf("坐标不合法\n");
system("pause");
}
}
}
void SetShow(char mine[ROWS][COLS],char show[ROWS][COLS], int row, int col){
for (int i = 1; i <= row; i++){
if (i == 1){
for (int j = 1; j <= col; j++){
if (j == 1){
show[i][j] = mine[1][2]-'0' + mine[2][1]-'0' + mine[2][2];
}
else if (j == col){
show[i][j] = mine[1][col-1]-'0' + mine[2][col-1]-'0' + mine[2][col];
}
else{
show[i][j] = mine[1][j-1]-'0' + mine[1][j+1]-'0' + mine[2][j]-'0' + mine[2][j-1]-'0' + mine[2][j+1];
}
}
}
else if (i == col){
for (int j = 1; j <= col; j++){
if (j == 1){
show[col][1] = mine[col][2] -'0'+ mine[col-1][1]-'0' + mine[col-1][2];
}
else if (j == col){
show[i][j] = mine[i][j-1]-'0' + mine[i-1][j-1]-'0' + mine[i-1][j];
}
else{
show[i][j] = mine[i][j-1]-'0' + mine[i][j+1]-'0' + mine[i-1][j-1]-'0' + mine[i-1][j]-'0' + mine[i-1][j+1];
}
}
}
else{
for (int j = 1; j <= col; j++){
if (j == 1){
show[i][j] = mine[i][j+1]-'0' + mine[i-1][j]-'0' + mine[i-1][j+1]-'0' + mine[i+1][j]-'0'+mine[i+1][j+1];
}
else if (j == col){
show[i][j] = mine[i][j-1]-'0' + mine[i-1][j-1]-'0' + mine[i-1][j]-'0' + mine[i+1][j-1]-'0' + mine[i+1][j];
}
else{
show[i][j] = mine[i][j-1]-'0' + mine[i][j+1]-'0' + mine[i-1][j]-'0' + mine[i-1][j-1]-'0' + mine[i+1][j-1]-'0' + mine[i+1][j]-'0' + mine[i-1][j+1]-'0' + mine[i+1][j+1];
}
}
}
}
}
在测试文件中实现main函数测试
#include"game.h"
int main(){
srand((unsigned int)time(NULL));
int input = 0;
do{
system("cls");
printf("请选择:\n");
menu();
scanf_s("%d", &input);
switch (input){
case 1:
game();
break;
case 0:
printf("欢迎下次再来\n");
Sleep(500);
break;
default:
printf("选择错误\n");
Sleep(500);
break;
}
} while (input);
}
|