欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

C++學(xué)習(xí)心得之掃雷游戲

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

成都創(chuàng)新互聯(lián)公司堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的平果網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

一、序言

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

二、創(chuàng)建

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

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdlib.h>//生成隨機數(shù)
#include<stdio.h>
#include<time.h>//生成時間戳

#define ROW 9//行數(shù)
#define COL 9//列數(shù)
#define ROWS ROW+2
#define COLS COL+2
#define EASY 10//雷數(shù)

void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);//初始函數(shù)
void DisplayBoard(char board[ROWS][COLS], int row, int col);//展示函數(shù)
void SetBoard(char board[ROWS][COLS], int row, int col);//造雷函數(shù)
void CheckBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);//掃描函數(shù)

三、選擇界面

進入游戲,可能出現(xiàn)情況三種,分別是退出,游戲和輸入錯誤
也許有人一局完了還想玩,所以要設(shè)置循環(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、聲明變量和初始化

建立存儲掃雷的元素的數(shù)組,這里咱們可以設(shè)置兩個字符形數(shù)組,一個是標(biāo)識著炸彈‘1'的mine數(shù)組,一個是用來給玩家展示的show數(shù)組
雖然是99的大小,但是在之后要由電腦掃描咱們選中點周圍的區(qū)域,如果數(shù)組為9行9列,電腦在掃描最外面一行時就跟中間的部分不一樣了,為了方便,咱們建立1111的數(shù)組

void game()
{
 srand((unsigned)time(NULL));//這里使用了time.h制造時間戳,以便隨機生成數(shù)
 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、展示函數(shù)

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

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);//這里是為了標(biāo)出列數(shù),便于定位
 }
 printf("\n");
 for (i = 1; i <= row; i++)
 {
 printf("%d ", i);//這里是在每行開頭標(biāo)出行數(shù),便于定位
 for (j = 1; j <= col; j++)
 {
 printf("%c ", board[i][j]);
 }
 printf("\n");
 }
 printf("----------------------------");
 printf("\n");
}

3、造炸彈

這里咱們在頭文件定義炸彈數(shù),以后想玩多點炸彈,修改一個數(shù)就行,方便快捷

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、掃描函數(shù)

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

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("輸入排查坐標(biāo)\n");
 scanf("%d%d", &x, &y);
 if (x > 0 && x <= row && y > 0 && y <= col)
 {
 if (mine[x][y] == '1')//選中雷區(qū),游戲結(jié)束
 {
 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);//掃描函數(shù),掃描該點周圍雷數(shù)
 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("輸入排查坐標(biāo)\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;
}

更多精彩游戲小代碼,請點擊《游戲?qū)n}》閱讀

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

標(biāo)題名稱:C++學(xué)習(xí)心得之掃雷游戲
本文地址:http://aaarwkj.com/article20/phooco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、服務(wù)器托管手機網(wǎng)站建設(shè)、微信小程序網(wǎng)站導(dǎo)航、網(wǎng)頁設(shè)計公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)
国产精品重口调教系列| 老熟女乱色一区二区三区| 欧美人妻不卡一区二区久久 | 91国产熟女自拍视频| 亚洲国际精品女人乱码| 亚洲狠狠爱一区二区三区 | 国产一区二区三区在线视频播放| 国产乡下三级_三级全黄| 亚洲中文有码在线播放| 亚洲国产精品高清久久| 欧美亚洲另类色自拍偷拍| 欧美亚洲另类国产精品| 最新免费观看男女啪啪视频| 亚洲成人av在线蜜桃| 亚洲综合日韩丝袜人妻| 精品一区中文字幕少妇人妻 | 欧美一区二区三区中文字幕| 国产色视频一区在线观看| 欧美日韩精品一区二区在线| 自拍偷拍一区蜜桃视频| 日韩欧美中文字幕综合网| 91伊人激情综合久久| 欧美日韩国产看片一区二区| 欧美av一区二区三区四区| 人妻91一区二区三区| 国产一区二区乱码在线| 大秀视频一区二区三区| 精品黄色大片不卡国产| 人妻中文字幕一区二区三| 国产精品自产在线观看一| 91国语对白在线观看| 日韩av在线黄色免费大全| 欧美一区日韩二区在线| 涩涩涩丁香色婷五月网| 一个人看的视频天堂色| 一区二区三区欧美日韩在线| 精品少妇人妻av免费久久久| 久久精品国产久精国产爱| 日韩视频一区二区三区四区| 伊人久久亚洲精品综合| 亚洲黄色av一区二区三区|