Hello,
I’m having trouble solving a problem which I don’t think should be very difficult, yet I haven’t been able to come close to how to figure it out.
The simplified version of my idea starts with a grid of objects, let’s say cells in a 5x5 grid, with all of them having a boolean state set to false except for one - for example the cell in the middle. I would like this one cell to be the starting point of a path travelling (randomly) through the grid meeting certain conditions:
-
Some input (a random number, noise, etc.) would determine the way of the path, moving left, up, right or down to the next cell, and from there on continue further until some parameter has been met (like a total amount of moves, a certain location has been reached, etc.).
-
I would like it’s current location to be a different color from colours where it has already been.
-
I want it to not be able to leave the grid structure and not re-enter cells which it has already passed unto.
For now though I’m having difficulties figuring out what a good structure for this would be, considering I’m trying to do this as neatly as possible and am therefor attempting to do it in a OOP-manner. I have tried different setups but I feel like every time I come short in the way that I approach them. The latest version I have tried is below here.
Grid g;
int scl, cols, rows, total, margin;
int wi, wj;
void setup() {
size(500, 500);
scl = 100;
cols = width/scl;
rows = height/scl;
total = cols*rows;
margin = scl/2;
g = new Grid();
}
void draw() {
background(0);
g.show();
}
void mousePressed() {
walk();
}
void walk() {
int a = int(random(4));
if (a == 0 && wi > 0) {
wi -= 1;
} else if (a == 1 && wj > 0) {
wj -= 1;
} else if (a == 2 && wi < cols-1) {
wi += 1;
} else if (a == 3 && wj < rows-1) {
wj += 1;
}
println(wi, wj);
}
class Grid {
Cell[] c;
Grid() {
c = new Cell[total];
wi = floor(cols/2);
wj = floor(rows/2);
int k = 0;
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
float x = i*scl;
float y = j*scl;
c[k] = new Cell(x, y, scl, scl, k, i, j);
k++;
}
}
}
void show() {
int k = 0;
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
c[k].show();
k++;
}
}
}
}
class Cell {
float x, y, w, h;
int count;
boolean state = false;
boolean prevState = false;
int i, j;
Cell(float x, float y, float w, float h, int k, int i, int j) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.i = i;
this.j = j;
count = k;
if (wi == i && wj == j) {
state = true;
prevState = true;
}
}
void show() {
checkState();
if (prevState) {
fill(0);
noStroke();
}
if (state && prevState) {
fill(255, 0, 0);
noStroke();
} else {
fill(-1);
stroke(0);
}
rect(x, y, w, h);
}
void checkState() {
if (wi == i && wj == j) {
state = true;
prevState = state;
} else{
state = true;
}
}
}
I hope any of you could help me with tackling these problems I’m facing here, or point me to sketches by others that make use of the same idea. Really any help is much obliged. Thanks a lot in advance.
This is a simplified diagram illustrating the idea I have. Black is it’s current location, grey is the available locations to travel to, and red is the locations where it has already been.