The goals of this assignment are to:
As with the first assignment...
A robot and a treasure are located somewhere in a room (which is empty except for the robot and the treasure). The floor of the room is covered with square tiles, so you can think of the room as having a giant piece of graph paper covering its floor. The robot and the treasure each occupy a different tile in this grid, and you know the location of each one. The locations are specified using (x,y) coordinates.
For this assignment...
Assume that the x- and y-coordinates of the room range from -5 to 5 (it's a small room). Your job is to write a program that will first randomly choose locations for the robot and the treasure in the room. The user may (optionally) enter a seed for the random number generator on the command line.
Your program must then determine and provide directions to the robot for how to travel to the treasure.
You can assume that the robot begins by facing in the positive Y direction.
Your robot can only move ONE tile at a time, so you need to loop, continually updating the robot's position and giving it new instructions, until it reaches the treasure.
So, your program has to do the following:
You can assume that the locations are whole numbers. Your calculated outputs should be whole numbers in terms of the number of tiles to travel in each direction.
Hint: keep track of which direction the robot is facing, so that you don't turn too many (or not enough) times! (The robot can only face North, South, East or West; it starts by facing North.)
In order to do this assignment, you will need to use the
classes discussed in class this week.
I don't think I pointed out in class that you need to include
a kind of statement called
import at the top of
a class file that uses classes from any class other than
java.lang (which is imported by default).
See the example on page 20 of the lecture slides from Wednesday's class. The statement
is located above the
public class ex2h statement,
which is what you'll have to do for this assignment.
Below is a sample run for two cases. The unix command line is highlighted in bold font.
unix$ java hw2sklar random number seed = 1044058435726 robot is at (-5,5) treasure is at (-3,0) turn around, go forward 1 tile go forward 1 tile go forward 1 tile go forward 1 tile go forward 1 tile turn left, go forward 1 tile go forward 1 tile gotcha!! unix$ java hw2sklar 3 random number seed = 3 robot is at (0,0) treasure is at (1,-2) turn around, go forward 1 tile go forward 1 tile turn left, go forward 1 tile gotcha!! unix$
Your source code (i.e., your
.java file) must be neat and
You must have a header comment and you should comment the end of each
block (i.e., each
This assignment is worth 15 points (out of 100 for the semester). Distribution of points is: