| 1 | /* |
| 2 | * Copyright (c) 2005 The PseudoQ Project. |
| 3 | * |
| 4 | * This file is part of PseudoQ. |
| 5 | * |
| 6 | * PseudoQ is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Lesser General Public License as published by |
| 8 | * the Free Software Foundation; either version 2.1 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * PseudoQ is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public License |
| 17 | * along with PseudoQ; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | package net.sourceforge.pseudoq.solver; |
| 22 | |
| 23 | import net.sourceforge.pseudoq.model.Coordinate; |
| 24 | import net.sourceforge.pseudoq.model.Grid; |
| 25 | import net.sourceforge.pseudoq.model.Region; |
| 26 | |
| 27 | /** |
| 28 | * Puzzle-solving strategy that checks a region to see if there is only one |
| 29 | * unknown cell remaining. If so, the missing number can be filled in. |
| 30 | * @author <a href="http://sourceforge.net/users/stevensa">Andrew Stevens</a> |
| 31 | */ |
| 32 | public class GapFillRegionStrategy implements Strategy { |
| 33 | /** Log4J logger */ |
| 34 | private static final org.apache.log4j.Logger log = |
| 35 | org.apache.log4j.LogManager.getLogger(GapFillRegionStrategy.class); |
| 36 | |
| 37 | private Grid grid; |
| 38 | private Counter counter; |
| 39 | private Region region; |
| 40 | private int digits = 0; |
| 41 | private int digitsTotal = 0; |
| 42 | |
| 43 | /** Creates a new instance of GapFillRegionStrategy */ |
| 44 | public GapFillRegionStrategy(Grid grid, Counter counter, Region region, int digits) { |
| 45 | this.grid = grid; |
| 46 | this.counter = counter; |
| 47 | this.region = region; |
| 48 | this.digits = digits; |
| 49 | for (int i = 1; i <= digits; i++) { |
| 50 | digitsTotal += i; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public SolutionStep evaluate() throws ObsoleteStrategyException { |
| 55 | SolutionStep nextStep = null; |
| 56 | |
| 57 | if (counter.getCount() == digits) { |
| 58 | throw new ObsoleteStrategyException(region.getName() + " complete"); |
| 59 | } else if (counter.getCount() == (digits - 1)) { |
| 60 | Coordinate solvedCell = null; |
| 61 | int total = 0; |
| 62 | // find remaining empty cell |
| 63 | for (Coordinate coord : region) { |
| 64 | int value = grid.get(coord).intValue(); |
| 65 | if (value == 0) { |
| 66 | if (solvedCell != null) { |
| 67 | throw new IllegalStateException( |
| 68 | "Counter out of sync with grid for " + region.getName()); |
| 69 | } else { |
| 70 | solvedCell = coord; |
| 71 | } |
| 72 | } |
| 73 | total += value; |
| 74 | } |
| 75 | if (solvedCell == null) { |
| 76 | throw new IllegalStateException("Counter out sync with grid for " + |
| 77 | region.getName()); |
| 78 | } else { |
| 79 | nextStep = new SolutionStep(solvedCell, (digitsTotal - total), |
| 80 | this.toString()); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return nextStep; |
| 85 | } |
| 86 | |
| 87 | public String toString() { |
| 88 | return "GapFill " + region.getName(); |
| 89 | } |
| 90 | |
| 91 | } |