fix boundary condition for isLegalPosition()