C語言實現(xiàn)掃雷小游戲
更新時間:2020年03月30日 16:38:01 作者:Stephen_curry_66
這篇文章主要為大家詳細介紹了C語言實現(xiàn)掃雷小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C語言實現(xiàn)掃雷游戲的具體代碼,供大家參考,具體內容如下
主函數(shù):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;
}
子函數(shù):game.c
#include "game.h"
void game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
memset(mine, '0', sizeof(mine));//初始化數(shù)組置為0
memset(show, '*', sizeof(show));//初始化數(shù)組置為*
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)//聲明布雷函數(shù)
{
int count = NUM;//設置計數(shù)器,統(tǒng)計布雷的個數(shù)
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)//聲明打印棋盤函數(shù)
{
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)//雷數(shù)統(tǒng)計
{
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);
}
}
}
}
函數(shù)聲明: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 //雷數(shù) 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
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C語言中對于循環(huán)結構優(yōu)化的一些入門級方法簡介
這篇文章主要介紹了C語言中對于循環(huán)結構優(yōu)化的一些入門級方法,包括算法設計的改進來提高一些并行性等方法,要的朋友可以參考下2015-12-12

