Exemple #1
0
        public void Part1(string filename, int expected)
        {
            var parser = new Parser(filename);
            var moves  = parser.Parse(new NavigationFactory());
            var sut    = new RegularShip(moves, new Compass(Direction.East));
            var actual = sut.Sail();

            Assert.Equal(expected, actual);
        }
        public BattleArea CreateBattleAreaWithShip()
        {
            ShipContainer.Ships = new List <List <ShipBase> >();

            do
            {
                Random random = new Random();
                bool   isEmptyField, isShipComplete;
                int    iArea, jArea;
                do
                {
                    isShipComplete = true;
                    do
                    {
                        isEmptyField = true;

                        iArea = random.Next(1, Height - maker.Board);
                        jArea = random.Next(1, Width - maker.Board);

                        Guid shipGuid = Guid.NewGuid();

                        if (!(Area.BattleFields[iArea, jArea].Field is ShipField) && !(Area.BattleFields[iArea, jArea].Field is NearPointShipField))
                        {
                            isEmptyField = false;
                            int             _shipsFieldCount = 1;
                            List <ShipBase> ships            = new List <ShipBase>();

                            List <Point> shipPoints  = new List <Point>();
                            DrawingType  drawingType = DrawingMethod();
                            ShipBase     shipToField = new RegularShip(false, shipGuid)
                            {
                                ShipsPoints = new Point(iArea, jArea)
                            };
                            Area.BattleFields[iArea, jArea] = new BattleField(new ShipField(shipToField));
                            ships.Add(shipToField);
                            shipPoints.Add(new Point(iArea, jArea));

                            for (int i = 1; i < Ships.First.Value.Lenght; i++)
                            {
                                var iTemp = drawingType == DrawingType.Vertical ? iArea + i : iArea;
                                var jTemp = drawingType == DrawingType.Horizontal ? jArea + i : jArea;

                                if (Area.BattleFields[iTemp, jTemp].Field is ShipField || Area.BattleFields[iTemp, jTemp].Field is BoundField || Area.BattleFields[iTemp, jTemp].Field is NearPointShipField)
                                {
                                    _shipsFieldCount = 0;
                                    isShipComplete   = false;
                                    DeleteInCompleteShip(Area, shipPoints);
                                    break;
                                }
                                _shipsFieldCount += 1;

                                shipToField = new RegularShip(false, shipGuid)
                                {
                                    ShipsPoints = new Point(iTemp, jTemp)
                                };
                                Area.BattleFields[iTemp, jTemp] = new BattleField(new ShipField(shipToField));
                                shipPoints.Add(new Point(iTemp, jTemp));
                                ships.Add(shipToField);
                            }

                            if (isShipComplete)
                            {
                                GenerateNearShipPoints(shipPoints, Area, drawingType, ships);
                                ShipCount += _shipsFieldCount;
                                ShipContainer.Ships.Add(ships);
                            }
                        }
                    }while (isEmptyField);
                }while (!isShipComplete);

                Ships.RemoveFirst();
            }while (Ships.Count > 0);

            return(Area);
        }