We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey!
I'm doing a CSC project that requires me to make a chess board that has 64 squares of black and white arranged in traditional chessboard fashion. The board has to be random dimensions each time you run the program, ranging from 80x80 to 800x800 in size. I've made a fair amount of progress, but whenever the variable I used for the length and width of each square is even, the entire board is filled in black! However, when that same variable is odd, it makes the board just fine (the variable I'm speaking of is the universal variable 'c' that I defined at the top of the program). Also I need the colors on the board to be inverted when I press the mouse, which is something I haven't even attempted yet because programming seems to get a temper when I try to put this into "void run ()" mode.
Any help is much appreciated!
Ryan
int c = int(random(10,100));
size(c*8+1,c*8+1);
background(255);
for (int y = 0; y < width; y = y + c) {
for (int x = 0; x < width; x = x + c){
if ((x + y) % 2 != 0) {
fill(255);
}
else {
fill(0);
}
stroke(0);
rect(x,y,c,c);
}
}
print(c);
Answers
Here's a hint in your code. Are the numbers alternating between even/odd every time? Run it, and see why your code doesn't make a chessboard when it gets an even number:
-- MenteCode
P.S. I don't like to give hints more than direct answers, but this sounds like a project from what you're supposed to learn.
I made one too. : ) As @MenteCode pointed, if the random number is even, x and y will never be odd (as you are adding c to them), so color engine fails. If you change the colour in line 11 to red you will see... This is what I came up with, but @TfGuy44 code solution seems better.
All awesome answers guys! Thanks so much! I GET IT NOW AHHH!! :D
Hey, me too! Got lotsa online grid examples:
http://studio.processingtogether.com/sp/pad/export/ro.98-pSvW97Q-RE/latest
http://studio.processingtogether.com/sp/pad/export/ro.9ABG0RKl9D2Bx/latest
http://studio.processingtogether.com/sp/pad/export/ro.93MhRWK35nIP8/latest
http://studio.processingtogether.com/sp/pad/export/ro.9L5jAtga7SpyF/latest
http://studio.processingtogether.com/sp/pad/export/ro.9A9O52aqsIjdx/latest
_vk:
I've tried your method now and it's working swimmingly so thanks! The only thing is that I'm having a hard time incorporating the mousePressed section with it. I figured I could redo the board in the draw section, but that ended up not working so i've tried to apply the function on its own outside the setup area, but that's also to no avail... Hints?
Thanks!
If you haven't noticed, @TfGuy44's solution already incorporates whether the mouse is pressed or not. It's in line 12: