使用C语言写一个扫雷小游戏
生活随笔
收集整理的這篇文章主要介紹了
使用C语言写一个扫雷小游戏
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
相信掃雷游戲小伙伴們肯定都玩過吧,學習了C語言中的數組、函數等基礎內容之后就可以自己寫一個簡易的掃雷小游戲了,今天就我寫掃雷小游戲的過程及思路寫一篇博客,希望大家看完我的博客能有所收獲。
軟件及環境
VS2013
(說明:以下所提到的mine為置雷的棋盤,show為顯示排雷信息的棋盤)
掃雷游戲基本過程
掃雷游戲:輸入坐標如果該位置不是雷,則顯示數字代表該坐標周圍有幾個雷;如果該位置是雷則游戲結束。
優化:如果輸入坐標該位置不是雷,且該位置周圍沒有雷,則可以展開一片,直到周圍有雷的地方。
大致的游戲過程就是這樣。
優化排雷思路
優化排雷應該注意的條件:
在代碼實現的過程中需要注意幾個問題:
①遞歸的截止條件②注意數組的邊界,不要越界③這里遞歸訪問很容易重復訪問某一個坐標,所以需要進行判斷
我的實現思路:
玩家輸入坐標,如果在mine棋盤中為’0‘(說明該位置沒有雷),在show棋盤中為’*’(說明該位置未被訪問過),1<=x<=col,1<=y<=row(沒有越界),進行判斷:如果周圍沒有雷就將該坐標置為空格,通過遞歸的思路依次判斷該坐標周圍的8個坐標即可。如果周圍有雷就計算周圍雷的數量轉化為字符形式存入show棋盤的相應坐標,返回上一層遞歸。
代碼如下:
代碼實現(含優化)
文件及目錄結構
game.h頭文件
#include<stdio.h> #include<time.h> #include<stdlib.h> #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 #define MINE_NUM 10 //放置雷的數量 #define TIME ROW*COL-MINE_NUM void DisplayBoard(char board[ROWS][COLS], int row, int col);void InitBoard(char board[ROWS][COLS], int row, int col, char s); void SetMine(char board[ROWS][COLS], int row, int col); void FindMine(char board1[ROWS][COLS], char board2[ROWS][COLS], int row, int col); void FindMinePlus(char board1[ROWS][COLS], char board2[ROWS][COLS], int row, int col); //優化排雷 void SetSpace(char board1[ROWS][COLS], char board2[ROWS][COLS], int x, int y);game.c文件
#define _CRT_SECURE_NO_WARNINGS 1 #include "game.h"int GetMine(char board1[ROWS][COLS], int x, int y) //統計坐標x,y周圍的雷的個數 {return (board1[x-1][y ] + board1[x - 1][y - 1] + board1[x ][y-1] + board1[x + 1][y - 1] + board1[x+1][y ]+ board1[x + 1][y + 1] + board1[x ][y+1] + board1[x - 1][y + 1] - 8 * '0'); } void InitBoard(char board[ROWS][COLS], int row, int col, char s) {int i = 0, j = 0;for ( i = 0; i < row; i++){for ( j = 0; j < col; j++){board[i][j] = s;}} }void DisplayBoard(char board[ROWS][COLS], int row, int col) {int i = 0, j = 0;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("*******************\n"); } void SetMine(char board[ROWS][COLS], int row, int col) //放置雷 {int mine_count = MINE_NUM;while (mine_count > 0){int x = rand() % col + 1;int y = rand() % row + 1;if (board[x][y] != '1'){board[x][y] = '1';mine_count--;}} } void FindMine(char board1[ROWS][COLS],char board2[ROWS][COLS], int row, int col) {int x = 0, y = 0,time = 0;while (time < TIME){printf("請輸入要排查的坐標:\n");scanf("%d%d", &x, &y);if (board1[x][y] == '1'){printf("很遺憾,你被炸死了\n");DisplayBoard(board1, row, col);break;}else{if (board2[x][y] == '*'){time++;}board2[x][y] = GetMine(board1,x,y)+'0';DisplayBoard(board2, row, col);}}if (time == TIME){printf("恭喜排雷成功!\n");} } void SetSpace(char board1[ROWS][COLS],char board2[ROWS][COLS], int x, int y) {while (board1[x][y] == '0' && board2[x][y] =='*' && x >=1 && x <= ROW && y >= 1 && y <= COL){if (GetMine(board1, x, y) == 0)//周圍沒有雷{board2[x][y] = ' ';SetSpace(board1, board2, x, y + 1);SetSpace(board1, board2, x - 1, y + 1);SetSpace(board1, board2, x - 1, y);SetSpace(board1, board2, x - 1, y - 1);SetSpace(board1, board2, x, y - 1);SetSpace(board1, board2, x + 1, y - 1);SetSpace(board1, board2, x + 1, y);SetSpace(board1, board2, x + 1, y + 1);}else //周圍有雷{board2[x][y] = GetMine(board1, x, y) + '0';break;}} } int Mine_Num(char board[ROWS][COLS], int row, int col) {int i = 0, j = 0,count = 0;for (i = 1; i <= row; i++){for (j = 1; j <= col; j++){if (board[i][j] == '*')count++;}}return count; } void FindMinePlus(char board1[ROWS][COLS], char board2[ROWS][COLS], int row, int col) {int x = 0, y = 0;while (1){printf("請輸入要排查的坐標:\n");scanf("%d%d", &x, &y);if (board1[x][y] == '1'){printf("很遺憾,你被炸死了!\n");DisplayBoard(board1, ROW, COL);break;}else{SetSpace(board1, board2, x, y);DisplayBoard(board2, ROW, COL);if (Mine_Num(board2, ROW, COL) == MINE_NUM){printf("恭喜你,排雷成功!\n");break;}}}}main.c文件
#define _CRT_SECURE_NO_WARNINGS 1 #include "game.h" void menu() {printf(" (^=^) \n");printf("**************歡迎進入掃雷游戲*************\n");printf("請選擇: \n");printf(" 1.掃雷 \n");printf(" 0.退出 \n"); } void game() {char mine[ROWS][COLS];char show[ROWS][COLS];InitBoard(mine,ROWS,COLS,'0');InitBoard(show, ROWS, COLS, '*');DisplayBoard(mine, ROW, COL);DisplayBoard(show, ROW, COL);SetMine(mine,ROW,COL);DisplayBoard(mine, ROW, COL);FindMinePlus(mine, show, ROW, COL);} void playGame() {int choice = 0;do{menu();scanf("%d", &choice);switch (choice){case 1:game();playGame();break;case 0:exit(0);break;default:printf("選擇錯誤,請重新選擇正確的選項!\n");break;}} while (choice !=1 && choice!= 0);} int main() {srand((unsigned int)time(NULL));playGame();return 0; }效果和截圖
(為了演示所以直接打印了置雷棋盤)
簡易掃雷:
此過程需要你把棋盤上所有不是雷的位置找出來最后才能獲得游戲勝利,過程未免太過漫長,給玩家帶來不好的體驗。
優化后:
優化后的游戲體驗明顯提升啦!
以上代碼以及思路如有不妥之處望積極提出,代碼我放在我的碼云上啦,需要看的小伙伴戳這里!!https://gitee.com/yuan-xinyi/c-language/tree/master/findmine
總結
以上是生活随笔為你收集整理的使用C语言写一个扫雷小游戏的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么值得买转运攻略:海带宝转运 手把手使
- 下一篇: 无法初始化 PowerShell 主机解