Esempio n. 1
0
        public void GetAllocation_MaxCriteria_1_Ok()
        {
            float[] expectedResult = new float[] { 200, 200, 100, 200 };

            float[]   resource = new float[] { 100, 200, 300, 400, 500, 600, 700 };
            float[]   f1       = new float[] { 42, 58, 71, 80, 89, 95, 100 };
            float[]   f2       = new float[] { 30, 49, 63, 68, 69, 65, 60 };
            float[]   f3       = new float[] { 22, 37, 49, 59, 68, 76, 82 };
            float[]   f4       = new float[] { 50, 68, 82, 92, 100, 107, 112 };
            float[][] data     = new float[][] { f1, f2, f3, f4 };

            AllocationParameters parameters = new AllocationParameters(resource, data, CriteriaType.Max);

            testAllocationResults(parameters, expectedResult, 197F);
        }
Esempio n. 2
0
 private void PutShip(Ship ship, AllocationParameters allocationParams)
 {
     if (allocationParams.Direction == Direction.Horizontal)
     {
         for (int i = allocationParams.StartPosX; i < allocationParams.StartPosX + ship.Size; i++)
         {
             _grid.GetCell(new Coordinates {
                 X = allocationParams.StartPosY, Y = i
             }).SetShip(ship);
         }
     }
     else if (allocationParams.Direction == Direction.Vertical)
     {
         for (int i = allocationParams.StartPosY; i < allocationParams.StartPosY + ship.Size; i++)
         {
             _grid.GetCell(new Coordinates {
                 X = i, Y = allocationParams.StartPosX
             }).SetShip(ship);
         }
     }
 }
Esempio n. 3
0
        private AllocationParameters GetStartPositionAndDirection(Ship ship)
        {
            Random random           = new Random();
            int    numOfAttempts    = _grid.GetSize() * _grid.GetSize() * 2;
            var    allocationParams = new AllocationParameters();

            int  attempts = 0;
            int  gridSize = _grid.GetSize();
            bool success  = false;

            while (attempts < numOfAttempts)
            {
                allocationParams.Direction = (Direction)random.Next(0, 2);

                allocationParams.StartPosX = (allocationParams.Direction == Direction.Horizontal)
                                                ? random.Next(0, gridSize - ship.Size + 1)
                                                : random.Next(0, gridSize);

                allocationParams.StartPosY = (allocationParams.Direction == Direction.Vertical)
                                                ? random.Next(0, gridSize - ship.Size + 1)
                                                : random.Next(0, gridSize);

                success = ShipCanBePlaced(ship, allocationParams);
                if (success)
                {
                    break;
                }
                attempts++;
            }

            if (!success)
            {
                return(null);
            }

            return(allocationParams);
        }