MicroMouse Maze Library  3703225
公開メンバ関数 | 限定公開メンバ関数 | 限定公開変数類 | フレンド | 全メンバ一覧
MazeLib::Maze クラス

迷路の壁情報を管理するクラス [詳解]

#include <Maze.h>

MazeLib::Maze 連携図
Collaboration graph
[凡例]

公開メンバ関数

 Maze (const Positions &goals=Positions(), const Position start=Position(0, 0))
 デフォルトコンストラクタ [詳解]
 
void reset (const bool set_start_wall=true, const bool set_range_full=false)
 迷路の初期化。壁を削除し、スタート区画を既知に [詳解]
 
bool isWall (const WallIndex i) const
 壁の有無を返す [詳解]
 
bool isWall (const Position p, const Direction d) const
 
bool isWall (const int8_t x, const int8_t y, const Direction d) const
 
void setWall (const WallIndex i, const bool b)
 壁を更新をする [詳解]
 
void setWall (const Position p, const Direction d, const bool b)
 
void setWall (const int8_t x, const int8_t y, const Direction d, const bool b)
 
bool isKnown (const WallIndex i) const
 壁が探索済みかを返す [詳解]
 
bool isKnown (const Position p, const Direction d) const
 
bool isKnown (const int8_t x, const int8_t y, const Direction d) const
 
void setKnown (const WallIndex i, const bool b)
 壁の既知を更新する [詳解]
 
void setKnown (const Position p, const Direction d, const bool b)
 
void setKnown (const int8_t x, const int8_t y, const Direction d, const bool b)
 
bool canGo (const WallIndex i) const
 通過可能かどうかを返す [詳解]
 
bool canGo (const Position p, const Direction d) const
 
bool canGo (const WallIndex &i, bool knownOnly) const
 
bool updateWall (const Position p, const Direction d, const bool b, const bool pushRecords=true)
 既知の壁情報と照らしあわせながら、壁を更新する関数 [詳解]
 
void resetLastWalls (const int num, const bool set_start_wall=true)
 直前に更新した壁を見探索状態にリセットする [詳解]
 
int8_t wallCount (const Position p) const
 引数区画の壁の数を返す [詳解]
 
int8_t unknownCount (const Position p) const
 引数区画に隣接する未知壁の数を返す [詳解]
 
void print (std::ostream &os=std::cout, const int mazeSize=MAZE_SIZE) const
 迷路の表示 [詳解]
 
void print (const Directions &dirs, const Position start=Position(0, 0), std::ostream &os=std::cout, const int mazeSize=MAZE_SIZE) const
 パス付きの迷路の表示 [詳解]
 
void print (const Positions &positions, std::ostream &os=std::cout, const int mazeSize=MAZE_SIZE) const
 位置のハイライト付きの迷路の表示 [詳解]
 
bool parse (std::istream &is)
 特定の迷路の文字列(*.maze ファイル)から壁をパースする [詳解]
 
bool parse (const std::string &filepath)
 
bool parse (const std::vector< std::string > &data, const int mazeSize)
 配列から迷路を読み込むパーサ [詳解]
 
void setGoals (const Positions &goals)
 ゴール区画の集合を更新 [詳解]
 
void setStart (const Position start)
 スタート区画を更新 [詳解]
 
const PositionsgetGoals () const
 ゴール区画の集合を取得 [詳解]
 
const PositiongetStart () const
 スタート区画を取得 [詳解]
 
const WallRecordsgetWallRecords () const
 壁ログを取得 [詳解]
 
int8_t getMinX () const
 既知部分の迷路サイズを返す。計算量を減らすために使用。 [詳解]
 
int8_t getMinY () const
 
int8_t getMaxX () const
 
int8_t getMaxY () const
 
bool backupWallRecordsToFile (const std::string &filepath, const bool clear=false)
 壁ログをファイルに追記保存する関数 [詳解]
 
bool restoreWallRecordsFromFile (const std::string &filepath)
 壁ログファイルから壁情報を復元する関数 [詳解]
 

限定公開メンバ関数

bool isWallBase (const std::bitset< WallIndex::SIZE > &wall, const WallIndex i) const
 壁の確認のベース関数。迷路外を参照すると壁ありと返す。 [詳解]
 
void setWallBase (std::bitset< WallIndex::SIZE > &wall, const WallIndex i, const bool b) const
 壁の更新のベース関数。迷路外を参照すると無視される。 [詳解]
 

限定公開変数類

std::bitset< WallIndex::SIZEwall
 壁情報 [詳解]
 
std::bitset< WallIndex::SIZEknown
 壁の既知未知情報 [詳解]
 
Positions goals
 ゴール区画の集合 [詳解]
 
Position start
 スタート区画 [詳解]
 
WallRecords wallRecords
 更新した壁のログ [詳解]
 
int8_t min_x
 既知壁の最小区画 [詳解]
 
int8_t min_y
 既知壁の最小区画 [詳解]
 
int8_t max_x
 既知壁の最大区画 [詳解]
 
int8_t max_y
 既知壁の最大区画 [詳解]
 
int wallRecordsBackupCounter
 壁ログバックアップのカウンタ [詳解]
 

フレンド

std::istream & operator>> (std::istream &is, Maze &maze)
 入力ストリームの迷路データをパースする [詳解]
 

詳解

迷路の壁情報を管理するクラス

構築子と解体子

◆ Maze()

MazeLib::Maze::Maze ( const Positions goals = Positions(),
const Position  start = Position(0, 0) 
)
inline

デフォルトコンストラクタ

引数
goalsゴール区画の集合
startスタート区画
655  : goals(goals), start(start) {
656  reset();
657  }
Position start
スタート区画
Definition: Maze.h:860
Positions goals
ゴール区画の集合
Definition: Maze.h:859
void reset(const bool set_start_wall=true, const bool set_range_full=false)
迷路の初期化。壁を削除し、スタート区画を既知に
Definition: Maze.cpp:108
呼び出し関係図:

関数詳解

◆ backupWallRecordsToFile()

bool MazeLib::Maze::backupWallRecordsToFile ( const std::string &  filepath,
const bool  clear = false 
)

壁ログをファイルに追記保存する関数

390  {
391  /* 変更なし */
392  if (!clear &&
393  wallRecordsBackupCounter == static_cast<int>(wallRecords.size()))
394  return true;
395  /* 前のデータが残っていたら削除 */
396  std::ifstream ifs(filepath, std::ios::ate);
397  const int num_items = ifs.tellg() / sizeof(WallRecord);
398  ifs.close();
399  if (clear || num_items > wallRecordsBackupCounter) {
400  std::remove(filepath.c_str());
402  }
403  /* WallRecords を追記 */
404  std::ofstream ofs(filepath, std::ios::binary | std::ios::app);
405  if (ofs.fail()) {
406  MAZE_LOGW << "failed to open file! " << filepath << std::endl;
407  return false;
408  }
409  while (wallRecordsBackupCounter < static_cast<int>(wallRecords.size())) {
410  const auto& wr = wallRecords[wallRecordsBackupCounter];
411  ofs.write(reinterpret_cast<const char*>(&wr), sizeof(wr));
413  }
414  return true;
415 }
#define MAZE_LOGW
Definition: Maze.h:83
WallRecords wallRecords
更新した壁のログ
Definition: Maze.h:861
int wallRecordsBackupCounter
壁ログバックアップのカウンタ
Definition: Maze.h:866

◆ canGo() [1/3]

bool MazeLib::Maze::canGo ( const Position  p,
const Direction  d 
) const
inline
723  {
724  return canGo(WallIndex(p, d));
725  }
unsigned int d
壁の方向
Definition: Maze.h:609
bool canGo(const WallIndex i) const
通過可能かどうかを返す
Definition: Maze.h:722
呼び出し関係図:

◆ canGo() [2/3]

bool MazeLib::Maze::canGo ( const WallIndex i,
bool  knownOnly 
) const
inline
726  {
727  return !isWall(i) && (isKnown(i) || !knownOnly);
728  }
bool isKnown(const WallIndex i) const
壁が探索済みかを返す
Definition: Maze.h:695
bool isWall(const WallIndex i) const
壁の有無を返す
Definition: Maze.h:669
呼び出し関係図:

◆ canGo() [3/3]

