We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › dynamic matrix creation
Page Index Toggle Pages: 1
dynamic matrix creation? (Read 390 times)
dynamic matrix creation?
Mar 28th, 2010, 7:28am
 
so here is my dilemma, i am using an arraylist to setup a grid of blocks so that they can be removed from the field allowing blocks above them to fall with gravity. i am currently recursively checking the top,right,bottom, and left locations for each block that is the same color and is touching the block that is clicked, however the way i have it coded now it only works for a few clicks and then i get an arrayindexoutofbounds error. i know why(look in the method checkblock()), its because im checking based on the grid and array list being full. only problem is i'm not sure how to dynamically update the 'grid' locations that i'm looking at. i thought about dynamically creating a matrix and updating it every redraw, and then checking based on that but i'll be honest i dont even know how to go about implementing it. any help, code or direction, would be greatly appreciated as i'm trying to make this a portfolio piece for school. code is below:
Code:
ArrayList blocks = new ArrayList();
ArrayList adjlocs = new ArrayList();
ArrayList loclist = new ArrayList();

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 + .1, 25, int(random(0,2))));
   }
 }
 for(int i = 0; i < blocks.size(); i++){
   Block tempblock = (Block) blocks.get(i);
   loclist.add(new Gravityarray(tempblock.blockx));
 }
}

void draw(){
 background(0);
 for(int i=0;i<blocks.size();i++){
   Block tempblock = (Block) blocks.get(i);
   if(tempblock.activated == false){
     tempblock.display();
     tempblock.gravity();
   }
   else{
     tempblock.display();
     deleteblocks();
   }
 }
}


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;
     blockcheck(i);
     while(adjlocs.isEmpty() != true){
       Checkint tempint = (Checkint) adjlocs.get(0);
       blockcheck(tempint.posvalue);
       adjlocs.remove(0);
     }
   }
 }
}
class Gravityarray{
 float xpos;
 
 Gravityarray(float tempx){
   xpos = tempx;
 }
 
}
 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 gravity(){
   float grav = .01;
   for(int i = 0; i < blocks.size()-1; i++){
     Block tempblock = (Block) blocks.get(i);
     Block tempblock2 = (Block) blocks.get(i+1);
     Gravityarray tempgrav = (Gravityarray) loclist.get(i);
     Gravityarray tempgrav2 = (Gravityarray) loclist.get(i+1);
     if(dist(tempblock.blockx, tempblock.blocky, tempblock2.blockx, tempblock2.blocky) > 25 && tempgrav.xpos == tempgrav2.xpos){
       if(dist(this.blockx, height, this.blockx, this.blocky) > 12.5){
         tempblock.blocky += grav;

       }
     }
   }
 }
}
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){
     if(topblock.activated != true){
       adjlocs.add(new Checkint(pos-1));
       topblock.activated = true;
     }
   }
 }
 //right
 if(pos >= 90){
 }
 else{
   Block rightblock = (Block) blocks.get(pos+10);
   if(rightblock.type == tempblock.type){
     if(rightblock.activated != true){
       adjlocs.add(new Checkint(pos+10));
       rightblock.activated = true;
     }
   }
 }
 //bottom
 if(pos%10 != 9){
   Block bottomblock = (Block) blocks.get(pos+1);
   if(bottomblock.type == tempblock.type){
     if(bottomblock.activated != true){
       adjlocs.add(new Checkint(pos+1));
       bottomblock.activated = true;
     }
   }
 }
 //left
 if(pos <= 9){
 }
 else{    
   Block leftblock = (Block) blocks.get(pos-10);
   if(leftblock.type == tempblock.type){
     if(leftblock.activated != true){
       adjlocs.add(new Checkint(pos-10));
       leftblock.activated = true;
     }
   }
 }
}
class Checkint{
 int posvalue;
 
 Checkint(int temp){
   posvalue = temp;
 }
}
void deleteblocks(){
 for(int x = 0; x < blocks.size(); x++){
   Block tempblock2 = (Block) blocks.get(x);
   if(tempblock2.activated == true){
     loclist.remove(x);
     blocks.remove(x);

   }
 }
}

Re: dynamic matrix creation?
Reply #1 - Mar 28th, 2010, 8:47am
 
I have modified the blockcheck() method so that it is prevented from asking for any block that is not inside the arraylist. Compare the code below with yours to see what I have done.
Smiley

Code:
void blockcheck(int pos){
 if(pos >= blocks.size())
   return;

 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){
     if(topblock.activated != true){
       adjlocs.add(new Checkint(pos-1));
       topblock.activated = true;
     }
   }
 }
 //right
 if(pos + 10 < blocks.size()){
   Block rightblock = (Block) blocks.get(pos+10);
   if(rightblock.type == tempblock.type){
     if(rightblock.activated != true){
       adjlocs.add(new Checkint(pos+10));
       rightblock.activated = true;
     }
   }
 }
 //bottom
 if(pos%10 != 9 && pos + 1 < blocks.size()){
   Block bottomblock = (Block) blocks.get(pos+1);
   if(bottomblock.type == tempblock.type){
     if(bottomblock.activated != true){
       adjlocs.add(new Checkint(pos+1));
       bottomblock.activated = true;
     }
   }
 }
 //left
 if(pos - 10 >= 0){
   Block leftblock = (Block) blocks.get(pos-10);
   if(leftblock.type == tempblock.type){
     if(leftblock.activated != true){
       adjlocs.add(new Checkint(pos-10));
       leftblock.activated = true;
     }
   }
 }
}
Page Index Toggle Pages: 1