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.model; |
22 | |
23 | import java.util.Map; |
24 | import java.util.Set; |
25 | import java.util.HashSet; |
26 | |
27 | import net.sourceforge.pseudoq.solver.Solution; |
28 | |
29 | /** |
30 | * Abstract superclass of all types of puzzle. |
31 | * @author <a href="http://sourceforge.net/users/stevensa">Andrew Stevens</a> |
32 | */ |
33 | public abstract class Puzzle { |
34 | protected Grid grid; |
35 | protected Set<Coordinate> givens = (Set<Coordinate>) new HashSet<Coordinate>(); |
36 | protected Solution solution = null; |
37 | /** |
38 | * Holds value of property filename. |
39 | */ |
40 | private String filename; |
41 | |
42 | public abstract PuzzleTypeEnum getType(); |
43 | |
44 | public Grid getGrid() { |
45 | return grid; |
46 | } |
47 | |
48 | public Set<Coordinate> getGivens() { |
49 | return givens; |
50 | } |
51 | |
52 | public abstract int getMaxInt(); |
53 | |
54 | public abstract int getSize(); |
55 | |
56 | public abstract Map<String, Region> getRegions(); |
57 | |
58 | /** |
59 | * Getter for property solution. |
60 | * @return Value of property solution. |
61 | */ |
62 | public Solution getSolution() { |
63 | return solution; |
64 | } |
65 | |
66 | /** |
67 | * Setter for property solution. |
68 | * @param solution New solution. |
69 | */ |
70 | public void setSolution(Solution solution) { |
71 | this.solution = solution; |
72 | } |
73 | |
74 | /** |
75 | * Getter for property filename. |
76 | * @return Value of property filename. |
77 | */ |
78 | public String getFilename() { |
79 | return this.filename; |
80 | } |
81 | |
82 | /** |
83 | * Setter for property filename. |
84 | * @param filename New value of property filename. |
85 | */ |
86 | public void setFilename(String filename) { |
87 | this.filename = filename; |
88 | } |
89 | |
90 | /** |
91 | * Whether the grid is completely filled. |
92 | */ |
93 | public boolean isComplete() { |
94 | return !grid.containsValue(Integer.valueOf(0)); |
95 | } |
96 | |
97 | /** |
98 | * Whether a solution for the puzzle has been found. |
99 | */ |
100 | public boolean isSolved() { |
101 | return (solution != null); |
102 | } |
103 | |
104 | /** |
105 | * Whether the grid contains values other than those which were given. |
106 | */ |
107 | public boolean isPlayed() { |
108 | boolean played = false; |
109 | for (Coordinate coord : grid.keySet()) { |
110 | if (!grid.get(coord).equals(Integer.valueOf(0)) && |
111 | !givens.contains(coord)) { |
112 | played = true; |
113 | } |
114 | } |
115 | return played; |
116 | } |
117 | |
118 | /** |
119 | * Reset the grid to an empty state. |
120 | */ |
121 | public void reset() { |
122 | for (Coordinate coord : grid.keySet()) { |
123 | grid.put(coord, Integer.valueOf(0)); |
124 | } |
125 | givens.clear(); |
126 | } |
127 | |
128 | /** |
129 | * Reset the grid to only the given values. |
130 | */ |
131 | public void resetToGivens() { |
132 | for (Coordinate coord : grid.keySet()) { |
133 | if (!givens.contains(coord)) { |
134 | grid.put(coord, Integer.valueOf(0)); |
135 | } |
136 | } |
137 | } |
138 | |
139 | } |