最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++學習心得之掃雷游戲

 更新時間:2020年03月17日 14:16:58   作者:璀璨的冰  
這篇文章主要為大家詳細介紹了C++學習心得之掃雷游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了C++實現(xiàn)掃雷游戲的具體代碼,供大家參考,具體內容如下

一、序言

創(chuàng)建一個9*9有10個雷的掃雷游戲
文章的順序是按照我當時的編程順序寫的,順便寫下我當初的一點思路,總的代碼在文章最后,前面的都是分散的函數,有需要的朋友直接復制最后的

二、創(chuàng)建

創(chuàng)建一個頭文件,一個放游戲的程序,一個放運行測試的程序

#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 row, int col);//造雷函數
void CheckBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);//掃描函數

三、選擇界面

進入游戲,可能出現(xiàn)情況三種,分別是退出,游戲和輸入錯誤
也許有人一局完了還想玩,所以要設置循環(huán),寫完代碼可以運行一下,避免最后出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的大小,但是在之后要由電腦掃描咱們選中點周圍的區(qū)域,如果數組為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, ROWS, COLS, '*');
 DisplayBoard(show, ROW, COL);
 SetBoard(mine, ROW, COL);
 CheckBoard(mine, show, ROW, COL);
}
void InitBoard(char board[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++)
 {
 board[i][j] = set;
 }
 } 
}

2、展示函數

申明和定義好變量,肯定要讓玩家看到游戲盤的變化情況才能玩,所以寫一個展示函數
mine數組中炸彈用‘1'來表示,不是炸彈用‘0'表示,show數組中我們用‘*'表示一個區(qū)域,然后選中的點要是周圍無炸彈,就是‘ ',否則就標識出周圍的炸彈數。

void DisplayBoard(char board[ROWS][COLS], int row, 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 row, 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、掃描函數

進入游戲,玩家只有選擇了要檢查的點才能繼續(xù),這里有三種情況,因為要有很多次選擇,所以采用循環(huán)
(1)選中雷區(qū),那么直接跳出循環(huán),游戲結束
(2)沒選中雷區(qū),電腦會掃描周圍的區(qū)域,把周圍無雷的點展開,展開周圍有雷的點
這里還要說一下mine數組為什么要用‘0'和‘1'來做標記,因為0和1這兩個字符在ascII碼表里是連續(xù)的,一會在電腦掃描周圍時可以直接通過減法算出周圍的雷數

void CheckBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int row, 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')//選中雷區(qū),游戲結束
 {
 printf("炸死\n");
 DisplayBoard(mine, row, col);//展示mine區(qū)域
 break;//跳出循環(huán)
 }
 else//沒踩中雷區(qū)
 {
  ZeroLine(mine, show, x, y);//展開周圍的區(qū)域
 DisplayBoard(show, row, col);
 ret++;
 }
 }
 else
 {
 printf("input wrong\n");
 }
 }
 if (ret == ROW * COL - EASY)
 {
 printf("勝利\n");
 }
}
void ZeroLine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
 int ret = 0;
 ret = AroundNum(mine, x, 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, show, x, y + 1);
 }
 if (mine[x][y - 1] == '0' && show[x][y - 1] == '*')
 {
  ZeroLine(mine, show, x, y - 1);
 }
 if (mine[x - 1][y] == '0' && show[x - 1][y] == '*')
 {
  ZeroLine(mine, show, x - 1, y);
 }
 if (mine[x + 1][y] == '0' && show[x + 1][y] == '*')
 {
  ZeroLine(mine, show, x + 1, y);
 }
 if (mine[x + 1][y + 1] == '0' && show[x + 1][y + 1] == '*')
 {
  ZeroLine(mine, show, x + 1, y + 1);
 }
 if (mine[x - 1][y - 1] == '0' && show[x - 1][y - 1] == '*')
 {
  ZeroLine(mine, show, x - 1, y - 1);
 } 
 if (mine[x + 1][y - 1] == '0' && show[x + 1][y - 1] == '*')
 {
  ZeroLine(mine, show, x + 1, y - 1);
 }
 if (mine[x - 1][y + 1] == '0' && show[x - 1][y - 1] == '*')
 {
  ZeroLine(mine, show, x - 1, y + 1);
 }
 }
 else
 {
 show[x][y] = ret + '0';
 }
 }
}
int AroundNum(char mine[ROWS][COLS], int x, 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], int rows, int cols, char set);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void SetBoard(char board[ROWS][COLS], int row, int col);
void CheckBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

2、游戲部分

#include"game.h"

