溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C語言怎么實現簡易的掃雷游戲

發布時間:2021-06-12 19:01:42 來源:億速云 閱讀:148 作者:小新 欄目:編程語言

這篇文章主要介紹C語言怎么實現簡易的掃雷游戲,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

本文實例為大家分享了C語言掃雷游戲的具體代碼,供大家參考,具體內容如下

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAX_ROW 9
#define MAX_COL 9
#define MINE_C0UNT 10
void menu() {
 printf("************************\n");
 printf("*****   1.play  ****\n");
 printf("*****   0.exit  ****\n");
 printf("************************\n");
}
//1、先初始化兩個地圖,玩家看到的,地雷布局圖。
void Init(char show_map[MAX_ROW][MAX_COL],char mine_map[MAX_ROW][MAX_COL]) {
 //對于玩家看到的地圖,未翻到的都設為*;
 for (int row = 0; row < MAX_ROW; row++) {
 for (int col = 0; col < MAX_COL; col++) {
  show_map[row][col] = '*';
 }
 }
 //對于地雷布局圖,用0表示沒有地雷,用1表示雷。
 for (int row = 0; row < MAX_ROW; row++) {
 for (int col = 0; col < MAX_COL; col++) {
  mine_map[row][col] = '0';
 }
 }
 //假設設置十個地雷
 int n = MINE_C0UNT;
 while (n > 0) {
 int row = rand() % MAX_ROW;
 int col = rand() % MAX_COL;
 if (mine_map == '1') {
  continue;
 }
 mine_map[row][col] = '1';
 --n;
 }
}
void printmap(char map[MAX_ROW][MAX_COL]) {
 //不光能打印出地圖,還能帶坐標
//先打印第一行
 printf("  ");
 for (int i = 0; i < MAX_COL; i++) {
 printf("%d ", i);
 }
 printf("\n");
 //打印一個分割線
 for (int col = 0; col < MAX_COL - 2; ++col) {
 printf("---");
 }
 printf("\n");
 //在打印其他行
 for (int row = 0; row < MAX_ROW; row++) {
 printf(" %d| ", row);
 //打印本行的每一列
 for (int col = 0; col < MAX_COL; col++) {
  printf("%c ", map[row][col]);
 }
 printf("\n");
 }
}
void updateshowmap(int row,int col,char show_map[MAX_ROW][MAX_COL], char mine_map[MAX_ROW][MAX_COL]) {
 int count = 0;
 if (row - 1 >= 0 && col - 1 >= 0 && row - 1 < MAX_ROW && col - 1 < MAX_COL && mine_map[row - 1][col - 1] == '1') {
 count++;
 }
 if (row - 1 >= 0 && col >= 0 && row - 1 < MAX_ROW && col < MAX_COL && mine_map[row - 1][col] == '1') {
 count++;
 }
 if (row - 1 >= 0 && col + 1 >= 0 && row - 1 < MAX_ROW && col + 1 < MAX_COL && mine_map[row - 1][col + 1] == '1') {
 count++;
 }
 if (row >= 0 && col - 1 >= 0 && row < MAX_ROW && col - 1 < MAX_COL && mine_map[row][col - 1] == '1') {
 count++;
 }
 if (row >= 0 && col + 1 >= 0 && row < MAX_ROW && col + 1 < MAX_COL && mine_map[row][col + 1] == '1') {
 count++;
 }
 if (row + 1 >= 0 && col - 1 >= 0 && row + 1 < MAX_ROW && col - 1 < MAX_COL && mine_map[row + 1][col - 1] == '1') {
 count++;
 }
 if (row + 1 >= 0 && col + 1 >= 0 && row + 1 < MAX_ROW && col + 1 < MAX_COL && mine_map[row + 1][col + 1] == '1') {
 count++;
 }
 show_map[row][col] = '0' + count;
}
void game() {
 char show_map[MAX_ROW][MAX_COL];
 char mine_map[MAX_ROW][MAX_COL];
 Init(show_map,mine_map);
 while (1) {
 printmap(show_map);
 printf("請玩家輸入一組坐標:");
 int row, col;
 int blank_count_already_show = 0;
 scanf("%d%d", &row, &col);
 system("cls");
 if (row < 0 || row >= MAX_ROW || col < 0 || col >= MAX_COL) {
  printf("您的輸入不合法,請您重新輸入!\n");
  continue;
 }
 if (show_map[row][col] != '*') {
  printf("您輸入的位置已經被占用了,請您重新輸入!\n");
  continue;
 }
 //判斷玩家輸入的坐標對應的是不是地雷,如果是地雷則游戲就結束了
 if (mine_map[row][col] == '1') {
  printf("游戲結束!\n");
  printmap(mine_map);
  break;
 }
 //判斷游戲是否勝利,通過計算已翻開的非雷的格子的個數
 ++blank_count_already_show;
 if (blank_count_already_show == MAX_ROW * MAX_COL - MINE_C0UNT) {
  printf("游戲勝利 !\n");
  printmap(mine_map);
  break;
 }
 //統計當前位置中周圍雷的個數
 updateshowmap(row, col, show_map, mine_map);
 }
}
int main() {
 srand((unsigned)time(0));
 int input = 0;
 while (1) {
 menu();
 printf("請選擇:");
 scanf("%d", &input);
 if (input == 1) {
  printf("開始游戲!\n");
  game();
 }
 else if (input == 0) {
  printf("退出游戲!\n");
  break;
 }
 else {
  printf("輸入錯誤,請重新輸入!\n");
  continue;
 }
 }
 system("pause");
 return 0;
}

以上是“C語言怎么實現簡易的掃雷游戲”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女