Example: The Random Rectangle Agent

This is a very basic implementation of a Rectangle Agent and serves mainly as an example on how to use the abstract class methods. There are multiple ways users can exploit the information given to them by our methods and this is a simple example.

Download complete source file here.

Declaring the Variables

  class RectangleAgent : AbstractRectangleAgent
        //agent implementation specificiation
        private bool implementedAgent;
        private string agentName = "RandRect";

        //auxiliary variables for agent action
        private Moves currentAction;
        private List<Moves> possibleMoves;
        private long lastMoveTime;
        private Random rnd;

        //Sensors Information
        private CountInformation numbersInfo;
        private RectangleRepresentation rectangleInfo;
        private CircleRepresentation circleInfo;
        private ObstacleRepresentation[] obstaclesInfo;
        private ObstacleRepresentation[] rectanglePlatformsInfo;
        private ObstacleRepresentation[] circlePlatformsInfo;
        private CollectibleRepresentation[] collectiblesInfo;

        private int nCollectiblesLeft;

        //Area of the game screen
        protected Rectangle area;

Defining the Constructor

      public RectangleAgent() 
            //Change flag if agent is not to be used
            implementedAgent = true;

            lastMoveTime = DateTime.Now.Second;
            currentAction = Moves.NO_ACTION;
            rnd = new Random();

            //prepare the possible moves  
            possibleMoves = new List<Moves>();

Overriding the Setup() Method

        public override void Setup(CountInformation nI, RectangleRepresentation rI, CircleRepresentation cI, ObstacleRepresentation[] oI, ObstacleRepresentation[] rPI, ObstacleRepresentation[] cPI, CollectibleRepresentation[] colI, Rectangle area, double timeLimit)
            numbersInfo = nI;
            nCollectiblesLeft = nI.CollectiblesCount;
            rectangleInfo = rI;
            circleInfo = cI;
            obstaclesInfo = oI;
            rectanglePlatformsInfo = rPI;
            circlePlatformsInfo = cPI;
            collectiblesInfo = colI;
            this.area = area;

Overriding the SensorsUpdated()

      public override void SensorsUpdated(int nC, RectangleRepresentation rI, CircleRepresentation cI, CollectibleRepresentation[] colI)
            nCollectiblesLeft = nC;

            rectangleInfo = rI;
            circleInfo = cI;
            collectiblesInfo = colI;

Overriding the ImplementedAgent() Method

      public override bool ImplementedAgent()
            return implementedAgent;

Creating a RandomAction() Method

      private void RandomAction()
             Rectangle Actions
             MOVE_LEFT = 5
             MOVE_RIGHT = 6
             MORPH_UP = 7
             MORPH_DOWN = 8

            currentAction = possibleMoves[rnd.Next(possibleMoves.Count)];

Overriding the GetAction() Method

      //Manager gets this action from agent
      public override Moves GetAction()
            return currentAction;

Overriding the Update() Method

      public override void Update(TimeSpan elapsedGameTime)
            if (lastMoveTime == 60)
                lastMoveTime = 0;

            if ((lastMoveTime) <= (DateTime.Now.Second) && (lastMoveTime < 60))
                if (!(DateTime.Now.Second == 59))
                    lastMoveTime = lastMoveTime + 1;
                    lastMoveTime = 60;

Overriding the AgentName()

      public override string AgentName()
          return agentName;