這篇文章主要講解了“Java如何實(shí)現(xiàn)迷宮游戲”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“Java如何實(shí)現(xiàn)迷宮游戲”吧!
創(chuàng)新互聯(lián)公司基于成都重慶香港及美國(guó)等地區(qū)分布式IDC機(jī)房數(shù)據(jù)中心構(gòu)建的電信大帶寬,聯(lián)通大帶寬,移動(dòng)大帶寬,多線BGP大帶寬租用,是為眾多客戶提供專業(yè)服務(wù)器托管報(bào)價(jià),主機(jī)托管價(jià)格性價(jià)比高,為金融證券行業(yè)成都服務(wù)器托管,ai人工智能服務(wù)器托管提供bgp線路100M獨(dú)享,G口帶寬及機(jī)柜租用的專業(yè)成都idc公司。
題目 通過(guò)讓游戲角色自動(dòng)尋找迷宮出口,走出迷宮,來(lái)練習(xí)C++面向?qū)ο笾庋b的基礎(chǔ)知識(shí)。迷宮圖如下所示,其中X表示墻。
1、程序分析
走出去的原理:遵循右手規(guī)則或左手規(guī)則。右手扶墻走,就會(huì)走出迷宮,反之,亦然。
step1 創(chuàng)建迷宮類,打印出迷宮地圖。
step2 創(chuàng)建走迷宮的人的類。
2、程序?qū)崿F(xiàn)
MazeMap.h
#ifndef MAZEMAP_H#define MAZEMAP_H#include#include //COORD//在二維數(shù)組里,1代表墻,0代表路#define WALL 1#define ROAD 0class MazeMap{ public: MazeMap(char wall = 'X'); ~MazeMap();/*設(shè)置迷宮地圖*/void setMazeMap(int *mazemap, int row, int col);void pintMazeMap();int** getMap(); //返回地圖二維數(shù)組指針const char m_cRoad; //常數(shù)據(jù)成員:路void setExitPosition(int x,int y); //設(shè)置迷宮出口 COORD m_COORDExitPostion; //迷宮的出口private:const char m_cWall; //常數(shù)據(jù)成員:墻int** m_pMap; //指向迷宮地圖二維數(shù)組的二級(jí)指針int m_iMapRow; //二維數(shù)組的行數(shù)int m_iMapCol; //二維數(shù)組的列數(shù)};#endif
MazeMap.cpp
#include "MazeMap.h"/************************** *函數(shù)名稱:MazeMap() *函數(shù)功能:構(gòu)造函數(shù),初始化參數(shù) *函數(shù)參數(shù):wall 墻的表示符號(hào) *修改時(shí)間:2016.7.18 ***************************/MazeMap::MazeMap(char wall):m_cWall(wall),m_cRoad(' ') { m_pMap = NULL; }/************************* *函數(shù)名稱:~MazeMap() *函數(shù)功能:析構(gòu)函數(shù),釋放動(dòng)態(tài)申請(qǐng)的內(nèi)存空間 *修改時(shí)間:2016.7.18 **************************/MazeMap::~MazeMap() {if(m_pMap) {for(int i = 0; i < m_iMapRow; i++) { delete m_pMap[i]; m_pMap[i] = NULL; } delete m_pMap; } }/******************************** *函數(shù)名稱:setMazeMap() *函數(shù)功能:設(shè)置迷宮地圖,傳遞參數(shù) *函數(shù)參數(shù):*mazemap 存儲(chǔ)地圖數(shù)據(jù)的二維數(shù)組的指針 * row 二維數(shù)組的行數(shù) * col 二維數(shù)組的列數(shù) *修改時(shí)間:2016.7.18 **********************************/void MazeMap::setMazeMap(int *mazemap, int row, int col) { m_iMapRow = row; m_iMapCol = col;//為存儲(chǔ)迷宮地圖的二維數(shù)組動(dòng)態(tài)分配內(nèi)存空間m_pMap = new int*[m_iMapRow]; //分配m_iMapRow個(gè)存儲(chǔ)int類型指針的內(nèi)存空間for(int i = 0; i < m_iMapRow; i++) m_pMap[i] = new int[m_iMapCol]; //分配m_iMapCol個(gè)存儲(chǔ)int類型的內(nèi)存空間//將二維數(shù)組迷宮地圖的數(shù)據(jù)拷貝給二級(jí)指針for(int i = 0; i < m_iMapRow; i++) {for(int j = 0; j < m_iMapCol; j++) { m_pMap[i][j] = *mazemap; mazemap++; } } }/************************************************ *函數(shù)名稱:pintMazeMap() *函數(shù)功能:打印迷宮地圖 *修改時(shí)間:2016.7.18 *************************************************/void MazeMap::pintMazeMap() { system("cls");for(int i = 0; i < m_iMapRow; i++) {for(int j = 0; j < m_iMapCol; j++) {if(m_pMap[i][j]) //數(shù)組元素為1,則打印代表墻的字符std::cout << m_cWall;else //否則,打印代表路的字符std::cout << m_cRoad; } std::cout << std::endl; } }/************************************************ *函數(shù)名稱:getMap() *函數(shù)功能:返回地圖二維數(shù)組指針 *返 回 值:二級(jí)指針 *修改時(shí)間:2016.7.18 *************************************************/int** MazeMap::getMap() {return m_pMap; }/************************************************ *函數(shù)名稱:setExitPosition() *函數(shù)功能:設(shè)置迷宮的出口 *函數(shù)參數(shù):x 迷宮出口位置所在行數(shù) y 迷宮出口位置所在列數(shù) *修改時(shí)間:2016.7.18 *************************************************/void MazeMap::setExitPosition(int x, int y) { m_COORDExitPostion.X = x; m_COORDExitPostion.Y = y; }
MazeMan.h
#ifndef MAZEMAN_H#define MAZEMAN_H#include#include "MazeMap.h"enum direction{U,D,L,R};class MazeMan{ public: MazeMan(char man = 'T',char manface = R);void setPosition(int x, int y); //設(shè)置游戲角色位置void setMap(MazeMap *map); //設(shè)置地圖對(duì)象bool walkUp(); //向上走bool walkDown(); //向下走bool walkLeft(); //向左走bool walkRight(); //向右走void moveForward(direction direct); //根據(jù)傳入的方向讓游戲角色前進(jìn)一步 void start(); //游戲開始函數(shù)private:char m_cMan; //代表游戲角色的字符char m_cManFace; //游戲角色的朝向int m_iSteps; //記錄游戲角色已經(jīng)走的步數(shù) COORD m_COORDManCurrentPosition; //游戲角色的當(dāng)前位置MazeMap *m_pMap; //地圖對(duì)象};#endif
MazeMan.cpp
#include "MazeMan.h"/************************* *函數(shù)名稱:MazeMan() *函數(shù)功能:構(gòu)造函數(shù),初始化數(shù)據(jù)成員 *函數(shù)參數(shù):man 表示游戲角色的字符 manface 游戲角色的朝向 *修改時(shí)間:2016.7.18 **************************/MazeMan::MazeMan(char man,char manface) { m_cMan = man; m_cManFace = manface; m_iSteps = 0; }/******************************* *函數(shù)名稱:setMap() *函數(shù)功能:設(shè)置地圖對(duì)象中的地圖 *函數(shù)參數(shù):*map 地圖對(duì)象的指針 *修改時(shí)間:2016.7.18 ********************************/void MazeMan::setMap(MazeMap *map) { m_pMap = map; }/****************************** *函數(shù)名稱:setPosition() *函數(shù)功能:設(shè)置游戲角色位置 *函數(shù)參數(shù):x 角色所在位置的橫坐標(biāo) y 角色所在位置的縱坐標(biāo) *修改時(shí)間:2016.7.18 ********************************/void MazeMan::setPosition(int x, int y) { unsigned long numWritten;//獲得命令行窗口的窗口句柄HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); m_COORDManCurrentPosition.X = x; m_COORDManCurrentPosition.Y = y;//在指定坐標(biāo)位置填充指定的字符,這里我們?cè)谟螒蚪巧漠?dāng)前位置填充代表游戲角色的字符,使游戲角色采出現(xiàn)FillConsoleOutputCharacter(handle,m_cMan,1,m_COORDManCurrentPosition,&numWritten);//參數(shù)說(shuō)明:控制臺(tái)屏幕緩沖區(qū)句柄,要向控制臺(tái)緩沖區(qū)寫入的字符;應(yīng)寫入的字符單元數(shù);//一個(gè)COORD結(jié)構(gòu),它指定字符的字符是要寫的第一個(gè)單元格的坐標(biāo);//指向接收的實(shí)際寫入控制臺(tái)屏幕緩沖區(qū)的字符數(shù)的變量的指針。}/************************************************ *函數(shù)名稱:moveForward() *函數(shù)功能:根據(jù)傳入的方向讓游戲角色前進(jìn)一步 *函數(shù)參數(shù):direct 移動(dòng)的方向 *修改時(shí)間:2016.7.18 *************************************************/void MazeMan::moveForward(direction direct) { unsigned long numWritten;//獲取命令行窗口的窗口句柄HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);//在指定坐標(biāo)位置填充指定的字符,這里我們?cè)谟螒蚪巧漠?dāng)前位置填充空格,使游戲角色消失FillConsoleOutputCharacter(handle,m_pMap->m_cRoad,1,m_COORDManCurrentPosition,&numWritten);switch(direct) {case U: m_COORDManCurrentPosition.Y -= 1;break;case D: m_COORDManCurrentPosition.Y += 1;break;case L: m_COORDManCurrentPosition.X -= 1;break;case R: m_COORDManCurrentPosition.X += 1;break;default:break; }//游戲角色坐標(biāo)更新,在新的坐標(biāo)FillConsoleOutputCharacter(handle,m_cMan,1,m_COORDManCurrentPosition,&numWritten);//移動(dòng)完成后跟新游戲角色的朝向m_cManFace = direct; }/************************************************ *函數(shù)名稱:walkUp() *函數(shù)功能:向上走一步,若成功,則返回ture;若失敗,則返回false *修改時(shí)間:2016.7.18 *************************************************/bool MazeMan::walkUp() {if(m_pMap->getMap()[m_COORDManCurrentPosition.Y - 1][m_COORDManCurrentPosition.X])return false;else moveForward(U);return true; }/************************************************ *函數(shù)名稱:walkDown() *函數(shù)功能:向下走一步,若成功,則返回ture;若失敗,則返回false *修改時(shí)間:2016.7.18 *************************************************/bool MazeMan::walkDown() {if(m_pMap->getMap()[m_COORDManCurrentPosition.Y + 1][m_COORDManCurrentPosition.X])return false;else moveForward(D);return true; }/************************************************ *函數(shù)名稱:walkLeft() *函數(shù)功能:向左走一步,若成功,則返回ture;若失敗,則返回false *修改時(shí)間:2016.7.18 *************************************************/bool MazeMan::walkLeft() {if(m_pMap->getMap()[m_COORDManCurrentPosition.Y][m_COORDManCurrentPosition.X - 1])return false;else moveForward(L);return true; }/************************************************ *函數(shù)名稱:walkRight() *函數(shù)功能:向右走一步,若成功,則返回ture;若失敗,則返回false *修改時(shí)間:2016.7.18 *************************************************/bool MazeMan::walkRight() {if(m_pMap->getMap()[m_COORDManCurrentPosition.Y][m_COORDManCurrentPosition.X + 1])return false;else moveForward(R);return true; }/************************************************ *函數(shù)名稱:start() *函數(shù)功能:游戲開始函數(shù) *修改時(shí)間:2016.7.18 *************************************************/void MazeMan::start() {while(true) { m_pMap->pintMazeMap();switch(m_cManFace) {case U://游戲角色朝上時(shí),先向右,再向上,再向左,最后向下(右手原則)walkRight() || walkUp() || walkLeft() || walkDown();break;case D: walkLeft() || walkDown() || walkRight() || walkUp();break;case L: walkUp() || walkLeft() || walkDown() || walkRight();break;case R: walkDown() || walkRight() || walkUp() || walkLeft();break;default:break; } m_iSteps++;if(m_COORDManCurrentPosition.X == m_pMap->m_COORDExitPostion.X && m_COORDManCurrentPosition.Y == m_pMap->m_COORDExitPostion.Y)break; std::cout << "已經(jīng)走了" << m_iSteps - 1 << "步"; Sleep(500); } }
Main.cpp
#include "MazeMap.h"#include "MazeMan.h"#define MapRow 11 //迷宮地圖的行數(shù)#define MapCol 11 //迷宮地圖的列數(shù)int main() {//定義并初始化存儲(chǔ)地圖數(shù)據(jù)的二維數(shù)組int map[MapRow][MapCol] = { {WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL}, {WALL, ROAD, ROAD, ROAD, WALL, ROAD, ROAD, ROAD, ROAD, ROAD, ROAD}, {WALL, ROAD, WALL, WALL, WALL, ROAD, WALL, WALL, WALL, ROAD, WALL}, {WALL, ROAD, ROAD, ROAD, WALL, ROAD, ROAD, ROAD, WALL, ROAD, WALL}, {WALL, ROAD, WALL, ROAD, WALL, ROAD, WALL, ROAD, WALL, WALL, WALL}, {WALL, ROAD, WALL, ROAD, ROAD, ROAD, WALL, ROAD, WALL, ROAD, WALL}, {WALL, ROAD, WALL, WALL, WALL, ROAD, WALL, WALL, WALL, ROAD, WALL}, {WALL, ROAD, WALL, ROAD, WALL, ROAD, ROAD, ROAD, WALL, ROAD, WALL}, {WALL, WALL, WALL, ROAD, WALL, WALL, WALL, ROAD, WALL, ROAD, WALL}, {ROAD, ROAD, ROAD, ROAD, ROAD, ROAD, ROAD, ROAD, ROAD, ROAD, WALL}, {WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL} };//從堆中實(shí)例化地圖對(duì)象MazeMap *mm = new MazeMap(); mm->setMazeMap(&map[0][0],MapRow,MapCol); mm->setExitPosition(10,1); mm->pintMazeMap();//從堆中實(shí)例化游戲角色對(duì)象MazeMan *man = new MazeMan();//設(shè)置角色起始位置man->setPosition(0,9);//設(shè)置游戲要走的地圖man->setMap(mm);//用new的方式從堆中實(shí)例化的對(duì)象,使用完畢之后需要手動(dòng)釋放內(nèi)存man->start();delete mm; mm = NULL;delete man; man = NULL;std::cout << "闖關(guān)成功!" << std::endl; system("pause"); }
3、運(yùn)行結(jié)果
T代表人
感謝各位的閱讀,以上就是“Java如何實(shí)現(xiàn)迷宮游戲”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)Java如何實(shí)現(xiàn)迷宮游戲這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!