Exemplo n.º 1
0
        public bool ProcessFile(string fileInputPath, string fileOutputPath)
        {
            Canvas canvas;
            List <Tuple <char, string[]> > parameters = _fileReader.ReadFile(fileInputPath);

            if (!parameters.Any(p => p.Item1 == 'C'))
            {
                Console.WriteLine("There is no canvas in the input file");
                return(false);
            }
            else
            {
                var canvasParameter = parameters.FirstOrDefault(p => p.Item1 == 'C');
                canvas = _canvasService.CreateCanvas(int.Parse(canvasParameter.Item2[0]), int.Parse(canvasParameter.Item2[1]));
                parameters.Remove(canvasParameter);
                _fileWriter.ClearFile(fileOutputPath);
                _fileWriter.DrawShapeIntoFile(canvas, fileOutputPath);
            }

            foreach (var parameter in parameters)
            {
                switch (parameter.Item1)
                {
                case 'L':
                {
                    Line line = new Line(new Point(int.Parse(parameter.Item2[0]), int.Parse(parameter.Item2[1])),
                                         new Point(int.Parse(parameter.Item2[2]), int.Parse(parameter.Item2[3])));
                    _lineService.CreateShape(ref canvas, line);
                    _fileWriter.DrawShapeIntoFile(canvas, fileOutputPath);
                }
                break;

                case 'R':
                {
                    Rectangle rectangle = new Rectangle(new Point(int.Parse(parameter.Item2[0]), int.Parse(parameter.Item2[1])),
                                                        new Point(int.Parse(parameter.Item2[2]), int.Parse(parameter.Item2[3])));
                    _rectangleService.CreateShape(ref canvas, rectangle);
                    _fileWriter.DrawShapeIntoFile(canvas, fileOutputPath);
                }
                break;

                case 'B':
                {
                    _bucketFillService.FillCanvas(ref canvas,
                                                  new Point(int.Parse(parameter.Item2[0]), int.Parse(parameter.Item2[1])),
                                                  char.Parse(parameter.Item2[2]));
                    _fileWriter.DrawShapeIntoFile(canvas, fileOutputPath);
                }
                break;
                }
            }
            return(true);
        }
        public void BucketFillService_FillCanvas_Test()
        {
            // Given
            Canvas canvas = new Canvas(5, 5);

            canvas.Matrix[1, 0] = 'x';
            canvas.Matrix[1, 1] = 'x';
            canvas.Matrix[1, 2] = 'x';
            canvas.Matrix[1, 3] = 'x';
            canvas.Matrix[1, 4] = 'x';
            Point      point = new Point(2, 2);
            const char color = 'c';

            // When
            _bucketFillGenerator.FillCanvas(ref canvas, point, color);

            // Then
            Assert.AreEqual('c', canvas.Matrix[2, 1]);
            Assert.AreEqual('c', canvas.Matrix[2, 2]);
            Assert.AreEqual('c', canvas.Matrix[2, 3]);
            Assert.AreEqual('c', canvas.Matrix[3, 1]);
            Assert.AreEqual('c', canvas.Matrix[3, 2]);
            Assert.AreEqual('c', canvas.Matrix[3, 3]);
            Assert.AreEqual('c', canvas.Matrix[4, 1]);
            Assert.AreEqual('c', canvas.Matrix[4, 2]);
            Assert.AreEqual('c', canvas.Matrix[4, 3]);
            Assert.AreEqual('x', canvas.Matrix[1, 1]);
            Assert.AreEqual('x', canvas.Matrix[1, 2]);
            Assert.AreEqual('x', canvas.Matrix[1, 3]);
            Assert.AreEqual('x', canvas.Matrix[1, 4]);
            Assert.AreEqual('x', canvas.Matrix[1, 3]);
            Assert.AreEqual('\0', canvas.Matrix[0, 0]);
            Assert.AreEqual('\0', canvas.Matrix[0, 1]);
            Assert.AreEqual('\0', canvas.Matrix[0, 2]);
            Assert.AreEqual('\0', canvas.Matrix[0, 3]);
            Assert.AreEqual('\0', canvas.Matrix[0, 4]);
        }