| 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 java.util.Map; |
| 24 | |
| 25 | import net.sourceforge.pseudoq.model.Coordinate; |
| 26 | import net.sourceforge.pseudoq.model.Region; |
| 27 | |
| 28 | /** |
| 29 | * Puzzle-solving strategy that checks a region for how many cells could possibly |
| 30 | * contain a particular value. If there's only one possibility left, it |
| 31 | * places the value in that cell. |
| 32 | * @author <a href="http://sourceforge.net/users/stevensa">Andrew Stevens</a> |
| 33 | */ |
| 34 | public class ValuePossibilitiesStrategy implements Strategy { |
| 35 | /** Log4J logger */ |
| 36 | private static final org.apache.log4j.Logger log = |
| 37 | org.apache.log4j.LogManager.getLogger(ValuePossibilitiesStrategy.class); |
| 38 | |
| 39 | private Region region; |
| 40 | private int value; |
| 41 | private Map<String, Counter> counters; |
| 42 | private RegionValueIndicatorCounter counter; |
| 43 | |
| 44 | /** |
| 45 | * Creates a new instance of ValuePossibilitiesStrategy. |
| 46 | * @param region Region to check. |
| 47 | * @param value Value to place. |
| 48 | * @param counter RegionValueIndicatorCounter for the region and value. |
| 49 | * @param counters Pointer to counters map, for looking up the region's |
| 50 | * cells' possibilities. |
| 51 | */ |
| 52 | public ValuePossibilitiesStrategy(Region region, int value, |
| 53 | RegionValueIndicatorCounter counter, Map<String, Counter> counters) { |
| 54 | this.region = region; |
| 55 | this.value = value; |
| 56 | this.counter = counter; |
| 57 | this.counters = counters; |
| 58 | } |
| 59 | |
| 60 | public SolutionStep evaluate() |
| 61 | throws ObsoleteStrategyException, UnsolveablePuzzleException { |
| 62 | assert (region != null); |
| 63 | assert (counters != null); |
| 64 | assert (value != 0); |
| 65 | assert (counter != null); |
| 66 | |
| 67 | SolutionStep nextStep = null; |
| 68 | |
| 69 | if (counter.getCount() > 0) { |
| 70 | throw new ObsoleteStrategyException("Region already contains value"); |
| 71 | } |
| 72 | int possibles = 0; |
| 73 | for (Coordinate coord : region) { |
| 74 | CellPossibilitiesCounter poss = (CellPossibilitiesCounter) counters.get( |
| 75 | "cellPossibilitiesCount-" + coord.getRow() + "-" + coord.getColumn()); |
| 76 | if (poss.getPossibilities().contains(Integer.valueOf(value))) { |
| 77 | possibles++; |
| 78 | nextStep = new SolutionStep(coord, value, this.toString()); |
| 79 | } |
| 80 | } |
| 81 | if (possibles == 0) { |
| 82 | throw new UnsolveablePuzzleException(this.toString()); |
| 83 | } else if (possibles > 1) { |
| 84 | nextStep = null; |
| 85 | } |
| 86 | |
| 87 | return nextStep; |
| 88 | } |
| 89 | |
| 90 | public String toString() { |
| 91 | return "ValuePossibilities for " + value + " in " + region.toString(); |
| 92 | } |
| 93 | |
| 94 | } |