Knowing when all of the matrix boxes are true

Hi, I have a Boolean matrix, and let me know when all the boxes are true. how can I do?

Answers

  • Easy

    When you mean a 2D grid / 2D array:

    Make a new function that returns boolean named allCellsAreTrue :

    boolean allCellsAreTrue() {

    double/ nested for loop over the grid

    Inside of it: if(!matrix [i][j] )

    return false;
    

    after both loops (!!!) :

    return true;

  • The ! means not

    so as soon as we hit one cell that's false, we leave the function

    Only after testing ALL cells we return true

  • ok thank you, another thing, if I'm in the draw and I want to execute an istructions only once, as I do?

  • edited January 2017

    if I'm in the draw and I want to execute an istructions only once

    Please provide more information. Some approaches, depending on what you are trying to do:

    • Write a simple sketch without a setup and draw loop
    • Execute instructions only once in setup(), before draw() starts
    • Use noLoop() so that draw() only runs once
    • To have something only happen once on a specific draw frame or at a specific time, put an instruction in draw() inside an if() statement. There are many ways of doing this -- such as checking a global variable, or checking millis() or frameCount();

    A simple sketch:

    println("run only once");
    

    Run during setup:

    void setup(){
      println("run only once");
    }
    void draw(){}
    

    Run with noLoop: [added]

    void setup(){
      noLoop();
    }
    void draw(){
      println("run only once");
    }
    

    Run on frame 100:

    void draw(){
      if(frameCount==100){
        println("run only once");
      }
    }
    

    Run once only:

    boolean once;
    void draw(){
      if(!once){
        once=true;
        println("run only once");
      }
    }
    

    Run once only after 10 seconds:

    boolean once;
    void draw(){
      if(!once && millis()>10000){
        once=true;
        println("run only once");
      }
    }
    
  • thanks you so much

  • see also noLoop()

Sign In or Register to comment.