本文实例为大家分享了C语言实现扫雷游戏的具体代码,供大家参考,具体内容如下
主函数:main.c
#include "game.h" void Menu() { printf("##########################\n"); printf("##1.play 0.exit##########\n"); printf("##########################\n"); printf("## Please Enter select! ##\n"); } int main() { Menu(); srand((unsigned int)time(NULL)); int quit = 0; while (!quit) { int select = 0; printf("请输入你的选择:\n"); scanf("%d", &select); switch (select) { case 1: game(); break; case 2: quit = 1; break; default : printf("你输入有误,请重新输入:\n"); break; } } printf("Bye Bye!\n"); system("pause"); return 0; }
子函数:game.c
#include "game.h" void game() { char mine[ROWS][COLS] = { 0 }; char show[ROWS][COLS] = { 0 }; memset(mine, '0', sizeof(mine));//初始化数组置为0 memset(show, '*', sizeof(show));//初始化数组置为* int no_y, no_x; set_mine(mine,ROWS,COLS,&no_x,&no_y);//布雷 ‘1'表示雷 int x = 0; int y = 0; int time = 100 - NUM; while (time > 0) { system("cls");//清屏 Show(show, ROWS, COLS);//打印 棋盘 printf("请输入坐标:\n"); scanf("%d%d", &x, &y); if (x<1 || x>10 || y<1 || y>10) { printf("你输入有误,请重新输入:\n"); continue; } if (show[x][y] != '*') { printf("你输入有误,请重新输入:\n"); continue; } if (mine[x][y] == '1') { if (time == 80)//如果第一次有雷,用一个没雷的与这个交换 { mine[x][y] = '0'; mine[no_y][no_y] = '1'; } else { printf("game over!\n"); Show(mine, ROWS, COLS); break; } } show[x][y] = get_mine_count(mine, x, y) + '0'; Expand(mine, show, x, y); time--; } } void set_mine(char mine[ROWS][COLS],int col,int row,int *no_x,int *no_y)//声明布雷函数 { int count = NUM;//设置计数器,统计布雷的个数 while (count > 0) { int x = rand() % (col-2) + 1; int y = rand() % (col-2) + 1; if ((mine[x][y]) == '0') { mine[x][y] = '1'; count--; } } for (int i = 1; i <= 10; i++) { for (int j = 1; i <= 10; j++) { if (mine[i][j] == '0') { no_x = i; no_y = j; return; } } } } void Show(char mine[ROWS][COLS], int row, int col)//声明打印棋盘函数 { int i = 0; int j = 0; printf(" "); for (i = 1; i <= 10 ; i++) { printf("%2d |", i); } printf("\n"); for (i = 1; i <= 11; i++) { printf("----"); } printf("\n"); for (i = 1; i <= 10 ; i++) { printf("%2d |", i); for (j = 1; j <= 10; j++) { printf("%2c |", mine[i][j]); } printf("\n"); for (int i = 1; i <= 11; i++) { printf("----"); } printf("\n"); } } int get_mine_count(char mine[ROWS][COLS], int x, int y)//雷数统计 { return mine[x - 1][y - 1]+ mine[x][y - 1] +\ mine[x + 1][y - 1]+ mine[x + 1][y]\ + mine[x + 1][y + 1]+ mine[x][y + 1] + \ + mine[x - 1][y + 1]+ mine[x - 1][y] - 8 * '0'; } void Expand(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y) { if (x >= 1 && x <= 10 && y >= 1 && y <= 10) { if (get_mine_count(mine, x, y) + '0' == '0') //表示x,y周围没雷 { show[x][y] = '0'; if (show[x - 1][y - 1] == '*') { Expand(mine, show, x - 1, y - 1); } if (show[x - 1][y] == '*') { Expand(mine, show, x - 1, y ); } if (show[x - 1][y + 1] == '*') { Expand(mine, show, x - 1, y + 1); } if (show[x ][y - 1] == '*') { Expand(mine, show, x , y - 1); } if (show[x][y + 1] == '*') { Expand(mine, show, x , y + 1); } if (show[x + 1][y - 1] == '*') { Expand(mine, show, x + 1, y - 1); } if (show[x + 1][y] == '*') { Expand(mine, show, x + 1, y ); } if (show[x + 1][y + 1] == '*') { Expand(mine, show, x + 1, y + 1); } } } }
函数声明:game.h
#ifndef _GAME_H_ #define _GAME_H_ #include<stdio.h> #include<Windows.h> #pragma warning(disable:4996) #include<time.h> #include<string.h> #define ROWS 12 #define COLS 12 #define NUM 20 //雷数 void game(); void set_mine(char mine[ROWS][COLS],int row, int col, int *no_x, int *no_y); void Show(char mine[ROWS][COLS], int row, int col); int get_mine_count(char mine[ROWS][COLS], int x, int y); void Expand(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y); #endif
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。