本代码在VS2019中调试
1.主文件:
#include "1.h" void menu(void) { printf("*****************************************\n"); printf("***** 1.---------->开始 *****\n"); printf("***** 0.---------->结束 *****\n"); printf("*****************************************\n"); } void game(void) { //1.埋雷棋盘 char ml[ROWS][COLS] = { 0 }; //2.找雷棋盘 char zl[ROWS][COLS] = { 0 }; //初始化 csh(ml,ROWS,COLS,'0');//11*11 csh(zl,ROWS,COLS,'*'); //打印棋盘/= //display(ml, ROW, COL);//9*9 display(zl, ROW, COL); //埋雷 mailei(ml, ROW, COL); //打印棋盘 /*display(ml, ROW, COL);*/ //找雷 finemine(ml,zl,ROW,COL); } void text(void) { int num; srand((unsigned int)time(NULL)); do { menu(); printf("请输入数字用以确定是否进入游戏:(1/0)"); scanf_s("%d", &num); switch (num) { case 1: printf("即将进入游戏:----->\n"); game(); break; case 0: printf("即将退出游戏------->\n"); Sleep(1000); system("cls"); break; default: printf("输入错误,请重新输入\n"); break; } } while (num); } int main() { text(); return 0; }
2.调用函数文件:
#include "1.h" //棋盘初始化 void csh(char ml[ROWS][COLS], int rows, int cols, char m) { int i, j; for (i=0;i<ROWS;i++) { j = 0; for (j=0;j<COLS;j++) { ml[i][j] = m; } } } //棋盘打印 void display(char ml[ROWS][COLS], int row, int col) { int i, j; //打印行标 for (i = 0; i <= row; i++) { printf(" %d ", i); } printf("\n"); for (i=1;i<=row;i++) { printf(" %d ", i); j = 0; for (j=1;j<=col;j++) { printf(" %c ", ml[i][j]); } printf("\n"); } } void mailei(char ml[ROWS][COLS], int row, int col) { int a, b; int count = EASY_LEI; while (count) { a = rand() % row + 1; b = rand() % col + 1; if (ml[a][b] == '0') { ml[a][b] = '1'; count--; } } } int hjkjj(char ml[ROWS][COLS], int x, int y) { return ml[x - 1][y] + ml[x - 1][y - 1] + ml[x - 1][y + 1] + ml[x][y - 1] + ml[x][y + 1] + ml[x + 1][y - 1] + ml[x + 1][y] + ml[x + 1][y + 1] - 8 * '0'; } void finemine(char ml[ROWS][COLS],char zl[ROWS][COLS], int row, int col) { int a, b; int num; num = 0; //输入值用以判断是否超过 while (num < ROW * COL - EASY_LEI) { printf("请输入要走的位置(例子:1 2一行二列):"); scanf_s("%d %d", &a, &b); if(a>=1&&a<=ROW&&b>=1&&b<=COL) { if (ml[a][b] == '1') { printf("不好意思你被炸死了\n"); display(ml, ROW, COL); break; } else { int f = 0; //没被炸死; f = hjkjj(ml, a, b); zl[a][b] = f+'0'; display(zl, ROW, COL); } num++; } else { printf("输入坐标非法,请重新输入!\n"); } } if (num == ROW * COL - EASY_LEI) { printf("You WIN!\n"); } }
3.定义文件:(头文件)
#pragma once #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> #include <time.h> #include <windows.h> #define EASY_LEI 10 //定义埋雷的数量 #define ROW 9 #define COL 9 #define ROWS ROW+3 #define COLS COL+3 void text(void); void menu(void); void game(void); void csh(char ml[ROWS][COLS],int rows,int cols,char m); void display(char ml[ROWS][COLS], int row, int col); void mailei(char ml[ROWS][COLS], int row, int col); void finemine(char ml[ROWS][COLS],char zl[ROWS][COLS],int row,int col); int hjkjj(char ml[ROWS][COLS], int x, int y);