27.09.2011, 15:57
(Dieser Beitrag wurde zuletzt bearbeitet: 27.09.2011, 15:58 von deepSubDiver.)
Ich will mitspielen!
(C# natürlich)
Code:
void Main() {
// 3x3 example
var sides = new[] {"U", "D", "F", "B", "L", "R"};
var prefixes = new string[]{};
var suffixes = new string[]{"", "2", "'"};
Func<string, string, bool> dispensableMoveDelegate = (prev, curr) => {
if (string.IsNullOrEmpty(prev) || string.IsNullOrEmpty(curr)) {
return false;
}
char sidePrev = prev[0];
char sideCurr = curr[0];
int axisPrev = "UDFBLR".IndexOf(sidePrev);
int axisCurr = "UDFBLR".IndexOf(sideCurr);
return axisPrev / 3 == axisCurr / 3;
};
var scramble = GenerateScramble(sides, prefixes, suffixes, dispensableMoveDelegate, 25);
scramble.Dump();
}
string GenerateScramble(IList<string> sides, IList<string> prefixes, IList<string> suffixes, Func<string, string, bool> dispensableMove, int moveAmount) {
if (sides == null || sides.Count() == 0) {
throw new ArgumentException("sides");
}
if (prefixes == null) {
prefixes = new string[] {};
}
if (suffixes == null) {
suffixes = new string[] {};
}
if (dispensableMove == null) {
dispensableMove = (prev, curr) => false;
}
if (moveAmount < 1) {
throw new ArgumentException("moveAmount");
}
var rng = new Random();
var sb = new StringBuilder();
string prevMove = string.Empty;
while (moveAmount-- > 0) {
var currMove = string.Empty;
do {
currMove = GenerateRandomMove(sides, prefixes, suffixes);
} while (dispensableMove(prevMove, currMove));
prevMove = currMove;
sb.AppendFormat(" {0}", currMove);
}
return sb.ToString().Substring(1);
}
string GenerateRandomMove(IList<string> sides, IList<string> prefixes, IList<string> suffixes) {
var rng = new Random();
var sb = new StringBuilder();
if (prefixes.Count() > 0) {
sb.Append(prefixes[rng.Next(prefixes.Count())]);
}
sb.Append(sides[rng.Next(sides.Count())]);
if (suffixes.Count() > 0) {
sb.Append(suffixes[rng.Next(suffixes.Count())]);
}
return sb.ToString();
}
(C# natürlich)