bool MazeLib::Maze::canGo ( const WallIndex  i) const
inline

通過可能かどうかを返す

戻り値
true: 既知かつ壁なし
false: それ以外
722 { return !isWall(i) && isKnown(i); }
呼び出し関係図:

◆ getGoals()

const Positions& MazeLib::Maze::getGoals ( ) const
inline

ゴール区画の集合を取得

830 { return goals; }

◆ getMaxX()

int8_t MazeLib::Maze::getMaxX ( ) const
inline
844 { return max_x; }
int8_t max_x
既知壁の最大区画
Definition: Maze.h:864

◆ getMaxY()

int8_t MazeLib::Maze::getMaxY ( ) const
inline
845 { return max_y; }
int8_t max_y
既知壁の最大区画
Definition: Maze.h:865

◆ getMinX()

int8_t MazeLib::Maze::getMinX ( ) const
inline

既知部分の迷路サイズを返す。計算量を減らすために使用。

842 { return min_x; }
int8_t min_x
既知壁の最小区画
Definition: Maze.h:862

◆ getMinY()

int8_t MazeLib::Maze::getMinY ( ) const
inline
843 { return min_y; }
int8_t min_y
既知壁の最小区画
Definition: Maze.h:863

◆ getStart()

const Position& MazeLib::Maze::getStart ( ) const
inline

スタート区画を取得

834 { return start; }

◆ getWallRecords()

const WallRecords& MazeLib::Maze::getWallRecords ( ) const
inline

壁ログを取得

838 { return wallRecords; }

◆ isKnown() [1/3]

bool MazeLib::Maze::isKnown ( const int8_t  x,
const int8_t  y,
const Direction  d 
) const
inline
699  {
700  return isWallBase(known, WallIndex(Position(x, y), d));
701  }
int y
区画のy座標
Definition: Maze.h:608
int x
区画のx座標
Definition: Maze.h:607
bool isWallBase(const std::bitset< WallIndex::SIZE > &wall, const WallIndex i) const
壁の確認のベース関数。迷路外を参照すると壁ありと返す。
Definition: Maze.h:871
std::bitset< WallIndex::SIZE > known
壁の既知未知情報
Definition: Maze.h:858
呼び出し関係図:

◆ isKnown() [2/3]

bool MazeLib::Maze::isKnown ( const Position  p,
const Direction  d 
) const
inline
696  {
697  return isWallBase(known, WallIndex(p, d));
698  }
呼び出し関係図:

◆ isKnown() [3/3]

bool MazeLib::Maze::isKnown ( const WallIndex  i) const
inline

壁が探索済みかを返す

戻り値
true: 探索済み、false: 未探索
695 { return isWallBase(known, i); }
呼び出し関係図:

◆ isWall() [1/3]

bool MazeLib::Maze::isWall ( const int8_t  x,
const int8_t  y,
const Direction  d 
) const
inline
673  {
674  return isWallBase(wall, WallIndex(Position(x, y), d));
675  }
std::bitset< WallIndex::SIZE > wall
壁情報
Definition: Maze.h:857
呼び出し関係図:

◆ isWall() [2/3]

bool MazeLib::Maze::isWall ( const Position  p,
const Direction  d 
) const
inline
670  {
671  return isWallBase(wall, WallIndex(p, d));
672  }
呼び出し関係図:

◆ isWall() [3/3]

bool MazeLib::Maze::isWall ( const WallIndex  i) const
inline

壁の有無を返す

戻り値
true: 壁あり、false: 壁なし
669 { return isWallBase(wall, i); }
呼び出し関係図:

◆ isWallBase()

bool MazeLib::Maze::isWallBase ( const std::bitset< WallIndex::SIZE > &  wall,
const WallIndex  i 
) const
inlineprotected

壁の確認のベース関数。迷路外を参照すると壁ありと返す。

872  {
873  return !i.isInsideOfField() || wall[i.getIndex()]; //< 範囲外は壁ありに
874  }
呼び出し関係図:

◆ parse() [1/3]

