国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

C語言實現簡易掃雷小游戲

2020-01-26 13:23:53
字體:
來源:轉載
供稿:網友

我們經常在電腦上面玩的掃雷游戲,很考驗我們的判斷能力,但是實現一個掃雷游戲并不是很困難,只要多注意一些細節就好,就可以將一個簡單的掃雷游戲寫出來!

接下來先介紹掃雷游戲要實現的功能:

首先,要對雷陣進行初始化,在初始化的時候要注意要定義兩個數組,一個是讓我們掃雷的陣,另外一個就是顯示某一個地方的周圍的雷的總個數的矩陣,在初始化的時候要注意為了避免傳址的問題,我們把它寫在主函數里面。

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] = '*'; } }

接下來就是電腦在隨機布局雷陣的函數,這個函數要用到rand() 函數,來產生隨機值,在雷陣里面隨機布雷。

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--; } }}

再有就是計算雷的個數的函數,要講某一個坐標位置的周圍8個位置的雷的個數算出來,并且將個數顯示出來

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;}

將掃雷函數的各個函數都實現了之后,我們來看一下完整的代碼

頭文件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();//菜單函數void display(char show[rows][cols]);int Game(char mine[rows][cols],char show[rows][cols]);//游戲void set_mine(char mine[rows][cols]);//設置雷的位置int Sweep(char mine[rows][cols], char show[rows][cols]);//開始掃雷int get_num(char mine[rows][cols], int x, int y);//計算雷的個數

實現函數 game.c

#include"game.h" //菜單函數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;}  //設置雷的位置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"); }} //計算雷的個數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("請輸入坐標:/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;}

最后就是測試函數 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("請選擇:"); scanf("%d", &input); if (input == 1) {  printf("進入游戲/n");  Game(mine,show);  break; } else if (input == 0) {  printf("退出游戲!/n");  exit(0);  break; } else {  printf("輸入有誤!/n"); } } return 0;}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 郎溪县| 故城县| 上犹县| 荣成市| 漯河市| 沧州市| 永川市| 涡阳县| 富阳市| 色达县| 诏安县| 唐河县| 宣威市| 桐梓县| 扬州市| 额济纳旗| 永寿县| 钦州市| 肥东县| 美姑县| 明星| 会同县| 侯马市| 大化| 吉林市| 儋州市| 双牌县| 江陵县| 丰顺县| 西充县| 怀化市| 新沂市| 金华市| 龙胜| 德庆县| 闽侯县| 绥宁县| 宜黄县| 湖南省| 台中县| 铜鼓县|