예제 #1
0
 public ArcadeProgram(IntCodeStateMachine cpu, IGameView gameView, IGameInput gameInput = null)
 {
     cpu.OnOutput      += Advance;
     _gameView          = gameView;
     gameInput.OnInput += (_, e) => cpu.SetInput(e);
     _gameState         = new GameState(gameView, gameInput);
 }
예제 #2
0
            public void Run(object sender, BigInteger value)
            {
                switch (_currentState)
                {
                case CodeState.Paint:
                    var colorToPaint = value == 0 ? SquareColor.Black : SquareColor.White;
                    paintedSquares[_currentPosition] = colorToPaint;

                    cpu.SetInput(value == 0 ? 0 : 1);

                    _currentState = CodeState.Move;
                    break;

                case CodeState.Move:
                    var turnDirection = value == 0 ? Direction.Left : Direction.Right;
                    _orientation = _orientation switch
                    {
                        Orientation.Up => turnDirection == Direction.Left ? Orientation.Left : Orientation.Right,
                        Orientation.Left => turnDirection == Direction.Left ? Orientation.Down : Orientation.Up,
                        Orientation.Down => turnDirection == Direction.Left ? Orientation.Right : Orientation.Left,
                        Orientation.Right => turnDirection == Direction.Left ? Orientation.Up : Orientation.Down
                    };

                    _currentPosition = _orientation switch
                    {
                        Orientation.Up => _currentPosition.MoveUp(),
                        Orientation.Left => _currentPosition.MoveLeft(),
                        Orientation.Down => _currentPosition.MoveDown(),
                        Orientation.Right => _currentPosition.MoveRight()
                    };

                    if (!paintedSquares.TryGetValue(_currentPosition, out var currentPanelColor))
                    {
                        currentPanelColor = SquareColor.Black;
                    }

                    var nextInput = currentPanelColor == SquareColor.Black ? 0 : 1;

                    cpu.SetInput(nextInput);

                    _currentState = CodeState.Paint;

                    break;
                }
            }
        }
    }
예제 #3
0
        private static int RunImpl(string input, int startValue, Dictionary <Coordinate, SquareColor> paintedSquares)
        {
            var cpu = new IntCodeStateMachine(input.Split(',').Select(BigInteger.Parse));
            var paintingRobotState = new MessageState(paintedSquares, cpu);

            cpu.OnOutput += paintingRobotState.Run;
            cpu.SetInput(startValue);

            cpu.RunAll();

            var totalPaintedSquares = paintedSquares.Count;

            return(totalPaintedSquares);
        }
예제 #4
0
        public RepairDroid(IntCodeStateMachine cpu, ICommandInput commandInput)
        {
            _cpu          = cpu;
            _commandInput = commandInput;
            _commandInput.InputReceived += (_, v) =>
            {
                var cmd = (MovementCommand)v;
                if (v < 1 || v > 4)
                {
                    throw new Exception("Wrong input value");
                }
                _cpu.SetInput((int)cmd);
            };

            _cpu.OnOutput += (_, o) =>
            {
                _lastOutput = (RepairDroidStatusCode)o;
                OnReply?.Invoke(this, _lastOutput);
                commandInput.AwaitInput();
            };
        }