bool MazeLib::Maze::parse ( const std::string &  filepath)
inline
797  {
798  std::ifstream ifs(filepath);
799  return ifs ? parse(ifs) : false;
800  }
bool parse(std::istream &is)
特定の迷路の文字列(*.maze ファイル)から壁をパースする
Definition: Maze.cpp:165
呼び出し関係図:

◆ parse() [2/3]

bool MazeLib::Maze::parse ( const std::vector< std::string > &  data,
const int  mazeSize 
)

配列から迷路を読み込むパーサ

引数
data各区画16進表記の文字列配列 例:{"abaf", "1234", "abab", "aaff"}
mazeSize迷路の1辺の区画数(正方形のみ対応)
210  {
211  for (const auto xr : {true, false}) {
212  for (const auto yr : {false, true}) {
213  for (const auto xy : {false, true}) {
214  for (const auto b0 : Direction::Along4()) {
215  for (const auto b1 : Direction::Along4()) {
216  for (const auto b2 : Direction::Along4()) {
217  for (const auto b3 : Direction::Along4()) {
218  const std::array<Direction, 4> bit_to_dir_map{{b0, b1, b2, b3}};
219  reset(false);
220  int diffs = 0;
221  for (int8_t y = 0; y < mazeSize; ++y) {
222  for (int8_t x = 0; x < mazeSize; ++x) {
223  const int8_t xd = xr ? x : (mazeSize - x - 1);
224  const int8_t yd = yr ? y : (mazeSize - y - 1);
225  const signed char c = xy ? data[xd][yd] : data[yd][xd];
226  uint8_t h = 0;
227  if ('0' <= c && c <= '9')
228  h = c - '0';
229  else if ('a' <= c && c <= 'f')
230  h = c - 'a' + 10;
231  else if ('A' <= c && c <= 'F')
232  h = c - 'A' + 10;
233  else if (0 <= c && c <= 15)
234  h = c;
235  if (!updateWall(Position(x, y), bit_to_dir_map[0], h & 0x01,
236  false))
237  ++diffs;
238  if (!updateWall(Position(x, y), bit_to_dir_map[1], h & 0x02,
239  false))
240  ++diffs;
241  if (!updateWall(Position(x, y), bit_to_dir_map[2], h & 0x04,
242  false))
243  ++diffs;
244  if (!updateWall(Position(x, y), bit_to_dir_map[3], h & 0x08,
245  false))
246  ++diffs;
247  }
248  }
249  if (diffs < MAZE_SIZE && isWall(0, 0, Direction::East) &&
250  !isWall(0, 0, Direction::North))
251  return true;
252  }
253  }
254  }
255  }
256  }
257  }
258  }
259  return false;
260 }
static constexpr const std::array< Direction, 4 > Along4()
斜めではない4方向の配列 (for文などで使用)
Definition: Maze.h:216
@ North
Definition: Maze.h:153
@ East
Definition: Maze.h:151
bool updateWall(const Position p, const Direction d, const bool b, const bool pushRecords=true)
既知の壁情報と照らしあわせながら、壁を更新する関数
Definition: Maze.cpp:130
static constexpr int MAZE_SIZE
迷路の1辺の区画数の定数。
Definition: Maze.h:106
呼び出し関係図:

◆ parse() [3/3]

bool MazeLib::Maze::parse ( std::istream &  is)

特定の迷路の文字列(*.maze ファイル)から壁をパースする

テキスト形式。S: スタート区画(単数)、G: ゴール区画(複数可)

