cfoley, on 12 September 2012 - 05:21 AM, said:
Sure, I had two options and it would have affected the way I coded the for loops. Here is the way I did it:
And here is another sensible option, the one I think you were getting at.
The only change is from +3 to +2 and the < in the loop is now <=.
In the first example, the +3 looks a bit strange. +2 in the second example makes more intuitive sense. But in Java for loops, most of the conditions take the form of i < array.length, i < string.length(), i < collection.size(), etc. <= often looks like a mistake.
So, I had to make a choice. Maybe my code would have been more readable if I had done this:
An interesting article on the subject (which I use as a guide when I have design choices like these):
http://www.cs.utexas...8xx/EWD831.html
int rowEnd = rowStart + 3;
// further down...
for(int r = rowStart; r < rowEnd; r++) {
And here is another sensible option, the one I think you were getting at.
int rowEnd = rowStart + 2;
// further down...
for(int r = rowStart; r <= rowEnd; r++) {
The only change is from +3 to +2 and the < in the loop is now <=.
In the first example, the +3 looks a bit strange. +2 in the second example makes more intuitive sense. But in Java for loops, most of the conditions take the form of i < array.length, i < string.length(), i < collection.size(), etc. <= often looks like a mistake.
So, I had to make a choice. Maybe my code would have been more readable if I had done this:
final int squareSize = 3
int rowEndExclusive = rowStart + squareSize;
// further down...
for(int r = rowStart; r < rowEndExclusive; r++) {
An interesting article on the subject (which I use as a guide when I have design choices like these):
http://www.cs.utexas...8xx/EWD831.html
Thanks alot for that! Yeah I was abit confused about why +3 rather than +2 but it all makes sense now thanks, one last think, why count--? what is its purpose, is it because board[row][col] refers to the cell itself which is not to be tested?

New Topic/Question
Reply



MultiQuote


|