Skip to content

Instantly share code, notes, and snippets.

@YutaSeya
Created May 9, 2019 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YutaSeya/7b9f8cf4133f9ed42962a5957baf84e8 to your computer and use it in GitHub Desktop.
Save YutaSeya/7b9f8cf4133f9ed42962a5957baf84e8 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include "map.h"
#include "maze.h"
int main()
{
Position mypos;
mypos.init();
Maze *maze = new Maze( 7, 7 );
ExistWall exist;
exist.north = false;
exist.east = false;
exist.south = false;
exist.west = false;
while( mypos.x != 7 || mypos.x != 7 ){
uint8_t next = maze->getNextAction( &mypos, &exist );
maze->show( mypos );
}
maze->show( mypos );
return 0;
}
/**
* maze.cpp
* @author yuta seya
* @date 2019 3.13
*/
#include "maze.h"
#include <cstdio>
#include <queue>
#include <utility>
#include "mazeConf.h"
/**
* @override Maze
* @brief 迷路クラスのコンストラクタ
* @param なし
* @return なし
* @detail mapクラスのインスタンスを得る,壁情報を初期化
*/
Maze::Maze()
{
map = new Map();
map->init();
}
/**
* @override Maze
* @brief 迷路クラスのコンストラクタ
* @param uint8_t _gx マウスのゴール座標
* @param uint8_t _gy マウスのゴール座標
* @return なし
* @detail mapクラスのインスタンスを得る,壁情報を初期化,ゴール座標の設定
*/
Maze::Maze( uint8_t _gx, uint8_t _gy )
{
gx = _gx;
gy = _gy;
map = new Map();
map->init();
}
/**
* @brief 迷路クラスのデストラクタ
* @param なし
* @return なし
*/
Maze::~Maze()
{
}
/**
* @brief ゴール座標を設定
* @param uint8_t _gx マウスのゴール座標
* @param uint8_t _gy マウスのゴール座標
* @return なし
*/
void Maze::setGoal( uint8_t _gx, uint8_t _gy )
{
gx = _gx;
gy = _gy;
}
/**
* @brief 迷路観戦のアップデートをしたのち、次の動作を返す
* @param Position pos マウスの座標、向きの情報をもった構造体
* @param ExsitWall exist 壁の有無の情報を持った構造体
* @return 次の動作
* @detail 壁情報の追加,歩数マップの更新,次の動作を決定,マウスの座標の更新を行ったのち
* 次の動作を返す
*/
uint8_t Maze::getNextAction( Position *pos, ExistWall *exist )
{
uint8_t next = Front;
if ( start ){
updatePosition( pos, 0 );
start = false;
} else {
map->addWall( pos->x, pos-> y, exist );
updateStepMap();
next = updateNextAction( pos );
updatePosition( pos, next );
}
return next;
}
/**
* @brief 壁情報、歩数マップを表示する
* @param Position pos マウスの座標、向きの情報をもった構造体
* @param ExsitWall exist 壁の有無の情報を持った構造体
* @return 次の動作
*/
void Maze::show( Position pos )
{
int x,y;
updateStepMap();
std::printf("\r\n");
printf(" ");
for ( x = 0; x < 17; x++ ){
if( x < 10 ) std::printf("%d ",x);
else std::printf("%d ",x);
}
std::printf("\n");
for ( y = 15; y >= 0; y-- ){
if ( y < 9 ) std::printf(" %d+",y+1);
else std::printf("%d+",y+1);
for ( x = 0; x < 16; x++ ){
if ( map->getData( x, y, North ) ){
std::printf("-----");
} else {
std::printf(" ");
}
std::printf("+");
}
std::printf("\r\n");
for ( x = 0; x < 16; x++ ){
if ( map->getData( x, y, West) ){
if ( x == 0 ) std::printf(" |");
else std::printf("|");
} else {
if ( x == 0 ) std::printf(" ");
std::printf(" ");
}
// step を表示
if ( pos.x == x && pos.y == y ){
std::printf( " M ");
} else if ( step[x][y] == 0 ) {
std::printf( " G ");
} else {
std::printf("%5d",step[x][y] );
}
}
if ( map->getData( 15, y, East ) ){
std::printf("|");
} else {
std::printf(" ");
}
std::printf("\r\n");
}
std::printf(" 0+");
for( x = 0; x < 16; x++ ){
if ( map->getData( x, 0, South ) ){
std::printf("-----");
} else {
std::printf(" ");
}
std::printf("+");
}
std::printf("\r\n");
}
/**
* @brief 歩数マップの更新を行う
* @param なし
* @return なし
* @detail キューを使用した歩数マップの更新
*/
void Maze::updateStepMap()
{
for ( int x = 0; x < 16; x++ ){
for ( int y = 0; y < 16; y++ ){
step[x][y] = MAX_STEP;
}
}
step[gx][gy] = 0;
std::queue<std::pair<uint8_t, uint8_t>> q;
std::pair<uint8_t, uint8_t> p;
q.push(std::pair<uint8_t, uint8_t>(gx,gy) );
while( !q.empty() ){
p = q.front();
uint8_t x = p.first;
uint8_t y = p.second;
q.pop();
if( y < 15 && !map->getData(x,y,North) && step[x][y+1] == MAX_STEP ){
step[x][y+1] = step[x][y] + 1;
q.push(std::pair<uint8_t, uint8_t>(x,y+1));
}
if ( x < 15 && !map->getData(x,y,East) && step[x+1][y] == MAX_STEP ){
step[x+1][y] = step[x][y] + 1;
q.push(std::pair<uint8_t, uint8_t>(x+1,y));
}
if ( y > 0 && !map->getData(x,y,South) && step[x][y-1] == MAX_STEP ){
step[x][y-1] = step[x][y] + 1;
q.push(std::pair<uint8_t, uint8_t>(x,y-1));
}
if ( x > 0 && !map->getData(x,y,West) && step[x-1][y] == MAX_STEP ){
step[x-1][y] = step[x][y] + 1;
q.push(std::pair<uint8_t, uint8_t>(x-1,y));
}
}
}
/**
* @brief 次の動作を決める
* @param Position pos マウスの座標、向き
* @return なし
* @detail 現在の向き、方向から一番歩数の少ないほうへ進む
*/
uint8_t Maze::updateNextAction( Position *pos )
{
// 考えてみてください!
// return は、Front, Left, Rear,Rightと次の動作を返します。
}
/**
* @brief マシンの座標を更新する
* @param Position pos マウスの座標、向き
* @return なし
* @detail 次の動作からマシンの方向、座標を更新する
*/
void Maze::updatePosition( Position *pos, uint8_t action )
{
pos->direction = (pos->direction + action) %4;
if ( pos->direction == North ){
pos->y++;
} else if ( pos->direction == West ){
pos->x--;
} else if ( pos->direction == South ){
pos->y--;
} else {
pos->x++;
}
}
/**
* maze.h
* @author yuta seya
* @date 2019 3.13
*/
#ifndef __MAZE__H
#define __MAZE__H
#include <stdint.h>
#include "mazeConf.h"
#include "map.h"
struct Position
{
public:
uint8_t x;
uint8_t y;
uint8_t direction;
void init()
{
x = 0;
y = 0;
direction = 0;
}
};
class Maze {
private:
#define MAX_STEP 0xffff
Map *map;
uint8_t gx = 0;
uint8_t gy = 0;
uint16_t step[16][16];
bool start = true;
public:
// コンストラクタ
Maze();
Maze( uint8_t _gx, uint8_t _gy );
// デストラクタ
~Maze();
// ゴール座標をセットする
void setGoal( uint8_t _gx, uint8_t _gy );
// 迷路情報のアップデートを行い、次の動作を返す
uint8_t getNextAction( Position *pos, ExistWall *exist );
// 歩数マップと壁情報を表示する
void show( Position pos);
// スタート動作かどうかのフラグをセットする
void setStartFlag( bool _flag )
{
start = _flag;
}
private:
// 歩数マップを更新
void updateStepMap();
// 次の動作を決める
uint8_t updateNextAction( Position *pos );
// マシンの座標を更新する
void updatePosition( Position *pos, uint8_t action );
};
#endif /* __MAZE__H */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment