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