本文实例为大家分享了C语言实现扫雷游戏的具体代码,供大家参考,具体内容如下
game.h
#include<stdio.h> #include<stdlib.h> #include<time.h> #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 #define EASY 10 //初始化棋盘 void InitBoard(char board[ROWS][COLS], int rows, int cols,char set); void PrintBoard(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
#include"game.h" //初始化棋盘 void InitBoard(char board[ROWS][COLS], int rows, int cols, char set) { int i = 0; int j = 0; for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { board[i][j] = set; } } } //打印棋盘 void PrintBoard(char board[ROWS][COLS], int row, int col) { int i = 1; int j = 1; printf("-------------------\n"); for (i = 0; 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]); } printf("\n"); } printf("-------------------\n"); } //放雷 void SetMine(char board[ROWS][COLS], int row, int col) { int count = EASY; while (count) { int x = rand() % 9 + 1;//1-9 int y = rand() % 9 + 1;//1-9 if (board[x][y] == '0') { board[x][y] = '1'; count--; } } } //查坐标四周雷的个数 int GetMineCount(char mine[ROWS][COLS], int x, int y) { //方法1: /*int i = 0; int j = 0; int count = 0; for (i = x - 1; i <= x + 1; i++) { for (j = y - 1; j <= y + 1; j++) { if (mine[i][j] == '1') { count++; } } } return count;*/ //方法2: 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 num = 0; while (num < row*col - EASY) { int x = 0; int y = 0; printf("请输入坐标:>"); scanf("%d%d", &x, &y); //判断坐标是否合法 if (x >= 1 && x <= row&&y >= 1 && y <= col) { if (mine[x][y] == '0') { int count = GetMineCount(mine, x, y); show[x][y] = count+'0'; PrintBoard(show, row, col); num++; } else { printf("很遗憾,您已被炸死!\n"); PrintBoard(mine, row, col); break; } } else { printf("坐标不合法\n"); } if (num == row*col - EASY) { printf("排雷成功!\n"); break; } } }
test.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 }; srand((unsigned int)time(NULL));//设置随机数生成起点 //初始化棋盘 InitBoard(mine, ROWS, COLS, '0'); InitBoard(show, ROWS, COLS, '*'); //PrintBoard(mine, ROW, COL);//无需打印 PrintBoard(show, ROW, COL); //1.放雷 SetMine(mine, ROW, COL); //PrintBoard(mine, ROW, COL);//test //2.扫雷 FindMine(mine, show, ROW, COL); } int main() { int input = 0; do { menu(); printf("请输入:>"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf("退出游戏\n"); break; default: printf("输入错误,请重新输入!\n"); break; } } while (input); return 0; }
输出结果:
更多有趣的经典小游戏实现专题,分享给大家:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。
- 本文固定链接: https://zxbcw.cn/post/196189/
- 转载请注明:必须在正文中标注并保留原文链接
- QQ群: PHP高手阵营官方总群(344148542)
- QQ群: Yii2.0开发(304864863)