+---+---+
| G |
+ + +
| S | G |
+---+---+
引数
is*.maze 形式のファイルの input-stream
165  {
166  /* determine the maze size */
167  /* get file size */
168  is.seekg(0, std::ios::end); //< move the position to end
169  const int file_size = 1 + is.tellg();
170  is.seekg(0, std::ios::beg); //< restore the position to begin
171  /* estimated (minimum) file size [byte] : F = (4*M + 1 + 1) * (2*M + 1) */
172  /* using quadratic formula, we have: M = (sqrt(2*F) - 2) / 4 */
173  const int mazeSize = (std::sqrt(2 * file_size) - 2) / 4;
174  if (mazeSize < 1) return false; //< file size error
175  /* reset existing maze */
176  reset(), goals.clear();
177  char c; //< temporal variable to use next
178  for (int8_t y = mazeSize; y >= 0; --y) {
179  /* vertical walls and cells */
180  if (y != mazeSize) {
181  is.ignore(10, '|'); //< skip until next '|'
182  for (int8_t x = 0; x < mazeSize; ++x) {
183  is.ignore(1); //< skip a space
184  c = is.get();
185  if (c == 'S')
186  start = Position(x, y);
187  else if (c == 'G')
188  goals.push_back(Position(x, y));
189  is.ignore(1); //< skip a space
190  c = is.get();
191  if (c == '|')
192  Maze::updateWall(Position(x, y), Direction::East, true, false);
193  else if (c == ' ')
194  Maze::updateWall(Position(x, y), Direction::East, false, false);
195  }
196  }
197  /* horizontal walls and pillars */
198  for (uint8_t x = 0; x < mazeSize; ++x) {
199  is >> c; //< skip until next '+' or 'o'
200  std::string s;
201  for (int i = 0; i < 3; ++i) s += static_cast<char>(is.get());
202  if (s == "---")
203  Maze::updateWall(Position(x, y), Direction::South, true, false);
204  else if (s == " ")
205  Maze::updateWall(Position(x, y), Direction::South, false, false);
206  }
207  }
208  return true;
209 }
@ South
Definition: Maze.h:157
呼び出し関係図:

◆ print() [1/3]

void MazeLib::Maze::print ( const Directions dirs,
const Position  start = Position(0, 0),
std::ostream &  os = std::cout,
const int  mazeSize = MAZE_SIZE 
) const

パス付きの迷路の表示

引数
startパスのスタート座標
dirs移動方向の配列
osoutput-stream
mazeSize迷路の1辺の区画数(正方形のみ対応)
288  {
289  /* preparation */
290  std::vector<Pose> path;
291  path.reserve(dirs.size());
292  {
293  Position p = start;
294  for (const auto d : dirs) path.push_back({p, d}), p = p.next(d);
295  }
296  const auto& maze = *this;
297  /* start to draw maze */
298  for (int8_t y = mazeSize; y >= 0; --y) {
299  if (y != mazeSize) {
300  for (uint8_t x = 0; x <= mazeSize; ++x) {
301  /* Vertical Wall */
302  const auto it =
303  std::find_if(path.cbegin(), path.cend(), [&](const Pose& pose) {
304  return WallIndex(pose.p, pose.d) ==
305  WallIndex(Position(x, y), Direction::West);
306  });
307  const auto w = maze.isWall(x, y, Direction::West);
308  const auto k = maze.isKnown(x, y, Direction::West);
309  if (it != path.cend())
310  os << C_YE << it->d << C_NO;
311  else
312  os << (k ? (w ? "|" : " ") : (C_RE "." C_NO));
313  /* Breaking Condition */
314  if (x == mazeSize) break;
315  /* Cell */
316  const auto p = Position(x, y);
317  if (p == start)
318  os << C_BL << " S " << C_NO;
319  else if (std::find(goals.cbegin(), goals.cend(), p) != goals.cend())
320  os << C_BL << " G " << C_NO;
321  else
322  os << " ";
323  }
324  os << std::endl;
325  }
326  for (uint8_t x = 0; x < mazeSize; ++x) {
327  /* Pillar */
328  os << '+';
329  /* Horizontal Wall */
330  const auto it =
331  std::find_if(path.cbegin(), path.cend(), [&](const Pose pose) {
332  return WallIndex(pose.p, pose.d) ==
333  WallIndex(Position(x, y), Direction::South);
334  });
335  const auto w = maze.isWall(x, y, Direction::South);
336  const auto k = maze.isKnown(x, y, Direction::South);
337  if (it != path.cend())
338  os << C_YE << ' ' << it->d << ' ' << C_NO;
339  else
340  os << (k ? (w ? "---" : " ") : (C_RE " . " C_NO));
341  }
342  /* Last Pillar */
343  os << '+' << std::endl;
344  }
345 }
#define C_YE
ANSI Escape Sequence YELLOW
Definition: Maze.h:61
#define C_BL
ANSI Escape Sequence BLUE
Definition: Maze.h:62
#define C_NO
ANSI Escape Sequence RESET
Definition: Maze.h:65
#define C_RE
ANSI Escape Sequence RED
Definition: Maze.h:59
@ West
Definition: Maze.h:155
Position next(const Direction d) const
自分の引数方向に隣接した区画の Position を返す
Definition: Maze.cpp:22
呼び出し関係図:

