-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNameGenerator.cs
97 lines (78 loc) · 2.94 KB
/
NameGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using Godot;
using System;
using System.Collections.Generic;
using System.Linq;
public class NameGenerator
{
private static readonly Dictionary<String, List<string>> Letters = new Dictionary<String, List<string>>()
{
{
"VOYELLE", new List<string>(){"a", "e", "i", "o", "u", "y"}
},
{
"DOUBLE_VOYELLE",
new List<string>() {"au", "oa", "ou", "ie", "ae", "eu"}},
{
"CONSONNE",
new List<string>() {"b", "c", "d", "f", "g", "h", "j", "l", "m", "n", "p", "r", "s", "t", "v", "w", "x", "z"}
},
{
"DOUBLE_CONSONNE",
new List<string>() {"mm", "nn", "st", "ch", "ll", "tt", "ss"}},
{
"COMPOSE",
new List<string>() {"gu", "cc", "sc", "tr", "fr", "pr", "br", "cr", "ch", "gn", "ix", "an", "do", "ir", "as"}
}
};
private static readonly Dictionary<String, List<String>> Transition = new Dictionary<String, List<string>>
{
{"INITIAL", new List<string>() {"VOYELLE", "CONSONNE", "COMPOSE"}},
{"VOYELLE", new List<string>() {"CONSONNE", "DOUBLE_CONSONNE", "COMPOSE"}},
{"DOUBLE_VOYELLE", new List<string>() {"CONSONNE", "DOUBLE_CONSONNE", "COMPOSE"}},
{"CONSONNE", new List<string>() {"VOYELLE", "DOUBLE_VOYELLE"}},
{"DOUBLE_CONSONNE", new List<string>() {"VOYELLE", "DOUBLE_VOYELLE"}},
{"COMPOSE", new List<string>() {"VOYELLE"}}
};
private static int PickRandomNumber(int maxValue, int minValue = 0)
{
return (int) (GD.Randi() % (maxValue - minValue) + minValue);
}
public static List<String> CloneArray(List<String> original)
{
var result = new List<String>();
result.AddRange(original);
return result;
}
private static List<String> GetLetter(string state, int maxLength)
{
List<String> transitions = CloneArray(Transition[state]);
if (maxLength < 3)
{
transitions.Remove("COMPOSE");
transitions.Remove("DOUBLE_CONSONNE");
transitions.Remove("DOUBLE_VOYELLE");
}
var stateIndex = PickRandomNumber(transitions.Count);
state = transitions[stateIndex];
List<String> lettersList = Letters[state];
int letterIndex = PickRandomNumber(lettersList.Count);
List<String> result = new List<string> {state, lettersList[letterIndex]};
return result;
}
public static string Generate(int minLength = 3, int maxLength = 12)
{
var length = PickRandomNumber(maxLength, minLength);
String name = "";
int index = 0;
String state = "INITIAL";
while (index < length)
{
var obj = GetLetter(state, length - index);
state = obj[0];
var lastLetter = obj[1];
name += lastLetter;
index += lastLetter.Length;
}
return Char.ToUpper(name[0]) + name.Substring(1);
}
}