Created
July 31, 2017 14:24
-
-
Save Pregum/9aa2279530080d3d8fc244f02730671d to your computer and use it in GitHub Desktop.
迷路自動生成アルゴリズムC#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace test | |
{ | |
using System.ComponentModel; | |
using System.Diagnostics; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var result = originalCode(); | |
Console.WriteLine(result); | |
} | |
private static string originalCode() | |
{ | |
// 変数の初期化 | |
var res = string.Empty; | |
// 迷路の初期化 | |
var width = 55; | |
var height = 91; | |
// var maze = Enumerable.Repeat(1, width * height).ToList(); | |
var maze = Enumerable.Repeat(0, width * height).ToList(); | |
for (int y = 1; y < height - 1; y++) | |
{ | |
for (int x = 1; x < width - 1; x++) | |
{ | |
maze[x + width * y] = 1; | |
} | |
} | |
// 開始位置と方向とパターン | |
var startX = width - 5; | |
var startY = 4; | |
var dir = new[] { new[] { -1, 0 }, new[] { 0, -1 }, new[] { 1, 0 }, new[] { 0, 1 } }; | |
var pattern = new[,] | |
{ | |
{ | |
0, 1, 2, 3 | |
}, { | |
0, 1, 3, 2 | |
}, { | |
0, 2, 1, 3 | |
}, { | |
0, 2, 3, 1 | |
}, { | |
0, 3, 1, 2 | |
}, | |
{ | |
0, 3, 2, 1 | |
}, { | |
1, 0, 2, 3 | |
}, { | |
1, 0, 3, 2 | |
}, { | |
1, 2, 0, 3 | |
}, { | |
1, 2, 3, 0 | |
}, | |
{ | |
1, 3, 0, 2 | |
}, { | |
1, 3, 2, 0 | |
}, { | |
2, 0, 1, 3 | |
}, { | |
2, 0, 3, 1 | |
}, { | |
2, 1, 0, 3 | |
}, | |
{ | |
2, 1, 3, 0 | |
}, { | |
2, 3, 0, 1 | |
}, { | |
2, 3, 1, 0 | |
}, { | |
3, 0, 1, 2 | |
}, { | |
3, 0, 2, 1 | |
}, | |
{ | |
3, 1, 0, 2 | |
}, { | |
3, 1, 2, 0 | |
}, { | |
3, 2, 0, 1 | |
}, { | |
3, 2, 1, 0 | |
}, | |
}; | |
// 穴掘り法 | |
Dig(startX, startY, pattern, dir, ref maze, width, height); | |
// 出力結果生成 | |
for (int y = 0; y < height; y++) | |
{ | |
for (int x = 0; x < width; x++) | |
{ | |
res += maze[(x + width * y)] == 1 ? "■" : " "; | |
} | |
res += "\n"; | |
} | |
return res; | |
} | |
private static void Dig( | |
int startX, | |
int startY, | |
int[,] pattern, | |
int[][] dir, | |
ref List<int> maze, | |
int width, | |
int height) | |
{ | |
// コードゴルフ用:ランダムを使わずに生成 | |
var type = (startX + 3) * (startY + 5) * 7 % pattern.GetLength(0); | |
for (var i = 0; i < dir.GetLength(0); i++) | |
{ | |
var next = dir[pattern[type, i]]; | |
if (maze[(startX + next[0] * 2) + width * (startY + next[1] * 2)] == 1) | |
{ | |
maze[(startX + next[0] * 2) + width * (startY + next[1] * 2)] = 0; | |
maze[(startX + next[0]) + width * (startY + next[1])] = 0; | |
Dig(startX + next[0] * 2, startY + next[1] * 2, pattern, dir, ref maze, width, height); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment