CtCI-6th-Edition
CtCI-6th-Edition copied to clipboard
Bug in Othello code (all moves are considered legal)
On this line in the Othello code:
https://github.com/careercup/CtCI-6th-Edition/blob/master/Java/Ch%2007.%20Object-Oriented%20Design/Q7_08_Othello/Board.java#L40
We have the some code that looks a bit like this (condensed slightly to fit in this tiny edit box):
int flipped = 0;
for (int result : results)
if (result > 0) flipped += result;
if (flipped < 0) return false;
It seems to me that there is no way the "return false" can be reached here, because the sum of positive numbers is never negative (assuming no integer overflow).
Granted, these kind of details aren't really the point of Chapter 7, which is more "big picture stuff" it's still nice if demo code actually works. (The code that centers the initial 4 pieces on the board is also wrong, but that's relatively cosmetic.)