We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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?
Answers
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 fromn = 0
ton = 63
inclusive. e.g.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:
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
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)
Ok, one more then! :D
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.
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.
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 ;)
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)
I wasn't being serious, hence the winking smiley :D
If while loops are not a requirement, then maybe a double for loop would be easier to understand:
(ha, quark, sorry - apparently my phone doesn't show smilies. which is good, because i hate the things)