Пример #1
0
        public Robot Execute()
        {
            var controllerTask = _controller.Execute();

            while (!controllerTask.IsCompleted)
            {
                _robotToControllerPipe.Output(_hull.GetColour(_location));
                var colourToPaint = (int)_controllerToRobotPipe.ReadInput().Result;
                _hull.SetColour(_location, colourToPaint);
                if (controllerTask.IsCompleted)
                {
                    break;
                }
                var directionToTurn = (int)_controllerToRobotPipe.ReadInput().Result;
                switch (directionToTurn)
                {
                case 0: _direction += 3;
                    break;

                case 1: _direction += 1;
                    break;
                }

                _direction = _direction % 4;
                _location  = new Point(
                    _location.X + _directions[_direction].X,
                    _location.Y + _directions[_direction].Y
                    );
            }

            return(this);
        }
        private static IOPipe InitialisedPipe(int initialisedValue)
        {
            var pipe = new IOPipe();

            pipe.Output(initialisedValue);
            return(pipe);
        }
Пример #3
0
        private async Task <long> GetTractorAreaAsync(int fieldSize)
        {
            long pullCount = 0;

            for (var y = 0; y < fieldSize; y++)
            {
                for (var x = 0; x < fieldSize; x++)
                {
                    InitialiseDrone();
                    var controllerTask = _droneController.Execute();

                    _droneInput.Output(x);
                    _droneInput.Output(y);

                    var isPulled = await _droneOutput.ReadInput();

                    pullCount += isPulled;
                }
            }
            return(pullCount);
        }
Пример #4
0
        public async Task HandleInput()
        {
            await Task.Run(async() =>
            {
                var i             = 0;
                var inputFile     = "Day13/GameInput.txt";
                var preparedInput = File.ReadAllLines(inputFile)
                                    .Select(ParseInputLine)
                                    .SelectMany(x => x)
                                    .ToArray();

                while (!_gameTask.IsCompleted)
                {
                    var output = 0;

                    if (i < preparedInput.Length)
                    {
                        output = preparedInput[i];
                        i     += 1;
                    }
                    else
                    {
                        if (true)
                        {
                            if (_paddleX < _ballX)
                            {
                                File.AppendAllLines(inputFile, new[] { "R1" });
                                output = 1;
                            }
                            else if (_ballX < _paddleX)
                            {
                                output = -1;
                                File.AppendAllLines(inputFile, new[] { "L1" });
                            }
                            else
                            {
                                output = 0;
                                File.AppendAllLines(inputFile, new[] { "S1" });
                            }
                        }
                        else
                        {
                            var input = Console.ReadKey();
                            switch (input.Key)
                            {
                            case ConsoleKey.LeftArrow:
                                output = -1;
                                File.AppendAllLines(inputFile, new[] { "L1" });
                                break;

                            case ConsoleKey.RightArrow:
                                File.AppendAllLines(inputFile, new[] { "R1" });
                                output = 1;
                                break;
                            }
                        }
                    }

                    _gameInput.Output(output);
                    await Task.Delay(1);
                }
            });
        }