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