Background Information

Gridworld is a simulation of simple agents.  Each agent is placed in a walled environment, and can move north, south, east, or west depending on what it percepts in its immediate surroundings.  The agent’s goal is to follow the walls in its environment.

Task to Complete

Develop a program that simulates Gridworld.  This program must:

  • be presented using graphics
  • feature the perception, decision, and action processes of the agent
  • be interactive with the user

September 8th, 2016 – Main Class

  • Create the main class Environment, that:
    • extends JFrame for the simulation window
    • has a JPanel for the display of the agents and the grid
    • has a boolean matrix to represent the grid (true means that there is a wall)
    • has a isWall(int x, int y) method that returns true or false based on whether or not there is a wall at location (x, y)
    • has a display() method that uses a BufferedImage to draw the grid onto the JPanel
  • Create the constructor of the Environment class that:
    • sets up the JFrame and JPanel
    • instantiates the boolean matrix with preset maps
    • loops through the display() method infinitely

September 12th, 2016 – Agent Class

  • Create Agent class, with the following attributes and methods:
    • integers x and y to store the location of the agent on the grid
    • boolean array around to store whether or not there is a wall in the immediate surroundings of the agent
    • boolean array feature for the feature vector of the agent
    • the moveNorth()moveSouth()moveEast()moveWest() methods that change the instance variables x and y based on the movement of the agent
    • the private percept() method that fills in the around array by calling the Environment.isWall(int x, int y) method for each of the surrounding grid tiles
    • the private decide() method that fills in feature based on around
    • the private act() method that uses if-else statements on feature to determine which of the “move” methods to call
    • the public update() method that calls percept()decide(), and act() in that order
    • the constructor of the Agent class that instantiates x and y based on the parameters it takes in
    • the getX() and getY() accessor methods that return x and y respectively
  • Incorporating the Agent class into the Environment class by:
    • creating an ArrayList agents of Agent objects in Environment
    • have the display() method loop through agents and:
      • update each agent by calling its update() method
      • display the location of each Agent on the grid
    • randomly creating some agents in the constructor

September 14th, 2016 – Additional Features

  • Having Environment implement MouseListenerMouseMotionListener, and KeyListener to allow for user interaction with the simulation
  • Adding the mouseX and mouseY instance variables, updated by the mouseMoved and mouseDragged method, that store the location of the cursor
  • Adding the Treasure class that:
    • has the instance variables x and y that store its location on the grid
    • has a constructor that instantiates x and y
    • has accessor getX() and getY() methods that return x and y specifically
  • Incoporating the Treasure class into the Environment class by:
    • creating an ArrayList treasures of Treasure objects in Environment
    • have the display() method loop through treasures and:
      • remove a Treasure object if an Agent has the same location
      • display the location of each Treasure on the grid
  • Adding the boolean running instance variable, used in the infinite loop in the Environment constructor, to determine the pausing of the simulation
  • Using a switch-case in the KeyPressed method to determine what to do based on the keys pressed, such as:
    • pressing the shift key to toggle between adding agents, blocks, and treasures
    • pressing the space bar to pause/resume the simulation by inverting running
    • pressing the up or down keys to increase/decrease the speed of the simulation, by decreasing/increasing the “sleep” times between each calling of display()
  • Implementing the mouseReleased method by adding agents, blocks, or treasures at the mouse’s location depending on the mode the user is in
  • At this point, the program is pretty much complete.