public Position FindNearestFreeStation(Robot.Common.Robot movingRobot, Map map,
                                               IList <Robot.Common.Robot> robots)
        {
            EnergyStation nearest     = null;
            int           minDistance = int.MaxValue;

            foreach (var station in map.Stations)


            {
                if (IsStationFree(station, movingRobot, robots))
                {
                    int d = DistanceHelper.FindDistance(station.Position, movingRobot.Position);
                    if (d < minDistance)
                    {
                        minDistance = d;
                        nearest     = station;
                    }
                }
            }
            return(nearest == null ? null : nearest.Position);
        }
        public Position FindNearAvailableStation(Robot.Common.Robot movingRobot, IList <Robot.Common.Robot> robots)
        {
            Position stationPosition = null;

            for (int i = 0; i < li.listAim.Count; i++)
            {
                if (IsCellFreeFromMyRobot(li.listAim[i].station, movingRobot, robots) && li.listAim[i].inProgress == false)
                {
                    if (stationPosition != null)
                    {
                        if (DistanceHelper.FindDistance(movingRobot.Position, li.listAim[i].station) < DistanceHelper.FindDistance(movingRobot.Position, stationPosition))
                        {
                            stationPosition = li.listAim[i].station;
                        }
                    }
                    else
                    {
                        stationPosition = li.listAim[i].station;
                    }
                }
            }
            return(stationPosition);
        }
        public Position FindNearEnemyStation(Robot.Common.Robot movingRobot, IList <Robot.Common.Robot> robots)
        {
            Position stationPosition = null;

            for (int i = 0; i < listStationPosition.Count; i++)
            {
                if (IsCellFreeFromMyRobot(listStationPosition[i].Pos, movingRobot, robots))
                {
                    if (stationPosition != null)
                    {
                        if (DistanceHelper.FindDistance(movingRobot.Position, listStationPosition[i].Pos) < DistanceHelper.FindDistance(movingRobot.Position, stationPosition))
                        {
                            stationPosition = listStationPosition[i].Pos;
                        }
                    }
                    else
                    {
                        stationPosition = listStationPosition[i].Pos;
                    }
                }
            }
            return(stationPosition);
        }
        public RobotCommand DoStep(IList <Robot.Common.Robot> robots, int robotToMoveIndex, Map map)
        {
            if (counter3)
            {
                FindAllStation(map);
                li       = new ListAim(listStationPosition);
                counter3 = false;
            }
            if (counter % 2 == 0)
            {
                li.GetFreeAim();
            }


            Robot.Common.Robot movingRobot = robots[robotToMoveIndex];
            li.DeleteErrorAim(FindAllMyStation(movingRobot, robots));
            Position nearestFreeStation      = FindNearestFreeStation(movingRobot, map, robots);
            Position movingRobotPosition     = movingRobot.Position;
            Position nearestEnemyStation     = FindNearEnemyStation(movingRobot, robots);
            Position nearestAvailableStation = FindNearAvailableStation(movingRobot, robots);


            int x = 8;

            if (DistanceHelper.FindDistance(nearestAvailableStation, movingRobotPosition) <= 200 && nearestAvailableStation != null && robotCount < 200)
            {
                if (nearestFreeStation != null)
                {
                    if (Math.Sqrt(DistanceHelper.FindDistance(nearestFreeStation, movingRobotPosition)) <= 7 && (movingRobot.Energy >= (counter <= x ? 300 : 450)))
                    {
                        robotCount++;
                        li.SetAim(new Aim(nearestAvailableStation, true));
                        return(new CreateNewRobotCommand()
                        {
                            NewRobotEnergy = (counter <= x ? 100 : 250)
                        });
                    }

                    else if (Math.Sqrt(DistanceHelper.FindDistance(nearestFreeStation, movingRobotPosition)) < 12 && movingRobot.Energy > (counter <= x ? 350 : 500) && counter < 40)
                    {
                        robotCount++;
                        return(new CreateNewRobotCommand()
                        {
                            NewRobotEnergy = (counter <= x ? 150 : 300)
                        });
                    }
                }

                if (Math.Sqrt(DistanceHelper.FindDistance(nearestEnemyStation, movingRobotPosition)) <= 7 && movingRobot.Energy >= (counter <= x ? 400 : 500))
                {
                    robotCount++;
                    li.SetAim(new Aim(nearestAvailableStation, true));
                    return(new CreateNewRobotCommand()
                    {
                        NewRobotEnergy = (counter <= x ? 200 : 300)
                    });
                }
                else if (Math.Sqrt(DistanceHelper.FindDistance(nearestEnemyStation, movingRobotPosition)) < 12 && movingRobot.Energy > (counter <= x ? 450 : 600) && counter < 40)
                {
                    robotCount++;
                    return(new CreateNewRobotCommand()
                    {
                        NewRobotEnergy = (counter <= x ? 250 : 400)
                    });
                }
            }
            else if (DistanceHelper.FindDistance(nearestAvailableStation, movingRobotPosition) <= 625 && nearestAvailableStation != null && robotCount < 200 && counter < 40)
            {
                if (nearestFreeStation != null)
                {
                    if (Math.Sqrt(DistanceHelper.FindDistance(nearestFreeStation, movingRobotPosition)) < 25 && movingRobot.Energy > 700 && counter <= 35)
                    {
                        robotCount++;
                        return(new CreateNewRobotCommand()
                        {
                            NewRobotEnergy = 400
                        });
                    }
                }
                else if (Math.Sqrt(DistanceHelper.FindDistance(nearestEnemyStation, movingRobotPosition)) < 20 && movingRobot.Energy > 700 && counter <= 35)
                {
                    robotCount++;
                    return(new CreateNewRobotCommand()
                    {
                        NewRobotEnergy = 450
                    });
                }
            }


            for (int i = 0; i < listStationPosition.Count; i++)
            {
                if (movingRobotPosition == listStationPosition[i].Pos)
                {
                    if (!li.IsMyStation(listStationPosition[i].Pos))
                    {
                        li.listMyStation.Add(new Aim(listStationPosition[i].Pos, true));
                        return(new CollectEnergyCommand());
                    }
                    else
                    {
                        return(new CollectEnergyCommand());
                    }
                }
            }
            if (nearestFreeStation != null)
            {
                if (Math.Sqrt(DistanceHelper.FindDistance(nearestFreeStation, movingRobotPosition)) <= 12 && movingRobot.Energy - (DistanceHelper.FindDistance(nearestFreeStation, movingRobotPosition)) > 0)
                {
                    li.SetAim(new Aim(nearestFreeStation, true));
                    return(new MoveCommand()
                    {
                        NewPosition = nearestFreeStation
                    });
                }
            }

            if (Math.Sqrt(DistanceHelper.FindDistance(nearestAvailableStation, movingRobotPosition)) <= 12 && movingRobot.Energy - (DistanceHelper.FindDistance(nearestAvailableStation, movingRobotPosition) + 50) > 0)
            {
                li.SetAim(new Aim(nearestAvailableStation, true));
                return(new MoveCommand()
                {
                    NewPosition = nearestAvailableStation
                });
            }
            if (Math.Sqrt(DistanceHelper.FindDistance(nearestAvailableStation, movingRobotPosition)) < 20 && movingRobot.Energy >= 300)
            {
                return(new MoveCommand()
                {
                    NewPosition = GetNextPosition(movingRobotPosition, nearestAvailableStation, (movingRobot.Energy / DistanceHelper.FindDistance(nearestAvailableStation, movingRobotPosition)) > 1 ? movingRobot.Energy / (int)DistanceHelper.FindDistance(nearestAvailableStation, movingRobotPosition) + 4 : 1)
                });
            }
            if (Math.Sqrt(DistanceHelper.FindDistance(nearestAvailableStation, movingRobotPosition)) < 30 && movingRobot.Energy >= 200)
            {
                return(new MoveCommand()
                {
                    NewPosition = GetNextPosition(movingRobotPosition, nearestAvailableStation, (movingRobot.Energy / DistanceHelper.FindDistance(nearestAvailableStation, movingRobotPosition)) > 1 ? movingRobot.Energy / (int)DistanceHelper.FindDistance(nearestAvailableStation, movingRobotPosition) + 1 : 1)
                });
            }
            else
            {
                return new MoveCommand()
                       {
                           NewPosition = GetNextPosition(movingRobotPosition, nearestAvailableStation, 1)
                       }
            };
        }