Example #1
0
 public static void Run(
     string playerName,
     int speed,
     TerminalFormatter formatter,
     WriteConsoleDelegate writeConsoleDelegate)
 {
     new NibblesGame(playerName, speed, formatter, writeConsoleDelegate).RunCore();
 }
Example #2
0
        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));
            }
        }
Example #3
0
        static Program()
        {
            // If we are running on Windows, try to enable virtual terminal processing.
            // On other platforms, this should be enabled by default.
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                try
                {
                    TerminalFormatter.EnableWindowsVirtualTerminalSequences();
                }
                catch (Exception)
                {
                    // Ignore. This could happen when the streams is redirected, or on
                    // Windows 10 Version 1511 and lower.
                }

                try
                {
                    TerminalFormatter.EnableWindowsVirtualTerminalSequences(stdErr: true);
                }
                catch (Exception)
                {
                    // Ignore. This could happen when the streams is redirected, or on
                    // Windows 10 Version 1511 and lower.
                }
            }

            Formatter = new TerminalFormatter();

            // Set UTF-8 as output encoding for the console to support Unicode characters.
            // UTF-8 is already the standard on Linux terminals, and on Windows it is supported
            // starting with Windows 10 (Version 1507).
            Console.OutputEncoding = Encoding.UTF8;

            // Note however, that setting Console.InputEncoding to Encoding.UTF8 will not work
            // on Windows when the standard input stream is not redirected,
            // whereas Encoding.Unicode works. Therefore, on Windows we
            // set the InputEncoding to Unicode if the stream is not redirected; otherwise we
            // use UTF-8 to be consistent with Linux and with the output encoding.
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Console.IsInputRedirected)
            {
                Console.InputEncoding = Encoding.Unicode;
            }
            else
            {
                Console.InputEncoding = Encoding.UTF8;
            }

            // Create a new StreamReader with a large buffer so that the user can enter more than
            // 254 characters when calling Console.ReadLine().
            const int consoleBufferSize = 8192;

            Console.SetIn(new StreamReader(Console.OpenStandardInput(consoleBufferSize),
                                           Console.InputEncoding, false, consoleBufferSize, true));
        }
Example #4
0
        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);
            }
        }
Example #5
0
        private SampleRunner(TerminalFormatter formatter)
        {
            _sampleList    = new Dictionary <char, SampleDefinition>();
            _consoleBuffer = new StringBuilder();

            if (formatter == null)
            {
                throw new ArgumentNullException(nameof(formatter));
            }
            _formatter = formatter;

            _consoleWidth  = ConsoleUtils.ConsoleWidth;
            _consoleHeight = ConsoleUtils.ConsoleHeight;
        }
Example #6
0
        static Program()
        {
            try
            {
                TerminalFormatter.EnableWindowsVirtualTerminalSequences();
            }
            catch (Exception)
            {
                // Ignore
            }

            try
            {
                TerminalFormatter.EnableWindowsVirtualTerminalSequences(stdErr: true);
            }
            catch (Exception)
            {
                // Ignore
            }

            _formatter = new TerminalFormatter();
        }
Example #7
0
 public static SampleRunner Build(TerminalFormatter formatter)
 {
     return(new SampleRunner(formatter));
 }