C語言實(shí)現(xiàn)簡易掃雷小游戲
我們經(jīng)常在電腦上面玩的掃雷游戲,很考驗(yàn)我們的判斷能力,但是實(shí)現(xiàn)一個(gè)掃雷游戲并不是很困難,只要多注意一些細(xì)節(jié)就好,就可以將一個(gè)簡單的掃雷游戲?qū)懗鰜恚?/p>
接下來先介紹掃雷游戲要實(shí)現(xiàn)的功能:
首先,要對(duì)雷陣進(jìn)行初始化,在初始化的時(shí)候要注意要定義兩個(gè)數(shù)組,一個(gè)是讓我們掃雷的陣,另外一個(gè)就是顯示某一個(gè)地方的周圍的雷的總個(gè)數(shù)的矩陣,在初始化的時(shí)候要注意為了避免傳址的問題,我們把它寫在主函數(shù)里面。
char mine[rows][cols]; char show[rows][cols]; int i = 0; int j = 0; for (i = 0; i < rows - 1; i++) { for (j = 0; j < cols - 1; j++) { mine[i][j] = '0'; show[i][j] = '*'; } }
接下來就是電腦在隨機(jī)布局雷陣的函數(shù),這個(gè)函數(shù)要用到rand() 函數(shù),來產(chǎn)生隨機(jī)值,在雷陣?yán)锩骐S機(jī)布雷。
void set_mine(char mine[rows][cols]) { int count = Count; int x = 0; int y = 0; srand((unsigned)time(NULL)); while (count) { x = rand() % 9 + 1; y = rand() % 9 + 1; if (mine[x][y] == '0') { mine[x][y] = '1'; count--; } } }
再有就是計(jì)算雷的個(gè)數(shù)的函數(shù),要講某一個(gè)坐標(biāo)位置的周圍8個(gè)位置的雷的個(gè)數(shù)算出來,并且將個(gè)數(shù)顯示出來
int get_num(char mine[rows][cols], int x, int y) { int count = 0; if (mine[x - 1][y - 1] == '1')//左上方 { count++; } if (mine[x - 1][y] == '1')//左邊 { count++; } if (mine[x - 1][y + 1] == '1')//左下方 { count++; } if (mine[x][y - 1] == '1')//上方 { count++; } if (mine[x][y + 1] == '1')//下方 { count++; } if (mine[x + 1][y - 1] == '1')//右上方 { count++; } if (mine[x + 1][y] == '1')//右方 { count++; } if (mine[x + 1][y + 1] == '1')//右下方 { count++; } return count; }
將掃雷函數(shù)的各個(gè)函數(shù)都實(shí)現(xiàn)了之后,我們來看一下完整的代碼
頭文件game.h
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> #include<time.h> #include<string.h> #define rows 11 #define cols 11 #define Count 10 int menu();//菜單函數(shù) void display(char show[rows][cols]); int Game(char mine[rows][cols],char show[rows][cols]);//游戲 void set_mine(char mine[rows][cols]);//設(shè)置雷的位置 int Sweep(char mine[rows][cols], char show[rows][cols]);//開始掃雷 int get_num(char mine[rows][cols], int x, int y);//計(jì)算雷的個(gè)數(shù)
實(shí)現(xiàn)函數(shù) game.c
#include"game.h" //菜單函數(shù) int menu() { printf("********************************************\n"); printf("********************************************\n"); printf("*************welcome to saolei*************\n"); printf("************* 1. play *************\n"); printf("************* 0. exit *************\n"); printf("********************************************\n"); printf("********************************************\n"); return 0; } //設(shè)置雷的位置 void set_mine(char mine[rows][cols]) { int count = Count; int x = 0; int y = 0; srand((unsigned)time(NULL)); while (count) { x = rand() % 9 + 1; y = rand() % 9 + 1; if (mine[x][y] == '0') { mine[x][y] = '1'; count--; } } } //打印下棋完了顯示的界面 void display(char show[rows][cols]) { int i = 0; int j = 0; printf(" "); for (i = 1; i < cols - 1; i++) { printf(" %d ", i); } printf("\n"); for (i = 1; i < rows - 1; i++) { printf("%d", i); for (j = 1; j < cols - 1; j++) { printf(" %c ", show[i][j]); } printf("\n"); } } //計(jì)算雷的個(gè)數(shù) int get_num(char mine[rows][cols], int x, int y) { int count = 0; if (mine[x - 1][y - 1] == '1')//左上方 { count++; } if (mine[x - 1][y] == '1')//左邊 { count++; } if (mine[x - 1][y + 1] == '1')//左下方 { count++; } if (mine[x][y - 1] == '1')//上方 { count++; } if (mine[x][y + 1] == '1')//下方 { count++; } if (mine[x + 1][y - 1] == '1')//右上方 { count++; } if (mine[x + 1][y] == '1')//右方 { count++; } if (mine[x + 1][y + 1] == '1')//右下方 { count++; } return count; } //掃雷 int Sweep(char mine[rows][cols], char show[rows][cols]) { int count = 0; int x = 0; int y = 0; while (count!=((rows-2)*(cols-2)-Count)) { printf("請(qǐng)輸入坐標(biāo):\n"); scanf("%d%d", &x, &y); if (mine[x][y] == '1') { printf("你踩到雷了!\n"); return 0; } else { int ret = get_num(mine, x, y); show[x][y] = ret + '0'; //set_mine(mine); display(show); count++; } } printf("恭喜你贏了!\n"); display(mine); return 0; } //游戲 int Game(char mine[rows][cols],char show[rows][cols]) { set_mine(mine); display(show); //display(mine);//可以將雷的位置顯示出來 Sweep(mine,show); return 0; }
最后就是測試函數(shù) text.c
#include"game.h" int main() { int input = 0; char mine[rows][cols]; char show[rows][cols]; int i = 0; int j = 0; for (i = 0; i < rows - 1; i++) { for (j = 0; j < cols - 1; j++) { mine[i][j] = '0'; show[i][j] = '*'; } } menu(); while (1) { printf("請(qǐng)選擇:"); scanf("%d", &input); if (input == 1) { printf("進(jìn)入游戲\n"); Game(mine,show); break; } else if (input == 0) { printf("退出游戲!\n"); exit(0); break; } else { printf("輸入有誤!\n"); } } return 0; }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:C語言
下一篇:單詞小助手C語言版
本文標(biāo)題:C語言實(shí)現(xiàn)簡易掃雷小游戲
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/155.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)數(shù)怎么表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子