亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C語言實現(xiàn)掃雷代碼

 更新時間:2022年06月07日 08:34:47   作者:杯淺  
這篇文章主要為大家詳細介紹了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++多線程編程時的數(shù)據(jù)保護

    C++多線程編程時的數(shù)據(jù)保護

    這篇文章主要介紹了C++多線程編程時的數(shù)據(jù)保護,作者針對C++11版本中的新特性做出了一些解說,需要的朋友可以參考下
    2015-07-07
  • C++ 繼承,虛繼承(內(nèi)存結(jié)構(gòu))詳解

    C++ 繼承,虛繼承(內(nèi)存結(jié)構(gòu))詳解

    C++繼承和虛繼承的內(nèi)存模型是一個老生常談的話題,實現(xiàn)方法主要依賴于編譯器,本文從多個角度通過代碼詳解C++中虛繼承的內(nèi)存模型知識,感興趣的朋友跟隨小編一起看看吧
    2021-09-09
  • typedef_struct與struct之間的區(qū)別

    typedef_struct與struct之間的區(qū)別

    本篇文章主要是對typedef struct與struct之間的區(qū)別進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2013-12-12
  • 深入聊聊C語言中的Const關(guān)鍵字

    深入聊聊C語言中的Const關(guān)鍵字

    關(guān)鍵字const用來定義只讀變量,被const定義的變量它的值是不允許改變的,即不允許給它重新賦值,即使是賦相同的值也不可以,下面這篇文章主要給大家介紹了關(guān)于C語言中Const關(guān)鍵字的相關(guān)資料,需要的朋友可以參考下
    2022-02-02
  • 淺析C語言中的內(nèi)存布局

    淺析C語言中的內(nèi)存布局

    以下是對C語言中的內(nèi)存布局進行了詳細的分析介紹。需要的朋友可以過來參考下
    2013-08-08
  • C++實現(xiàn)的大數(shù)相乘算法示例

    C++實現(xiàn)的大數(shù)相乘算法示例

    這篇文章主要介紹了C++實現(xiàn)的大數(shù)相乘算法,結(jié)合實例形式分析了C++大數(shù)相乘的概念、原理及代碼實現(xiàn)技巧,需要的朋友可以參考下
    2017-08-08
  • C++基于人工智能搜索策略解決農(nóng)夫過河問題示例

    C++基于人工智能搜索策略解決農(nóng)夫過河問題示例

    這篇文章主要介紹了C++基于人工智能搜索策略解決農(nóng)夫過河問題,簡單描述了農(nóng)夫過河問題的概念、實現(xiàn)原理并結(jié)合具體實例形式給出了C++使用人工智能搜索策略解決農(nóng)夫過河問題的相關(guān)操作技巧,需要的朋友可以參考下
    2017-12-12
  • C++實現(xiàn)LeetCode(241.添加括號的不同方式)

    C++實現(xiàn)LeetCode(241.添加括號的不同方式)

    這篇文章主要介紹了C++實現(xiàn)LeetCode(241.添加括號的不同方式),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • C語言中指針常量和常量指針的區(qū)別

    C語言中指針常量和常量指針的區(qū)別

    本文主要介紹了C語言中指針常量和常量指針的區(qū)別,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 素數(shù)判定算法的實現(xiàn)

    素數(shù)判定算法的實現(xiàn)

    這篇文章主要介紹了素數(shù)判定算法的實現(xiàn),素數(shù)判定問題是一個非常常見的問題,本文介紹了常用的幾種判定方法,需要的朋友可以參考下
    2014-08-08

最新評論