◆ print() [2/3]

void MazeLib::Maze::print ( const Positions positions,
std::ostream &  os = std::cout,
const int  mazeSize = MAZE_SIZE 
) const

位置のハイライト付きの迷路の表示

引数
positionsハイライトする位置の集合
osoutput-stream
mazeSize迷路の1辺の区画数(正方形のみ対応)
347  {
348  /* preparation */
349  const auto exists = [&](const Position p) {
350  return std::find(positions.cbegin(), positions.cend(), p) !=
351  positions.cend();
352  };
353  const auto& maze = *this;
354  /* start to draw maze */
355  for (int8_t y = mazeSize; y >= 0; --y) {
356  if (y != mazeSize) {
357  for (uint8_t x = 0; x <= mazeSize; ++x) {
358  /* Vertical Wall */
359  const auto w = maze.isWall(x, y, Direction::West);
360  const auto k = maze.isKnown(x, y, Direction::West);
361  os << (k ? (w ? "|" : " ") : (C_RE "." C_NO));
362  /* Breaking Condition */
363  if (x == mazeSize) break;
364  /* Cell */
365  const auto p = Position(x, y);
366  if (p == start)
367  os << C_BL << " S " << C_NO;
368  else if (std::find(goals.cbegin(), goals.cend(), p) != goals.cend())
369  os << C_BL << " G " << C_NO;
370  else if (exists(p))
371  os << C_YE << " X " << C_NO;
372  else
373  os << " ";
374  }
375  os << std::endl;
376  }
377  for (uint8_t x = 0; x < mazeSize; ++x) {
378  /* Pillar */
379  os << '+';
380  /* Horizontal Wall */
381  const auto w = maze.isWall(x, y, Direction::South);
382  const auto k = maze.isKnown(x, y, Direction::South);
383  os << (k ? (w ? "---" : " ") : (C_RE " . " C_NO));
384  }
385  /* Last Pillar */
386  os << '+' << std::endl;
387  }
388 }

◆ print() [3/3]

void MazeLib::Maze::print ( std::ostream &  os = std::cout,
const int  mazeSize = MAZE_SIZE 
) const

迷路の表示

261  {
262  for (int8_t y = mazeSize; y >= 0; --y) {
263  if (y != mazeSize) {
264  os << '|';
265  for (int8_t x = 0; x < mazeSize; ++x) {
266  const auto p = Position(x, y);
267  if (p == start)
268  os << " S ";
269  else if (std::find(goals.cbegin(), goals.cend(), p) != goals.cend())
270  os << " G ";
271  else
272  os << " ";
273  const auto k = isKnown(x, y, Direction::East);
274  const auto w = isWall(x, y, Direction::East);
275  os << (k ? (w ? '|' : ' ') : '.');
276  }
277  os << std::endl;
278  }
279  for (int8_t x = 0; x < mazeSize; ++x) {
280  const auto k = isKnown(x, y, Direction::South);
281  const auto w = isWall(x, y, Direction::South);
282  os << '+' << (k ? (w ? "---" : " ") : " . ");
283  }
284  os << '+' << std::endl;
285  }
286 }
呼び出し関係図:

◆ reset()

void MazeLib::Maze::reset ( const bool  set_start_wall = true,
const bool  set_range_full = false 
)

迷路の初期化。壁を削除し、スタート区画を既知に

引数
set_start_wallスタート区画の East と North の壁を設定するかどうか
set_range_full高速化用の min_x などを予め最大に設定するかどうか
108  {
109  wall.reset();
110  known.reset();
111  min_x = min_y = set_range_full ? 0 : (MAZE_SIZE - 1);
112  max_x = max_y = set_range_full ? (MAZE_SIZE - 1) : 0;
114  if (set_start_wall) {
115  updateWall(Position(0, 0), Direction::East, true); //< start cell
116  updateWall(Position(0, 0), Direction::North, false); //< start cell
117  }
118  wallRecords.clear();
119 }
呼び出し関係図:

