Themabewertung:
  • 0 Bewertung(en) - 0 im Durchschnitt
  • 1
  • 2
  • 3
  • 4
  • 5
Scrambler Gem
#4
Ich will mitspielen!

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)
[Bild: img.php?id=2009DUMO01&ranking=NR&event_1...vent_3=sq1]
Zitieren


Nachrichten in diesem Thema
Scrambler Gem - von tim - 27.09.2011, 14:54

Gehe zu: