C++學(xué)習(xí)心得之掃雷游戲
本文實(shí)例為大家分享了C++實(shí)現(xiàn)掃雷游戲的具體代碼,供大家參考,具體內(nèi)容如下
一、序言
創(chuàng)建一個(gè)9*9有10個(gè)雷的掃雷游戲
文章的順序是按照我當(dāng)時(shí)的編程順序?qū)懙?,順便寫下我?dāng)初的一點(diǎn)思路,總的代碼在文章最后,前面的都是分散的函數(shù),有需要的朋友直接復(fù)制最后的
二、創(chuàng)建
創(chuàng)建一個(gè)頭文件,一個(gè)放游戲的程序,一個(gè)放運(yùn)行測(cè)試的程序
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdlib.h>//生成隨機(jī)數(shù) #include<stdio.h> #include<time.h>//生成時(shí)間戳 #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ù)
三、選擇界面
進(jìn)入游戲,可能出現(xiàn)情況三種,分別是退出,游戲和輸入錯(cuò)誤
也許有人一局完了還想玩,所以要設(shè)置循環(huán),寫完代碼可以運(yù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、聲明變量和初始化
建立存儲(chǔ)掃雷的元素的數(shù)組,這里咱們可以設(shè)置兩個(gè)字符形數(shù)組,一個(gè)是標(biāo)識(shí)著炸彈‘1'的mine數(shù)組,一個(gè)是用來給玩家展示的show數(shù)組
雖然是99的大小,但是在之后要由電腦掃描咱們選中點(diǎn)周圍的區(qū)域,如果數(shù)組為9行9列,電腦在掃描最外面一行時(shí)就跟中間的部分不一樣了,為了方便,咱們建立1111的數(shù)組
void game()
{
srand((unsigned)time(NULL));//這里使用了time.h制造時(shí)間戳,以便隨機(jī)生成數(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ù)
申明和定義好變量,肯定要讓玩家看到游戲盤的變化情況才能玩,所以寫一個(gè)展示函數(shù)
mine數(shù)組中炸彈用‘1'來表示,不是炸彈用‘0'表示,show數(shù)組中我們用‘*'表示一個(gè)區(qū)域,然后選中的點(diǎn)要是周圍無炸彈,就是‘ ',否則就標(biāo)識(shí)出周圍的炸彈數(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、造炸彈
這里咱們?cè)陬^文件定義炸彈數(shù),以后想玩多點(diǎn)炸彈,修改一個(gè)數(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ù)
進(jìn)入游戲,玩家只有選擇了要檢查的點(diǎn)才能繼續(xù),這里有三種情況,因?yàn)橐泻芏啻芜x擇,所以采用循環(huán)
(1)選中雷區(qū),那么直接跳出循環(huán),游戲結(jié)束
(2)沒選中雷區(qū),電腦會(huì)掃描周圍的區(qū)域,把周圍無雷的點(diǎn)展開,展開周圍有雷的點(diǎn)
這里還要說一下mine數(shù)組為什么要用‘0'和‘1'來做標(biāo)記,因?yàn)?和1這兩個(gè)字符在ascII碼表里是連續(xù)的,一會(huì)在電腦掃描周圍時(shí)可以直接通過減法算出周圍的雷數(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ù),掃描該點(diǎn)周圍雷數(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、檢測(cè)部分
#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ǐng)點(diǎn)擊《游戲?qū)n}》閱讀
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++判斷一個(gè)點(diǎn)是否在圓內(nèi)的方法
這篇文章主要為大家詳細(xì)介紹了C++判斷一個(gè)點(diǎn)是否在圓內(nèi)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
基于Turbo C(V2.0)編譯錯(cuò)誤信息的詳細(xì)介紹
本篇文章對(duì)Turbo C(V2.0)編譯的錯(cuò)誤信息進(jìn)行了詳細(xì)的介紹。需要的朋友參考下2013-05-05
C語言實(shí)現(xiàn)輸入兩個(gè)數(shù)字將其按從小到大輸出的方法
這篇文章主要介紹了C語言實(shí)現(xiàn)輸入兩個(gè)數(shù)字將其按從小到大輸出的方法,本文通過代碼講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04

