CISC181 S2017 TurtleTester
Contents
Basic instructions
After adding TurtleTester.java to your project, put the following field into your Lab3 class:
static TurtleTester myTurtleTester;
Each "test" is a static method you define in your Lab3 class. Here is an example:
// go up slowly, turn right, and jump to corner; default constructor static void test1() { myTurtleTester.startTest(); Turtle myTurtle = new Turtle(); myTurtleTester.print(myTurtle); for (int i = 0; i < 5; i++) { myTurtle.move(1); myTurtleTester.print(myTurtle); } myTurtle.turn(3); myTurtle.move(4); myTurtleTester.print(myTurtle); myTurtleTester.endTest(); }
Turtle is the class that you are writing for this assignment. The body of EVERY "turtle test" method starts with a call to the TurtleTester object's startTest() method which initializes a Swing window for drawing.
After this, you can do what the assignment instructions say: construct a Turtle object and make a sequence of calls to move(), turn(), flip(), and so on. However, every time you call the TurtleTester's print() method on your Turtle object, the turtle's position, direction, and other information are printed in the console and saved. When the TurtleTester's endTest() method is called, all of that saved information is drawn in the window, and then the program waits for a mouse click to continue.
Finally, in your main(), initialize the testing object and call the test method as follows:
myTurtleTester = new TurtleTester(10); // 10 x 10 is the expected grid size test1();
When you run the program, here is what you should see in the window if your Turtle class has been written correctly:
The turtle is drawn at each grid square saved by a print() call, numbered (0 is its initial location, 1 is after the first move(), and so on), and colored and oriented according to its current direction:
- Green = NORTH
- Pink = SOUTH
- Orange = EAST
- Cyan = WEST
Note that if the turtle "revisits" a previously-visited square, the earlier turtle will be covered by the later one.
Also, the following should be printed in the console:
Beginning test... 0: (5, 5, NORTH) squares = 0, dist = 0.00 1: (4, 5, NORTH) squares = 1, dist = 1.00 2: (3, 5, NORTH) squares = 2, dist = 2.00 3: (2, 5, NORTH) squares = 3, dist = 3.00 4: (1, 5, NORTH) squares = 4, dist = 4.00 5: (0, 5, NORTH) squares = 5, dist = 5.00 6: (0, 9, EAST) squares = 9, dist = 6.40 ...finished. Click window to continue
Three more test methods and the expected graphical and printed output for each are below.
test2
// initial forward, then lots of going in reverse, with an attempted escape from grid; 3-arg constructor static void test2() { myTurtleTester.startTest(); Turtle myTurtle = new Turtle(7, 7, Direction.EAST); myTurtleTester.print(myTurtle); myTurtle.move(2); myTurtleTester.print(myTurtle); myTurtle.flip(true, true); myTurtle.move(7); myTurtleTester.print(myTurtle); myTurtle.turn(1); myTurtle.move(1); myTurtleTester.print(myTurtle); myTurtle.move(2); myTurtleTester.print(myTurtle); myTurtleTester.endTest(); }
Beginning test... 0: (7, 7, EAST) squares = 0, dist = 0.00 1: (7, 9, EAST) squares = 2, dist = 2.00 2: (7, 2, EAST) squares = 9, dist = 5.00 3: (8, 2, NORTH) squares = 10, dist = 5.10 Trying to leave grid! 4: (9, 2, NORTH) squares = 11, dist = 5.39 ...finished. Click window to continue
test3
// knight's moves with a reverse in the middle; 5-arg constructor static void test3() { myTurtleTester.startTest(); Turtle myTurtle = new Turtle(2, 3, Direction.NORTH, 2, -1); myTurtleTester.print(myTurtle); myTurtle.move(1); myTurtleTester.print(myTurtle); myTurtle.turn(1); myTurtle.move(1); myTurtleTester.print(myTurtle); myTurtle.turn(1); for (int i = 0; i < 3; i++) { myTurtle.move(1); myTurtleTester.print(myTurtle); } myTurtle.flip(true, false); myTurtle.move(3); myTurtleTester.print(myTurtle); myTurtleTester.endTest(); }
Beginning test... 0: (2, 3, NORTH) squares = 0, dist = 0.00 1: (0, 2, NORTH) squares = 3, dist = 2.24 2: (1, 0, WEST) squares = 6, dist = 3.16 3: (3, 1, SOUTH) squares = 9, dist = 2.24 4: (5, 2, SOUTH) squares = 12, dist = 3.16 5: (7, 3, SOUTH) squares = 15, dist = 5.00 6: (1, 6, SOUTH) squares = 24, dist = 3.16 ...finished. Click window to continue
test4
// grid-filling outward spiral; default constructor static void test4() { myTurtleTester.startTest(); int i, j, dist; Turtle myTurtle = new Turtle(); myTurtleTester.print(myTurtle); // 9 iterations is magic number for starting (row, col) and size of grid for (i = 0, dist = 1; i < 9; i++, dist++) { // do two legs of spiral before extending length of leg for (j = 0; j < dist; j++) { myTurtle.move(1); myTurtleTester.print(myTurtle); } myTurtle.turn(1); for (j = 0; j < dist; j++) { myTurtle.move(1); myTurtleTester.print(myTurtle); } myTurtle.turn(1); } // complete left-most column for (i = 0; i < 9; i++) { myTurtle.move(1); myTurtleTester.print(myTurtle); } myTurtleTester.endTest(); }
Here is the console output for the first 10 moves...
Beginning test... 0: (5, 5, NORTH) squares = 0, dist = 0.00 1: (4, 5, NORTH) squares = 1, dist = 1.00 2: (4, 4, WEST) squares = 2, dist = 1.41 3: (5, 4, SOUTH) squares = 3, dist = 1.00 4: (6, 4, SOUTH) squares = 4, dist = 1.41 5: (6, 5, EAST) squares = 5, dist = 1.00 6: (6, 6, EAST) squares = 6, dist = 1.41 7: (5, 6, NORTH) squares = 7, dist = 1.00 8: (4, 6, NORTH) squares = 8, dist = 1.41 9: (3, 6, NORTH) squares = 9, dist = 2.24
...and the last 10:
90: (0, 0, WEST) squares = 90, dist = 7.07 91: (1, 0, SOUTH) squares = 91, dist = 6.40 92: (2, 0, SOUTH) squares = 92, dist = 5.83 93: (3, 0, SOUTH) squares = 93, dist = 5.39 94: (4, 0, SOUTH) squares = 94, dist = 5.10 95: (5, 0, SOUTH) squares = 95, dist = 5.00 96: (6, 0, SOUTH) squares = 96, dist = 5.10 97: (7, 0, SOUTH) squares = 97, dist = 5.39 98: (8, 0, SOUTH) squares = 98, dist = 5.83 99: (9, 0, SOUTH) squares = 99, dist = 6.40 ...finished. Click window to continue