1. #1

    Registriert seit
    09.11.2011
    Beiträge
    140
    Thanked 153 Times in 59 Posts

    Standard [C#] WordMix Konsole

    Hi,

    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:
    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++;
                }
            }

        }
    } 
    Util.cs:
    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;
                }
            }
        }
    } 
    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;
            }
        }
    } 
    Ist aber nicht ganz so umfangreich, es wird beispielsweise nicht überprüft, ob man doppelte Zeichen oder auch "verbotene" Zeichen benutzt hat.

    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

  2. The Following 2 Users Say Thank You to Brainy For This Useful Post:

    AmJano (26.08.2012), BL4cK (20.11.2011)

Ähnliche Themen

  1. Frage Wordmix bot.
    Von x Like a Boss x3 im Forum Knuddels Bots
    Antworten: 1
    Letzter Beitrag: 20.07.2013, 15:22
  2. C# Konsole Schließen Button deaktivieren
    Von Minecraft im Forum .Net
    Antworten: 1
    Letzter Beitrag: 08.08.2012, 01:34
  3. Konsole mit als Handgepäck oder Baggage?
    Von BL4cK im Forum RealLife
    Antworten: 1
    Letzter Beitrag: 04.03.2012, 11:55
  4. [C#] Taschenrechner-Konsole
    Von Snees im Forum .Net
    Antworten: 2
    Letzter Beitrag: 17.12.2011, 12:58
  5. ASCII-Buchstaben für Konsole
    Von Snees im Forum Hochsprachen
    Antworten: 0
    Letzter Beitrag: 01.12.2011, 21:14
Diese Seite nutzt Cookies, um das Nutzererlebnis zu verbessern. Klicken Sie hier, um das Cookie-Tracking zu deaktivieren.