欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于C語言實現(xiàn)簡單的掃雷游戲

 更新時間:2022年05月09日 09:59:28   作者:愛彈吉他的小奔同學  
windows自帶的游戲《掃雷》是陪伴了無數(shù)人的經(jīng)典游戲,本文將利用C語言實現(xiàn)這一經(jīng)典的游戲,文中的示例代碼講解詳細,感興趣的可以學習一下

效果展示

開始的界面

輸入0結束程序

輸入1開始游戲

選擇標記地雷或者選擇踩坐標

輸入0標記地雷模式

輸入坐標

輸入1踩坐標模式

輸入坐標

在輸入坐標處輸入0 0結束游戲

踩到炸彈,出現(xiàn)炸彈位置

(1表示炸彈的位置,0表示沒有炸彈的位置)

輸入0結束程序

輸入1重新開始游戲

勝利

輸入0結束程序

輸入1重新開始游戲

代碼

我創(chuàng)建了兩個.c源文件,一個.h頭文件

test.c

#define _CRT_SECURE_NO_WARNINGS

#include"game.h"

int main()
{
	int exi = 0;
	srand((unsigned int)time(NULL));
	board();
	printf("請輸入是否開始游戲:>");
	scanf("%d", &exi);
	do
	{
		switch (exi)
		{
		case 1:
		{
			game();
			printf("是否輸入1重新開始游戲:>");
			scanf("%d", &exi);
			if (exi == 0)
			{
				printf("游戲結束");
			}
			break;
		}
		case 0:
		{
			printf("游戲結束");
			break;
		}
		default:
		{
			printf("輸入錯誤,請重新輸入:>");
			scanf("%d", &exi);
			if (exi == 0)
			{
				printf("游戲結束\n");
			}
			break;
		}
		}
	} while (exi);



	return 0;
}

game.h

#pragma once

#include<stdio.h>
#include<time.h>
#include<stdlib.h>

#define WID 9
#define LON 9
#define WIDS WID+2
#define LONS LON+2
#define RAN 5

void board();
//打印開始的面板

void game();
//游戲運行的起點

void initialization(char mane[WIDS][LONS], char siz, int x, int y);
//把數(shù)組內(nèi)框初始化為siz

void display(char mane[WIDS][LONS], int x, int y);
//打印數(shù)組內(nèi)框的字符

void random(char mane[WIDS][LONS], int count);
//在數(shù)組中隨機賦予count個炸彈

int look(char mane[WIDS][LONS], int x, int y);
//計算mane數(shù)組x,y位置周圍有多少炸彈

void judge(char mane[WIDS][LONS], char show[WIDS][LONS],char include[WIDS][LONS]);
//判斷輸入是否獲得勝利

void xunhuan(char mane[WIDS][LONS], char show[WIDS][LONS], char include[WIDS][LONS], int X, int Y);
//判斷周圍沒有雷,會向外繼續(xù)推,直到出現(xiàn)雷

void change(char show[WIDS][LONS], int x, int y, char siz);
//改變數(shù)組show位置(x,y)為字符siz

void jishu();
//統(tǒng)計選擇了幾次的位置,包括類推的位置,實現(xiàn)一點出現(xiàn)一大片的功能

game掃雷.c

#define _CRT_SECURE_NO_WARNINGS

#include"game.h"

static int a = 0;


void board()
{
	printf("****************************\n");
	printf("****************************\n");
	printf("********* 1.play  **********\n");
	printf("********* 0.exit  **********\n");
	printf("****************************\n");
	printf("****************************\n");

}

//數(shù)組初始化
void initialization(char mane[WIDS][LONS], char siz, int x, int y)
{
	int i = 0;
	for (i = 0; i <= x+1; i++)
	{
		int j = 0;
		for (j = 0; j <= y+1; j++)
		{
			mane[i][j] = siz;
		}

	}
}


