Refactord calcAdjacencies() to make the logic readable (included new function validTargetCell to encapsulate common logic).