So, I'm having some trouble while using |"while".

edited September 2014 in How To...

I've gotten into processing a few weeks ago, even though there is a long way ahead, chances look promissing. So I was just talking to a friend of mine when a challenge came up, create a chess board using the fewest while commands as possible. The farthest that I got is using a black background and white squares(rect) to create the board, and that every four squares I've got to make (somehow) the code start creating squares in the next row. So, Processing champions, how do I get to that?

Tagged:

Answers

  • edited September 2014

    It can be done with 1 while statement :)

    Start with a counter e.g. n and intialise it to zero. Since a chessboard has 64 squares we need a loop to loop through from n = 0 to n = 63 inclusive. e.g.

    int n = 0;
    while(n < 64){
      // Relying on integer division
      // n / 8 gives you the row number (0-7)
      // n % 8 gives you the column number (0-7)
      // (column number + row number) % 2 gives us the square type (black or white)
      // this will take care of alternating rows.
    
      // Draw a rectangle of the desired size and colour at the position
      // specified by the column number and row number taking into account
      // the size of the square
    
      // last thing to do before end of loop, increment n by 1
      n++;
    }
    

    Should be enough here to code the solution. :D

  • Well, technically it can be done with 0 while statements (and just for loops), but I suppose lateral thinking isn't allowed for this challenge... :-)

    This is probably the clearest code example I've ever written:

    int a=2;
    int b=a<<a;
    int c=a<<(2*a+1);
    int n=a>>(a+1);
    size(b*c, b*c);
    noStroke();
    while(n<c) {
      fill((n%(a*b)<b?n%a==0?1:0:n%a==0?0:1)*255);
      rect(n%b*c,n/b*c,c,c);
      n++;
    }
    
  • edited September 2014
    int SIZE = 10;
    size(8 * SIZE, 8 * SIZE);
    noStroke();
    int n = -1;
    while (n++ < 64) {
        fill(255 * (((n & 0x8) >> 3) ^ (n & 0x1)));
        rect(SIZE * (n / 8), SIZE * (n % 8), SIZE, SIZE);
    };
    

    DON'T DO THIS!

  • edited September 2014

    This is probably the clearest code example I've ever written:

    ;))

    DON'T DO THIS!

    Agreed since by convention chessboards are drawn with the bottom-left square being black. Having said that don't do this either

    int SIZE = 10;
    size(8 * SIZE, 8 * SIZE);
    noStroke();
    int n = -1;
    while (n++ < 64) {
        fill(255 * (((n & 8) >> 3) ^ ((n+1) & 1)));
        rect(SIZE * (n / 8), SIZE * (n % 8), SIZE, SIZE);
    };
    
  • by convention chessboards are drawn with the bottom-left square being black

    ha, i did know there was a convention but couldn't remember what it was. was a 50/50 chance. which i lost.

    as for DON'T DO THIS, it was mainly about writing code that obtuse. it'd be fine with a little comment saying what all the bit twiddling was doing and, preferably, tests that proved it worked but as it stands it'd fail (and rightly so) a code review. plus all your colleagues will hate you. readability and, from that, maintainability is the most important thing with code. and two for loops would be much clearer.

    (the >> stuff on line 6 is quarks (column number + row number) % 2 done using bits. (n & 8) >> 3 is the row number, n & 1 is the column, ^ is basically the % 2 bit, odd or even)

    (tiny sketches done sat on the sofa in front of the telly for your own amusement on a saturday afternoon don't have to apply the same standards, but some habits are good to get into)

  • edited September 2014

    Ok, one more then! :D

    int e=2<<2;
    size(e*e*e,e*e*e,P2D);
    ((PGraphicsOpenGL)g).textureSampling(3);
    PImage t = createImage(e,e,RGB);
    int n=0;
    while (n<e*e) {
      t.pixels[n]=color((n/e+n%e)%2==0?255:0);
      n++;
    }
    copy(t,0,0,e,e,0,0,width,height);
    
  • edited September 2014

    Thanks for all the examples guys. While (pun intended) this may seem like greek for me at the moment, I'm sure it'll eventually get cleared up. By the way, some other doubts ... still don't know what the ++ and some others mean. Well, this week is my third since I knew of processing so ... ya, a bit fresh. Maybe best to take a look into the basic topics.

  • edited September 2014

    You can find most of them in Processing's reference page: http://processing.org/reference/
    Although I guess they made it hard on purpose! I don't go that far! @-)

  • @Krieger I thought this was a challenge between you and you friends and you were seeking guidance, hence my comments above.

    Here is my solution. It should be easier to follow if you look at the comments I made previously and Processing's reference material.

    int n = 0;
    int step = 20;
    
    void setup() {
      size(200, 200);
      background(200, 255, 200);
      noStroke();
      translate(20, 20);
      while (n < 64) {
        int row = n / 8;
        int col = n % 8;
        int type = (col + row) % 2;
        fill(type == 0 ? 255 : 0);
        rect(col * step, row * step, step, step);
        n++;
      }
    }
    

    The solutions provided by amnon and koogs are extremely difficult to follow because the syntax is complex enough to obscure the semantics. They do it because they can ;)

  • They do it because they can

    in my defence, the question did suggest that there was a code-golf aspect to this. no obfuscation for obfuscation sake in mine either, i just did away with the local variables and used bit twiddling instead of your % and /.

    and i did say not to do this 8)

  • They do it because they can

    I wasn't being serious, hence the winking smiley :D

  • edited September 2014

    If while loops are not a requirement, then maybe a double for loop would be easier to understand:

    void setup() {
      size(400, 400);
      noStroke();
    
      for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
          // The % is the modulo operator
          // Below it is testing if (i + j) is even or odd
          // When it is even white is picked, 255
          // When it is odd black is picked, 0
          if ((i + j) % 2 == 0) fill(255);
          else fill(0);
    
          rect(i*50, j*50, 50, 50);
    
          println("(i + j) = "+(i+j)+" and (i + j) % 2 = "+((i+j)%2));
        }
      }
    }
    
  • (ha, quark, sorry - apparently my phone doesn't show smilies. which is good, because i hate the things)

Sign In or Register to comment.