Skip to content

Instantly share code, notes, and snippets.

@oppaopp4
Last active May 28, 2019 16:07
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 oppaopp4/c2387e46c20b645094ecadcc5611cd79 to your computer and use it in GitHub Desktop.
Save oppaopp4/c2387e46c20b645094ecadcc5611cd79 to your computer and use it in GitHub Desktop.
迷路自動生成(穴掘り法)
using System;
using System.Collections.Generic;
namespace meiro_test
{
class MainClass
{
static int WIDTH = 17;
static int HEIGHT = 11;
static Random r = new Random();
static int cx;
static int cy;
static int[] searchtmp = { 0, 0, 0, 0 };
static MeiroCell[,] list = new MeiroCell[HEIGHT, WIDTH];
static string[] str = new string[HEIGHT];
public static void Main(string[] args)
{
while (true)
{
Init();
GenMeiro();
SetItem();
Kekka();
Console.Write("end\n");
if (Console.ReadLine() == "q") break;
}
}
public static void GenMeiro()
{
while (true)
{
//*上下左右をランダムで順番を決める
UDLR();
//決まった方向の2つ先が壁(0)なら道(1)にして、カレントポジションを移動してcheckをtrueにして*に戻る
//決まった方向の2つ先が道(1)なら次の方向を試す
if (!Horu())
{
//4回やってもだめなら、全ての(cx,cyともに偶数&&道(1))がcheck済か調べて、まだfalseがあればランダム(cx,cyともに偶数&&道(1))でカレントポジションを決めて*戻る
if (!CheckChecked())
{
break;
}
RefleshPosition();
}
}
}
public static void SetItem()
{
int it_x;
int it_y;
//2=スタート 3=ゴール 4,5,6=アイテム
for (int i = 2; i <= 6; i++)
{
while (true)
{
it_x = r.Next(WIDTH);
it_y = r.Next(HEIGHT);
if (list[it_y, it_x].Status == 1)
{
list[it_y, it_x].Status = i;
break;
}
}
}
}
public static void DebugLog()
{
for (int i = 0; i < HEIGHT; i++)
{
str[i] = "";
}
for (int i = 0; i < HEIGHT; i++)
{
for (int j = 0; j < WIDTH; j++)
{
str[i] += list[i, j].Status;
}
}
}
public static void RefleshPosition()
{
int px;
int py;
while (true)
{
while (true)
{
px = r.Next(WIDTH);
if (px % 2 == 1) break;
}
while (true)
{
py = r.Next(HEIGHT);
if (py % 2 == 1) break;
}
if (list[py, px].Status == 1 && list[py, px].Checked == false)
{
cx = px;
cy = py;
list[py, px].Checked = true;
break;
}
}
}
public static bool CheckChecked()
{
for (int i = 1; i < HEIGHT; i++)
{
for (int j = 1; j < WIDTH; j++)
{
if (i % 2 == 1 && j % 2 == 1)
{
if (list[i, j].Status == 1 && list[i, j].Checked == false)
{
return true;
}
}
}
}
return false;
}
public static bool Horu()
{
for (int i = 0; i < 4; i++)
{
switch (searchtmp[i])
{
case 0:
//上
if (cy - 2 >= 1)
{
if (list[cy - 2, cx].Status == 0)
{
list[cy - 1, cx].Status = 1;
list[cy - 2, cx].Status = 1;
list[cy - 1, cx].Checked = true;
cy -= 2;
DebugLog();
return true;
}
}
break;
case 1:
//下
if (cy + 2 <= HEIGHT - 2)
{
if (list[cy + 2, cx].Status == 0)
{
list[cy + 1, cx].Status = 1;
list[cy + 2, cx].Status = 1;
list[cy + 1, cx].Checked = true;
cy += 2;
DebugLog();
return true;
}
}
break;
case 2:
//左
if (cx - 2 >= 1)
{
if (list[cy, cx - 2].Status == 0)
{
list[cy, cx - 1].Status = 1;
list[cy, cx - 2].Status = 1;
list[cy, cx - 1].Checked = true;
cx -= 2;
DebugLog();
return true;
}
}
break;
case 3:
//右
if (cx + 2 <= WIDTH - 2)
{
if (list[cy, cx + 2].Status == 0)
{
list[cy, cx + 1].Status = 1;
list[cy, cx + 2].Status = 1;
list[cy, cx + 1].Checked = true;
cx += 2;
DebugLog();
return true;
}
}
break;
}
}
return false;
}
public static void UDLR()
{
for (int i = 0; i < 4; i++)
{
searchtmp[i] = -1;
}
int s = 0;
for (int i = 0; i < 4; i++)
{
while (true)
{
s = r.Next(4);
if (searchtmp[s] == -1)
{
searchtmp[s] = i;
break;
}
}
}
}
public static void Init()
{
cx = 1;
cy = 1;
for (int i = 0; i < HEIGHT; i++)
{
for (int j = 0; j < WIDTH; j++)
{
list[i, j] = new MeiroCell(0, false);
}
}
list[cx, cy].Status = 1;
list[cx, cy].Checked = true;
}
public static void Kekka()
{
for (int i = 0; i < HEIGHT; i++)
{
for (int j = 0; j < WIDTH; j++)
{
Console.Write(list[i, j].Status);
}
Console.Write("\n");
}
for (int i = 0; i < HEIGHT; i++)
{
for (int j = 0; j < WIDTH; j++)
{
string s = "";
switch (list[i, j].Status)
{
case 0:
s = "■";
break;
case 1:
s = "□";
break;
case 2:
s = "人";
break;
case 3:
s = "▽";
break;
case 4:
s = "☆";
break;
case 5:
s = "★";
break;
case 6:
s = "※";
break;
}
Console.Write(s);
}
Console.Write("\n");
}
}
}
public class MeiroCell
{
public int Status { get; set; }//0=壁,1=道,2=外周
public bool Checked { get; set; }
public MeiroCell(int s, bool c)
{
Status = s;
Checked = c;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment