| 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.List; |
| 25 | import java.util.Map; |
| 26 | |
| 27 | import net.sourceforge.pseudoq.model.Coordinate; |
| 28 | import net.sourceforge.pseudoq.model.Puzzle; |
| 29 | import net.sourceforge.pseudoq.model.PuzzleFactory; |
| 30 | import net.sourceforge.pseudoq.model.PuzzleTypeEnum; |
| 31 | import net.sourceforge.pseudoq.model.Region; |
| 32 | import net.sourceforge.pseudoq.solver.Solver; |
| 33 | import net.sourceforge.pseudoq.solver.SolverFactory; |
| 34 | import net.sourceforge.pseudoq.solver.UnsolveablePuzzleException; |
| 35 | |
| 36 | /** |
| 37 | * Generator for TINY type puzzles. |
| 38 | * @author <a href="http://sourceforge.net/users/stevensa">Andrew Stevens</a> |
| 39 | */ |
| 40 | public class TinyGenerator implements Generator { |
| 41 | /** Log4J logger */ |
| 42 | private static final org.apache.log4j.Logger log = |
| 43 | org.apache.log4j.LogManager.getLogger(TinyGenerator.class); |
| 44 | |
| 45 | private GridFiller filler = null; |
| 46 | |
| 47 | /** Creates a new instance of TinyGenerator */ |
| 48 | public TinyGenerator(GridFiller filler) { |
| 49 | this.filler = filler; |
| 50 | } |
| 51 | |
| 52 | public Puzzle generate() throws GenerationException { |
| 53 | Puzzle puzzle = PuzzleFactory.newInstance(PuzzleTypeEnum.TINY); |
| 54 | |
| 55 | // fill the entire grid such that it's consistent |
| 56 | log.debug("Filling grid"); |
| 57 | filler.fill(puzzle.getGrid(), puzzle.getRegions(), puzzle.getMaxInt()); |
| 58 | // pick the given values |
| 59 | log.debug("Picking givens"); |
| 60 | int size = puzzle.getSize(); |
| 61 | for (int i = 0; i < (size * size * 3 / 8); i++) { |
| 62 | Coordinate coord = new Coordinate((int) (Math.random() * size), |
| 63 | (int) (Math.random() * size)); |
| 64 | puzzle.getGivens().add(coord); |
| 65 | } |
| 66 | log.debug("Clearing non-givens"); |
| 67 | // remove all the values apart from the givens |
| 68 | puzzle.resetToGivens(); |
| 69 | |
| 70 | return puzzle; |
| 71 | } |
| 72 | |
| 73 | } |