BotPlay

Overview

We will play with Bots in a BotWorld in a way similar to playing the Pac-Man game. (If you're not familiar with Pac-Man, you can play online via the link in the provided Resources.) All the code is written in the Java programming language. Instead of using the arrow keys to move the Bots, you'll type in Java commands. Don't worry, we'll tell you what to do!

Goals

Prerequisites

Set Up

First you need to get the code by downloading a JAR file. JAR stand for "Java archive". A JAR file is like a code library. Instead of containing books, it contains code for each of the objects you'll create (BotWorld code, Bot code, Wall code, and more). After you download the file, you'll tell DrJava where the JAR is.
  1. In the Resources section above, find the link for the JAR. Click to download it. Make sure to save it to disk, don't open it. Unless you're told otherwise by your instructor, you may put it anywhere that is convenient for you such as on your desktop or in one of your directories or folders. Remember where you put it - you need to tell DrJava where it is.
  2. In the Resources section above, follow the directions about adding and removing JARs to tell DrJava where you put the jar.

Assignment

Create a BotWorld and a Bot

Type this statement in to the DrJava Interactions Pane (at the bottom).
BotWorld world = new BotWorld();                                       
This creates a new BotWorld object called 'world' which should appear on your screen. Now type:
Bot bono = new Bot(world);

This creates a new Bot object called 'bono' in the world.

Because we'd like to explain each line of code that you type in, and let you see the difference between text that you should type and text that is given you as feedback, going forward we will include in our "sample interactions":

  • The > which signifies the DrJava prompt.
  • Comments, which go from // to the end of the line. Comments are for humans to read and are ignored by Java.
The same interactions above, with the prompt and comments, are shown below. (There's no need to enter these commands.)
> BotWorld world = new BotWorld();  // Create a new BotWorld object called 'world'.
> Bot bono = new Bot(world);        // Create a Bot object called 'bono' in the world.

Remember, don't type the prompt or comments into the interactions pane.

Give the Bot Commands and Ask it Questions

Interactions

> bono.move();   // Call the Bot's move() method.
// Next we'll get it to eat the Dot in front of it. > bono.move(); > bono.eatDot(); > bono.move(); // You should see that the Dot disappeared. > boolean result; // Create a boolean (logical variable that is either true or false) > result = bono.move() // The move() method returns true if successful, false if not. > result true > bono.turnLeft(); // A Bot can turn left, and it's now facing north. > char direction; > direction = bono.getDirection(); > direction 'n' > bono.jump() // Bots can't jump, so you get an error Static Error: No method in Bot with name 'jump' accepts arguments ()

// Note below that for methods that return a value (as seen in the Javadoc), you can
// easily see the value it returns by leaving out the semicolon in the interactions.
// That's because you're providing DrJava with an expression (that has a value) rather
// than a statment (that ends with a semicolon).
> bono.turnLeft(); > bono.getDirection() 'w'

More Exploration

  • Create two more Bots. Each Bot has its own state (data) which includes its (x,y) position, its orientation, and the number of dots it has eaten. Convince yourself that you can move the Bots independently even though they are built from the same template.
  • Can more than one Bot occupy the same position in the world?
  • Can you make a Bot turn around and face the opposite direction?

Explore the Bot Javadocs

  • Look at the Javadocs for Bot. Scroll down to the Method Summary section. A method is a way to manipulate an object by giving a command or asking a question. How can you…
    1. ask a Bot for its x coordinate?
    2. ask a Bot for its orientation?
    3. ask a Bot how many dots it has eaten?
    4. tell a Bot to put a dot back into the world?
    Once you've found these methods, try using them.
  • When you asked the Bot for its x coordinate you saw that the leftmost column of cells has an x coordinate of 0. In Java programming, we will usually start at 0 when referring to an index (a location). Now access the Bot's y coordinate at multiple locations. Where is (0, 0) in BotWorld? Which way does the positive X axis go? Positive Y? Negative X? Negative Y?

Explore the BotWorld Javadocs

Clear Cells and Has Dot and Is Wall

Before executing these statements, reset the DrJava interactions pane so that it discards the variables and objects from the previous section.

> BotWorld world = new BotWorld();  // Make sure you can find the following methods
// in the Javadocs for BotWorld
// before you enter them into DrJava. > world.clearCells(); // a command method to get rid of the world's contents > world.hasDot(0, 1); // a query method
// If we forget to omit the semicolon, we
// don't see the return value. > world.hasDot(0, 1) // In this case, the method returns a boolean
// (true / false) value. This is called its return
// type. Find this information in the Javadocs! false > world.loadFile("worlds/default.txt"); // This loads an empty world
// with a surrounding wall. > world.isWall(1, 1) false > world.isWall(0, -1) true

Further Exploration

  • Figure out how to add a dot at (0, 1). Verify that it worked both visually (by calling world.updateView()) and programatically (by calling world's hasDot method).
  • Figure out how to create and remove walls. Again, verify that it worked both visually and programatically.
  • Make a 5x5 box in the middle of the world using dots or walls.
  • Can you suggest a challenge problem? If you accomplish something challenging, tell us about it!

Wishlist

What do you wish a Bot could do? You will get a chance to make a better bot once we learn some Java fundamentals.


All contents © UPenn CIS. Mail botworld@seas.upenn.edu if modifying material or reproducing it for use outside your classroom.