RSS feed
[root]
/
c13
/
code
/
TI Pattern
/
software design
/
document
login:
password:
title search:
Search this site
Enter your search terms
Web
www.carfield.com.hk
Submit search form
Prev
Next
Wed Dec 26 16:00:00 GMT 2001
Rat
//: c13:Rat.java 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 then the rat will die. try { Thread.sleep(2000); } catch(InterruptedException ie) {} } System.out.println("Rat no." + myRatNo + " can't move..dying..aarrgggh."); } } ///:~
(google search)
(amazon search)
1
2
3
second
download zip of files only
Prev
Next