//打印第一個面板
void display(char mane[WIDS][LONS], int x,int y)
{
	int i = 0;
	int j = 0;
	printf("-----------掃雷-----------\n");
	printf("0 | ");

	for (j = 1; j <= y; j++)
	{
		printf("%d ",j);
	}
	printf("\n");
	printf("- - -");

	for (j = 1; j <= y; j++)
	{
		printf(" -");
	}


	for (i = 1; i <= x; i++)
	{
		printf("\n");
		printf("%d | ",i);
		for (j = 1; j <= y; j++)
		{
			printf("%c ", mane[i][j]);
		}

	}
	printf("\n-----------掃雷-----------\n");
}



void random(char mane[WIDS][LONS],int count)
{
	int x = 0;
	int y = 0;
	while (count)
	{
		x = rand() % WID + 1;
		y = rand() % LON + 1;
		if (mane[x][y] == '0')
		{
			mane[x][y] = '1';
			count--;
		}

	}

}

int look(char mane[WIDS][LONS],int x,int y)
{
	return mane[x][y + 1] +
		mane[x][y - 1] +
		mane[x - 1][y + 1] +
		mane[x - 1][y - 1] +
		mane[x + 1][y + 1] +
		mane[x + 1][y - 1] +
		mane[x - 1][y] +
		mane[x + 1][y]-8*'0';


}

void jishu()
{
	a++;
}

void xunhuan(char mane[WIDS][LONS], char show[WIDS][LONS], char include[WIDS][LONS],int X,int Y)
{
	if (include[X][Y] != '1')
	{
		int count = 0;
		count = look(mane, X, Y);
		show[X][Y] = count + '0';
		include[X][Y] = '1';
		jishu();
		if (count == 0)
		{
			
			xunhuan(mane, show, include, X + 1, Y + 1);
			xunhuan(mane, show, include, X - 1, Y - 1);
			xunhuan(mane, show, include, X + 1, Y);
			xunhuan(mane, show, include, X - 1, Y);
			xunhuan(mane, show, include, X, Y + 1);
			xunhuan(mane, show, include, X, Y - 1);
			xunhuan(mane, show, include, X + 1, Y - 1);
			xunhuan(mane, show, include, X - 1, Y + 1);
		}
	
	}

}

void change(char show[WIDS][LONS], int x, int y,char siz)
{
	show[x][y] = siz;

}

void judge(char mane[WIDS][LONS], char show[WIDS][LONS], char include[WIDS][LONS])
{
	int X = 0;
	int Y = 0;
	display(show, WID, LON);


	do
	{
		int num = a;

		if (num == WID * LON - RAN)
		{
			printf("恭喜你獲得勝利!\n\n");
			display(mane, WID, LON);

			break;
		}


		printf("想要標記地雷就輸入0,想要選擇就輸入1):>");
		int choose = 0;
		scanf("%d", &choose);
		printf("\n");

		if (choose==1)
		{
			printf("輸入0 0結束游戲\n");

			printf("請輸入你選擇的坐標:>");

			scanf("%d%d", &X, &Y);

			if (X == 0 && Y == 0)
			{
				printf("\n結束此次游戲\n\n");
				break;
			}

			if (X >= 1 && X <= 9 && Y >= 1 && Y <= 9)
			{
				if (mane[X][Y] == '1')
				{
					printf("\n你吃到炸彈啦,死翹翹了\n\n");
					display(mane, WID, LON);
					break;
				}
				else
				{
					xunhuan(mane, show, include, X, Y);
					display(show, WID, LON);

					//display(mane, WID, LON);
				}
			}
			else
			{
				printf("\n你輸?shù)某^范圍啦,");
			}
		}
		else
		{

			printf("\n輸入0 0結束游戲\n");

			printf("請輸入你選擇的坐標:>");

			scanf("%d%d", &X, &Y);

			if (X == 0 && Y == 0)
			{
				printf("\n結束此次游戲\n\n");
				break;
			}
			change(show,X,Y,'F');
			display(show, WID, LON);

		}
	} while (1);


}

void chu(char mane[WIDS][LONS], char siz,int x, int y)
{
	int i = 0;
	for (i = 1; i <= x ; i++)
	{
		int j = 0;
		for (j = 1; j <= y ; j++)
		{
			mane[i][j] = siz;
		}

	}

}

