Thema: [C#] WordMix Konsole
- 
	20.11.2011, 12:35 #1Nachrichtendienst  
 - Registriert seit
- 09.11.2011
- Beiträge
- 129
 Thanked 141 Times in 52 Posts [C#] WordMix Konsole [C#] WordMix KonsoleHi, 
 
 hab das auf meinem Rechner gefunden, ich weiß nicht, was ich damit anfangen soll. 
 
 Vielleicht kann es ja einer von euch gebrauchen, bevor ich das Projekt lösche.
 
 Program.cs:
 Util.cs:PHP-Code:using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.IO;
 
 namespace WordMix
 {
 class Program
 {
 static string[] sentences;
 static Program()
 {
 sentences = Util.ReadSentences();
 }
 
 static void Main(string[] args)
 {
 int counter = 1;
 Console.Title = "GayMix 1337";
 while (true)
 {
 Util.Print("Drücke °Y°<Enter>°r°, um eine neue Runde zu starten.");
 Console.ReadLine();
 
 string rawSentence = Util.GetRandomSentence(sentences);
 string mixedSentence = WMUtil.MixSentence(rawSentence);
 Util.Print(String.Format("Runde [°G°{0}°r°] beginnt!##{1}#", new Object[] { counter, mixedSentence }));
 
 DateTime started = DateTime.Now;
 
 Console.Write("Lösung: ");
 int t = 1;
 
 string userSentence = null;
 while (!(userSentence = WMUtil.MakeSentence(mixedSentence, Console.ReadLine())).Equals(rawSentence))
 {
 Util.Print(String.Format("-> '°R°{0}°r°'#", userSentence));
 Console.Write("Lösung: ");
 t++;
 }
 
 Util.Print(String.Format("-> '°R°{0}°r°'#", userSentence));
 double time = Math.Round(DateTime.Now.Subtract(started).TotalMilliseconds / 1000, 2);
 Util.Print(String.Format("Du hast °B°{0} Sekunden°r° gebraucht, um den Satz zu lösen. (°R°{1} Versuche°r° insgesamt)#", new Object[] { time, t }));
 
 counter++;
 }
 }
 
 }
 }
 
 PHP-Code:using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.IO;
 
 namespace WordMix
 {
 public static class Util
 {
 private static Random r;
 
 static Util()
 {
 r = new Random();
 }
 
 public static string GetRandomSentence(string[] sentences)
 {
 return sentences[r.Next(sentences.Length)];
 }
 
 public static string[] ReadSentences()
 {
 return File.ReadAllLines(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\sentences.txt", Encoding.Default);
 }
 
 public static void Print(string value)
 {
 for (int i = 0; i < value.Length; i++)
 {
 char temp = value[i];
 
 if (temp == '°')
 {
 string kcode = value.Substring(i + 1);
 int index = kcode.IndexOf('°');
 if (index == -1)
 continue;
 
 kcode = kcode.Substring(0, index);
 
 if (String.IsNullOrEmpty(kcode))
 Console.ForegroundColor = ConsoleColor.Gray;
 else if (kcode.Length == 1)
 Console.ForegroundColor = GetColor(char.Parse(kcode));
 
 i += kcode.Length + 1;
 continue;
 }
 else if (temp == '#')
 {
 Console.Write("\n");
 continue;
 }
 else
 {
 Console.Write(temp);
 }
 }
 
 Console.ForegroundColor = ConsoleColor.Gray;
 Console.Write("\n");
 }
 
 private static ConsoleColor GetColor(char value)
 {
 switch (value)
 {
 case 'A': return ConsoleColor.Gray;
 case 'B': return ConsoleColor.Blue;
 case 'C': return ConsoleColor.Cyan;
 case 'E': return ConsoleColor.DarkGreen;
 case 'G': return ConsoleColor.Green;
 case 'K': return ConsoleColor.Black;
 case 'M': return ConsoleColor.Magenta;
 case 'R': return ConsoleColor.Red;
 case 'W': return ConsoleColor.White;
 case 'Y': return ConsoleColor.Yellow;
 default: return ConsoleColor.Gray;
 }
 }
 }
 }
 Ist aber nicht ganz so umfangreich, es wird beispielsweise nicht überprüft, ob man doppelte Zeichen oder auch "verbotene" Zeichen benutzt hat.PHP-Code:using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Text.RegularExpressions;
 
 namespace WordMix
 {
 public class WMUtil
 {
 private static Random r;
 
 static WMUtil()
 {
 r = new Random();
 }
 
 public static String MakeSentence(String sentence, String solution)
 {
 StringBuilder builder = new StringBuilder();
 Dictionary<Char, String> map = new Dictionary<Char, String>();
 
 MatchCollection matches = Regex.Matches(sentence, "\\s*([^\\)]+)\\(°B°(.)°°\\)\\s*");
 foreach (Match m in matches)
 {
 String value = m.Groups[1].Value.Trim();
 Char key = Char.Parse(m.Groups[2].Value);
 
 if (!map.ContainsKey(key))
 {
 map.Add(key, value);
 }
 }
 
 for (int i = 0; i < solution.Length; i++)
 {
 Char temp = solution[i];
 
 if (map.ContainsKey(temp))
 {
 builder.Append(map[temp]);
 
 if (i < solution.Length - 1)
 {
 builder.Append(' ');
 }
 }
 }
 
 
 return builder.ToString();
 }
 
 public static String MixSentence(String rawSentence)
 {
 StringBuilder builder = new StringBuilder();
 Char[] keys = new Char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
 
 if (rawSentence == null)
 {
 return null;
 }
 
 String[] randomizedArray = RandomizeSentence(rawSentence);
 
 if (randomizedArray.Length > keys.Length || randomizedArray.Length < 3)
 {
 return null;
 }
 
 for (int i = 0; i < randomizedArray.Length; i++)
 {
 String param = randomizedArray[i];
 Char paramKey = keys[i];
 
 builder.Append(String.Format("{0}(°B°{1}°°)", new Object[] { param, paramKey }));
 
 if (i < randomizedArray.Length - 1)
 {
 builder.Append(' ');
 }
 }
 
 return builder.ToString();
 }
 
 private static string[] RandomizeSentence(string sentence)
 {
 string[] splitted = sentence.Split(' ');
 List<KeyValuePair<int, string>> list = new List<KeyValuePair<int, string>>();
 foreach (string s in splitted)
 {
 list.Add(new KeyValuePair<int, string>(r.Next(), s));
 }
 
 IOrderedEnumerable<KeyValuePair<int, string>> sorted = from item in list
 orderby item.Key
 select item;
 
 string[] result = new string[splitted.Length];
 int index = 0;
 foreach (KeyValuePair<int, string> pair in sorted)
 {
 result[index] = pair.Value;
 index++;
 }
 return result;
 }
 }
 }
 
 
 Die Sätze werden aus einer Textdatei (DESKTOP\sentences.txt) gelesen, dort müssen normale Sätze drinstehen, den Pfad kann man ja anpassen, dafür ist es schließlich OpenSource.
 
 MfG
 
- 
	The Following 2 Users Say Thank You to Brainy For This Useful Post:
 
Ähnliche Themen
- 
  Frage Wordmix bot.Von x Like a Boss x3 im Forum Knuddels BotsAntworten: 1Letzter Beitrag: 20.07.2013, 15:22
- 
  C# Konsole Schließen Button deaktivierenVon Minecraft im Forum .NetAntworten: 1Letzter Beitrag: 08.08.2012, 01:34
- 
  Konsole mit als Handgepäck oder Baggage?Von BL4cK im Forum RealLifeAntworten: 1Letzter Beitrag: 04.03.2012, 11:55
- 
  [C#] Taschenrechner-KonsoleVon Snees im Forum .NetAntworten: 2Letzter Beitrag: 17.12.2011, 12:58
- 
  ASCII-Buchstaben für KonsoleVon Snees im Forum HochsprachenAntworten: 0Letzter Beitrag: 01.12.2011, 21:14
Diese Seite nutzt Cookies, um das Nutzererlebnis zu verbessern. Klicken Sie hier, um das Cookie-Tracking zu deaktivieren.
 
									 
														 
														 
					
					
					
						 Zitieren
  Zitieren