◆ resetLastWalls()

void MazeLib::Maze::resetLastWalls ( const int  num,
const bool  set_start_wall = true 
)

直前に更新した壁を見探索状態にリセットする

引数
num消去する直近の壁の数
set_start_wallスタート区画の East と North の壁を設定するかどうか
154  {
155  /* 直近の壁情報を削除 */
156  for (int i = 0; i < num && !wallRecords.empty(); ++i) wallRecords.pop_back();
157  /* 削除後の壁情報を取得 */
158  const auto new_wallRecords = wallRecords;
159  /* スタート壁を考慮して迷路を再構築 */
160  reset(set_start_wall);
161  for (const auto wr : new_wallRecords)
162  updateWall(wr.getPosition(), wr.getDirection(), wr.b);
163  return;
164 }
呼び出し関係図:

◆ restoreWallRecordsFromFile()

bool MazeLib::Maze::restoreWallRecordsFromFile ( const std::string &  filepath)

壁ログファイルから壁情報を復元する関数

416  {
417  std::ifstream f(filepath, std::ios::binary);
418  if (f.fail()) {
419  MAZE_LOGW << "failed to open file! " << filepath << std::endl;
420  return false;
421  }
422  reset();
423  while (!f.eof()) {
424  WallRecord wr;
425  f.read(reinterpret_cast<char*>(&wr), sizeof(WallRecord));
426  Position p = Position(wr.x, wr.y);
427  Direction d = Direction(wr.d);
428  bool b = wr.b;
429  updateWall(p, d, b);
431  }
432  return true;
433 }
unsigned int b
壁の有無
Definition: Maze.h:610
呼び出し関係図:

◆ setGoals()

void MazeLib::Maze::setGoals ( const Positions goals)
inline

ゴール区画の集合を更新

822 { this->goals = goals; }

◆ setKnown() [1/3]

void MazeLib::Maze::setKnown ( const int8_t  x,
const int8_t  y,
const Direction  d,
const bool  b 
)
inline
714  {
715  return setWallBase(known, WallIndex(Position(x, y), d), b);
716  }
void setWallBase(std::bitset< WallIndex::SIZE > &wall, const WallIndex i, const bool b) const
壁の更新のベース関数。迷路外を参照すると無視される。
Definition: Maze.h:878
呼び出し関係図:

◆ setKnown() [2/3]

void MazeLib::Maze::setKnown ( const Position  p,
const Direction  d,
const bool  b 
)
inline
710  {
711  return setWallBase(known, WallIndex(p, d), b);
712  }
呼び出し関係図:

◆ setKnown() [3/3]

void MazeLib::Maze::setKnown ( const WallIndex  i,
const bool  b 
)
inline

壁の既知を更新する

引数
i壁の位置
b壁の未知既知 true:既知、false:未知
707  {
708  return setWallBase(known, i, b);
709  }
呼び出し関係図:

◆ setStart()

void MazeLib::Maze::setStart ( const Position  start)
inline

スタート区画を更新

826 { this->start = start; }

◆ setWall() [1/3]

void MazeLib::Maze::setWall ( const int8_t  x,
const int8_t  y,
const Direction  d,
const bool  b 
)
inline
688  {
689  return setWallBase(wall, WallIndex(Position(x, y), d), b);
690  }
呼び出し関係図:

◆ setWall() [2/3]

void MazeLib::Maze::setWall ( const Position  p,
const Direction  d,
const bool  b 
)
inline
684  {
685  return setWallBase(wall, WallIndex(p, d), b);
686  }
呼び出し関係図:

◆ setWall() [3/3]

void MazeLib::Maze::setWall ( const WallIndex  i,
const bool  b 
)
inline

壁を更新をする

引数
i壁の位置
b壁の有無 true:壁あり、false:壁なし
681  {
682  return setWallBase(wall, i, b);
683  }
呼び出し関係図:

◆ setWallBase()

void MazeLib::Maze::setWallBase ( std::bitset< WallIndex::SIZE > &  wall,
const WallIndex  i,
const bool  b 
) const
inlineprotected

壁の更新のベース関数。迷路外を参照すると無視される。

