Chessboard with array pixels

I need to code a chessboard making use of array pixels. I can’t use the function rect. I don’t know how to do it.
I would appreciate it if you can help me. Thanks.

Hello,

and welcome to the forum!

Great to have you here!

Please look at pixels[] \ Language (API) \ Processing 3+

We can’t do homework for you and don’t provide code solutions due to forum policy.

It would be great when you come up with a Sketch that shows your attempt. Then we can help you with concrete code questions.

The main idea is (pseudo code):

for loop over 8 columns xcol
        for loop over 8 rows yrow (**inside** the first for-loop) 
              
             Now you know where your field (one of 64) starts (e.g. at 30*xcol, 30*yrow) 
             determine the color col: dark or light!! 
             
             for loop over pixels for the field x1 (**inside** the first for-loop)   
                   for loop over pixels for the field y1 (**inside** the first for-loop)   
                       determine the color col: dark or light!! Tricky.
                       calc x,y from the 4 for-loops 
                       pixels[y*width+x] = col; 
}
}
}
}

Chrisir

3 Likes

Hi @S1993 , welcome to the forum! :wink:

As @Chrisir said, we can’t give you the full solution for homework related questions, however when you are in front of a problem that you need to solve, ask you the right questions :

  • If I can’t use the rect() function, how am I going to change the color the pixels? → Images and Pixels \ Processing.org

  • What is the characteristic of a chess board? That a black cell doesn’t directly touch another black cell and it’s the same for white. So it’s alternating between black and white. Try to think of something that alternate every two iteration…

  • For the drawing part, a chess board is 8 x 8 cells so what will be the size of a cell in pixels? How can you compute that?

  • You are not going to color every pixel by hand, writing repeating code is not the role of programming so how can you do multiple things by specifying it once in the code?

Hope it helps :wink:

1 Like

Hi, Chrisir and joseph. Thanks for your help.

I have been working on it considering your support.

This is the code I could reach, however, I don’t get the mistake.

void setup(){
size(240,240);
loadPixels();

color col;
for(int x=0;x<8;x++){

for(int y=0;y<8;y++){

if((x+y)%2==0){
col=color(0);
}else{
col=color(255);
}

for (int x1=0; x1<x*30;x1++){
 for (int y1=0; y1<y*30;y1++){
 pixels[x+y*width]=col;

}

}

                  }
                  }

updatePixels();

}
void draw(){

}

Once again I would appreciate it if you let me know about the failure. Thanks

Hi, Chrisir and joseph. Thanks for your help.

I have been working on it considering your support.

This is the code I could reach, however, I don’t get the mistake.

void setup(){
size(240,240);
loadPixels();

color col;
for(int x=0;x<8;x++){

for(int y=0;y<8;y++){

if((x+y)%2==0){
col=color(0);
}else{
col=color(255);
}

for (int x1=0; x1<x*30;x1++){
 for (int y1=0; y1<y*30;y1++){
 pixels[x+y*width]=col;

}

}

                  }
                  }

updatePixels();

}
void draw(){

}

Once again I would appreciate it if you let me know about the failure. Thanks

You haven’t stated what the mistake is

What I see. You forgot to calculate x,y

1 Like

change the two inner for loops to this:

for (int x1=0; x1<30;x1++){
 for (int y1=0; y1<30;y1++){
   float x2= x*30 + x1; 
   float y2= y*30 + y1; 
   pixels[x2+y2*width]=col;
1 Like

by the way:

these are the classical colors for the chess field

 = color( 209.0 , 139.0 , 70.0 );
 = color( 254.0 , 206.0 , 158.0 );

from Chess - Wikipedia

(ah, my apologies, I just saw that you have the colors defined in the video)

How do you record this video?

its not mine its posted on youtube i post the link just

but if you need to record or take screen shots from your pc

i am using this software

video capture software

i do not know why the link not appears and shows just the video

https://www.youtube.com/watch?v=KbyPfLHch2g
1 Like

i am just following the way you are giving the solution to improve my learning :heart: :heart: :heart: :heart: :heart:

1 Like