| 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.Puzzle; |
| 27 | import net.sourceforge.pseudoq.model.Region; |
| 28 | |
| 29 | /** |
| 30 | * Solver for STANDARD type puzzles. |
| 31 | * @author <a href="http://sourceforge.net/users/stevensa">Andrew Stevens</a> |
| 32 | */ |
| 33 | public class StandardSolver extends AbstractSolver { |
| 34 | /** Log4J logger */ |
| 35 | private static final org.apache.log4j.Logger log = |
| 36 | org.apache.log4j.LogManager.getLogger(StandardSolver.class); |
| 37 | |
| 38 | /** |
| 39 | * Creates a new instance of StandardSolver. |
| 40 | */ |
| 41 | public StandardSolver(Puzzle puzzle) { |
| 42 | super(puzzle); |
| 43 | } |
| 44 | |
| 45 | protected void setupCounters(Puzzle puzzle) { |
| 46 | Map<String, Region> regions = puzzle.getRegions(); |
| 47 | int maxint = puzzle.getMaxInt(); |
| 48 | |
| 49 | log.debug("Setting up counters"); |
| 50 | for (int i = 0; i < maxint; i++) { |
| 51 | counters.put("rowValueCount-" + i, new RegionValueCounter(regions.get("row-" + i))); |
| 52 | counters.put("columnValueCount-" + i, new RegionValueCounter(regions.get("column-" + i))); |
| 53 | counters.put("boxValueCount-" + i, new RegionValueCounter(regions.get("box-" + i))); |
| 54 | for (int j = 1; j <= maxint; j++) { |
| 55 | counters.put("rowIndicatorCount-" + i + "-" + j, |
| 56 | new RegionValueIndicatorCounter(regions.get("row-" + i), j)); |
| 57 | counters.put("columnIndicatorCount-" + i + "-" + j, |
| 58 | new RegionValueIndicatorCounter(regions.get("column-" + i), j)); |
| 59 | counters.put("boxIndicatorCount-" + i + "-" + j, |
| 60 | new RegionValueIndicatorCounter(regions.get("box-" + i), j)); |
| 61 | } |
| 62 | } |
| 63 | for (int i = 0; i < 3; i++) { |
| 64 | for (int j = 1; j <= maxint; j++) { |
| 65 | counters.put("superRowIndicatorCount-" + i + "-" + j, |
| 66 | new RegionValueIndicatorCounter(regions.get("superRow-" + i), j)); |
| 67 | counters.put("superColumnIndicatorCount-" + i + "-" + j, |
| 68 | new RegionValueIndicatorCounter(regions.get("superColumn-" + i), j)); |
| 69 | } |
| 70 | } |
| 71 | for (Coordinate coord : grid.keySet()) { |
| 72 | Region cellRegions = new Region(); |
| 73 | // find all the Rows, Columns & Boxes that contain this cell |
| 74 | for (Region region : regions.values()) { |
| 75 | if (region.contains(coord) && region.getName() != null && |
| 76 | (region.getName().startsWith("Row ") || |
| 77 | region.getName().startsWith("Column ") || |
| 78 | region.getName().startsWith("Box "))) { |
| 79 | cellRegions.addAll(region); |
| 80 | } |
| 81 | } |
| 82 | counters.put("cellPossibilitiesCount-" + coord.getRow() + "-" + coord.getColumn(), |
| 83 | new CellPossibilitiesCounter(coord, maxint, cellRegions)); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | protected void setupStrategies(Puzzle puzzle) { |
| 88 | Map<String, Region> regions = puzzle.getRegions(); |
| 89 | int maxint = puzzle.getMaxInt(); |
| 90 | |
| 91 | log.debug("Setting up strategies"); |
| 92 | for (int i = 0; i < maxint; i++) { |
| 93 | strategies.add(new GapFillRegionStrategy(grid, counters.get("rowValueCount-" + i), |
| 94 | regions.get("row-" + i), maxint)); |
| 95 | strategies.add(new GapFillRegionStrategy(grid, counters.get("columnValueCount-" + i), |
| 96 | regions.get("column-" + i), maxint)); |
| 97 | strategies.add(new GapFillRegionStrategy(grid, counters.get("boxValueCount-" + i), |
| 98 | regions.get("box-" + i), maxint)); |
| 99 | } |
| 100 | for (int i = 0; i < maxint; i++) { |
| 101 | strategies.add(new ScanRowsStrategy(0, i + 1, 0, 2, grid, counters, 0, 2, 3)); |
| 102 | strategies.add(new ScanColumnsStrategy(0, i + 1, 0, 2, grid, counters, 0, 6, 3, 3)); |
| 103 | strategies.add(new ScanRowsStrategy(1, i + 1, 3, 5, grid, counters, 3, 5, 3)); |
| 104 | strategies.add(new ScanColumnsStrategy(1, i + 1, 3, 5, grid, counters, 1, 7, 3, 3)); |
| 105 | strategies.add(new ScanRowsStrategy(2, i + 1, 6, 8, grid, counters, 6, 8, 3)); |
| 106 | strategies.add(new ScanColumnsStrategy(2, i + 1, 6, 8, grid, counters, 2, 8, 3, 3)); |
| 107 | } |
| 108 | for (Coordinate coord : grid.keySet()) { |
| 109 | // only care about cells that aren't already filled |
| 110 | if (Integer.valueOf(0).equals(grid.get(coord))) { |
| 111 | strategies.add(new SingleCellStrategy(coord, grid, |
| 112 | counters.get("cellPossibilitiesCount-" + coord.getRow() + "-" + coord.getColumn()))); |
| 113 | } |
| 114 | } |
| 115 | for (int i = 0; i < maxint; i++) { |
| 116 | for (int j = 1; j <= maxint; j++) { |
| 117 | strategies.add(new ValuePossibilitiesStrategy(regions.get("row-" + i), j, |
| 118 | (RegionValueIndicatorCounter) counters.get("rowIndicatorCount-" + i + "-" + j), counters)); |
| 119 | strategies.add(new ValuePossibilitiesStrategy(regions.get("column-" + i), j, |
| 120 | (RegionValueIndicatorCounter) counters.get("columnIndicatorCount-" + i + "-" + j), counters)); |
| 121 | strategies.add(new ValuePossibilitiesStrategy(regions.get("box-" + i), j, |
| 122 | (RegionValueIndicatorCounter) counters.get("boxIndicatorCount-" + i + "-" + j), counters)); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | } |