/// <summary> /// 迷路を解くための後処理をする。 /// </summary> /// <param name="solveArrayInner">迷路の答えを示す内部データ</param> private void SolveFinalize(MazeSolveArrayInner solveArrayInner) { for (int x = 0; x < this.SizeX; x++) { for (int y = 0; y < this.SizeY; y++) { switch (solveArrayInner[x, y]) { case MazeSolveArrayInner.CellType.Right: { //正解ルート this.SolveArray[x, y] = MazeSolveArray.CellType.Right; break; } case MazeSolveArrayInner.CellType.Wrong: case MazeSolveArrayInner.CellType.Temp: { //間違ったルートまたは壁 this.SolveArray[x, y] = MazeSolveArray.CellType.Wrong; break; } } } } }
/// <summary> /// 迷路を解くための準備を行う。 /// </summary> /// <param name="solveArrayInner">迷路の答えを示す内部データ</param> private void SolveInit(MazeSolveArrayInner solveArrayInner) { //答えのデータを初期化する。 for (int x = 0; x < this.SizeX; x++) { for (int y = 0; y < this.SizeY; y++) { solveArrayInner[x, y] = MazeSolveArrayInner.CellType.Wrong; } } }
/// <summary> /// 迷路を解く。 /// </summary> /// <returns>true 成功, false 失敗</returns> public bool Solve() { if (this.Solved) { return(true); } this.SolveArray = new MazeSolveArray(this.SizeX, this.SizeY); MazeSolveArrayInner solveArrayInner = new MazeSolveArrayInner(this.SizeX, this.SizeY); this.SolveInit(solveArrayInner); if (!this.SolveMain(solveArrayInner)) { this.SolveArray = null; return(false); } this.SolveFinalize(solveArrayInner); return(true); }
/// <summary> /// 迷路を解く。 /// </summary> /// <param name="solveArrayInner">迷路の答えを示す内部データ</param> /// <returns></returns> private bool SolveMain(MazeSolveArrayInner solveArrayInner) { (int, int)curPos = (this.StartX, this.StartY); //迷路の現在位置を初期化(スタート位置にする) (int, int)rot; //スタート位置を答えの道にする。 solveArrayInner[curPos.Item1, curPos.Item2] = MazeSolveArrayInner.CellType.Right; //迷路を解く。 while (true) { //最大4回向きを変える。 for (int rotNo = 0; rotNo < 4; rotNo++) { //向き番号からベクトル座標を得る rot = this.GetRotation(rotNo); //先が道だったらとりあえず進む if (curPos.Item1 + rot.Item1 >= 0 && curPos.Item1 + rot.Item1 < this.SizeX && curPos.Item2 + rot.Item2 >= 0 && curPos.Item2 + rot.Item2 < this.SizeY && ( this.fieldArray[curPos.Item1 + rot.Item1, curPos.Item2 + rot.Item2] == MazeFieldArray.CellType.Goal || this.fieldArray[curPos.Item1 + rot.Item1, curPos.Item2 + rot.Item2] == MazeFieldArray.CellType.Start || this.fieldArray[curPos.Item1 + rot.Item1, curPos.Item2 + rot.Item2] == MazeFieldArray.CellType.Road ) && solveArrayInner[curPos.Item1 + rot.Item1, curPos.Item2 + rot.Item2] == MazeSolveArrayInner.CellType.Wrong ) { //★答えの道にする solveArrayInner[curPos.Item1 + rot.Item1, curPos.Item2 + rot.Item2] = MazeSolveArrayInner.CellType.Right; curPos.Item1 += rot.Item1; curPos.Item2 += rot.Item2; break; } //★行き止まりだったら if (rotNo == 3) { //最大4回向きを変える。 for (int rotNo2 = 0; rotNo2 < 4; rotNo2++) { rot = this.GetRotation(rotNo2); //今まで来た道を戻る。 if (curPos.Item1 + rot.Item1 >= 0 && curPos.Item1 + rot.Item1 < this.SizeX && curPos.Item2 + rot.Item2 >= 0 && curPos.Item2 + rot.Item2 < this.SizeY && ( this.fieldArray[curPos.Item1 + rot.Item1, curPos.Item2 + rot.Item2] == MazeFieldArray.CellType.Goal || this.fieldArray[curPos.Item1 + rot.Item1, curPos.Item2 + rot.Item2] == MazeFieldArray.CellType.Start || this.fieldArray[curPos.Item1 + rot.Item1, curPos.Item2 + rot.Item2] == MazeFieldArray.CellType.Road ) && solveArrayInner[curPos.Item1 + rot.Item1, curPos.Item2 + rot.Item2] == MazeSolveArrayInner.CellType.Right ) { //ダミーのフラグを立てる。(その道は2度と進まないようになる) solveArrayInner[curPos.Item1, curPos.Item2] = MazeSolveArrayInner.CellType.Temp; curPos.Item1 += rot.Item1; curPos.Item2 += rot.Item2; break; } if (rotNo2 == 3) { //★★解けない迷路であった… return(false); } } } } if (curPos.Item1 == this.GoalX && curPos.Item2 == this.GoalY) { break; } } //★★迷路を解くことができた! return(true); }