Building the labyrinth: variables and objects
Add walls and creatures to your maze.
Our labyrinth is not very interesting yet, Daedalus. Let’s add some walls, and perhaps, a minotaur? Here’s the code for setting up the maze, again, but with one additional line of code to add a single wall to the maze.
import com.educative.robot.*;// A program that creates a robot in a mazeclass RobotDemo {public static void main(String[] args) {// set up variables to store the robot and the mazeRobot robbie;Maze maze;// create the robot and maze, and add// the robot to the mazerobbie = new Robot(0.0, 0.0, "red");maze = new Maze(8, 8, 50);maze.add(robbie);maze.add(new Wall(3.0, 2.0));// set the robot velocity:robbie.setVelocity(1.0, 0.0);// move things in the maze for 1 second:maze.turn();}}
Add several more walls to build your own maze. The add
method of the maze
object adds a new object to the maze. In the example, the code new Wall(3.0, 2.0)
creates a Wall object, which the add
method then adds to the maze.
Adding creatures to your maze
Let’s take another look at the lines of code that set up the maze and the robot:
// set up variables to store the robot and the mazeRobot robbie;Maze maze;// create the robot and maze, and add// the robot to the mazerobbie = new Robot(0.0, 0.0, "red");maze = new Maze(8, 8, 50);maze.add(robbie);
Java allows you to create objects and run methods on them. In this program, Maze and Robot are both types, or classes, of object. So, we can create Maze
and Robot
objects and run methods (commands) on them.
Once we have created an object, what do we do with it? We might like to store a reference to that object so that we can use it later.
Variables store information in Java. To store a reference to an object, we need to create, or declare, a variable. The code Robot robbie;
on line 2 creates the variable named robbie
, and says that robbie
is a variable that can hold a reference to a Robot
object.
But although line 2 creates a variable to store a reference to an object, it doesn’t actually create an object. The code new Robot(0.0, 0.0, "red")
creates a new Robot object, with x and y coordinates 0.0
, 0.0
, with the color "red"
.
The line robbie = new Robot(0.0, 0.0, "red");
tells Java to store a reference to the new robot object in the variable named robbie
.
Whew. The main thing to remember is that if we want to use an object, we probably want to do two things first:
-
Create a variable of the correct type to store a refence to that object.
-
Create the object, and store the reference into the variable.
Go back to your code at the top of the page, and add another robot at location 2.0, 1.0, or anywhere that doesn’t already have a robot or a maze. Some hints are below if you need them.
-
Remember to create a variable of type robot first, near the line
Robot robbbie;
. Choose a name for the variable that is different fromrobbie
, like “wally”, or “tiktok”. -
Create a new Robot object somewhere after that in the code, at the desired coordinates, with the desired color, and store the reference into the variable.
-
Add the robot to the maze, using the
add
method ofmaze
, with the new robot name. Your line of code should be immediately after the line of code that addsrobbie
to the maze.