public void HelloWorld()
        {
            const string        HelloWorldWikipedia = @"(=<`#9]~6ZY32Vx/4Rs+0No-&Jk)""Fh}|Bcy?`=*z]Kw%oG4UUS0/@-ejc(:'8dc";
            string              output      = "";
            MalbolgeInterpreter interpreter = new MalbolgeInterpreter(_ =>
            {
                Console.Write(_);
                output += _;
            }, null);

            interpreter.LoadProgram(HelloWorldWikipedia);
            interpreter.Run();
            Assert.AreEqual("Hello World!", output);
        }
Beispiel #2
0
        public string GenerateOpCodes(string target)
        {
            StringBuilder       outputBuilder    = new StringBuilder();
            MalbolgeInterpreter interpreter      = new MalbolgeInterpreter(output => outputBuilder.Append(output), null);
            StringBuilder       programAsOpCodes = new StringBuilder();

            for (int i = 1; i <= target.Length; ++i)
            {
                string currentTarget = target.Substring(0, i);
                bool   found         = false;
                while (!found)
                {
                    StringBuilder currentProgram = new StringBuilder(programAsOpCodes.ToString());
                    foreach (string opCodeMutation in OpCodeMutations)
                    {
                        currentProgram.Append(opCodeMutation);
                        currentProgram.Append(PrintOpCode);
                        currentProgram.Append(TerminalOpCode);
                        string malbolgeProgram = MalbolgeInterpreter.DeNormalize(currentProgram.ToString());
                        interpreter.LoadProgram(malbolgeProgram);
                        interpreter.Run();
                        string currentOutput = outputBuilder.ToString();
                        if (currentTarget.Equals(currentOutput))
                        {
                            programAsOpCodes.Append(opCodeMutation);
                            programAsOpCodes.Append(PrintOpCode);
                            found = true;
                            Console.Write(target[i]);
                            break;
                        }
                        int currentAttemptLength = opCodeMutation.Length + 2;
                        currentProgram.Remove(currentProgram.Length - currentAttemptLength, currentAttemptLength);
                    }
                    if (!found)
                    {
                        // If none found, just pick one at random, it'll be fine.
                        int    randomIndex    = RGen.Next(ForcedOpCodeMutations.Count);
                        string opCodeMutation = ForcedOpCodeMutations[randomIndex];
                        programAsOpCodes.Append(opCodeMutation);
                    }
                }
            }

            programAsOpCodes.Append(TerminalOpCode);
            return(programAsOpCodes.ToString());
        }