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