这篇文章主要介绍了如何使用C语言实现简易扫雷游戏,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
1、头文件
#define _CRT_SECURE_NO_WARNINGS//包含头文件#include <stdio.h>#include <stdlib.h>//定义数值#define ROW 9#define COL 9#define ROWS ROW+2#define COLS COL+2//定义雷的数量#define mine_num 10//函数定义//初始化void init_board(char board[ROWS][COLS], int row, int col, char set);//打印棋盘void print_board(char board[ROWS][COLS], int row, int col);//布雷void set_mine(char mine[ROWS][COLS], int row, int col);//扫雷void find_mine(char mine[ROWS][COLS], char board[ROWS][COLS], int row, int col);
2、游戏实现函数
#include "game.h"//初始化void init_board(char board[ROWS][COLS], int row, int col, char set) { int x = 0; int y = 0; for (x = 0; x < row; x++) { for (y = 0; y < col; y++) { board[x][y] = set; } }}//打印棋盘void print_board(char board[ROWS][COLS], int row, int col) { int x = 0; int y = 0; for (x = 0; x <= row; x++) { printf("%d ", x); } printf("\n"); for (x = 1; x <= row; x++) { printf("%d ", x); for (y = 1; y <= col; y++) { printf("%c ", board[x][y]); } printf("\n"); }}//布雷void set_mine(char mine[ROWS][COLS], int row, int col) { int count = mine_num; while (count) { int x = rand() % row + 1; int y = rand() % col + 1; if (mine[x][y] == '0') { mine[x][y] = '1'; count--; } }}//计算周围雷的数量int get_mine(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 find_mine(char mine[ROWS][COLS], char board[ROWS][COLS], int row, int col) { //获取坐标 int x = 0; int y = 0; int count = row * col - mine_num; while (count) { printf("请输入要排查的坐标>:\n"); scanf("%d%d", &x, &y); //判断坐标的合法性 if (x >= 1 && y >= 1 && x <= row && y <= col) { if (mine[x][y] == '1') { printf("是雷!!!炸死了\n"); print_board(mine, ROW, COL); break; } else { int num = get_mine(mine, x, y); board[x][y] = num + '0'; count--; print_board(board, ROW, COL); } } else { printf("你输入的坐标不合法,请重新输入\n"); } } if (count == 0) { printf("恭喜你排雷成功!!!\n"); }}
3、主程序
#include "game.h"//打印游戏菜单void menu() { printf("**************************\n"); printf("********* 扫 雷 ********\n"); printf("********* 1. play ********\n"); printf("********* 0. exit ********\n"); printf("**************************\n");}//游戏实现void game() { //创建数组存储雷和游戏棋盘 char mine[ROWS][COLS] = { 0 }; char board[ROWS][COLS] = { 0 }; //初始化布雷 init_board(mine, ROWS, COLS, '0'); //初始化游戏棋盘 init_board(board, ROWS, COLS, '*'); //打印布雷棋盘 //printf("------布雷棋盘------\n"); //print_board(mine, ROW, COL); //打印扫雷棋盘 printf("------扫雷棋盘------\n"); print_board(board, ROW, COL); //布雷 set_mine(mine, ROW, COL); printf("------布雷棋盘------\n"); //print_board(mine, ROW, COL); //扫雷 find_mine(mine, board, ROW, COL);}int main(void) { int num = 0; srand((unsigned int)time(NULL)); do { menu(); printf("请选择菜单!>:"); scanf("%d", &num); switch (num) { case 1: game(); break; case 0: printf("退出游戏\n"); break; default: printf("你的选择有误,请重新选择\n"); break; } } while (num); system("pause"); return 0;}
感谢你能够认真阅读完这篇文章,希望小编分享的“如何使用C语言实现简易扫雷游戏”这篇文章对大家有帮助,同时也希望大家多多支持编程网,关注编程网行业资讯频道,更多相关知识等着你来学习!