| 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.generation; |
| 22 | |
| 23 | import java.util.HashMap; |
| 24 | import java.util.Map; |
| 25 | |
| 26 | import net.sourceforge.pseudoq.model.Coordinate; |
| 27 | import net.sourceforge.pseudoq.model.Puzzle; |
| 28 | import net.sourceforge.pseudoq.model.Region; |
| 29 | import net.sourceforge.pseudoq.solver.Solver; |
| 30 | import net.sourceforge.pseudoq.solver.SolverFactory; |
| 31 | import net.sourceforge.pseudoq.solver.UnsolveablePuzzleException; |
| 32 | |
| 33 | /** |
| 34 | * Simple helper class for generating puzzles randomly. Does not ensure the |
| 35 | * generated puzzles are solveable, and should therefore be replaced as soon |
| 36 | * as possible. |
| 37 | * @author <a href="http://sourceforge.net/users/stevensa">Andrew Stevens</a> |
| 38 | */ |
| 39 | public class SimpleRandomGeneratorHelper { |
| 40 | /** Log4J logger */ |
| 41 | private static final org.apache.log4j.Logger log = |
| 42 | org.apache.log4j.LogManager.getLogger(SimpleRandomGeneratorHelper.class); |
| 43 | |
| 44 | /** Prevent creating instances as all methods are static */ |
| 45 | private SimpleRandomGeneratorHelper() { |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Generate some random values into the supplied puzzle in random positions. |
| 50 | * @param puzzle An empty puzzle of the required type |
| 51 | * @throws GenerationException If we can't generate a valid puzzle. |
| 52 | */ |
| 53 | public static void generate(Puzzle puzzle) throws GenerationException { |
| 54 | // boolean done = false; |
| 55 | // int attempts = 0; |
| 56 | Map<Coordinate, CellPossibilities> possibilities = null; |
| 57 | int maxint = puzzle.getMaxInt(); |
| 58 | int size = puzzle.getSize(); |
| 59 | // int maxAttempts = (size * 10); |
| 60 | |
| 61 | // while (!done && attempts < maxAttempts) { |
| 62 | puzzle.reset(); |
| 63 | possibilities = new HashMap<Coordinate, CellPossibilities>(); |
| 64 | for (Coordinate coord : puzzle.getGrid().keySet()) { |
| 65 | Region cellRegions = new Region(); |
| 66 | // find all the Rows, Columns & Boxes that contain this cell |
| 67 | for (Region region : puzzle.getRegions().values()) { |
| 68 | if (region.contains(coord) && region.getName() != null && |
| 69 | (region.getName().startsWith("Row ") || |
| 70 | region.getName().startsWith("Column ") || |
| 71 | region.getName().startsWith("Box "))) { |
| 72 | cellRegions.addAll(region); |
| 73 | } |
| 74 | } |
| 75 | possibilities.put(coord, |
| 76 | new CellPossibilities(coord, maxint, cellRegions)); |
| 77 | } |
| 78 | |
| 79 | for (int i = 0; i < (size * size * 3 / 8); i++) { |
| 80 | Coordinate coord = new Coordinate((int) (Math.random() * size), |
| 81 | (int) (Math.random() * size)); |
| 82 | CellPossibilities choices = possibilities.get(coord); |
| 83 | if (choices != null && choices.getCount() > 0) { |
| 84 | Integer value = choices.getPossibilities().get( |
| 85 | (int) (Math.random() * choices.getCount())); |
| 86 | puzzle.getGivens().add(coord); |
| 87 | puzzle.getGrid().put(coord, value); |
| 88 | for (CellPossibilities cp : possibilities.values()) { |
| 89 | cp.place(coord, value); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | // log.debug("Attempt " + (attempts + 1) + ":\n" + puzzle.toString()); |
| 94 | // Solver solver = SolverFactory.newInstance(puzzle); |
| 95 | // try { |
| 96 | // solver.solveEntirePuzzle(); |
| 97 | // log.debug("Solveable (attempts = " + attempts + ")"); |
| 98 | // done = true; |
| 99 | // } catch (IllegalStateException e) { |
| 100 | // log.warn("Unsolveable", e); |
| 101 | // // try again |
| 102 | // attempts++; |
| 103 | // } catch (UnsolveablePuzzleException e) { |
| 104 | // log.debug("Unsolveable", e); |
| 105 | // // try again |
| 106 | // attempts++; |
| 107 | // } |
| 108 | // } |
| 109 | // if (attempts == maxAttempts) { |
| 110 | // throw new GenerationException("Couldn't make a solveable puzzle in " + |
| 111 | // maxAttempts + " attempts"); |
| 112 | // } |
| 113 | } |
| 114 | |
| 115 | } |