Exemple #1
0
        public void RunGame()
        {
            int preProgrammedInputsIndex = 0;

            while (IntcodeProgramStatus.Running.Equals(GameStatus) ||
                   IntcodeProgramStatus.AwaitingInput.Equals(GameStatus))
            {
                OutputListener.Values.Clear();

                if (preProgrammedInputsIndex < PreProgrammedInputs.Count)
                {
                    InputProvider.AddInputValue(PreProgrammedInputs[preProgrammedInputsIndex]);
                    preProgrammedInputsIndex++;
                }
                if (!InputProvider.HasInput() &&
                    IntcodeProgramStatus.AwaitingInput.Equals(GameStatus))
                {
                    int paddleCommand = GetPaddleCommand();
                    InputProvider.AddInputValue(paddleCommand);
                }
                GameStatus = _computer.RunProgram();

                // Output values must be a multiple of 3
                if (OutputListener.Values.Count % 3 != 0)
                {
                    throw new Exception("Invalid output count encountered");
                }

                RefreshGridCells();
                if (DrawBoard)
                {
                    DrawGameBoard();
                }
                if (GameMode.Manual.Equals(Mode) &&
                    IntcodeProgramStatus.AwaitingInput.Equals(GameStatus))
                {
                    PauseGameAndCheckForInput();
                }
                else if (DrawBoard)
                {
                    Thread.Sleep(_automatedModeTheradSleepTimerMs);
                }

                _timeIndex++;
            }
        }
Exemple #2
0
        private void PauseGameAndCheckForInput()
        {
            // If the joystick is in the neutral position, provide 0.
            // If the joystick is tilted to the left, provide -1.
            // If the joystick is tilted to the right, provide 1.
            Console.WriteLine("Move the paddle: -1: Left, 0: Don't mode; 1: Right");
            string inputValue;

            Thread.Sleep(_manualModeThreadSleepTimerMs);
            bool success = GameConsoleReader.TryReadKey(out inputValue, _manualModeThreadSleepTimerMs);

            //Thread.Sleep(2000);
            if (success)
            {
                if (inputValue == "-1")
                {
                    InputProvider.AddInputValue(-1);
                }
                else if (inputValue == "0")
                {
                    InputProvider.AddInputValue(0);
                }
                else if (inputValue == "1")
                {
                    InputProvider.AddInputValue(1);
                }
                else
                {
                    InputProvider.AddInputValue(0);
                }
            }
            else
            {
                InputProvider.AddInputValue(0);
            }
        }