BotPlay!
Inspiration
Goals
- Play with PacMan-like Bots that live in a cartoon BotWorld
- Get Bots to do new things by building on skills they already have
- Learn about object-oriented programming. As we play with Bots, we will
- Create objects such as a BotWorld object and one or more Bot objects
- Give a Bot commands with methods such as move(), turnLeft(), and eatDot()
- Ask a Bot questions with methods such as getX() and getY() to find out its X and Y cooordinates
- Note that each Bot has its own state (data), such as its X and Y coordinates, orientation (north, east, south, west), and number of Dots eaten.
Step #1: Download the Supplied Code and Tell DrJava Where It Is
- Download botworldsimple.jar. Make sure you save it in location other than the the Desktop. Remember where you put it so you can tell DrJava where it is.
- Note: jar means "java archive". This jar is a file that contains all the supplied code (bytecode, actually) for Bot and BotWorld.
- How to tell Dr Java
about Jar File
- Click on Edit > Preferences
- Under Resource Locations (at left) , click Add button under Extra Classpath
- Browse and chose the botworldsimple.jar. Click Apply and then Ok.
- Finally Reset interaction pane
Note: Step 2 to 4 must be done in the Dr Java interactions pane
Step #2: Create a BotWorld and a Bot
- Note: the comments tell what is going on with every instruction
> BotWorld world = new BotWorld(); // Create a BotWorld. A cartoon world should appear on your screen.
> Bot bot = new Bot(world); // Create a Bot; it should appear in the cartoon world.
// Now the Bot and BotWorld know about each other.
Step #3: Give the Bot Commands and Ask it Questions
> bot.move(); // Command the Bot to move by "calling" its move method. Next we'll get it to eat the Dot in front of it.
> bot.move();
> bot.eatDot();
> bot.move(); // You should see that the Dot disappeared.
> bot.move() // If you leave out the semicolon, DrJava tells you the value "returned"/output by a method.
true // If it's facing a (green) wall, it won't move, and it will return false.
> bot.turnLeft() // A Bot can turn left, and it's now facing north.
'n'
> bot.turnRight() // But there is no turnRight() method.
Error: No 'turnRight' method in 'Bot' with arguments: ()
Further Exploration
- Can just make the bot turn right just knowing that it can only turn left?
- Create a second Bot, and then a third. Each one has its own state/data (X, Y position, orientation, etc.). Prove to yourself that you can move them independently.
- Can more than one Bot occupy the same position in the world?
- Write a loop that will make the Bot move forward 5 times.
- Write a loop that will make the Bot travel in a 3 x 3 square (it's your choice about whether to go clockwise or counterclockwise).
Step #4: Wishlist
- What do you wish a Bot could do?
- Finally checkout more BotWorld and Bot behaviors documented in javadocs (Javadocs document information about classes. Look at Method Summary for short description on behaviors that the object can perform)