void game()
{
	char mane[WIDS][LONS];
	char show[WIDS][LONS];
	char include[WIDS][LONS];

	initialization(mane, '0', WID, LON);
	initialization(show, '*', WID, LON);
	initialization(include, '1', WID, LON);

	chu(include, '0', WID, LON);

	random(mane,RAN);

	//display(mane, WID, LON);
	//display(show, WID, LON);
	
	judge(mane,show,include);
}

我寫的這個小游戲還很粗糙,不過才開始學,進步空間還是很大的,代碼就上傳到gitee

以上就是基于C語言實現(xiàn)簡單的掃雷游戲的詳細內(nèi)容,更多關于C語言 掃雷的資料請關注腳本之家其它相關文章!

相關文章

  • C++如何實現(xiàn)BCD碼和ASCII碼的相互轉(zhuǎn)換

    C++如何實現(xiàn)BCD碼和ASCII碼的相互轉(zhuǎn)換

    這篇文章主要介紹了C++實現(xiàn)BCD碼和ASCII碼互轉(zhuǎn),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • C語言舉例講解轉(zhuǎn)義字符的使用

    C語言舉例講解轉(zhuǎn)義字符的使用

    轉(zhuǎn)義字符是很多程序語言、數(shù)據(jù)格式和通信協(xié)議的形式文法的一部分。對于一個給定的字母表,一個轉(zhuǎn)義字符的目的是開始一個字符序列,使得轉(zhuǎn)義字符開頭的該字符序列具有不同于該字符序列單獨出現(xiàn)(沒有轉(zhuǎn)義字符開頭)時的語義。因此轉(zhuǎn)義字符開頭的字符序列被叫做轉(zhuǎn)義序列
    2022-05-05
  • 基于Windows API分解路徑問題的詳解

    基于Windows API分解路徑問題的詳解

    本篇文章是對Windows API分解路徑進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • C++?MiniZip實現(xiàn)目錄壓縮與解壓的示例詳解

    C++?MiniZip實現(xiàn)目錄壓縮與解壓的示例詳解

    Zlib是一個開源的數(shù)據(jù)壓縮庫,提供了一種通用的數(shù)據(jù)壓縮和解壓縮算法,本文主要為大家詳細介紹了如何利用Zlib實現(xiàn)目錄壓縮與解壓,需要的小伙伴可以參考下
    2023-11-11
  • include包含頭文件的語句中,雙引號和尖括號的區(qū)別(詳解)

    include包含頭文件的語句中,雙引號和尖括號的區(qū)別(詳解)

    下面小編就為大家?guī)硪黄猧nclude包含頭文件的語句中,雙引號和尖括號的區(qū)別(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • C++實現(xiàn)簡單通訊錄

    C++實現(xiàn)簡單通訊錄

    這篇文章主要為大家詳細介紹了C++實現(xiàn)簡單通訊錄,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • C++中模板和STL介紹詳解

    C++中模板和STL介紹詳解

    今天小編就為大家分享一篇關于C++模板和STL的介紹,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2021-09-09
  • c語言實現(xiàn)含遞歸清場版掃雷游戲

    c語言實現(xiàn)含遞歸清場版掃雷游戲

    掃雷大家應該都玩過,這是一個十分經(jīng)典的游戲,下面這篇文章主要給大家介紹了關于c語言實現(xiàn)含遞歸清場版掃雷游戲的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2021-11-11
  • C++ tuple元組的基本用法(總結)

    C++ tuple元組的基本用法(總結)

    這篇文章主要介紹了C++ tuple元組的基本用法(總結),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-07-07
  • C++中的循環(huán)引用

    C++中的循環(huán)引用

    雖然C++11引入了智能指針的,但是開發(fā)人員在與內(nèi)存的斗爭問題上并沒有解放,如果我門實用不當仍然有內(nèi)存泄漏問題,其中智能指針的循環(huán)引用缺陷是最大的問題。下面通過實例代碼給大家介紹c++中的循環(huán)引用,一起看看吧
    2017-09-09

最新評論