游戏设计的思路:
如图9*9的扫雷棋盘
首先我们可以用二维数组来实现对数据的存储,并通过雷与非雷存储的数据的不同加以区分(这里我用的是字符’1‘和’0‘)
为了方便我们用两个二维数组,一个用来布置雷,另一个来给玩家展示
这里我们选择用char类型的二维数组
为了防止在统计周围雷的个数时超出数组的范围,如在统计坐标(1,1)的位置时,上面和左边就越界了,所以我们可以用11*11的二维数组,然后给玩家显示9*9的棋盘。
代码的实现:
(前面是部分代码的实现,最后有完整的代码)
1.游戏进入是菜单的打印:
代码:
void menu() { printf("-------------------\n"); printf("*******************\n"); printf("******1.play*******\n"); printf("*******************\n"); printf("******0.exit*******\n"); printf("*******************\n"); printf("-------------------\n"); } int main() { srand((unsigned int)time(NULL)); int input = 0; do { menu();//打印菜单 printf("请输入你的选择:> \n"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf("退出游戏\n"); break; default: printf("输入错误,请重新选择\n"); break; } } while (input); return 0; }
2.数组的创建与初始化:
代码:
//存放布置好的的雷 char Mine[ROWS][COLS]; //存放排查好的雷 char Show[ROWS][COLS]; //初始化棋盘 //Mine 最开始全部放'0' //Show 最开始全部放'*' InitBoard(Mine, ROWS, COLS,'0'); InitBoard(Show, ROWS, COLS,'*'); void InitBoard(char Board[ROWS][COLS], int rows, int cols, char set) { int i = 0; for (i = 0; i < rows; i++) { int j = 0; for (j = 0; j < cols; j++) { Board[i][j] = set; } } }
3.棋盘的打印
代码:
//打印棋盘 void DisplayBoard(char Board[ROWS][COLS], int row, int col) { int i = 0; printf("------扫雷游戏-----\n"); for (i = 0; i <= col; ++i) { printf("%d ", i); } printf("\n"); for (i = 1; i <= row; i++) { printf("%d ",i); int j = 0; for (j = 1; j <= col; j++) { printf("%c ",Board[i][j]); } printf("\n"); } }
4.布置雷(这里简单点,用时间戳,电脑实随机布置10个,如下图运行结果中的字符 ’1‘ )
代码:
//布置雷 void SetMine(char Board[ROWS][COLS], int row, int col) { int count = MineCount; while (count) { int x = rand() % row + 1; int y = rand() % col + 1; if (Board[x][y] == '0') { Board[x][y] = '1'; count--; } } }
5.排雷的实现
代码:
//排雷 int CountMine(char Mine[ROWS][COLS],int x,int y) { return (Mine[x-1][y-1]+ Mine[x-1][y]+ Mine[x-1][y+1]+ Mine[x][y-1] +Mine[x][y+1]+ Mine[x+1][y-1]+Mine[x+1][y]+Mine[x+1][y+1])-8*'0'; } void FindMine(char Mine[ROWS][COLS],char Show[ROWS][COLS], int row, int col) { int x = 0; int y = 0; int Win = ROW*COL-MineCount; while (Win>0) { printf("请输入你要排查雷的坐标:>\n"); scanf("%d %d",&x,&y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (Mine[x][y] == '1') { printf("你被炸死了,游戏结束\n"); DisplayBoard(Mine, ROW, COL); break; } else { int count = CountMine(Mine, x, y); Show[x][y] = count + '0'; DisplayBoard(Show, ROW, COL); Win--; } } else { printf("输入坐标已超出棋盘,请吃重新输入\n"); } } if (Win == 0) { printf("恭喜你,赢了\n"); DisplayBoard(Mine, ROW, COL); } }
完整代码:
1.游戏函数的申明
Game.h文件
//Game.h文件 #pragma once #include <stdio.h> #include <stdlib.h> #include <time.h> #define ROW 9 #define COL 9 #define MineCount 10 #define ROWS ROW+2 #define COLS COL+2 void InitBoard(char Board[ROWS][COLS],int rows,int cols,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);
2.游戏函数的实现
Game.c 文件
1. ///Game.c文件 #define _CRT_SECURE_NO_WARNINGS 1 #include "game.h" //初始化棋盘 void InitBoard(char Board[ROWS][COLS], int rows, int cols, char set) { int i = 0; for (i = 0; i < rows; i++) { int j = 0; for (j = 0; j < cols; j++) { Board[i][j] = set ; } } } //打印棋盘 void DisplayBoard(char Board[ROWS][COLS], int row, int col) { int i = 0; printf("------扫雷游戏-----\n"); for (i = 0; i <= col; ++i) { printf("%d ", i); } printf("\n"); for (i = 1; i <= row; i++) { printf("%d ",i); int j = 0; for (j = 1; j <= col; j++) { printf("%c ",Board[i][j]); } printf("\n"); } } //布置雷 void SetMine(char Board[ROWS][COLS], int row, int col) { int count = MineCount; while (count) { int x = rand() % row + 1; int y = rand() % col + 1; if (Board[x][y] == '0') { Board[x][y] = '1'; count--; } } } //排雷 int CountMine(char Mine[ROWS][COLS],int x,int y) { return (Mine[x-1][y-1]+ Mine[x-1][y]+ Mine[x-1][y+1]+ Mine[x][y-1] +Mine[x][y+1]+ Mine[x+1][y-1]+Mine[x+1][y]+Mine[x+1][y+1])-8*'0'; } void FindMine(char Mine[ROWS][COLS],char Show[ROWS][COLS], int row, int col) { int x = 0; int y = 0; int Win = ROW*COL-MineCount; while (Win>0) { printf("请输入你要排查雷的坐标:>\n"); scanf("%d %d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (Mine[x][y] == '1') { printf("你被炸死了,游戏结束\n"); DisplayBoard(Mine, ROW, COL); break; } else { int count = CountMine(Mine, x, y); Show[x][y] = count + '0'; DisplayBoard(Show, ROW, COL); Win--; } } else { printf("输入坐标已超出棋盘,请吃重新输入\n"); } } if (Win == 0) { printf("恭喜你,赢了\n"); DisplayBoard(Mine, ROW, COL); }
3.游戏代码实现
test.c文件
//test.c文件 #define _CRT_SECURE_NO_WARNINGS 1 #include "game.h" void menu() { printf("-------------------\n"); printf("*******************\n"); printf("******1.play*******\n"); printf("*******************\n"); printf("******0.exit*******\n"); printf("*******************\n"); printf("-------------------\n"); } void game() { //存放布置好的的雷 char Mine[ROWS][COLS]; //存放排查好的雷 char Show[ROWS][COLS]; //初始化棋盘 //Mine 最开始全部放'0' //Show 最开始全部放'*' InitBoard(Mine, ROWS, COLS, '0'); InitBoard(Show, ROWS, COLS, '*'); //打印棋盘 DisplayBoard(Mine, ROW, COL); DisplayBoard(Show, ROW, COL); //布置雷,有雷放'1' SetMine(Mine,ROW,COL); DisplayBoard(Mine, ROW, COL); 排雷 //FindMine(Mine,Show,ROW,COL); } int main() { srand((unsigned int)time(NULL)); int input = 0; do { menu();//打印菜单 printf("请输入你的选择:> \n"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf("退出游戏\n"); break; default: printf("输入错误,请重新选择\n"); break; } } while (input); return 0; }