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 net.sourceforge.pseudoq.model.Coordinate; |
24 | import net.sourceforge.pseudoq.model.Grid; |
25 | |
26 | /** |
27 | * Puzzle-solving strategy that checks a single cell to see if there is only one |
28 | * possible value that can go there. |
29 | * @author <a href="http://sourceforge.net/users/stevensa">Andrew Stevens</a> |
30 | */ |
31 | public class SingleCellStrategy implements Strategy { |
32 | /** Log4J logger */ |
33 | private static final org.apache.log4j.Logger log = |
34 | org.apache.log4j.LogManager.getLogger(SingleCellStrategy.class); |
35 | |
36 | private Grid grid; |
37 | private Coordinate coord; |
38 | private CellPossibilitiesCounter counter; |
39 | |
40 | /** Creates a new instance of SingleCellStrategy */ |
41 | public SingleCellStrategy(Coordinate coord, Grid grid, Counter counter) { |
42 | if (coord == null) { |
43 | throw new IllegalArgumentException("Can't create strategy for null coord"); |
44 | } |
45 | if (grid == null) { |
46 | throw new IllegalArgumentException("Can't create strategy for null grid"); |
47 | } |
48 | if (counter == null) { |
49 | throw new IllegalArgumentException("Can't create strategy for null counter"); |
50 | } |
51 | if (!(counter instanceof CellPossibilitiesCounter)) { |
52 | throw new IllegalArgumentException("SingleCellStrategy needs a CellPossibilitiesCounter"); |
53 | } |
54 | |
55 | this.coord = coord; |
56 | this.grid = grid; |
57 | this.counter = (CellPossibilitiesCounter) counter; |
58 | } |
59 | |
60 | public SolutionStep evaluate() throws ObsoleteStrategyException, UnsolveablePuzzleException { |
61 | assert (coord != null); |
62 | assert (grid != null); |
63 | assert (counter != null); |
64 | |
65 | SolutionStep nextStep = null; |
66 | |
67 | if (!Integer.valueOf(0).equals(grid.get(coord))) { |
68 | throw new ObsoleteStrategyException(coord.toString() + " filled"); |
69 | } |
70 | if (counter.getCount() == 0) { |
71 | throw new UnsolveablePuzzleException(this.toString()); |
72 | } else if (counter.getCount() == 1) { |
73 | log.debug("Only one possibility left for " + coord.toString()); |
74 | int value = counter.getPossibilities().get(0).intValue(); |
75 | nextStep = new SolutionStep(coord, value, this.toString()); |
76 | } |
77 | |
78 | return nextStep; |
79 | } |
80 | |
81 | public String toString() { |
82 | return "SingleCell " + coord.toString(); |
83 | } |
84 | |
85 | } |