private boolean checkValidity() { int answer = mOperation.getResult(); numbers.clear(); for (Cell cell : cells.values()) { Point p = cell.location(); numbers.add(mLogic.getValue(p.y, p.x)); } // first check to make sure no elements are zero to avoid early // exit errors (e.g., solution = 3, cage is (3,) // because default (missing values are zero) // also prevent division by zero errors for (Integer i : numbers) { if (i == 0) { return false; } } if (mOperation.isAdd()) { int sum = 0; for (Integer i : numbers) { sum += i; } return answer == sum; } else if (mOperation.isMultiply()) { int product = 1; for (Integer i : numbers) { product *= i; } return product == answer; } else if (mOperation.isSubtract()) { return permuteSubtractions(answer, numbers, 0); } else if (mOperation.isDivide()) { // try all the possible combinations // don't just check against the solution. That would make // it to easy for a player to fill in missing values // by eliminating order-based guesses for them by showing // solved cages for only one possible solution out of many // (e.g, solution = 2 (4,2) might be ok but (2,4) would not return permuteDivision(answer, numbers, 0); } else if (numbers.size() == 1) { return answer == numbers.get(0); } return false; }
public String getCellsString() { String operationString = "[no op] "; if (mOperation != null) { operationString = mOperation.toString(); } StringBuilder builder = new StringBuilder(operationString); builder.append(" (row ,col)| "); for (Cell cell : cells.values()) { Point p = cell.location(); builder.append("(").append(p.y).append(", ").append(p.x).append(") "); } return builder.toString(); }