so im getting a stack overflow error:this sketch is attempting too much recursion error. i understand why but i have no idea how to go about fixing it as i need to recursively check a set of values in an arraylist. any help would be greatly appreciated
the issue is cropping up at the end of my blockcheck() function so you know where to look.
Code:ArrayList blocks = new ArrayList();
ArrayList adjlocs = new ArrayList();
public int tracer = 0;
void setup(){
size(960,600);
for(int i = 0; i< 10;i++){
for(int x = 0; x< 10;x++){
blocks.add(new Block(width/2+i*25, height/2+x*25, 25, int(random(0,2))));
}
}
println(blocks.size());
}
void draw(){
background(0);
for(int i=0;i<blocks.size();i++){
Block tempblock = (Block) blocks.get(i);
if(tempblock.activated == false){
tempblock.display();
}
else{
tempblock.nodisplay();
}
}
}
void mouseClicked(){
for(int i=0;i<blocks.size();i++){
Block tempblock = (Block) blocks.get(i);
if(dist(mouseX, mouseY, tempblock.blockx, tempblock.blocky) <=12.5){
tempblock.activated = true;
println(i);
blockcheck(i);
}
}
}
class Block{
public float blockx;
public float blocky;
private float blocksize;
public boolean activated;
public int type;
Block(float x, float y, float _blocksize, int _type){
blockx = x;
blocky = y;
blocksize = _blocksize;
activated = false;
type = _type;
}
void nodisplay(){
}
void display(){
if(activated == false){
strokeWeight(1);
if(this.type == 0){
stroke(0,80,250);
}
else if(this.type == 1){
stroke(250,45,45);
}
}
else if(activated == true){
strokeWeight(2);
if(this.type == 0){
stroke(0,80,250);
}
else if(this.type == 1){
stroke(250,45,45);
}
}
if(this.type == 0){
fill(0,80,250,75);
}
else if(this.type == 1){
fill(250,45,45,75);
}
rectMode(CENTER);
rect(blockx, blocky, blocksize,blocksize);
}
}
void blockcheck(int pos){
Block tempblock = (Block) blocks.get(pos);
//top
if(pos !=0 && pos%10 != 0){
Block topblock = (Block) blocks.get(pos-1);
if(topblock.type == tempblock.type){
topblock.activated = true;
adjlocs.add(new Checkint(pos-1));
}
}
//right
if(pos >= 90){
}
else{
Block rightblock = (Block) blocks.get(pos+10);
if(rightblock.type == tempblock.type){
rightblock.activated = true;
adjlocs.add(new Checkint(pos+10));
}
}
//bottom
if(pos%10 != 9 ){
Block bottomblock = (Block) blocks.get(pos+1);
if(bottomblock.type == tempblock.type){
bottomblock.activated = true;
adjlocs.add(new Checkint(pos+1));
}
}
//left
if(pos <= 9){
}
else{
Block leftblock = (Block) blocks.get(pos-10);
if(leftblock.type == tempblock.type){
leftblock.activated = true;
adjlocs.add(new Checkint(pos-10));
}
}
if(adjlocs.size() != 0){
Checkint tempint = (Checkint) adjlocs.get(0);
int newpos = tempint.posvalue;
//println(adjlocs);
if(pos == newpos){
adjlocs.remove(0);
}
blockcheck(newpos);
}
}
class Checkint{
int posvalue;
Checkint(int temp){
posvalue = temp;
}
}