int W = 40; int H = 25; int SQ = 10; boolean timeToDie; Walker w = new Walker(); color DARK = color(0, 0, 128); color LIGHT = color(100, 100, 100); int screen[][] = new int[W][H]; color wallcolor[][] = new color[W][H]; void setup() { size(400,250); background(DARK); frameRate(100); size(W*SQ, H*SQ); strokeWeight(4); for (int x = 0; x < W; x++) { for (int y = 0; y < H; y++) { screen[x][y] = round(random(1)); wallcolor[x][y] = LIGHT; drawSlash(x,y); } } } int DOWNSLASH = 0; int UPSLASH = 1; void mouseReleased() { timeToDie = true; } void draw() { if(w != null){ w.drawWall(); if(timeToDie){ w = new Walker(); timeToDie = false; } w.move(); w.draw(); if(w.markAndCheckForLoop()){ timeToDie = true; } } } void drawSlash(int x, int y){ noStroke(); fill(DARK); rect(x * SQ, y*SQ,SQ,SQ); stroke(wallcolor[x][y]); if (screen[x][y] == DOWNSLASH) { line(x * SQ, y*SQ, (x+1)*SQ, (y+1)*SQ); } else { line(x*SQ, (y+1)*SQ, (x+1)*SQ, y*SQ); } } int LEFT = 1; int RIGHT = 2; int UP = 3; int DOWN = 4; class Walker { int x, y; int godir; color c; int myhist[][] = new int[W][H]; Walker() { x = floor(random(W)); y = floor(random(H)); //x = mouseX / SQ; //y = mouseY / SQ; godir = floor(random(4))+1; c = color(128+random(128), 128+random(128), 128+random(128)); } void move() { int revx = x; int revy = y; int revdir = -1; if (godir == LEFT) { x -= 1; revdir = RIGHT; } if (godir == RIGHT) { x += 1; revdir = LEFT; } if (godir == UP) { y -= 1; revdir = DOWN; } if (godir == DOWN) { y += 1; revdir = UP; } if (x < 0 || x >= W || y < 0 || y >= H) { x = revx; y = revy; godir = revdir; } bounce(); // println(":::"+godir); } int score = 0; boolean markAndCheckForLoop(){ //if(wallcolor[x][y] == LIGHT) { wallcolor[x][y] = c; score++; //} //only mark first doreciton this way... if(myhist[x][y] == 0) myhist[x][y] = godir; else { //if seen before, see if we saw it in this direction... if(godir == myhist[x][y]){ drawWall(); //println(score); return true; } } return false; } void bounce() { if (godir == RIGHT) { if (screen[x][y] == UPSLASH) godir = UP; else godir = DOWN; return; } if (godir == LEFT) { if (screen[x][y] == UPSLASH) godir = DOWN; else godir = UP; return; } if (godir == UP) { if (screen[x][y] == UPSLASH) godir = RIGHT; else godir = LEFT; return; } if (godir == DOWN) { if (screen[x][y] == UPSLASH) godir = LEFT; else godir = RIGHT; return; } return; } void drawWall(){ drawSlash(x,y); } void draw() { noFill(); stroke(c); ellipse((x+.5)*SQ, (y+.5)*SQ, SQ/2, SQ/2); } }