基于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),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06C++?MiniZip實現(xiàn)目錄壓縮與解壓的示例詳解
Zlib是一個開源的數(shù)據(jù)壓縮庫,提供了一種通用的數(shù)據(jù)壓縮和解壓縮算法,本文主要為大家詳細介紹了如何利用Zlib實現(xiàn)目錄壓縮與解壓,需要的小伙伴可以參考下2023-11-11include包含頭文件的語句中,雙引號和尖括號的區(qū)別(詳解)
下面小編就為大家?guī)硪黄猧nclude包含頭文件的語句中,雙引號和尖括號的區(qū)別(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07