Example: The Random Circle Agent

This is a very basic implementation of a Circle 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

Here we will declare some basic variables to aid us when running our Circle Agent.

  class CircleAgent : AbstractCircleAgent 
  {
        //agent implementation specificiation
        private bool implementedAgent;
        private string agentName = "RandCircle";

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

        //Sensors Information and level state
        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
        private Rectangle area;
      ...

Defining the Constructor

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

            //setup for action updates
            lastMoveTime = DateTime.Now.Second;
            currentAction = Moves.NO_ACTION;
            rnd = new Random();

            //prepare the possible moves  
            possibleMoves = new List<Moves>();
            possibleMoves.Add(Moves.ROLL_LEFT);
            possibleMoves.Add(Moves.ROLL_RIGHT);
            possibleMoves.Add(Moves.JUMP);  
      }

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()
        {
            /*
             Circle Actions
             ROLL_LEFT = 1      
             ROLL_RIGHT = 2
             JUMP = 3
             GROW = 4
            */

            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)
        {
            //Every second one new action is choosen
            if (lastMoveTime == 60)
                lastMoveTime = 0;

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

Overriding the AgentName()

      public override string AgentName()
      {
          return agentName;
      }