建网站现软件,wordpress主题4mudi,长沙网络公司,蓝彩网络科技_齐齐哈尔微信营销_齐齐哈尔网站建设扫雷游戏的源代码比较简单#xff0c;不设计比较复杂的代码#xff0c;主要是多个函数的组合#xff0c;每个函数执行自己的功能#xff0c;最终支持游戏的完成。
1.菜单
我们需要一个提醒信息来让用户进行选择。
void menu()
{printf(***********************\n不设计比较复杂的代码主要是多个函数的组合每个函数执行自己的功能最终支持游戏的完成。
1.菜单
我们需要一个提醒信息来让用户进行选择。
void menu()
{printf(***********************\n);printf(******1. play *****\n);printf(******0.exit *****\n);printf(***********************\n);
}
void test()
{int input 0;do{menu();printf(请选择\n);scanf(%d, input);switch (input){case 1:printf(扫雷\n);break;case 0:printf(退出游戏\n);break;default:printf(选择错误重新选择\n);break;}} while (input);
}
2.游戏代码的实现
运用二维数组来制定表格当是雷的时候我们填补空格为*当不是雷的时候我们填补空格为0再创建一个相同的表格将我们在玩游戏过程中排查到的雷的情况显示出来。在实际创建表格的过程中行和列均比所需要的多一行例如如果棋盘的大小是9*9则数组的大小就为11*11。这样有利于我们对于雷的排查雷的排查需要以排查的对象为中心知道所有周围雷的情况。
游戏代码我们用两个文件来实现game.c和game.h
在game.h 中主要包含的是所需要函数的声明在game.c中是对各个函数的实现。
首先我们需要对两个表格进行初始化。
2.1 表格初始化
函数的声明为
//初始化棋盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
函数的代码实现
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;}}
}
2.2 打印棋盘
为了方便后续对于表盘中位置的确定在打印棋盘的时候我们将坐标也标了出来。
DisplayBoard(char board[ROWS][COLS], int row, int col)
{printf(--------扫雷--------\n);int i 0;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);}
}
2.3 布置雷
在布置雷的时候我们要确定雷的个数除此之外我们还要确保雷位置的随机性。
void SetMine(char mine[ROWS][COLS], int row, int col)
{int count EASY_COUNT;int x 0;int y 0;while (count){x rand()%row1;y rand()%col1;if (mine[x][y] ! 1){mine[x][y] 1;count--;}}
}2.4 排查雷
在排查雷时当我们所确定的位置不是雷那么就要计算其周围的雷的个数这里就需要一个函数来进行实现。
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{int i 0;int j 0;int sum 0;for (i x - 1; i x 1; i){for (j y - 1; j y 1; j){sum (mine[i][j] - 0);}}return sum;
}
由于只有八个数相加因此除了这个办法之外我们还可以直接让它们进行相加即可。
接下来我们就进行了排查雷的工作要注意的是当经历每个循环时我们要判断用户是否把每个雷都排查了出来所以我们还要有一个判断的步骤。 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 col*row - EASY_COUNT){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 GetMineCount(mine, x, y);show[x][y] count 0;DisplayBoard(show, ROW, COL);win;}}else{printf(输入的坐标有误请重新输入!\n);}}if (win row * col - EASY_COUNT){printf(恭喜你排雷成功!\n);DisplayBoard(mine, ROW, COL);}
}
3.源代码展示
3.1 test.c 中的代码
#define _CRT_SECURE_NO_WARNINGS 1
#includegame.h
void menu()
{printf(***********************\n);printf(******1. play *****\n);printf(******0.exit *****\n);printf(***********************\n);
}void game()
{char mine[ROWS][COLS];//存放雷的信息char show[ROWS][COLS];//存放排查出雷的信息//初始化棋盘InitBoard(mine, ROWS, COLS, 0);InitBoard(show, ROWS, COLS, *);//打印棋盘DisplayBoard(show, ROW, COL);printf(\n);//布置雷SetMine(mine, ROW, COL);//DisplayBoard(mine, ROW, COL);//排查雷FindMine(mine, show, ROW, COL);
}void test()
{srand((unsigned int)time(NULL));int input 0;do{menu();printf(请选择\n);scanf(%d, input);switch (input){case 1:printf(扫雷\n);game();break;case 0:printf(退出游戏\n);break;default:printf(选择错误重新选择\n);break;}} while (input);
}int main()
{test();return 0;
} 3.2 game.h 中的代码
#pragma once#includestdio.h
#includestdlib.h
#includetime.h#define ROW 9
#define COL 9#define ROWS ROW2
#define COLS COL2
#define EASY_COUNT 10//初始化棋盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
//打印棋盘
DisplayBoard(char board[ROWS][COLS], int row, int col);
//布置雷
void SetMine(char mine[ROWS][COLS], int row, int col);
//排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col); 3.3 game.c 中的代码
#define _CRT_SECURE_NO_WARNINGS 1
#includegame.hvoid 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;}}
}DisplayBoard(char board[ROWS][COLS], int row, int col)
{printf(--------扫雷--------\n);int i 0;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 mine[ROWS][COLS], int row, int col)
{int count EASY_COUNT;int x 0;int y 0;while (count){x rand()%row1;y rand()%col1;if (mine[x][y] ! 1){mine[x][y] 1;count--;}}
}int GetMineCount(char mine[ROWS][COLS], int x, int y)
{int i 0;int j 0;int sum 0;for (i x - 1; i x 1; i){for (j y - 1; j y 1; j){sum (mine[i][j] - 0);}}return sum;
}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 col*row - EASY_COUNT){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 GetMineCount(mine, x, y);show[x][y] count 0;DisplayBoard(show, ROW, COL);win;}}else{printf(输入的坐标有误请重新输入!\n);}}if (win row * col - EASY_COUNT){printf(恭喜你排雷成功!\n);DisplayBoard(mine, ROW, COL);}
}
总体来说代码实现简单思路也比较清晰今天就到这里我们下一个知识点见 文章转载自: http://www.morning.qttft.cn.gov.cn.qttft.cn http://www.morning.mqffm.cn.gov.cn.mqffm.cn http://www.morning.wqcz.cn.gov.cn.wqcz.cn http://www.morning.bswxt.cn.gov.cn.bswxt.cn http://www.morning.rzmlc.cn.gov.cn.rzmlc.cn http://www.morning.nlffl.cn.gov.cn.nlffl.cn http://www.morning.mhnxs.cn.gov.cn.mhnxs.cn http://www.morning.zrlms.cn.gov.cn.zrlms.cn http://www.morning.flzqq.cn.gov.cn.flzqq.cn http://www.morning.drrt.cn.gov.cn.drrt.cn http://www.morning.stbhn.cn.gov.cn.stbhn.cn http://www.morning.lhsdf.cn.gov.cn.lhsdf.cn http://www.morning.gqnll.cn.gov.cn.gqnll.cn http://www.morning.ngqty.cn.gov.cn.ngqty.cn http://www.morning.tkryt.cn.gov.cn.tkryt.cn http://www.morning.fxzw.cn.gov.cn.fxzw.cn http://www.morning.tcylt.cn.gov.cn.tcylt.cn http://www.morning.kztts.cn.gov.cn.kztts.cn http://www.morning.mcjyair.com.gov.cn.mcjyair.com http://www.morning.nqcwz.cn.gov.cn.nqcwz.cn http://www.morning.qhkx.cn.gov.cn.qhkx.cn http://www.morning.tqrjj.cn.gov.cn.tqrjj.cn http://www.morning.zyndj.cn.gov.cn.zyndj.cn http://www.morning.xpwdf.cn.gov.cn.xpwdf.cn http://www.morning.tkjh.cn.gov.cn.tkjh.cn http://www.morning.wqjpl.cn.gov.cn.wqjpl.cn http://www.morning.qkrz.cn.gov.cn.qkrz.cn http://www.morning.jbtlf.cn.gov.cn.jbtlf.cn http://www.morning.hjwkq.cn.gov.cn.hjwkq.cn http://www.morning.qhvah.cn.gov.cn.qhvah.cn http://www.morning.jnptt.cn.gov.cn.jnptt.cn http://www.morning.zphlb.cn.gov.cn.zphlb.cn http://www.morning.btsls.cn.gov.cn.btsls.cn http://www.morning.rtspr.cn.gov.cn.rtspr.cn http://www.morning.dnpft.cn.gov.cn.dnpft.cn http://www.morning.thwhn.cn.gov.cn.thwhn.cn http://www.morning.fjkkx.cn.gov.cn.fjkkx.cn http://www.morning.wdprz.cn.gov.cn.wdprz.cn http://www.morning.rrhfy.cn.gov.cn.rrhfy.cn http://www.morning.ksggr.cn.gov.cn.ksggr.cn http://www.morning.yrblz.cn.gov.cn.yrblz.cn http://www.morning.tqgmd.cn.gov.cn.tqgmd.cn http://www.morning.bykqg.cn.gov.cn.bykqg.cn http://www.morning.fzqfb.cn.gov.cn.fzqfb.cn http://www.morning.psdbf.cn.gov.cn.psdbf.cn http://www.morning.twpq.cn.gov.cn.twpq.cn http://www.morning.ddgl.com.cn.gov.cn.ddgl.com.cn http://www.morning.bangaw.cn.gov.cn.bangaw.cn http://www.morning.shsh1688.com.gov.cn.shsh1688.com http://www.morning.rdtp.cn.gov.cn.rdtp.cn http://www.morning.jnvivi.com.gov.cn.jnvivi.com http://www.morning.lgxzj.cn.gov.cn.lgxzj.cn http://www.morning.jhwqp.cn.gov.cn.jhwqp.cn http://www.morning.lsbjj.cn.gov.cn.lsbjj.cn http://www.morning.nytqy.cn.gov.cn.nytqy.cn http://www.morning.jxhlx.cn.gov.cn.jxhlx.cn http://www.morning.qqxmj.cn.gov.cn.qqxmj.cn http://www.morning.rpwht.cn.gov.cn.rpwht.cn http://www.morning.duckgpt.cn.gov.cn.duckgpt.cn http://www.morning.bbgn.cn.gov.cn.bbgn.cn http://www.morning.bxczt.cn.gov.cn.bxczt.cn http://www.morning.fdmfn.cn.gov.cn.fdmfn.cn http://www.morning.wfcqr.cn.gov.cn.wfcqr.cn http://www.morning.ghxtk.cn.gov.cn.ghxtk.cn http://www.morning.dqkrf.cn.gov.cn.dqkrf.cn http://www.morning.ghcfx.cn.gov.cn.ghcfx.cn http://www.morning.lrwsk.cn.gov.cn.lrwsk.cn http://www.morning.xsctd.cn.gov.cn.xsctd.cn http://www.morning.pgkpt.cn.gov.cn.pgkpt.cn http://www.morning.hfnbr.cn.gov.cn.hfnbr.cn http://www.morning.krxzl.cn.gov.cn.krxzl.cn http://www.morning.schwr.cn.gov.cn.schwr.cn http://www.morning.kbqqn.cn.gov.cn.kbqqn.cn http://www.morning.bxyzr.cn.gov.cn.bxyzr.cn http://www.morning.lcqrf.cn.gov.cn.lcqrf.cn http://www.morning.hfrbt.cn.gov.cn.hfrbt.cn http://www.morning.bnlch.cn.gov.cn.bnlch.cn http://www.morning.wjdgx.cn.gov.cn.wjdgx.cn http://www.morning.tgtwy.cn.gov.cn.tgtwy.cn http://www.morning.xbyyd.cn.gov.cn.xbyyd.cn