////////////////////////////////////////////////////////////// public UBSModuleDelegates(SendMessageDelegate pSendMessageFunction, GetAvailableModuleNamesDelegate pGetAvailableModulesFunction, GetModule pGetModule, LogDelegate pLogFunction, WriteConsoleDelegate pWriteConsoleFunction, NotifyDelegate pNotifyFunction, ErrorDelegate pErrorFunction, ExecutionTimeDelegate pExecutionTimeFunction, SetGlobalParameterDelegate pSetGlobalParameterFunction, GetGlobalParameterDelegate pGetGlobalParameterFunction, GoToModuleDelegate pGoToModuleFunction, ButtonColorDelegate pButtonColoFunction) { SendMessageFunction = pSendMessageFunction; GetAvailableModulesFunction = pGetAvailableModulesFunction; GetModule = pGetModule; LogFunction = pLogFunction; WriteConsoleFunction = pWriteConsoleFunction; NotifyFunction = pNotifyFunction; ErrorFunction = pErrorFunction; ExecutionTimeFunction = pExecutionTimeFunction; SetGlobalParameterFunction = pSetGlobalParameterFunction; GetGlobalParameterFunction = pGetGlobalParameterFunction; GoToModuleFunction = pGoToModuleFunction; ButtonColorFunction = pButtonColoFunction; }
public static void Run( string playerName, int speed, TerminalFormatter formatter, WriteConsoleDelegate writeConsoleDelegate) { new NibblesGame(playerName, speed, formatter, writeConsoleDelegate).RunCore(); }
public static void RunConsole(TerminalFormatter formatter, WriteConsoleDelegate writeConsoleDelegate = null) { if (formatter == null) { throw new ArgumentNullException(nameof(formatter)); } if (writeConsoleDelegate == null) { writeConsoleDelegate = (str, newLine) => { if (newLine) { Console.Out.WriteLine(str); } else { Console.Out.Write(str); } }; } ConsoleUtils.TrySetConsoleTitle("Nibbles!"); // Add a \r to overwrite the title if the terminal doesn't support "ESC ] 0" var sb = new StringBuilder("\r"); const string heading = "N I B B L E S !"; sb.AppendLine(new string(' ', Math.Max(0, ConsoleUtils.ConsoleWidth / 2 - heading.Length / 2)) + heading); sb.AppendLine(); sb.AppendLine(" Game Controls:"); sb.AppendLine(); sb.AppendLine(" General Player"); sb.AppendLine(" (Up)"); sb.AppendLine(" P - Pause ↑"); sb.AppendLine(" Q - Quit (Left) ← → (Right)"); sb.AppendLine(" ↓"); sb.AppendLine(" (Down)"); sb.AppendLine(); sb.Append("Please enter your name: "); writeConsoleDelegate(sb.ToString(), false); string name = Console.ReadLine(); writeConsoleDelegate(); writeConsoleDelegate("Please enter the speed (1-10): ", false); try { int speed = int.Parse(Console.ReadLine()); Run(name, speed, formatter, writeConsoleDelegate); } catch (Exception ex) { writeConsoleDelegate(); writeConsoleDelegate(formatter.Format(TerminalFormatting.ForegroundRed, TerminalFormatting.BoldBright) + "ERROR:" + formatter.Format(TerminalFormatting.None) + " " + ConsoleUtils.FixDisplayCharacters(ex.Message)); } }
private NibblesGame( string playerName, int speed, TerminalFormatter formatter, WriteConsoleDelegate writeConsoleDelegate) : base() { if (speed < 0 || speed > 10) { throw new InvalidOperationException("Invalid speed: " + speed); } this.formatter = formatter ?? throw new ArgumentNullException(nameof(formatter)); this.writeConsoleDelegate = writeConsoleDelegate ?? throw new ArgumentNullException(nameof(writeConsoleDelegate)); this.playerName = playerName ?? throw new ArgumentNullException(nameof(playerName)); this.tickWaitTime = (int)Math.Round(180d * Math.Pow(2, -(speed - 1) * 0.35d)); // Ensure the console is large enough. if (ConsoleUtils.ConsoleWidth < 50 || ConsoleUtils.ConsoleHeight < 16) { throw new ArgumentException("The console must have a size of at least 50x16."); } // Determine the current console size. this.consoleWidth = ConsoleUtils.ConsoleWidth; this.consoleHeight = ConsoleUtils.ConsoleHeight; this.inputThread = new Thread(RunInputThread); this.fieldScreenBuffer = new FieldScreenBufferElement[this.FieldWidth, this.FieldHeight]; //// Create the initial border. // First line for (int i = 0; i < this.FieldWidth; i++) { this.currentObstacleCoordinates.Add(i); } // Middle lines for (int i = 1; i < this.FieldHeight - 1; i++) { this.currentObstacleCoordinates.Add(i * this.FieldWidth + 0); // Left this.currentObstacleCoordinates.Add((i + 1) * this.FieldWidth - 1); // Right } // Last line for (int i = 0; i < this.FieldWidth; i++) { this.currentObstacleCoordinates.Add((this.FieldHeight - 1) * this.FieldWidth + i); } }
////////////////////////////////////////////////////////////// public void AppendDelegates(UBSModuleDelegates module_delegates) { this.SendMessageFunction = module_delegates.SendMessageFunction; this.GetAvailableModulesFunction = module_delegates.GetAvailableModulesFunction; this.GetModuleFunction = module_delegates.GetModule; this.LogFunction = module_delegates.LogFunction; this.WriteConsoleFunction = module_delegates.WriteConsoleFunction; this.NotifyFunction = module_delegates.NotifyFunction; this.ErrorFunction = module_delegates.ErrorFunction; this.ExecutionTimeFunction = module_delegates.ExecutionTimeFunction; this.SetGlobalParameterFunction = module_delegates.SetGlobalParameterFunction; this.GetGlobalParameterFunction = module_delegates.GetGlobalParameterFunction; this.GoToModuleFunction = module_delegates.GoToModuleFunction; this.ButtonColorFunction = module_delegates.ButtonColorFunction; }
public void WriteConsole(MessageStatus Status, string text) { if (MainRTB.InvokeRequired) { var invokedMethod = new WriteConsoleDelegate(WriteConsole); this.Invoke(invokedMethod, new object[] { Status, text }); return; } //if (Status == MessageStatus.Error) //{ // MessageBox.Show(text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); //} MainRTB.AppendText(Environment.NewLine); MainRTB.SelectionStart = MainRTB.TextLength; MainRTB.SelectionLength = 0; MainRTB.SelectionColor = Status.DrawColor; MainRTB.AppendText(text); MainRTB.SelectionColor = MainRTB.ForeColor; }