I have written a code that demonstrates removal of 3 cells that are the same in the same row.
Check it out below. I removed your update of function and I rewrote an algorithm that it might be useful to you.
I have added the following keys: ‘t’ and ‘c’ in addition to your key commands ‘r’ and ’ ’ aka. space key. Their functionality are:
- ‘t’ For testing
- ‘c’ for checking when at least three items in a row are the same, and mark them for further processing
- ‘r’ to randomize your color table (generates a new one)
- space: Removes those items that are marked by the ‘c’ command.
The proper way to run this example:
- Press r as many times as you see three cells of the same type adjacent to each other in the same row. Then press ‘c’ for checking and marking them. Finally, press ‘space’ bar to remove them.
- Same as 1 but start pressing the ‘t’ to manually set certain adjacent cells the same. Then press ‘c’ followed by ‘space’
I hope this helps.
Kf
final int NROWS=5;
final int NCOLS=5;
ArrayList<Block> block = new ArrayList<Block>();
int wide = NROWS;
int hei = NCOLS;
void setup() {
size(500, 500);
for (int i = 0; i < wide; i++) {
for (int j = 0; j < hei; j++) {
float t = random(4);
t = round(t);
block.add(new Block(50+(50*i), 50+(50*j), 50, t));
}
}
}
void draw() {
background(0);
for (int i = 0; i < block.size(); i++) {
Block b = block.get(i);
b.show();
}
}
void keyPressed() {
if (key == 'r') {
for (int i = block.size()-1; i >= 0; i--) {
block.remove(i);
}
for (int i = 0; i < wide; i++) {
for (int j = 0; j < hei; j++) {
float t = random(4);
t = round(t);
block.add(new Block(50+(50*i), 50+(50*j), 50, t));
}
}
}
//c for 'check' for overlap on the same row
//Notice blocks are added by column
//Algorithm: Traversing down each column from left to right
//Skip the last two columns
//In each step, take the curent cell (aka c1) and the two
// adjacents cells to the right in the same row - aka cells
// c2 and c3) and check if there are of the same type. If
// they are, mark them for removal.
//Since I am taking thre cells in the same row, I have to watch
// I am inside the table at all times. This is the reason I
// skipped the last two columns.
if (key == 'c') {
for (int i = 0; i < block.size()-2*NCOLS; i++) {
Block c1 = block.get(i+0*NCOLS);
Block c2 = block.get(i+1*NCOLS);
Block c3 = block.get(i+2*NCOLS);
if (c1.type==c2.type && c2.type==c3.type) {
c1.markForRemoval=c2.markForRemoval=c3.markForRemoval=true;
}
}
}
if (key == ' ') {
for (int i = block.size()-1; i >= 0; i--) {
Block b = block.get(i);
if (b.markForRemoval == true) {
block.remove(i);
}
}//////
}
//'t' for testing
//For testing purposes. It marks the first three blocks
// in a row the smae. It also marks the last three blocks in a row
//the same.
//This is for testing purposes.
if (key == 't') {
((Block)block.get(0+0*NCOLS)).type=0;
((Block)block.get(0+1*NCOLS)).type=0;
((Block)block.get(0+2*NCOLS)).type=0;
((Block)block.get((NROWS-2)*NCOLS-1)).type=0;
((Block)block.get((NROWS-1)*NCOLS-1)).type=0;
((Block)block.get((NROWS-0)*NCOLS-1)).type=0;
}
}
class Block {
float x, y, w, type, a, d, c;
boolean markForRemoval;
boolean first, second, third, fourth, check = false;
Block(float x, float y, float w, float type) {
this.x = x;
this.y = y;
this.w = w;
this.type = type;
this.markForRemoval=false;
}
void show() {
if (type == 0) {
fill(255, 0, 0);
}
if (type == 1) {
fill(0, 255, 0);
}
if (type == 2) {
fill(0, 0, 255);
}
if (type == 3) {
fill(255, 0, 255);
}
if (type == 4) {
fill(255, 255, 0);
}
if(markForRemoval){
stroke(255);
strokeWeight(5);
}
else{
stroke(0);
strokeWeight(1);
}
rect(x, y, w, w);
}
}