Checking objects in a certain area

İ = 8
J= 6

How can I check if there is a part between these rows and columns? If there is no part, I want to have an operation.

Edit; I’m doing a chess-like game … example I want to check if there is a piece in row 6 or between row 6 and column 8

please explain the context

I’m doing a chess-like game … example I want to check if there is a piece in row 6 or between row 6 and column 8

you mean check a few cells in one row

or check an area in a sector FROM 6,6 TO 8,8 or so

use a single for-loop OR a nested for-loop and then count if there is a piece

Chrisir

thanks for your answer. I’m trying to figure it out yet. Can you write a little example?

here is an example.

You can select/ unselect cells with the mouse

Two areas are marked with a black outline. (one line, one rectangle area)

The cells in the areas are counted separately.

Make sure that the area is marked like the area that you count in count() function

Chrisir


Cell [] [] grid;

int unit = 44; // size of one tile

int wideCount = 8;
int highCount = 8; 
int count = wideCount * highCount;

int count1=0; 
int count2=0; 

// ---------------------------------------------------------

void setup() {  
  size(700, 400);

  noStroke();
  background(255);

  String[] list = {"b", "c", "d", "q"};

  grid = new Cell[wideCount] [highCount];

  int index = 0;
  for (int x=0; x < wideCount; x++) {
    for (int y=0; y < highCount; y++) {
      //start creating objects
      grid[x][y] = new Cell (x*unit, y*unit, unit, str(index));
      index++;
    } //for
  } //for

  // making AREAS
  for (int x=4; x < 8; x++) {
    for (int y=4; y < 7; y++) {  
      grid[x][y].area=true;  
      //
    }//for
  }//for

  for (int x=4; x < 7; x++) {
    grid[x][2].area=true;
  }//for
  //
} //func 

void draw() {
  background(255);

  for (int x=0; x < wideCount; x++) {
    for (int y=0; y < highCount; y++) {  
      grid[x][y].display();
    }//for
  }//for

  fill(0); 
  text(" <- in this area: "
    +count1, 
    grid[7][7].x+grid[7][7].side+6, grid[6][6].y-1);
  //
  fill(0); 
  text(" <- in this line: "
    +count2, 
    grid[7][7].x+grid[7][7].side+6, grid[2][2].y+19);
}//func

void mousePressed() {
  //
  for (int x=0; x < wideCount; x++) {
    for (int y=0; y < highCount; y++) {  
      grid[x][y].click();
      //
    }//for
  }//for

  count();
}

void count() {

  // count areas 

  count1=0;
  for (int x=4; x < 8; x++) {
    for (int y=4; y < 7; y++) {  
      if (grid[x][y].selected) 
        count1++; 
      //
    }//for
  }//for

  count2=0;
  for (int x=4; x < 7; x++) {
    if (grid[x][2].selected) 
      count2++;
  }//for
  //
}

// =============================================
// and the class

class Cell {
  ///
  float x, y;
  float side;  // size 
  String text=""; 
  boolean selected; 
  boolean area=false;  

  Cell (float tempX, float tempY, 
    float tempSide, 
    String tempText) {
    x = tempX;
    y = tempY;
    side = tempSide;
    text = tempText; 
    //
    float fate = random(2);
  }//constr

  void display() {
    if (selected)
      fill(122, 0, 0); // dark
    else
      fill(255, 0, 220);  // light 

    stroke(255);
    if (area)
      stroke(0); 
    //fill(255, 0, 222);  // light 
    rect (x, y, side, side);

    fill(255); 
    text(text, x+55, y+60);
  }//method

  void click () {
    if (mouseX>x&&
      mouseX<x+side&&
      mouseY>y&&
      mouseY<y+side)
    {
      selected = !
        selected ;
    }
  }
  //
}//class
//

1 Like

I’ll find a simpler way to do this anyway thank you …

apologies

these are the relevant lines

void count() {

  // count areas 

  count1=0;
  for (int x=4; x < 8; x++) {
    for (int y=4; y < 7; y++) {  
      if (grid[x][y].selected) 
        count1++; 
      //
    }//for
  }//for

  count2=0;
  for (int x=4; x < 7; x++) {
    if (grid[x][2].selected) 
      count2++;
  }//for
  //
}
1 Like

ok thanks a lot, i will thin these.

2 Likes