C++學習心得之掃雷遊戲
本文例項為大家分享了C++實現掃雷遊戲的具體程式碼,供大家參考,具體內容如下
一、序言
建立一個9*9有10個雷的掃雷遊戲
文章的順序是按照我當時的程式設計順序寫的,順便寫下我當初的一點思路,總的程式碼在文章最後,前面的都是分散的函式,有需要的朋友直接複製最後的
二、建立
建立一個頭檔案,一個放遊戲的程式,一個放執行測試的程式
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdlib.h>//生成隨機數 #include<stdio.h> #include<time.h>//生成時間戳 #define ROW 9//行數 #define COL 9//列數 #define ROWS ROW+2 #define COLS COL+2 #define EASY 10//雷數 void InitBoard(char board[ROWS][COLS],int rows,int cols,char set);//初始函式 void DisplayBoard(char board[ROWS][COLS],int row,int col);//展示函式 void SetBoard(char board[ROWS][COLS],int col);//造雷函式 void CheckBoard(char mine[ROWS][COLS],char show[ROWS][COLS],int col);//掃描函式
三、選擇介面
進入遊戲,可能出現情況三種,分別是退出,遊戲和輸入錯誤
也許有人一局完了還想玩,所以要設定迴圈,寫完程式碼可以執行一下,避免最後出bug範圍太大
#include"game.h" void test() { int input = 0; do { menu(); printf("input choice:>"); scanf("%d",&input); switch (input) { case 0: printf("over\n"); break; case 1: game(); break; default: printf("input wrong\n"); break; } } while (input); } void menu() { printf("*********************\n"); printf("******* 1.game ******\n"); printf("****** 0.over ******\n"); printf("*********************\n"); } int main() { test(); return 0; }
四、遊戲部分
1、宣告變數和初始化
建立儲存掃雷的元素的陣列,這裡咱們可以設定兩個字元形陣列,一個是標識著炸彈‘1'的mine陣列,一個是用來給玩家展示的show陣列
雖然是99的大小,但是在之後要由電腦掃描咱們選中點周圍的區域,如果陣列為9行9列,電腦在掃描最外面一行時就跟中間的部分不一樣了,為了方便,咱們建立1111的陣列
void game() { srand((unsigned)time(NULL));//這裡使用了time.h製造時間戳,以便隨機生成數 char mine[ROWS][COLS]; char show[ROWS][COLS]; InitBoard(mine,ROWS,COLS,'0'); InitBoard(show,'*'); DisplayBoard(show,ROW,COL); SetBoard(mine,COL); CheckBoard(mine,show,COL); } void InitBoard(char board[ROWS][COLS],char set) { int i = 0; int j = 0; for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { board[i][j] = set; } } }
2、展示函式
申明和定義好變數,肯定要讓玩家看到遊戲盤的變化情況才能玩,所以寫一個展示函式
mine陣列中炸彈用‘1'來表示,不是炸彈用‘0'表示,show陣列中我們用‘*'表示一個區域,然後選中的點要是周圍無炸彈,就是‘ ',否則就標識出周圍的炸彈數。
void DisplayBoard(char board[ROWS][COLS],int col) { int i = 0; int j = 0; printf("----------------------------"); printf("\n"); for (i = 0; i <= row; i++) { printf("%d ",i);//這裡是為了標出列數,便於定位 } printf("\n"); for (i = 1; i <= row; i++) { printf("%d ",i);//這裡是在每行開頭標出行數,便於定位 for (j = 1; j <= col; j++) { printf("%c ",board[i][j]); } printf("\n"); } printf("----------------------------"); printf("\n"); }
3、造炸彈
這裡咱們在標頭檔案定義炸彈數,以後想玩多點炸彈,修改一個數就行,方便快捷
void SetBoard(char board[ROWS][COLS],int col) { int x = 0; int y = 0; int num = EASY; while (num) { x = rand() % ROW + 1; y = rand() % COL + 1; if (board[x][y] == '0') { board[x][y] = '1'; num--; } } }
4、掃描函式
進入遊戲,玩家只有選擇了要檢查的點才能繼續,這裡有三種情況,因為要有很多次選擇,所以採用迴圈
(1)選中雷區,那麼直接跳出迴圈,遊戲結束
(2)沒選中雷區,電腦會掃描周圍的區域,把周圍無雷的點展開,展開周圍有雷的點
這裡還要說一下mine陣列為什麼要用‘0'和‘1'來做標記,因為0和1這兩個字元在ascII碼錶裡是連續的,一會在電腦掃描周圍時可以直接通過減法算出周圍的雷數
void CheckBoard(char mine[ROWS][COLS],int col) { int ret = 0; int x = 0; int y = 0; int num = 0; while (ret < ROW*COL - EASY) { printf("輸入排查座標\n"); scanf("%d%d",&x,&y); if (x > 0 && x <= row && y > 0 && y <= col) { if (mine[x][y] == '1')//選中雷區,遊戲結束 { printf("炸死\n"); DisplayBoard(mine,row,col);//展示mine區域 break;//跳出迴圈 } else//沒踩中雷區 { ZeroLine(mine,x,y);//展開周圍的區域 DisplayBoard(show,col); ret++; } } else { printf("input wrong\n"); } } if (ret == ROW * COL - EASY) { printf("勝利\n"); } } void ZeroLine(char mine[ROWS][COLS],int x,int y) { int ret = 0; ret = AroundNum(mine,y);//掃描函式,掃描該點周圍雷數 if (x >= 0 && y >= 0 && x < ROWS && y < COLS) { if (ret == 0) { show[x][y] = ' ';//無雷則為空白 if (mine[x][y + 1] == '0' && show[x][y + 1] == '*') { ZeroLine(mine,y + 1); } if (mine[x][y - 1] == '0' && show[x][y - 1] == '*') { ZeroLine(mine,y - 1); } if (mine[x - 1][y] == '0' && show[x - 1][y] == '*') { ZeroLine(mine,x - 1,y); } if (mine[x + 1][y] == '0' && show[x + 1][y] == '*') { ZeroLine(mine,x + 1,y); } if (mine[x + 1][y + 1] == '0' && show[x + 1][y + 1] == '*') { ZeroLine(mine,y + 1); } if (mine[x - 1][y - 1] == '0' && show[x - 1][y - 1] == '*') { ZeroLine(mine,y - 1); } if (mine[x + 1][y - 1] == '0' && show[x + 1][y - 1] == '*') { ZeroLine(mine,y - 1); } if (mine[x - 1][y + 1] == '0' && show[x - 1][y - 1] == '*') { ZeroLine(mine,y + 1); } } else { show[x][y] = ret + '0'; } } } int AroundNum(char mine[ROWS][COLS],int y) { return mine[x - 1][y - 1] + mine[x][y - 1] + mine[x - 1][y] + mine[x + 1][y + 1] + mine[x][y + 1] + mine[x + 1][y] + mine[x - 1][y + 1] + mine[x + 1][y - 1] - 8 * mine[x][y]; }
五、總程式碼
1、標頭檔案
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdlib.h> #include<stdio.h> #include<time.h> #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 #define EASY 10 void InitBoard(char board[ROWS][COLS],char set); void DisplayBoard(char board[ROWS][COLS],int col); void SetBoard(char board[ROWS][COLS],int col); void CheckBoard(char mine[ROWS][COLS],int col);
2、遊戲部分
#include"game.h" void InitBoard(char board[ROWS][COLS],char set) { int i = 0; int j = 0; for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { board[i][j] = set; } } } void DisplayBoard(char board[ROWS][COLS],i); } printf("\n"); for (i = 1; i <= row; i++) { printf("%d ",i); for (j = 1; j <= col; j++) { printf("%c ",board[i][j]); } printf("\n"); } printf("----------------------------"); printf("\n"); } void SetBoard(char board[ROWS][COLS],int col) { int x = 0; int y = 0; int num = EASY; while (num) { x = rand() % ROW + 1; y = rand() % COL + 1; if (board[x][y] == '0') { board[x][y] = '1'; num--; } } } int AroundNum(char mine[ROWS][COLS],int y) { return mine[x - 1][y - 1] + mine[x][y - 1] + mine[x - 1][y] + mine[x + 1][y + 1] + mine[x][y + 1] + mine[x + 1][y] + mine[x - 1][y + 1] + mine[x + 1][y - 1] - 8 * mine[x][y]; } void ZeroLine(char mine[ROWS][COLS],y); if (x >= 0 && y >= 0 && x < ROWS && y < COLS) { if (ret == 0) { show[x][y] = ' '; if (mine[x][y + 1] == '0' && show[x][y + 1] == '*') { ZeroLine(mine,y + 1); } } else { show[x][y] = ret + '0'; } } } void CheckBoard(char mine[ROWS][COLS],&y); if (x > 0 && x <= row && y > 0 && y <= col) { if (mine[x][y] == '1') { printf("炸死\n"); DisplayBoard(mine,col); break; } else { ZeroLine(mine,y); DisplayBoard(show,col); ret++; } } else { printf("input wrong\n"); } } if (ret == ROW * COL - EASY) { printf("勝利\n"); } }
3、檢測部分
#include"game.h" void menu() { printf("*********************\n"); printf("******* 1.game ******\n"); printf("****** 0.over ******\n"); printf("*********************\n"); } void game() { srand((unsigned)time(NULL)); char mine[ROWS][COLS]; char show[ROWS][COLS]; InitBoard(mine,COL); } void test() { int input = 0; do { menu(); printf("input choice:>"); scanf("%d",&input); switch (input) { case 0: printf("over\n"); break; case 1: game(); break; default: printf("input wrong\n"); break; } } while (input); } int main() { test(); return 0; }
更多精彩遊戲小程式碼,請點選《遊戲專題》閱讀
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。