In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to realize the maze game in Java". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to realize the maze game in Java".
The topic exercises the basic knowledge of C++ object-oriented encapsulation by letting the game characters automatically find the maze exit and get out of the maze. The maze diagram is shown below, where X represents the wall.
1. Program analysis
The principle of going out: follow the right-handed or left-handed rules. If you hold the wall with your right hand, you will walk out of the maze, and vice versa.
Step1 creates maze classes and prints out maze maps.
Step2 creates a class of people who walk the maze.
2. Program realization
MazeMap.h
# ifndef MAZEMAP_H#define MAZEMAP_H#include # include / / COORD// in the 2D array, 1 represents the wall, 0 represents the road # define WALL 1#define ROAD 0class MazeMap {public: MazeMap (char wall ='X'); ~ MazeMap (); / * set the maze map * / void setMazeMap (int* mazemap, int row, int col); void pintMazeMap (); int** getMap () / return the map two-dimensional array pointer const char msqucRoad; / / constant data member: Lu void setExitPosition (int x drawing int y); / / set the maze exit COORD massively COORDExitPOS; / / the maze exit private:const char masked cWall; / / constant data member: Wall int** m_pMap / / the second-level pointer to the two-dimensional array of the maze map, int map endif; / / the number of rows of the two-dimensional array, the number of rows of the two-dimensional array; the number of columns of the two-dimensional array}; # endif
MazeMap.cpp
# include "MazeMap.h" / * * function name: MazeMap () * function function: constructor Initialization parameter * function parameter: symbol of wall wall * modification time: 2016.7.18**/MazeMap::MazeMap (char wall): m_cWall (wall), m_cRoad ('') {m_pMap = NULL } / * * function name: ~ MazeMap () * function function: destructor to free dynamically applied memory space * modification time: 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; }}/*********************************函数名称:setMazeMap()*函数功能:设置迷宫地图,传递参数*函数参数:*mazemap 存储地图数据的二维数组的指针* row 二维数组的行数* col 二维数组的列数*修改时间:2016.7.18**********************************/void MazeMap::setMazeMap(int *mazemap, int row, int col){ m_iMapRow = row; m_iMapCol = col;//为存储迷宫地图的二维数组动态分配内存空间m_pMap = new int*[m_iMapRow]; //分配m_iMapRow个存储int类型指针的内存空间for(int i = 0; i < m_iMapRow; i++) m_pMap[i] = new int[m_iMapCol]; //分配m_iMapCol个存储int类型的内存空间//将二维数组迷宫地图的数据拷贝给二级指针for(int i = 0; i < m_iMapRow; i++) {for(int j = 0; j < m_iMapCol; j++) { m_pMap[i][j] = *mazemap; mazemap++; } }}/*************************************************函数名称:pintMazeMap()*函数功能:打印迷宫地图*修改时间: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]) //数组元素为1,则打印代表墙的字符std::cout getMap()[m_COORDManCurrentPosition.Y + 1][m_COORDManCurrentPosition.X])return false;else moveForward(D);return true;}/*************************************************函数名称:walkLeft()*函数功能:向左走一步,若成功,则返回ture;若失败,则返回false*修改时间:2016.7.18*************************************************/bool MazeMan::walkLeft(){if(m_pMap->GetMap () [m_COORDManCurrentPosition.Y] [m_COORDManCurrentPosition.X-1]) return false;else moveForward (L); return true;} / * function name: walkRight () * function function: take a step to the right and return ture if successful If it fails, the false* modification time is returned: 2016.7.18***/bool MazeMan::walkRight () {if (masks pMap-> getMap () [m_COORDManCurrentPosition.Y] [m_COORDManCurrentPosition.X + 1]) return false;else moveForward (R); return true } / * function name: start () * function function: game start function * modification time: 2016.7.18 games * * / void MazeMan::start () {while (true) {masked pMap-> pintMazeMap () Switch (m_cManFace) {case uplink / game character facing up, first right, then up, then left, and finally downward (right hand principle) 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_COORDManCurrentPosition.X * man = new MazeMan (); instantiate the game character object MazeMan * man = new MazeMan () from the heap / / set the starting position of the character man- > setPosition (0Magne9); / / set the map of the game to go man- > setMap (mm); / / for objects instantiated from the heap by new, you need to manually release memory man- > start () after use; delete mm; mm = NULL;delete man; man = NULL;std::cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 205
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.