C語言實現(xiàn)掃雷代碼
本文實例為大家分享了C語言實現(xiàn)掃雷的具體代碼,供大家參考,具體內(nèi)容如下
前言
掃雷實現(xiàn)的基本規(guī)劃:
1、玩家可以自由選擇進入和退出游戲
2、玩家通過輸入坐標來排雷
3、排查雷給出提示
4、玩家可以任意標記雷區(qū)
5、排雷同時展開非雷區(qū)
下面為效果圖:
一、主函數(shù)(test.c):
通過do while嵌套switch對游戲菜單進行控制:
- 玩游戲輸入1:進入case1開始游戲。
- 輸入2:進入case2退出游戲 。
- 輸入錯誤:進入default,進入下一次循環(huán)重新輸入。
- 菜單給出標記操作提示。
#include "game.h" void menu()//游戲菜單 { ?? ?printf("**********************************\n"); ?? ?printf("******* ? ? ?掃雷游戲 ? ? ********\n"); ?? ?printf("******* ? ? ? 1.play ? ? ?********\n"); ?? ?printf("******* ? ? ? 2.exit ? ? ?********\n"); ?? ?printf("*******tip1:坐標輸入 0 0 ?********\n"); ?? ?printf("******* ? ?進行標記操作 ? ********\n"); ?? ?printf("*******tip2:坐標輸入 0 1 ?********\n"); ?? ?printf("******* ? ?取消標記操作 ? ********\n"); ?? ?printf("**********************************\n"); } void game() { ?? ?//定義兩個棋盤,mine里面雷圖,'*'為雷,' '為非雷 ?? ?//show里面-為未排雷區(qū)域,數(shù)字為周圍有幾顆雷 ?? ?//空格區(qū)域表示安全區(qū)域,周圍沒有雷 ?? ?char mine[ROWS][COLS] = { 0 }; ?? ?char show[ROWS][COLS] = { 0 }; ?? ?//初始化棋盤 ?? ?init_board(mine, ROWS, COLS, ' '); ?? ?init_board(show, ROWS, COLS, '-'); ?? ?//布置雷 ?? ?set_mine(mine, ROW, COL); ?? ?Display_board(show, ROW, COL);//打印棋盤 ?? ?//排查雷 ?? ?find_mine(mine, show, ROW, COL); } int main() { ?? ?int input = 0; ?? ?srand((unsigned int)time(NULL)); ?? ?do ?? ?{ ?? ??? ?menu(); ?? ??? ?printf("請選擇:>"); ?? ??? ?scanf("%d", &input); ?? ??? ?switch (input) ?? ??? ?{ ?? ??? ?case 1: ?? ??? ??? ?printf("開始游戲\n"); ?? ??? ??? ?game(); ?? ??? ??? ?break; ?? ??? ?case 2: ?? ??? ??? ?printf("游戲結(jié)束\n"); ?? ??? ??? ?break; ?? ??? ?default: ?? ??? ??? ?printf("輸入錯誤,請重新輸入\n"); ?? ??? ?} ?? ?} while (input != 2); ?? ?return 0; }
二、頭文件及定義(game.h):
行數(shù)、列數(shù)、雷數(shù)可隨時更改
#include <stdio.h> #include <stdlib.h> #include <time.h> #define ROW 9 //定義行數(shù)為9 #define COL 9 //定義列數(shù)為9 #define EASY_COUNT 10 //簡單定義一下雷數(shù) #define ROWS ROW+2 #define COLS COL+2 //初始化棋盤 void init_board(char arr[ROWS][COLS], int rows, int cols, char set); //打印棋盤 void Display_board(char arr[ROWS][COLS], int row, int col); //布置雷 void set_mine(char mine[ROWS][COLS], int row, int col); //排查雷 void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col); //標記坐標 void sign_show(char show[ROWS][COLS], int row, int col); //刪除標記坐標 void del_sign_show(char show[ROWS][COLS], int row, int col);
三、game.c(游戲代碼實現(xiàn)全過程):
基本邏輯就是兩個棋盤:
1、mine棋盤為設(shè)雷棋盤,用來設(shè)置雷。
2、show棋盤給玩家看,打印信息給玩家。
#include "game.h" //初始化棋盤 void init_board(char arr[ROWS][COLS], int rows, int cols,char set) { ?? ?int i = 0; ?? ?int j = 0; ?? ?for (i = 0; i < rows; i++) ?? ?{ ?? ??? ?for (j = 0; j < cols; j++) ?? ??? ?{ ?? ??? ??? ?arr[i][j] = set; ?? ??? ?} ?? ?} } //打印棋盤 void Display_board(char arr[ROWS][COLS], int row, int col) { ?? ?int i = 0; ?? ?int j = 0; ?? ?for (i = 0; i <= col; i++) ?? ?{ ?? ??? ?printf(" %d ", i); ?? ??? ?if (i < col) ?? ??? ?{ ?? ??? ??? ?printf("|"); ?? ??? ?} ?? ?} ?? ?printf("\n"); ?? ?for (i = 0; i <= col; i++) ?? ?{ ?? ??? ?printf("---"); ?? ??? ?if (i < col) ?? ??? ?{ ?? ??? ??? ?printf("|"); ?? ??? ?} ?? ?} ?? ?printf("\n"); ?? ?for (i = 1; i <= row; i++) ?? ?{ ?? ??? ?printf(" %d ", i); ?? ??? ?printf("|"); ?? ??? ?for (j = 1; j <= col; j++) ?? ??? ?{ ?? ??? ??? ?printf(" %c ", arr[i][j]); ?? ??? ??? ?if (j < col) ?? ??? ??? ?{ ?? ??? ??? ??? ?printf("|"); ?? ??? ??? ?} ?? ??? ?} ?? ??? ?printf("\n"); ?? ??? ?if (i < row) ?? ??? ?{ ?? ??? ??? ?for (j = 0; j <= col; j++) ?? ??? ??? ?{ ?? ??? ??? ??? ?printf("---"); ?? ??? ??? ??? ?if (j < col) ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?printf("|"); ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?} ?? ??? ?printf("\n"); ?? ?} ?? ?printf("=======================================\n"); } //布置雷 void set_mine(char mine[ROWS][COLS], int row, int col) { ?? ?int count = EASY_COUNT; ?? ?int x = 0; ?? ?int y = 0; ?? ?while (count) ?? ?{ ?? ??? ?x = rand() % row + 1; ?? ??? ?y = rand() % col + 1; ?? ??? ?if (mine[x][y] == ' ') ?? ??? ?{ ?? ??? ??? ?mine[x][y] = '*'; ?? ??? ??? ?count--; ?? ??? ?} ?? ?} } //排查雷 static int get_mine_count(char mine[ROWS][COLS], int x, int y)//查輸入坐標周圍雷數(shù) { ?? ?int i = 0; ?? ?int j = 0; ?? ?int count = 0; ?? ?for (i = x - 1; i <= x + 1; i++) ?? ?{ ?? ??? ?for (j = y - 1; j <= y + 1; j++) ?? ??? ?{ ?? ??? ??? ?if (mine[i][j] == '*') ?? ??? ??? ?{ ?? ??? ??? ??? ?count++; ?? ??? ??? ?} ?? ??? ?} ?? ?} ?? ?return count; } void spread_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)//展開非雷區(qū) { ?? ?int i = 0; ?? ?int j = 0; ?? ?for (i = x - 1; i <= x + 1; i++) ?? ?{ ?? ??? ?for (j = y - 1; j <= y + 1; j++) ?? ??? ?{ ?? ??? ??? ?if (i >= 1 && i <= ROW && j >= 1 && j <= COL) ?? ??? ??? ?{ ?? ??? ??? ??? ?if (get_mine_count(mine, i, j) == 0 && show[i][j] == '-') ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?show[i][j] = ' '; ?? ??? ??? ??? ??? ?spread_mine(mine, show, i, j); ?? ??? ??? ??? ?} ?? ??? ??? ??? ?else if(get_mine_count(mine, i, j) != 0 && show[i][j] == '-') ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?int count = get_mine_count(mine, i, j); ?? ??? ??? ??? ??? ?show[i][j] = count + '0'; ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?} ?? ?} } int is_win(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) { ?? ?int i = 0; ?? ?int j = 0; ?? ?int count = 0; ?? ?for (i = 1; i <= row; i++) ?? ?{ ?? ??? ?for (j = 1; j <= col; j++) ?? ??? ?{ ?? ??? ??? ?if (mine[i][j] != '*' && show[i][j] == '-') ?? ??? ??? ?{ ?? ??? ??? ??? ?return 0; ?? ??? ??? ?} ?? ??? ?} ?? ?} ?? ?return 1; } void sign_show(char show[ROWS][COLS], int row, int col)//標記坐標 { ?? ?int x = 0; ?? ?int y = 0; ?? ?printf("請選擇需要標記的坐標:>"); ?? ?while (1) ?? ?{ ?? ??? ?scanf("%d %d", &x, &y); ?? ??? ?if (show[x][y] == '-') ?? ??? ?{ ?? ??? ??? ?show[x][y] = '#'; ?? ??? ??? ?break; ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?printf("輸入錯誤,請重新輸入"); ?? ??? ?} ?? ?} } void del_sign_show(char show[ROWS][COLS], int row, int col)//刪除標記坐標 { ?? ?int x = 0; ?? ?int y = 0; ?? ?printf("請選擇需要刪除標記的坐標:>"); ?? ?while (1) ?? ?{ ?? ??? ?scanf("%d %d", &x, &y); ?? ??? ?if (show[x][y] == '#') ?? ??? ?{ ?? ??? ??? ?show[x][y] = '-'; ?? ??? ??? ?break; ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?printf("輸入錯誤,請重新輸入"); ?? ??? ?} ?? ?} } void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) { ?? ?int x = 0; ?? ?int y = 0; ?? ?while (1) ?? ?{ ?? ??? ?printf("請輸入排雷的坐標:>"); ?? ??? ?scanf("%d %d", &x, &y); ?? ??? ?if (show[x][y] != '-') ?? ??? ?{ ?? ??? ??? ?printf("此坐標已掃過,請重新輸入\n"); ?? ??? ??? ?continue; ?? ??? ?} ?? ??? ?else if (x >= 1 && x <= row && y >= 1 && y <= col) ?? ??? ?{ ?? ??? ??? ?if (mine[x][y] == '*') ?? ??? ??? ?{ ?? ??? ??? ??? ?printf("很遺憾,你被炸死了\n"); ?? ??? ??? ??? ?Display_board(mine, ROW, COL); ?? ??? ??? ??? ?break; ?? ??? ??? ?} ?? ??? ??? ?else ?? ??? ??? ?{ ?? ??? ??? ??? ?int count = get_mine_count(mine, x, y); ?? ??? ??? ??? ?if (count + '0' == '0') ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?show[x][y] = ' '; ?? ??? ??? ??? ??? ?spread_mine(mine, show, x, y);//展開非雷區(qū) ?? ??? ??? ??? ?} ?? ??? ??? ??? ?else ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?show[x][y] = count + '0'; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?Display_board(show, ROW, COL); ?? ??? ??? ?} ?? ??? ?} ?? ??? ?else if (x == 0 && y == 0)//輸入坐標為兩個0時進行標記操作 ?? ??? ?{ ?? ??? ??? ?sign_show(show, row, col);//標記坐標 ?? ??? ??? ?Display_board(show, ROW, COL); ?? ??? ?} ?? ??? ?else if (x == 0 && y == 1)//輸入坐標為兩個0時進行標記操作 ?? ??? ?{ ?? ??? ??? ?del_sign_show(show, row, col);//刪除標記 ?? ??? ??? ?Display_board(show, ROW, COL); ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?printf("輸入坐標超出雷區(qū)范圍,請重新輸入\n"); ?? ??? ?} ?? ??? ?if (is_win(mine,show,row,col)==1) ?? ??? ?{ ?? ??? ??? ?printf("恭喜你,掃雷成功\n"); ?? ??? ??? ?Display_board(mine, ROW, COL); ?? ??? ?} ?? ?} }
總結(jié)
以上就是自寫C語言掃雷的代碼全部內(nèi)容了:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++ 繼承,虛繼承(內(nèi)存結(jié)構(gòu))詳解
C++繼承和虛繼承的內(nèi)存模型是一個老生常談的話題,實現(xiàn)方法主要依賴于編譯器,本文從多個角度通過代碼詳解C++中虛繼承的內(nèi)存模型知識,感興趣的朋友跟隨小編一起看看吧2021-09-09typedef_struct與struct之間的區(qū)別
本篇文章主要是對typedef struct與struct之間的區(qū)別進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2013-12-12C++實現(xiàn)LeetCode(241.添加括號的不同方式)
這篇文章主要介紹了C++實現(xiàn)LeetCode(241.添加括號的不同方式),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07