project
Here is some challenging projects who left you resolved. [Some some may be an example of this book, so it may take it from here]
Mouse and maze
First, create a blackboard (CITE REFERENCE?) Object is used to record information. Draw maze on this special blackboard, and use it to display structural information of the maze probing by mice.
Now create a real maze, this object only exposes itself very little information - for a given coordinates, it will tell you that the wall is still a wall or open, but there will be more information . For new hands, you can read the maze (data) from a text file, but you can consider finding an algorithm for constructing a labyrinth on the Internet. In any case, the last result should be such an object, this object can find the wall and empty space around it according to a given coordinate. Another point, it must also provide the entrance to this maze for inquiry.
Finally, create a mouse RAT class in the maze probe. Each mouse can be deal with the blackboard and labyrinth, which reports its current information to the blackboard, and requests new information to the maze according to the location of the location. In any case, each time the mouse encounters the labyrinth branch point to make a decision, it creates a new thread to detect every branch. Each mouse is driven by its own thread. After it approaches the dead alley, it will first report the final result of the probe to the blackboard, and then it will end your thread.
The ultimate goal is to draw a complete map of the labyrinth, but you must decide whether the last end condition is that it is naturally found or is determined by the blackboard.
Below is an example of an implementation written by Jeremy Meyer:
//: Projects: Maze.java
Package Projects;
Import java.util. *;
Import java.io. *;
Import java.awt. *;
Public class maze extends Canvas {
Private vector lines; // a line is a char Array
PRIVATE INT WIDTH = -1;
PRIVATE INT Height = -1;
Public static void main (string [] args)
THROWS IOEXCEPTION {
IF (args.length <1) {
System.out.println ("Enter FileName");
System.exit (0);
}
Maze m = new maze ();
M.LOAD (args [0]);
Frame f = new frame ();
F.setsize (m.Width * 20, M.Height * 20);
F.Add (m);
Rat r = new rat (m, 0, 0);
F.setVisible (TRUE);
}
Public maze () {
LINES = new vector ();
SetBackground (color.lightgray);
}
Synchronized public boolean
ISemptyxy (int x, int y) {
IF (x <0) x = width;
IF (Y <0) Y = Height;
// Use mod Arithmetic to Bring Rat in line:
Byte [] by =
(Byte []) (Lines.elementat));
Return by [x% width] == ';
}
Synchronized public void
Setxy (int X, int y, byte newbyte) {
IF (x <0) x = width; if (y <0) y = height;
Byte [] by =
(Byte []) (Lines.elementat));
BY [x% width] = newbyte;
Repaint ();
}
Public void
Load (string filename) throws oewception {
String currentline = NULL;
BufferedReader Br = New BufferedReader
New FileReader (filename);
Currentline = Br.Readline ();
Currentline! = NULL;
Currentline = br.ready ()) {
Lines.addelement (currentline.getbytes ());
IF (Width <0 ||
Currentline.getbytes (). Length> Width)
Width = currenteline.getbytes (). Length;
}
HEIGHT = lines.size ();
br.close ();
}
Public void Update (graphics g) {Paint (g);
Public void paint (graphics g) {
Int canvasheight = this.getbounds (). HEIGHT;
Int canvaswidth = this.getbounds (). width;
IF (HEIGHT <1 || Width <1)
Return; // Nothing to do
Int width =
((Byte []) (Lines.eMentat (0))).
For (int y = 0; y Byte [] B; b = (Byte []) (Lines.ementat (Y)); For (int x = 0; x Switch (B [x]) { Case '': // EMPTY Part of MAZE g.setcolor (color.lightgray); G.fillRect X * (canvaswidth / width), Y * (canvasheight / height), Canvaswidth / Width, Canvasheight / height); Break; Case '*': // a Wall g.setcolor (color.darkgray); G.fillRect X * (canvaswidth / width), Y * (canvasheight / height), (canvaswidth / width) -1, CANVASHEIGHT / Height) -1); Break; Default: // Must Be Rat g.setColor (Color.Red); g.filloval (x * (canvaswidth / width), Y * (canvasheight / height), Canvaswidth / Width, Canvasheight / height); Break; } } } } } ///: ~ //: Projects: Rat.java Package Projects; Public class rat { Static int Ratcount = 0; Private maze prison; private int vertdir = 0; Private Int horizdir = 0; Private int x, y; Private int myratno = 0; Public Rat (Maze Maze, Int XStart, Int Ystart) { Myratno = ratcount ; System.out.println ("Rat No." Myratno Ready to scurry. "); Prison = maze; x = xstart; y = ystart; Prison.setxy (x, y, (byte) 'r'); New thread () { Public void run () {scurry (); } .start (); } Public void scurry () { // Try and maintain direction if Possible. // Horizontal Backward Boolean Ratcanmove = true; While (Ratcanmove) { Ratcanmove = false; // South IF (Prison.isemptyxy (x, y 1)) { VERTDIR = 1; horizdir = 0; Ratcanmove = TRUE; } // north IF (Prison.isemptyxy (x, y - 1)) IF (Ratcanmove) New Rat (Prison, X, Y-1); // Rat Can Move Already, SO GIVE // this choice to the next rat. Else { VERTDIR = -1; horizdir = 0; Ratcanmove = TRUE; } // West IF (Prison.isemptyxy (X-1, Y)) IF (Ratcanmove) New Rat (Prison, X-1, Y); // Rat Can Move Already, SO GIVE // this choice to the next rat. Else { VERTDIR = 0; horizdir = -1; Ratcanmove = TRUE; } // EAST IF (Prison.isemptyxy (x 1, y)) IF (Ratcanmove) New Rat (Prison, X 1, Y); // Rat Can Move Already, SO GIVE // this choice to the next rat. Else { VERTDIR = 0; horizdir = 1; Ratcanmove = TRUE; } IF (Ratcanmove) {// Move Original Rat. X = Horizdir; Y = vertdir; Prison.setxy (x, y, (byte) 'r'); } // if not the the Rat Will Die. Try { Thread.sleep (2000); } catch (interruptedexception e) { Throw new runtimeException (e); } } System.out.println ("Rat No." Myratno "can't move..DYING..aarrgggh."); } ///: ~ The Maze Initialization File: // :! Projects: Amaze.txt * ** * * ** * *** * ******* * **** *** *** ***** ********* ***** * * * * * ** * * * ** * * * * * ** * * * * ** * ** * ** * ** * ** * ** * ** *** * *** ***** * *** ** * * * * * * * ** * * * ** * * / //: ~ Other resources about maze on creating labyrinth algorithms and achieve their Java source code: http://www.mazeworks.com/mazegen/mazegen.htm Discussion on collision detection algorithms and other discussions for single or group movement behaviors for autonomy physical objects: http://www.red3d.com/cwr/steer/ The XML modifier writes a pair of modifiers (Decorators) for I / O readers (Decorators) and decodes (readroechers). table of Contents