Skip to content

Instantly share code, notes, and snippets.

@Osinko
Created June 5, 2021 12:52
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 Osinko/e0599dec2b6a9c008d7be651f7be915f to your computer and use it in GitHub Desktop.
Save Osinko/e0599dec2b6a9c008d7be651f7be915f to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class TreeGen : MonoBehaviour
{
void Start()
{
int length = 6; //試行数
char[] set = { 'オ', 'チ', 'ン', 'マ', 'コ', 'ウ' }; //標本空間
List<string> strList = Root(set, length);
string folder = Application.dataPath; //これだけでunityの実行ファイルがあるフォルダがわかる
SaveText(folder, @"\Gen1.txt", strList.ToArray());
}
//基底部(ドミノの最初_起点)
List<string> Root(char[] set, int length)
{
List<string> strList = new List<string>();
int level = 0;
for (int i = 0; i < set.Length; i++)
{
char[] dat = new char[length];
dat[0] = set[i];
Loop(dat, level, set, strList);
}
return strList;
}
//帰納部(倒れ続けるドミノの部分)
void Loop(char[] dat, int level, char[] set, List<string> strList)
{
level++;
if (level < dat.Length) //帰納関数の終了条件
{
for (int i = 0; i < set.Length; i++)
{
char[] cloneDat = (char[])dat.Clone(); //オブジェクトを複製する
cloneDat[level] = set[i];
Loop(cloneDat, level, set, strList); //帰納関数には複製された参照値が渡される為、呼び出し元側の値が書き換えられることは無い
}
}
else
{
strList.Add(new string(dat));
}
}
//資料:StreamWriter クラス (System.IO)
//http://msdn.microsoft.com/ja-jp/library/system.io.streamwriter(v=vs.110).aspx
//テキストファイルとしてセーブ
public void SaveText(string fileFolder, string filename, string[] dataStr)
{
using (StreamWriter w = new StreamWriter(fileFolder + filename, false, System.Text.Encoding.GetEncoding("shift_jis")))
{
foreach (var item in dataStr)
{
w.WriteLine(item);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment