import java.io.*; import java.awt.*; import java.util.Random; /** Program 6.3, from the textbook "Ada 95", rewritten for two Spiders named "worm" and "apple". Worm starts out at an arbitrary point in the Room, walks random distances similarly to "Drunken_Spider" (Program 7.7), until it reaches the Spider named "apple", in the center of the Room.
------------------------------------------------------------------
--| Displays distances between a worm and an apple.  The worm keeps reducing
--| the distance by its body length until it is close enough to bite the apple.
--| Author: Michael B. Feldman, The George Washington University
--| Last Modified: July 1995
------------------------------------------------------------------
  
Before the program runs, it asks the user to input the starting X and Y (pixel) coordinates of the Spider named "apple". The input prompt and response is displayed in a "System window" (which under Windows 95 looks like an MS-DOS window). Before responding to the prompt the user should move the System window to not overlap the drawing area (Room). If this is not done, hidden areas of Room will appear blank when Room is exposed later, because this program does not repaint the drawing area in response to window events.

Click on the "close button" (marked X) in the top right corner of the spider's window, to terminate the application. @see Spider @see Room @see AppFrame @author Java translation by Istvan Mohos, 1996 */ public class Worm_and_Apple { /** Constant upper limit of how may pixels "worm" may crawl in one operation. */ static final int MAX_DISTANCE = 200; public static void main(String[] args) throws IOException { StreamTokenizer myTokens = // set up input data stream new StreamTokenizer(System.in); Room barn = new Room(); // a Room called "barn" new AppFrame( // window to hold "barn" "Worm_and_Apple", barn, 400, 400); Spider apple = new Spider( // a Spider named "apple" 0, 0, Room.NORTH, Color.black, Color.blue); Point middle = barn.jumpToMiddle(apple); // place apple in center // Set up an invisible rectangle around the apple, 20 by 20 pixels. // The first two args refer to the x, y coordinates of the top left // corner of the rectangle, the last two are the width and height Rectangle target = new Rectangle(middle.x - 10, middle.y - 10, 20, 20); Random generator = new Random(); // current date is seed Point hereIam; // current X, Y coordinates of worm int nowX; // the X coordinate int nowY; // the Y coordinate int nextDistance; // how many pixels to crawl int oddEven; // to turn left or right System.out.print ("Input the initial X Y position of worm > "); System.out.flush(); myTokens.nextToken(); nowX = (int)myTokens.nval; myTokens.nextToken(); nowY = (int)myTokens.nval; // Start worm out at the designated point. Spider worm = new Spider(nowX, nowY, Room.NORTH, Color.green, Color.red); // Keep drawing until worm reaches the target rectangle in the center. while (!target.inside(nowX, nowY)) { // Get a random number of pixels to crawl, but discard any that are // smaller than 5 pixels. while ((nextDistance = generator.nextInt() % MAX_DISTANCE) < 5); hereIam = barn.crawl(worm, nextDistance); nowX = hereIam.x; nowY = hereIam.y; oddEven = generator.nextInt() % 2; if (oddEven == 0) worm.turnRight(); else worm.turnLeft(); } // end of the outer while-loop barn.showMe(worm); barn.showMe(apple); } // end definition of main method } // end definition of class Worm_and_Apple;