Background Information

Vacuum World is a simulation of a vacuum cleaner robot/agent with dirt sensors.  It has to vacuum up the dirt on the ground in the least amount of moves possible.

Task to Complete

Develop a program that simulates Vacuum World.  This program must:

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

September 22th, 2016 – Vacuum, Environment Classes

  • Create the class Vacuum to simulate the vacuum agent, that:
    • has the private position and state integer instance variables to represent the position of the agent in the “world” (0 is left, 1 is right), and the state of the world (involving both the position of the agent and the dirt)
    • has the private dirtLeftdirtRight, and done boolean instance variables to record the surroundings of the agent and whether or not its task is complete
    • has the private count integer instance variable to count the number of moves the agent made to complete the task
    • has a private move() method that changes position from 0 to 1 or from 1 to 0, and increments count
    • has a private suck() method that will remove/add dirt at the location of the agent using Environment‘s changeDirt() method, and increments count
    • has a private sense() method that fills in dirtLeft and dirtRight after obtaining information from the Environment class
    • has a private decide() method that uses dirtLeftdirtRight, and position to determine the state variable of the agent
    • has a private act() method that uses state to determine which method, suck() or move(), to call, or to do nothing and change done to true
    • has a public update() method that calls sense()decide(), and act()
    • has accessor/mutator methods to access/change position
    • has a restart() method that changes done back to false and count back to 0
    • has a constructor that sets done to false and position to 0
  • Create the class Environment that:
    • is a “singleton” (there should only be one “environment” in the simulation)
    • has one private Vacuum class
    • has a getVacuum() method which returns the Vacuum class if it exists, or constructs one if it does not exist
    • has a boolean array dirt to represent whether or not there is dirt
    • has a changeDirt() method the inverts the dirt boolean value, and a isDirt() accessor method that returns the dirt boolean value at the given index
    • has a private constructor that initializes dirt

September 26th, 2016 – Display, Runner Classes

  • Create the class Display, used to present the graphics, that:
    • is a “singleton” class
    • extends JFrame and has a JPanel panel
    • implements MouseListenerMouseMotionListener, and KeyListener for interaction with the user
    • has a private boolean instance variable running, which is used to control the pausing of the simulation
    • has the private integer instance variables mouseX and mouseY to store the location of the mouse cursor
    • has a private constructor that sets up the JFrame and JPanel, sets running to true, and loads the .png images for the dirt and the vacuum agent
    • implements the mouseMoved() and mouseDragged() methods by having them update mouseX and mouseY
    • has a paint() method which:
      • creates a new BufferedImage with the same dimensions as panel
      • draws the vacuum image on the BufferedImage at a location corresponding to the agent’s position
      • draws the dirt image(s) on the BuferedImage depending on whether or not there is dirt at each of the two possible locations
      • if mouseX and mouseY are within the areas where the vacuum/dirt are dispalyed, draws a dark gray border around the area to let the user know that they are about to add/remove a vacuum/dirt there
      • draws this BufferedImage onto panel
    • implements the mouseClicked() method such that:
      • if the mouseX and mouseY are within the “dirt areas”, to update the dirt value at that position with Environment‘s changeDirt() method
      • when dirt is added, if the vacuum agent is done, to use the restart() method to resume its work
      • if the mouseX and mouseY are within an unoccupied “vacuum area”, to change the position of the vacuum agent to that location with the setPosition() method, and to restart the agent as well
    • implements the keyPressed() method such that when the space bar is pressed, running will be inverted
  • Create the class Runner with a main(String[] args) method that:
    • sets an integer variable count to 0
    • runs an infinite loop, in which:
      • the paint() method is called to update the display
      • count is incremented every iteration
      • if count is 100, count will be reset to 0, and then, if running is true and the vacuum agent isn’t “done”, then the update() method of the vacuum agent will be called to let it make its next move
  • At this point, the program is pretty much complete.