import java.util.*; public class Worm extends Thread { static final int maxwormlen=100; static final int max_didnt_moves = 4; static final int pause_sleep = 100; //ms static final int coma_sleep = 4000; static java.util.Random rand; static {rand = new java.util.Random(System.currentTimeMillis());} //each worm: int length; int head; int color; int didnt_move = 0; boolean keepRunning=true; int[] x = new int[maxwormlen]; int[] y = new int[maxwormlen]; public Worm() { length = rand.nextInt(WormWorld.maxlength-WormWorld.minlength+1) + WormWorld.minlength; head = 0; //queue color = rand.nextInt(WormWorld.numcolors-1) + 1; //assume black is BG x[0] = rand.nextInt(WormWorld.cols) + 1; y[0] = rand.nextInt(WormWorld.rows) + 1; for (int i=1; i 1) x[i] = x[i-1] - 1; else x[i] = x[i-1]; y[i] = y[i-1]; break; case 1: //up x[i] = x[i-1]; if (y[i-1] > 1) y[i] = y[i-1] - 1; else y[i] = y[i-1]; break; case 2: //right if (x[i-1] < WormWorld.cols) x[i] = x[i-1] + 1; else x[i] = x[i-1]; y[i] = y[i-1]; break; case 3: //down x[i] = x[i-1]; if (y[i-1] < WormWorld.rows) y[i] = y[i-1] + 1; else y[i] = y[i-1]; break; } } } public void run() { while (keepRunning) { display(); try { Thread.sleep(pause_sleep); } catch (InterruptedException e) { System.out.println("InterruptedException caught"); } move(); } //System.out.println(this); try { Thread.sleep(coma_sleep); } catch (InterruptedException e) { } WormWorld.worm_remove(this); //from the set, use finalizer to cleanup //WormWorld.wormsetsize(); death(); } void death() { // protected void finalize() { Ansiscreen.colorback_black(); for (int i=0; i1 && !self_intercept(x[(i+1)%maxwormlen]-1, y[(i+1)%maxwormlen]) && !WormWorld.worms_intercept(x[(i+1)%maxwormlen]-1, y[(i+1)%maxwormlen]) //allow worm to get tied up in a "knot" //|| tries==max_tries ) { x[i] = x[(i+1)%maxwormlen] - 1; y[i] = y[(i+1)%maxwormlen]; moved = true; } break; case 1: //up if (y[(i+1)%maxwormlen]>1 && !self_intercept(x[(i+1)%maxwormlen], y[(i+1)%maxwormlen]-1) && !WormWorld.worms_intercept(x[(i+1)%maxwormlen], y[(i+1)%maxwormlen]-1) //|| tries==max_tries ) { y[i] = y[(i+1)%maxwormlen] - 1; x[i] = x[(i+1)%maxwormlen]; moved = true; } break; case 2: //right if (x[(i+1)%maxwormlen]