Exemplo n.º 1
0
        public void DoStep()
        {
            var args = new UpdateViewAfterRobotStepEventArgs();

            if (_roundNumber > MaxNumbersOfRound)
            {
                return;
            }

            if (_currentRobotIndex >= Robots.Count)
            {
                PrepareNextRound();
            }
            args.OwnerName = Robots[_currentRobotIndex].OwnerName;
            var ownerName = args.OwnerName;

            args.RobotPosition = Robots[_currentRobotIndex].Position;

            if (Robots[_currentRobotIndex].Energy > 0)
            {
                try
                {
                    //make a copy to pass to the algorithm
                    var command = Algorithms[args.OwnerName].DoStep(CopyRopots(), _currentRobotIndex, Map.Copy());
                    //To make sure that student are not cheating by adding new class

                    var type            = command.GetType();
                    var allowedCommands = new List <Type>()
                    {
                        typeof(Robot.Common.CreateNewRobotCommand), typeof(Robot.Common.MoveCommand), typeof(Robot.Common.CollectEnergyCommand)
                    };

                    if (allowedCommands.Contains(type))
                    {
                        //command.RobotStepCompleted += _callback;
                        args               = command.ChangeModel(Robots, _currentRobotIndex, Map);
                        args.OwnerName     = ownerName;
                        args.RobotPosition = Robots[_currentRobotIndex].Position;
                        //command.Apply(Robots, _currentRobotIndex, Map);
                        Logger.LogMessage(ownerName, command.Description, LogValue.Normal);
                    }
                    else
                    {
                        Logger.LogMessage(ownerName,
                                          $"{Robots[_currentRobotIndex].OwnerName} is nasty cheater, let's kill his robot for that ))", LogValue.High);
                        Robots[_currentRobotIndex].Energy = 0;
                    }
                }
                catch (Exception e)
                {
                    Logger.LogMessage(ownerName, $"Error: {e.Message} ", LogValue.Error);
                    //simply do nothing
                    _callback(null, args);
                }
            }

            _currentRobotIndex++;
            _callback(null, args);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Apply changes on the screen
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        public void ModelChanged(object sender, UpdateViewAfterRobotStepEventArgs args)
        {
            _positionChanges = new Dictionary <Position, RobotControl>();

            if (args.MovedFrom == null || args.MovedFrom.Count == 0)
            {
                if (args.NewRobotPosition != null)
                {
                    CreateRobot(args.OwnerName, args.NewRobotPosition);
                    var r = RobotPlace[args.NewRobotPosition];
                    currentAnimation            = r.AnimateOpacity();
                    currentAnimation.Completed += new EventHandler(ViewUpdatedChanged);
                    currentAnimation.Begin();
                }
                else if (args.RobotPosition != null)
                {
                    var r = RobotPlace[args.RobotPosition];
                    currentAnimation            = r.AnimateOpacity();
                    currentAnimation.Completed += new EventHandler(ViewUpdatedChanged);
                    currentAnimation.Begin();
                }
            }

            else
            {
                for (int i = 0; i < args.MovedFrom.Count; i++)
                {
                    var from = args.MovedFrom[i];
                    var to   = args.MovedTo[i];
                    var r    = RobotPlace[from];

                    SetAnimation(r, from, to, i == args.MovedFrom.Count - 1);

                    RobotPlace.Remove(from);
                    _positionChanges.Add(to, r);
                }
            }
        }