void InitBoard(char board[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++)
 {
 board[i][j] = set;
 }
 } 
}
void DisplayBoard(char board[ROWS][COLS], int row, 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");
}
void SetBoard(char board[ROWS][COLS], int row, 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 x, 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], char show[ROWS][COLS], int x, int y)
{
 int ret = 0;
 ret = AroundNum(mine, x, 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, show, x, y + 1);
 }
 if (mine[x][y - 1] == '0' && show[x][y - 1] == '*')
 {
  ZeroLine(mine, show, x, y - 1);
 }
 if (mine[x - 1][y] == '0' && show[x - 1][y] == '*')
 {
  ZeroLine(mine, show, x - 1, y);
 }
 if (mine[x + 1][y] == '0' && show[x + 1][y] == '*')
 {
  ZeroLine(mine, show, x + 1, y);
 }
 if (mine[x + 1][y + 1] == '0' && show[x + 1][y + 1] == '*')
 {
  ZeroLine(mine, show, x + 1, y + 1);
 }
 if (mine[x - 1][y - 1] == '0' && show[x - 1][y - 1] == '*')
 {
  ZeroLine(mine, show, x - 1, y - 1);
 } 
 if (mine[x + 1][y - 1] == '0' && show[x + 1][y - 1] == '*')
 {
  ZeroLine(mine, show, x + 1, y - 1);
 }
 if (mine[x - 1][y + 1] == '0' && show[x - 1][y - 1] == '*')
 {
  ZeroLine(mine, show, x - 1, y + 1);
 }
 }
 else
 {
 show[x][y] = ret + '0';
 }
 }
}
void CheckBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int row, 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);
 break;
 }
 else
 {
  ZeroLine(mine, show, x, y);
 DisplayBoard(show, row, 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, ROWS, COLS, '0');
 InitBoard(show, ROWS, COLS, '*');
 DisplayBoard(show, ROW, COL);
 SetBoard(mine, ROW, COL);
 CheckBoard(mine, show, ROW, 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;
}

更多精彩游戲小代碼,請點擊《游戲專題》閱讀

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Qt音視頻開發(fā)之利用ffmpeg實現(xiàn)解碼本地攝像頭

    Qt音視頻開發(fā)之利用ffmpeg實現(xiàn)解碼本地攝像頭

    一開始用ffmpeg做的是視頻流的解析,后面增加了本地視頻文件的支持,到后面發(fā)現(xiàn)ffmpeg也是支持本地攝像頭設備的,所以本文就來用ffmpeg實現(xiàn)解碼本地攝像頭功能吧
    2023-03-03
  • SQL Server中的數據復制到的Access中的函數

    SQL Server中的數據復制到的Access中的函數

    SQL Server中的數據復制到的Access中,表的結構相同 不要提用openrowset,因為Access文件和SQL Server不在一臺機器上
    2008-11-11
  • 深入詳解C編寫Windows服務程序的五個步驟

    深入詳解C編寫Windows服務程序的五個步驟

    本篇文章介紹了,使用C編寫Windows服務程序的五個步驟的詳細概述。需要的朋友參考下
    2013-05-05
  • CreateCompatibleDC()函數案例詳解

    CreateCompatibleDC()函數案例詳解

    這篇文章主要介紹了CreateCompatibleDC()函數案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下
    2021-08-08
  • C語言切割多層字符串(strtok_r strtok使用方法)

    C語言切割多層字符串(strtok_r strtok使用方法)

    這篇文章主要介紹了C語言切割多層字符串的方法,說了strtok的弱點,使用strtok_r的方法
    2013-11-11
  • C++深入分析回顧函數重載

    C++深入分析回顧函數重載

    C++ 允許多個函數擁有相同的名字,只要它們的參數列表不同就可以,這就是函數的重載(Function Overloading),借助重載,一個函數名可以有多種用途
    2022-06-06
  • c++函數指針和回調函數示例

    c++函數指針和回調函數示例

    這篇文章主要介紹了c++函數指針和回調函數示例,需要的朋友可以參考下
    2014-05-05
  • C/C++中指針的深入理解

    C/C++中指針的深入理解

    指針在 C\C++ 語言中是很重要的內容,并且和指針有關的內容一向令初學者頭大,這篇文章主要給大家介紹了關于C/C++中指針的相關資料,需要的朋友可以參考下
    2021-07-07
  • C++實現(xiàn)堆排序實例介紹

    C++實現(xiàn)堆排序實例介紹

    大家好,本篇文章主要講的是C++實現(xiàn)堆排序實例介紹,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • C++制作DLL文件的方法詳解

    C++制作DLL文件的方法詳解

    本文主要介紹如何制作DLL,將代碼類中的方法以接口的形式暴露出來給exe程序使用。會涉及類廠創(chuàng)建方法實例、聲明DLL接口、.def文件的使用等,感興趣的可以了解一下
    2023-04-04

最新評論

清涧县| 都匀市| 新余市| 西丰县| 旺苍县| 武强县| 双辽市| 三台县| 屯留县| 盘山县| 耒阳市| 双流县| 中山市| 永康市| 莱阳市| 璧山县| 汉沽区| 唐山市| 手游| 南木林县| 沧州市| 卓资县| 双柏县| 彭泽县| 凌海市| 通化市| 哈巴河县| 伊金霍洛旗| 都匀市| 怀集县| 江都市| 广平县| 伊宁市| 连州市| 高阳县| 施甸县| 新民市| 枣阳市| 奈曼旗| 申扎县| 资溪县|