1.game.h
#define _CRT_SECURE_NO_WARNINGS #include <string.h> #include <stdio.h> #include <time.h> #include<stdlib.h> #define ROW 9 #define COL 9 #define ROWS 11 #define COLS 11 #define EASY_MODE 9 void InitBoard(char board[ROWS][COLS], int rows, int cols, char ret);//初始化棋盘 void DisplayBoard(char board[ROWS][COLS], int row, int col);//打印棋盘 void setmine(char board[ROWS][COLS], int row, int col);//布置雷 void SaoLei(char board[ROWS][COLS], char Double[ROWS][COLS], int row, int col);
2.game.c
#include "game.h" void InitBoard(char board[ROWS][COLS], int rows, int cols, char ret)//初始化棋盘 { int i = 0; int j = 0; for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { board[i][j] = ret; } } } void DisplayBoard(char board[ROWS][COLS], int row, int col)//打印棋盘 { printf("\n"); printf("-------扫雷--------\n"); int i = 0; int j = 0; for (j = 0; j <= col; j++) { printf("%d ", j); } printf("\n"); for (i = 0; i < row; i++) { printf("%d ", i+1); for (j = 1; j <= col; j++) { printf("%c ", board[i+1][j]); } printf("\n"); } printf("-------扫雷--------\n"); } void setmine(char board[ROWS][COLS], int row, int col)//布置雷 { int count = EASY_MODE; int x = 0; int y = 0; while (count) { x = rand() % row + 1; y = rand() % col + 1; if (board[x][y] == '0') { board[x][y] = '1'; count--; } } } void SaoLei(char board[ROWS][COLS],char Double[ROWS][COLS], int row, int col) { int a = 0; int b = 0; int z = 0; int m = 0; int g = row*col - EASY_MODE; while (g) { printf("请输入>:"); scanf("%d%d", &a, &b); if (a > 0 && a < 10 && b > 0 && b < 10) { char t = '0'; if (board[a][b] == '0') { for (z = -1; z <= 1; z++) { for (m = -1; m <= 1; m++) { t = t + board[a + z][b + m]; } } t = t - '0' * 10 + '0'; Double[a][b] = t; g--; DisplayBoard(Double, ROW, COL);//打印棋盘 } else { printf("你被炸死了,游戏结束."); break; } } else { printf("输入格式有误,请重新输入."); } } printf("游戏结束了,你赢了."); }
3.progress.c
#include "game.h" void menu() { printf("---------------------\n"); printf("*******1.play********\n"); printf("*******0.exit********\n"); printf("---------------------\n"); } void game() { char mine[ROWS][COLS] = { 0 }; char show[ROWS][COLS] = { 0 }; InitBoard(mine, ROWS, COLS, '0');//初始化棋盘 InitBoard(show, ROWS, COLS, '*');//初始化棋盘 DisplayBoard(show, ROW, COL);//打印棋盘 //DisplayBoard(mine, ROW, COL);//打印棋盘 setmine(mine, ROW, COL);//布置雷 SaoLei(mine, show, ROW, COL); DisplayBoard(mine, ROW, COL);//打印棋盘 } int main() { srand((unsigned int)time(NULL)); int input = 0; menu(); do { printf("请选择>:"); scanf("%d", &input); switch (input) { case 1: printf("开始游戏\n"); game(); break; case 0: printf("退出游戏\n"); break; default : printf("输入格式有误,请重新输入.\n"); break; } } while (input); return 0; }
4.运行结果