目录
1、打印欢迎界面
void welcome() { printf("******************************\n"); printf("********* 扫雷 *********\n"); printf("*** 1.play 0.exit ***\n"); printf("******************************\n"); } void game() { } void playgame() { int input = 0; do { welcome(); printf("请选择:>"); scanf("%d", &input); switch (input) { case 1: printf("开始游戏\n"); game(); break; case 0: printf("退出游戏\n"); break; default: printf("选择错误,请重新选择\n"); break; } } while (input); } int main() { playgame(); return 0; }
2、创建数组和打印棋盘
//test.c
#include "game.h" void game() { char mine[ROWS][COLS] = { 0 };//存雷 char show[ROWS][COLS] = { 0 };//显示 initial(mine, ROWS, COLS,'0'); initial(show, ROWS, COLS,'*');//初始化 // DisplayBoard(mine, ROW, COL); DisplayBoard(show, ROW, COL);//打印棋盘 }
//game.h
#pragma once #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 #include<stdio.h> void initial(char board[ROWS][COLS], int row, int col,char set); void DisplayBoard(char board[ROWS][COLS], int row, int col);
//game.c
#include"game.h" void initial(char board[ROWS][COLS], int row, int col,char set) { int i = 0; int j = 0; for (i = 0;i < row;i++) { for (j = 0;j < col;j++) { board[i][j] = set; } } } void DisplayBoard(char board[ROWS][COLS], int row, int col) { int i = 0; int j = 0; for (i = 1;i <= row;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]); if (j < col) printf("|"); } printf("\n"); printf(" "); if (i < row ) { for (j = 1;j <= col;j++) { printf("---"); if (j < col) printf("|"); } printf("\n"); } } }
3、随机生成地雷
//test.c
void game() { ... ... DisplayBoard(show, ROW, COL);//打印棋盘 SetMine(mine, ROWS, COLS); // DisplayBoard(mine, ROW, COL);打印雷池来验证 } void playgame() { int input = 0; srand((unsigned int)time(NULL)); do { ... ... } while (input); }
//game.h
#define EASY 10 #include<stdlib.h> #include<time.h> void SetMine(char board[ROWS][COLS], int row, int col);
//game.c
void SetMine(char board[ROWS][COLS], int row, int col) { int count = EASY; while (count) { int x = rand() % row+1; int y = rand() % col+1; if (board[x][y] == '0') { board[x][y] = '1'; count--; } } }
4、扫雷
//test.c
void game() { ... ... SetMine(mine, ROW, COL); FindMine(mine,show, ROW, COL); }
//game.h
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
//game.c
int GetMine(char mine[ROWS][COLS], int x, int y) { int count = 0; int i = 0; int j = 0; for (i = x-1;i <= x+1;i++) { for (j = y - 1;j <= y + 1;j++) { count = count + mine[i][j] - '0'; } } return count; } void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) { int x = 0; int y = 0; int win = 0; while (win < row * col - EASY) { printf("请输入排雷坐标:"); scanf("%d %d", &x, &y); if (1 <= x && x <= row && 1 <= y && y <= col) { if (mine[x][y] == '1') { printf("你被炸死了!\n"); DisplayBoard(mine, row, col); break; } else { int count = GetMine(mine,x,y); show[x][y] = count + '0'; DisplayBoard(show, row, col); win++; } } else printf("坐标非法,请重新输入!\n"); } if (win == row * col - EASY) { printf("恭喜你,排雷成功!\n"); DisplayBoard(mine, row, col); }
5、代码总览
//test.c
#define _CRT_SECURE_NO_WARNINGS #include "game.h" void welcome() { printf("******************************\n"); printf("********* 扫雷 *********\n"); printf("*** 1.play 0.exit ***\n"); printf("******************************\n"); } void game() { char mine[ROWS][COLS] = { 0 };//存雷 char show[ROWS][COLS] = { 0 };//显示 initial(mine, ROWS, COLS,'0'); initial(show, ROWS, COLS,'*');//数组初始化 DisplayBoard(show, ROW, COL);//打印棋盘 SetMine(mine, ROW, COL); FindMine(mine,show, ROW, COL); } void playgame() { int input = 0; srand((unsigned int)time(NULL)); do { welcome(); printf("请选择:>"); scanf("%d", &input); switch (input) { case 1: printf("开始游戏\n"); game(); break; case 0: printf("退出游戏\n"); break; default: printf("选择错误,请重新选择\n"); break; } } while (input); } int main() { playgame(); return 0; }
//game.h
#pragma once #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 #define EASY 10 #include<stdio.h> #include<stdlib.h> #include<time.h> void initial(char board[ROWS][COLS], int row, int col,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);
//game.c
#define _CRT_SECURE_NO_WARNINGS #include"game.h" void initial(char board[ROWS][COLS], int row, int col,char set) { int i = 0; int j = 0; for (i = 0;i < row;i++) { for (j = 0;j < col;j++) { board[i][j] = set; } } } void DisplayBoard(char board[ROWS][COLS], int row, int col) { int i = 0; int j = 0; for (i = 1;i <= row;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]); if (j < col) printf("|"); } printf("\n"); printf(" "); if (i < row ) { for (j = 1;j <= col;j++) { printf("---"); if (j < col) printf("|"); } printf("\n"); } } printf("\n"); } void SetMine(char board[ROWS][COLS], int row, int col) { int count = EASY; while (count) { int x = rand() % row+1; int y = rand() % col+1; if (board[x][y] == '0') { board[x][y] = '1'; count--; } } } int GetMine(char mine[ROWS][COLS], int x, int y) { int count = 0; int i = 0; int j = 0; for (i = x-1;i <= x+1;i++) { for (j = y - 1;j <= y + 1;j++) { count = count + mine[i][j] - '0'; } } return count; } void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) { int x = 0; int y = 0; int win = 0; while (win < row * col - EASY) { printf("请输入排雷坐标:"); scanf("%d %d", &x, &y); if (1 <= x && x <= row && 1 <= y && y <= col) { if (mine[x][y] == '1') { printf("你被炸死了!\n"); DisplayBoard(mine, row, col); break; } else { int count = GetMine(mine,x,y); show[x][y] = count + '0'; DisplayBoard(show, row, col); win++; } } else printf("坐标非法,请重新输入!\n"); } if (win == row * col - EASY) { printf("恭喜你,排雷成功!\n"); DisplayBoard(mine, row, col); } }