int hsb = 16; int d = 5; Maze maze; void setup() { colorMode(HSB, hsb); int [][] data = { { 1, 1, 1, 1, 1, 1, 1, 1 }, { 0, 1, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 1, 1, 1, 0, 0 }, { 0, 1, 0, 1, 0, 1, 0, 0 }, { 0, 1, 1, 1, 0, 1, 1, 1 }, { 0, 0, 0, 0, 0, 1, 0, 1 }, { 0, 0, 0, 0, 1, 1, 0, 1 }, { 0, 1, 1, 1, 1, 0, 0, 1 }, { 0, 0, 0, 1, 0, 0, 0, 0 }, { 0, 0, 1, 1, 0, 1, 0, 0 }, { 0, 0, 1, 0, 0, 1, 0, 0 }, { 0, 0, 1, 1, 1, 1, 0, 0 }, { 0, 0, 1, 0, 0, 0, 0, 0 }, { 0, 1, 1, 1, 1, 1, 0, 0 }, { 0, 0, 0, 0, 0, 1, 1, 1 }, { 0, 1, 1, 0, 0, 0, 0, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1 } }; size(data[0].length * d, data.length * d); maze = new Maze(data, d, 20); maze.render(); } void draw() { if (!maze.solved) { maze.render(); rectMode(CORNER); fill(hsb, hsb * 0.25); rect(0, 0, width, height); } } class Maze { int [][] data; int block_size; int nparticles; Particle[] particles; boolean solved = false; Maze(int[][] data_, int block_size_, int nparticles_) { data = data_; block_size = block_size_; nparticles = nparticles_; particles = new Particle[nparticles]; for (int i=0; i < nparticles; i++) { particles[i] = new Particle(0, 0, block_size, this); } } void render() { rectMode(CORNER); fill(0); for (int i=0; i < data.length; i++) { for (int j=0; j < data[i].length; j++) { if (data[i][j] != 1) { rect(j * block_size, i * block_size, block_size, block_size); } } } for (int i=0; i < nparticles; i++) { Particle p = particles[i]; p.move(); p.render(); if (isGoal(p)) { solved = true; break; } } } boolean isBlock(int x, int y) { int xx = x / block_size; int yy = y / block_size; if (xx >= data[0].length || yy >= data.length) { return true; } return (data[yy][xx] != 1); } boolean isGoal(Particle p) { return p.y + block_size >= height; } } class Particle { int x; int y; int d; Maze m; Particle(int x_, int y_, int d_, Maze m_) { x = x_; y = y_; d = d_; m = m_; } void move() { float r = random(1); int x0 = x; int y0 = y; if (r < 0.25) { x += d; if (x > width) { x = width; } } else if (r < 0.5) { x -= d; if (x < 0) { x = 0; } } else if (r < 0.75) { y += d; if (y > height) { y = height; } } else { y -= d; if (y < 0) { y = 0; } } if (m.isBlock(x,y)) { x = x0; y = y0; } } void render() { rectMode(CORNER); rect(x, y, d, d); } }