#Chessboard no % and with nested loop

24 messages · Page 1 of 1 (latest)

hollow grail
#

Hello everyone I don't know if this is the right section to post problems/questions about codes...but here I go I'm Andrea I followed Dan (AKA the legend) for a long time. I started to get involved in coding lately and I was wondering if it is possible to make a chessboard 8X8 only using the conditional "if/else/else if" and "for/nested loop" without the module operator (%)...I managed to make it with just one loop but I really want to use a nested loop for it

#

I get this with one for loop:

function setup() {
createCanvas(450, 450);
}

function draw() {
background(220);
let x = 0;
let y = 0;
let c = false;
let lato = 50;

for (i = 0; i < 64; i++) {

if (c) {
  fill(0);
} else {
  fill(255);
}

rect(x * lato, y * lato, lato);
x++;
c = !c;

if (x == 8) {
  y++;
  x = 0;
  c = !c;
}

}
}

slate rune
#

you might be able to get away with just having a for loop for both rows and columns. Have a variable that keeps inversing itself (so every iteration it becomes the opposite of what it was) and then use that to determine the colour of the field. Just calculate the X and Y coordinates then and draw it out

#

if that's what you mean

hollow grail
#

let x = 50;
let y = 50;
let c = false;

function setup() {
createCanvas(500, 500);
background(128);
for (let j = 0; j < 8; j++) {
for (let i = 0; i < 8; i++) {
if (c) {
fill(0);
} else {
fill(255);
}
rect(x * i, y * j, 50);
c = !c;
}
}
}

#

I mean that...

slate rune
#

so you figured it out? 😅

hollow grail
#

I don't know how to get the right code...in my mind is correct

slate rune
#

it looks right to me

#

it doesn't work?

hollow grail
#

no if u open the editor and put the code u will see

#

it color only column

#

all the column

slate rune
#

ah okay I see

#

just add c = !c after the first nested loop

#

to get it to offset

hollow grail
#

-.- OMFG it was so simple... and I'm so stupid

slate rune
#

haha no worries

#

it doesn't offset because you're dealing with an even number of columns & rows

hollow grail
#

if you ever pass through Italy in Roma send me a DM you got a free beer

slate rune
#

good luck with the project!

hollow grail
#

I have a lot to learn...I'm start tinking that I'm hopeless

slate rune
#

Everyone has to start somewhere. We all started with 0 knowledge and through practicing a lot and learning from our mistakes, ended up with the skills we have now!