C++實(shí)現(xiàn)掃雷小游戲(控制臺)
本文實(shí)例為大家分享了C++實(shí)現(xiàn)掃雷小游戲的具體代碼,供大家參考,具體內(nèi)容如下
1.問題描述
用c++寫一個(gè)掃雷小游戲,掃雷大家都玩過吧,先任意點(diǎn)一個(gè)方格,沒有爆炸時(shí),會出現(xiàn)一個(gè)數(shù)字,這個(gè)數(shù)字是以它為中心的9個(gè)格子內(nèi)所有雷的個(gè)數(shù)。一般圍在一堆數(shù)字中間的有可能是雷,你在你認(rèn)為是雷的那里右擊,就可以把它設(shè)定為雷,然后在數(shù)字區(qū)用鼠標(biāo)左右鍵雙擊,可以打開非雷區(qū),所有雷被標(biāo)記后,就贏了。
今天我們寫的程序需要能實(shí)現(xiàn)以下幾個(gè)功能
(1).輸入坐標(biāo)打開一個(gè)格子,此格子若是雷則游戲結(jié)束,若不是則顯示周圍雷的個(gè)數(shù)。
(2).輸入坐標(biāo)為格子插上棋子和取消插旗子。
2.設(shè)計(jì)思路
(1)創(chuàng)建兩個(gè)數(shù)組,一個(gè)是開發(fā)者數(shù)組,一個(gè)是玩家數(shù)組。生成兩個(gè)界面,開發(fā)者界面顯示雷和數(shù)字,玩家界面顯示問號和數(shù)字。
(2)初始化兩個(gè)雷陣,然后用隨機(jī)數(shù)布雷。
(3)開始游戲,點(diǎn)到不是雷的地方將周圍無雷的地方展開,如果點(diǎn)到雷游戲結(jié)束。
其他詳細(xì)內(nèi)容見代碼
3.上代碼
#include "pch.h"
#include <iostream>
#include <stdlib.h>?
#include<cstdlib>
#include<ctime>
using namespace std;
int shuzu1[12][12];
char show[12][12];
void wjm()
{
?? ?cout << " ?1 ? ? 2 ? ? 3 ? ? 4 ? ? 5 ? ? 6 ? ? 7 ? ? 8 ? ? 9 ? ?10 ? " << endl << endl;
?? ?for (int j = 1; j < 11; j++)
?? ?{
?? ??? ?cout << " ?" << shuzu1[1][j] << " ?|";
?? ?}
?? ?cout << " ? 1" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
?? ?for (int j = 1; j < 11; j++)
?? ?{
?? ??? ?cout << " ?" << shuzu1[2][j] << " ?|";
?? ?}
?? ?cout << " ? 2" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
?? ?for (int j = 1; j < 11; j++)
?? ?{
?? ??? ?cout << " ?" << shuzu1[3][j] << " ?|";
?
?? ?}
?? ?cout << " ? 3" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
?? ?for (int j = 1; j < 11; j++)
?? ?{
?? ??? ?cout << " ?" << shuzu1[4][j] << " ?|";
?? ?}
?? ?cout << " ? 4" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
?? ?for (int j = 1; j < 11; j++)
?? ?{
?? ??? ?cout << " ?" << shuzu1[5][j] << " ?|";
?? ?}
?? ?cout << " ? 5" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
?? ?for (int j = 1; j < 11; j++)
?? ?{
?? ??? ?cout << " ?" << shuzu1[6][j] << " ?|";
?? ?}
?? ?cout << " ? 6" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
?? ?for (int j = 1; j < 11; j++)
?? ?{
?? ??? ?cout << " ?" << shuzu1[7][j] << " ?|";
?? ?}
?? ?cout << " ? 7" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
?? ?for (int j = 1; j < 11; j++)
?? ?{
?? ??? ?cout << " ?" << shuzu1[8][j] << " ?|";
?? ?}
?? ?cout << " ? 8" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
?? ?for (int j = 1; j < 11; j++)
?? ?{
?? ??? ?cout << " ?" << shuzu1[9][j] << " ?|";
?? ?}
?? ?cout << " ? 9" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
?? ?for (int j = 1; j < 11; j++)
?? ?{
?? ??? ?cout << " ?" << shuzu1[10][j] << " ?|";
?? ?}
?? ?cout << " ? 10" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
}
//開發(fā)者界面
void first()//初始化
{
?? ?for (int i = 0; i < 12; i++)
?? ?{
?? ??? ?for (int j = 0; j < 12; j++)
?? ??? ?{
?? ??? ??? ?shuzu1[i][j] = 0;//開發(fā)者數(shù)組
?? ??? ??? ?
?? ??? ?}
?? ?}
?? ?for (int i = 0; i < 12; i++)
?? ?{
?? ??? ?for (int j = 0; j <12; j++)?
?? ??? ?{
?? ??? ??? ?show[i][j] = '?';//玩家數(shù)組
?? ??? ?}
?? ?}
}
//初始化兩個(gè)雷陣
void jm()//界面
{
?? ?cout << " ?1 ? ? 2 ? ? 3 ? ? 4 ? ? 5 ? ? 6 ? ? 7 ? ? 8 ? ? 9 ? ?10 ? " << endl << endl;
?? ?
?? ?for (int j = 1; j < 11; j++)
?? ?{
?? ??? ?cout << " ?" << show[1][j] << " ?|";
?? ?}
?? ?cout << " ? 1" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
?? ?for (int j = 1; j < 11; j++)
?? ?{
?? ??? ?cout << " ?" << show[2][j] << " ?|";
?? ?}
?? ?cout << " ? 2" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
?? ?for (int j = 1; j < 11; j++)
?? ?{
?? ??? ?cout << " ?" << show[3][j] << " ?|";
?? ?}
?? ?cout << " ? 3" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
?? ?for (int j = 1; j < 11; j++)
?? ?{
?? ??? ?cout << " ?" << show[4][j] << " ?|";
?? ?}
?? ?cout << " ? 4" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
?? ?for (int j = 1; j < 11; j++)
?? ?{
?? ??? ?cout << " ?" << show[5][j] << " ?|";
?? ?}
?? ?cout << " ? 5" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
?? ?for (int j = 1; j < 11; j++)
?? ?{
?? ??? ?cout << " ?" << show[6][j] << " ?|";
?? ?}
?? ?cout << " ? 6" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
?? ?for (int j = 1; j < 11; j++)
?? ?{
?? ??? ?cout << " ?" << show[7][j] << " ?|";
?? ?}
?? ?cout << " ? 7" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
?? ?for (int j = 1; j < 11; j++)
?? ?{
?? ??? ?cout << " ?" << show[8][j] << " ?|";
?? ?}
?? ?cout << " ? 8" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
?? ?for (int j = 1; j < 11; j++)
?? ?{
?? ??? ?cout << " ?" << show[9][j] << " ?|";
?? ?}
?? ?cout << " ? 9" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
?? ?for (int j = 1; j < 11; j++)
?? ?{
?? ??? ?cout << " ?" << show[10][j] << " ?|";
?? ?}
?? ?cout << " ? 10" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
?? ?cout << '\n' << "選項(xiàng)" << '\n' << "提示:輸入橫坐標(biāo)后回車再輸入縱坐標(biāo)\n" << "1-點(diǎn)擊(x,y)" << '\n' << "2-在(x,y)插旗子" << '\n' << "3-取消插旗子(x,y)" << '\n' << "4-老子不玩了" << endl;
}
//玩家界面
void bulei()
{
?? ?srand(time(NULL));
?? ?for (int a=0; a <10; a++)//生成10個(gè)雷
?? ?{
?? ??? ?int m = rand() % 10 + 1;
?? ??? ?int n = rand() % 10 + 1;
?? ??? ?if (shuzu1[m][n] != 9)
?? ??? ?{
?? ??? ??? ?shuzu1[m][n] = 9;
?? ??? ?}
?? ?}
?? ?
?? ?
}
//布雷
void number()
{
?? ?int count = 0;
?? ?for (int x = 1; x < 11; x++)
?? ?{
?? ??? ?for (int y = 1; y < 11; y++)
?? ??? ?{
?? ??? ??? ?if (shuzu1[x][y] == 9)
?? ??? ??? ?{
?? ??? ??? ??? ?if(shuzu1[x - 1][y - 1]!=9)
?? ??? ??? ??? ?shuzu1[x - 1][y-1]++;
?? ??? ??? ??? ?if(shuzu1[x - 1][y]!=9)
?? ??? ??? ??? ?shuzu1[x - 1][y]++;
?? ??? ??? ??? ?if(shuzu1[x - 1][y + 1]!=9)
?? ??? ??? ??? ?shuzu1[x - 1][y + 1]++;
?? ??? ??? ??? ?if(shuzu1[x][y - 1]!=9)
?? ??? ??? ??? ?shuzu1[x][y - 1]++;
?? ??? ??? ??? ?if (shuzu1[x][y + 1] != 9)
?? ??? ??? ??? ?shuzu1[x][y + 1]++;
?? ??? ??? ??? ?if (shuzu1[x + 1][y - 1] != 9)
?? ??? ??? ??? ?shuzu1[x + 1][y - 1]++;
?? ??? ??? ??? ?if (shuzu1[x + 1][y] != 9)
?? ??? ??? ??? ?shuzu1[x + 1][y]++;
?? ??? ??? ??? ?if (shuzu1[x + 1][y + 1] != 9)
?? ??? ??? ??? ?shuzu1[x + 1][y + 1]++;
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ??? ?
}
//生成數(shù)字
void unfold(int x,int y)
{
?? ?if (x >= 1 && x <= 10 && y >= 1 && y <= 10)
?? ?{
?? ??? ?if (shuzu1[x][y] == 0)
?? ??? ?{
?? ??? ??? ?show[x][y] = ' ';
?? ??? ??? ?if (show[x][y + 1] == '?')
?? ??? ??? ??? ?unfold(x, y + 1);
?? ??? ??? ?if (show[x][y - 1] == '?')
?? ??? ??? ??? ?unfold(x, y - 1);
?? ??? ??? ?if (show[x + 1][y] == '?')
?? ??? ??? ??? ?unfold(x + 1, y);
?? ??? ??? ?if (show[x - 1][y] == '?')
?? ??? ??? ??? ?unfold(x - 1, y);
?? ??? ??? ?
?? ??? ?}
?? ??? ?if (shuzu1[x][y] != 0 && shuzu1[x][y] != 9)
?? ??? ?{
?? ??? ??? ?show[x][y] = shuzu1[x][y] + '0';
?? ??? ?}
?? ?}
?? ??? ?
} ? ?
//無雷展開
void flag(int x, int y)
{
?? ?show[x][y] = 'F';
?? ?jm();
}
//插旗子
void unflag(int x, int y)
{
?? ?if (show[x][y] == 'F')
?? ?{
?? ??? ?show[x][y] = '?';
?? ??? ?jm();
?? ?}
?? ?else?
?? ?{
?? ??? ?cout << "錯(cuò)誤";
?? ?}
}
//取消插旗子
void start(int x,int y)
{
?? ?if (shuzu1[x][y] == 9)
?? ?{
?? ??? ?cout << "你輸了";
?? ??? ?exit(0);
?? ?}
?? ?if (shuzu1[x][y] != 9 && shuzu1[x][y] != 0)
?? ?{
?? ??? ?show[x][y] = shuzu1[x][y]+'0';
?? ??? ?jm();
?? ?}
?? ?if (shuzu1[x][y] == 0)
?? ?{
?? ??? ?unfold(x, y);
?? ??? ?jm();
?? ?}
}
//展開格子
void end()
{
?? ?int count = 0;
?? ?for (int i = 1; i <= 10; i++)
?? ?{
?? ??? ?for (int j = 1; j <= 10; j++)
?? ??? ?{
?? ??? ??? ?if (show[i][j] == '?'||show[i][j]=='F')
?? ??? ??? ?{
?? ??? ??? ??? ?count++;
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?if (count == 10)
?? ?{
?? ??? ?cout << "你贏了";
?? ??? ?exit(0);
?? ?}
?? ?
?? ?
}
//結(jié)束游戲
int main()
{
?? ?int x = 5;
?? ?int y = 8;
?? ?int z;
?? ?first();
?? ?bulei();
?? ?number();
?? ?jm();
?? ?for (;;)
?? ?{
?? ??? ?cin >> z;
?? ??? ?switch (z)
?? ??? ?{
?? ??? ??? ?case 1:
?? ??? ??? ?{
?? ??? ??? ?cin >> x >> y;
?? ??? ??? ??? ?if (x >= 1 && x <= 10 && y >= 1 && y <= 10)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?start(x, y);
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?cout << "錯(cuò)誤"; break;
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ?}
?? ??? ?
?? ??? ??? ?}break;
?? ??? ??? ?case 2:
?? ??? ??? ?{
?? ??? ??? ??? ?cin >> x >> y;
?? ??? ??? ??? ?if (x >= 1 && x <= 10 && y >= 1 && y <= 10)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?flag(x, y);
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?cout << "錯(cuò)誤";
?? ??? ??? ??? ?}
?? ??? ??? ?}break;
?? ??? ??? ?case 3:
?? ??? ??? ?{
?? ??? ??? ??? ?cin >> x >> y;
?? ??? ??? ??? ?if (x >= 1 && x <= 10 && y >= 1 && y <= 10)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?unflag(x, y);
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?cout << "錯(cuò)誤";
?? ??? ??? ??? ?}
?? ??? ??? ?}break;
?? ??? ??? ?case 4:
?? ??? ??? ?{
?? ??? ??? ??? ?exit(0);
?? ??? ??? ?}break;
?? ??? ?}
?? ??? ?end();
?? ?}
}4.運(yùn)行結(jié)果部分截圖


以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳細(xì)分析C++ 數(shù)據(jù)封裝和數(shù)據(jù)抽象
這篇文章主要介紹了C++ 數(shù)據(jù)封裝和數(shù)據(jù)抽象的的相關(guān)資料,文中代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06
Linux C 時(shí)間函數(shù)應(yīng)用
本文是關(guān)于Linux C時(shí)間函數(shù) time_t struct tm 進(jìn)行了詳細(xì)的分析介紹并有應(yīng)用實(shí)例,希望能幫到有需要的同學(xué)2016-07-07
c++連接mysql5.6的出錯(cuò)問題總結(jié)
下面小編就為大家?guī)硪黄猚++連接mysql5.6的出錯(cuò)問題總結(jié)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,祝大家游戲愉快哦2016-12-12
c++ 入門——淺析構(gòu)造函數(shù)和析構(gòu)函數(shù)
這篇文章主要介紹了c++ 淺析構(gòu)造函數(shù)和析構(gòu)函數(shù)的相關(guān)資料,幫助大家入門c++ 編程,感興趣的朋友可以了解下2020-08-08