879  {
880  if (i.isInsideOfField()) //< 範囲外アクセスの防止
881  wall[i.getIndex()] = b;
882  }
呼び出し関係図:

◆ unknownCount()

int8_t MazeLib::Maze::unknownCount ( const Position  p) const

引数区画に隣接する未知壁の数を返す

引数
p区画の座標
戻り値
既知壁の数 0~4
125  {
126  const auto dirs = Direction::Along4();
127  return std::count_if(dirs.cbegin(), dirs.cend(),
128  [&](const Direction d) { return !isKnown(p, d); });
129 }
呼び出し関係図:

◆ updateWall()

bool MazeLib::Maze::updateWall ( const Position  p,
const Direction  d,
const bool  b,
const bool  pushRecords = true 
)

既知の壁情報と照らしあわせながら、壁を更新する関数

既知の壁と非一致した場合、未知壁にして return する

引数
p区画の座標
d壁の方向
b壁の有無
pushRecords壁更新の記録に追加する
戻り値
true: 正常に更新された
false: 既知の情報と不一致だった
131  {
132  /* 既知の壁と食い違いがあったら未知壁としてreturn */
133  if (isKnown(p, d) && isWall(p, d) != b) {
134  setWall(p, d, false);
135  setKnown(p, d, false);
136  /* ログに追加 */
137  if (pushRecords) wallRecords.push_back(WallRecord(p, d, b));
138  return false;
139  }
140  /* 未知壁なら壁情報を更新 */
141  if (!isKnown(p, d)) {
142  setWall(p, d, b);
143  setKnown(p, d, true);
144  /* ログに追加 */
145  if (pushRecords) wallRecords.push_back(WallRecord(p, d, b));
146  /* 最大最小区画を更新 */
147  min_x = std::min(p.x, min_x);
148  min_y = std::min(p.y, min_y);
149  max_x = std::max(p.x, max_x);
150  max_y = std::max(p.y, max_y);
151  }
152  return true;
153 }
void setKnown(const WallIndex i, const bool b)
壁の既知を更新する
Definition: Maze.h:707
void setWall(const WallIndex i, const bool b)
壁を更新をする
Definition: Maze.h:681
呼び出し関係図:

◆ wallCount()

int8_t MazeLib::Maze::wallCount ( const Position  p) const

引数区画の壁の数を返す

引数
p区画の座標
戻り値
壁の数 0~4
120  {
121  const auto dirs = Direction::Along4();
122  return std::count_if(dirs.cbegin(), dirs.cend(),
123  [&](const Direction d) { return isWall(p, d); });
124 }
呼び出し関係図:

フレンドと関連関数の詳解

◆ operator>>

std::istream& operator>> ( std::istream &  is,
Maze maze 
)
friend

入力ストリームの迷路データをパースする

使用例: Maze maze; maze << std::cin;

引数
isテキスト形式の迷路データを含む入力ストリーム
mazeパース結果を書き出す迷路の参照
戻り値
std::istream& 引数の is をそのまま返す
808  {
809  maze.parse(is);
810  return is;
811  }

メンバ詳解

◆ goals

Positions MazeLib::Maze::goals
protected

ゴール区画の集合

◆ known

std::bitset<WallIndex::SIZE> MazeLib::Maze::known
protected

壁の既知未知情報

◆ max_x

int8_t MazeLib::Maze::max_x
protected

既知壁の最大区画

◆ max_y

int8_t MazeLib::Maze::max_y
protected

既知壁の最大区画

◆ min_x

int8_t MazeLib::Maze::min_x
protected

既知壁の最小区画

◆ min_y

int8_t MazeLib::Maze::min_y
protected

既知壁の最小区画

◆ start

Position MazeLib::Maze::start
protected

スタート区画

◆ wall

std::bitset<WallIndex::SIZE> MazeLib::Maze::wall
protected

壁情報

◆ wallRecords

WallRecords MazeLib::Maze::wallRecords
protected

更新した壁のログ

◆ wallRecordsBackupCounter

int MazeLib::Maze::wallRecordsBackupCounter
protected

壁ログバックアップのカウンタ


このクラス詳解は次のファイルから抽出されました: