C++学习一走出迷宫

来源:互联网 发布:软件著作权资料 编辑:程序博客网 时间:2024/06/05 18:08

main.cpp

//文件中的格式为 前两位数字代表地图 (高度与宽度) 然后两位是 (起点坐标) 然后两位是(终点坐标)//地图上如果是0代表不能走  1代表能走//真人认知算法#define FILENAME "map.txt"#include "declear.h"#include <stdlib.h>#include <iostream>#include <Windows.h>using namespace std;using namespace mg;struct mapHW {int high;int wide;};struct Coor {int x;int y;};int dirction;//1 2 3 4void Goto(Coor pos) {COORD coor;coor.X = pos.x;coor.Y = pos.y;HANDLE hand = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorPosition(hand, coor);}int main(void) {Coor coorBegin,//起始点坐标coorEnd,//终点坐标coorNow;//现在的坐标Person man;//申请人物mapHW maphw;//存储地图高度宽度信息Coordinate CoorTemp;FILE * fp;//文件指针if ( fopen_s(&fp, FILENAME, "r")) {cout << "地图文件无法开启!" << endl;return 0;}fscanf_s(fp, "%d %d %d %d %d %d", &maphw.high, &maphw.wide,&coorBegin.x,&coorBegin.y,&coorEnd.x,&coorEnd.y);//读取地图初始数据Map map_Mg(maphw.high, maphw.wide); //地图对象申请CoorTemp.setX(coorBegin.x);CoorTemp.setY(coorBegin.y);map_Mg.setBegin(CoorTemp);//设置地图开始CoorTemp.setX(coorEnd.x);CoorTemp.setY(coorEnd.y);map_Mg.setEnd(CoorTemp);//设置地图终止位置//-------------------------------------------------------------------------------------------地图初始化for (int i = 0; i < maphw.wide + (maphw.high - 1) * maphw.wide; i++) {//读入输出地图内容fscanf_s(fp, "%d", &map_Mg.getpMap()[i]);//一次读入一个数字if (i % maphw.wide == 0 && i !=0 )//换行cout << endl;if (!map_Mg.getpMap()[i])cout << "*";elsecout << " ";}fclose(fp);//--------------------------------------------------------------------------------------------起始点位置放置人物Goto(coorBegin);man.SetInitPos(coorBegin.x, coorBegin.y);//设置人物现在的位置coorNow = coorBegin;//-------------------------------------------------------------------------------------------走出迷宫while (1) {Up:if (dirction == 1) {dirction = 0;goto Left;}if (man.Up(false, map_Mg)) {Sleep(1000);dirction = 1;//cout << "向上";//testman.Up(true, map_Mg);coorNow.y -= 1;Goto(coorNow);if (map_Mg.JudWin(man.GotNowPos()))break;goto Up;}Right:if (dirction == 2){dirction = 0;goto Up;}if (man.Right(false, map_Mg)) {Sleep(1000);dirction = 2;//cout << "向右";//testman.Right(true, map_Mg);coorNow.x += 1;Goto(coorNow);if (map_Mg.JudWin(man.GotNowPos())) {break;}goto Right;}Down:if (dirction == 3) {dirction = 0;goto Right;}if (man.Down(false, map_Mg)) {Sleep(1000);dirction = 3;//cout << "向下";//testman.Down(true, map_Mg);coorNow.y += 1;Goto(coorNow);if (map_Mg.JudWin(man.GotNowPos()))break;goto Down;}Left:if (dirction == 4) {dirction = 0;goto Down;}if (man.Left(false, map_Mg)) {Sleep(1000);dirction = 4;//cout << "向左";//testman.Left(true, map_Mg);coorNow.x -= 1;Goto(coorNow);if (map_Mg.JudWin(man.GotNowPos()))break;goto Left;}}system("pause");return 0;}


define.cpp

#include "declear.h"#include <iostream>using namespace std;using namespace mg;//-----------------------------------------------------Coordinateint Coordinate::getX(void) {return m_iX;}int Coordinate::getY(void) {return m_iY;}void Coordinate::setX(int x) {m_iX = x;}void Coordinate::setY(int y) {m_iY = y;}//-----------------------------------------------------mapMap::Map(int high, int wide) : m_iHight(high), m_iWide(wide), m_pMap(new int[high * wide]) {}Map::~Map() {delete[]m_pMap;}bool Map::CanRun(Coordinate coor) {if (m_pMap[coor.getX() + m_iWide * coor.getY()] == 1) {if (coor.getX() >= 0 && coor.getY() >= 0 && coor.getX() < m_iWide && coor.getY() < m_iWide)//在地图区域内return true;}else {return false;}}int * Map::getpMap(void) {return m_pMap;}bool Map::JudWin(Coordinate coor) {if (coor.getX() == m_coorEnd.getX() && coor.getY() == m_coorEnd.getY()) {cout << "you win";return true;}elsereturn false;}int Map::getHigh(void) {return m_iHight;}int Map::getWige(void) {return m_iWide;}void Map::setBegin(Coordinate coor) {m_coorBegin.setX(coor.getX());m_coorBegin.setY(coor.getY());}void Map::setEnd(Coordinate coor) {m_coorEnd.setX(coor.getX());m_coorEnd.setY(coor.getY());}//-----------------------------------------------------Personbool Person::Up(bool run,Map &map) {if (run) {m_coorPerson.setY(m_coorPerson.getY() - 1);return true;}Coordinate temp = m_coorPerson;temp.setY(temp.getY() - 1);if (map.CanRun(temp))return true;elsereturn false;}bool Person::Down(bool run,Map & map) {if (run) {m_coorPerson.setY(m_coorPerson.getY() + 1);return true;}Coordinate temp = m_coorPerson;temp.setY(temp.getY() + 1);if (map.CanRun(temp))return true;elsereturn false;}bool Person::Left(bool run, Map & map) {if (run) {m_coorPerson.setX(m_coorPerson.getX() - 1);return true;}Coordinate temp = m_coorPerson;temp.setX(temp.getX() - 1);if (map.CanRun(temp))return true;elsereturn false;}bool Person::Right(bool run, Map & map) {if (run) {m_coorPerson.setX(m_coorPerson.getX() + 1);return true;}Coordinate temp = m_coorPerson;temp.setX(temp.getX() + 1);if (map.CanRun(temp))return true;elsereturn false;}void Person::SetInitPos(int x, int y) {m_coorPerson.setX(x);m_coorPerson.setY(y);}Coordinate Person::GotNowPos(void) {return m_coorPerson;}

declear.h

#pragma oncenamespace mg {class Coordinate {private:int m_iX;//X坐标int m_iY;//Y坐标public:int getX();int getY();void setX(int x);void setY(int y);};class Map {public:Map(int high, int wide);//构造函数申请地图内存空间~Map();//析构函数bool CanRun(Coordinate coor);//判断该位置是否可以行走bool JudWin(Coordinate coor);//判断是否到了终点int * getpMap(void);//获取地图内存区域指针int getHigh(void);//获取地图高度int getWige(void);//获取地图宽度void setBegin(Coordinate coor);//设置起点位置void setEnd(Coordinate coor);//设置终点private:int * m_pMap;//地图内存区域指针const int m_iHight;//地图高度const int m_iWide;//地图宽度Coordinate m_coorBegin;Coordinate m_coorEnd;};class Person {public://参数是是否要走,返回值是是不是能走。bool Up(bool run, Map &map);//上走bool Down(bool run, Map &map);//下走bool Left(bool run, Map &map);//左走bool Right(bool run, Map &map);//右走void SetInitPos(int x, int y);//设置人物的初始位置Coordinate GotNowPos(void);//获取人物现在的位置private:Coordinate m_coorPerson;//在地图中的位置};}


0 0
原创粉丝点击