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 java.util.HashSet; |
24 | import java.util.LinkedList; |
25 | import java.util.List; |
26 | |
27 | import net.sourceforge.pseudoq.model.Coordinate; |
28 | import net.sourceforge.pseudoq.model.Region; |
29 | |
30 | /** |
31 | * Counts the number of possible values that can be placed in a given cell. |
32 | * @author <a href="http://sourceforge.net/users/stevensa">Andrew Stevens</a> |
33 | */ |
34 | public class CellPossibilitiesCounter implements Counter { |
35 | private List<Integer> possibilities; |
36 | private Coordinate coord; |
37 | private Region region; |
38 | private int digits; |
39 | |
40 | /** Creates a new instance of CellPossibilitiesCounter */ |
41 | public CellPossibilitiesCounter(Coordinate coord, int digits, Region region) { |
42 | if (coord == null) { |
43 | throw new IllegalArgumentException("Can't create counter for null coordinates"); |
44 | } |
45 | if (region == null) { |
46 | throw new IllegalArgumentException("Can't create counter for null region"); |
47 | } |
48 | if (digits <= 0) { |
49 | throw new IllegalArgumentException("Can't create counter with no digits"); |
50 | } |
51 | |
52 | this.coord = coord; |
53 | this.region = region; |
54 | this.digits = digits; |
55 | reset(); |
56 | } |
57 | |
58 | public void place(Coordinate coord, int value) { |
59 | assert (this.coord != null); |
60 | assert (region != null); |
61 | assert (possibilities != null); |
62 | |
63 | if (this.coord.equals(coord)) { |
64 | possibilities.clear(); |
65 | possibilities.add(Integer.valueOf(value)); |
66 | } else if (region.contains(coord)) { |
67 | possibilities.remove(Integer.valueOf(value)); |
68 | } |
69 | } |
70 | |
71 | public int getCount() { |
72 | assert (possibilities != null); |
73 | |
74 | return possibilities.size(); |
75 | } |
76 | |
77 | public void reset() { |
78 | assert (digits > 0); |
79 | |
80 | possibilities = new LinkedList<Integer>(); |
81 | for (int i = 1; i <= digits; i++) { |
82 | possibilities.add(Integer.valueOf(i)); |
83 | } |
84 | } |
85 | |
86 | /** |
87 | * Getter for property possibilities. |
88 | * @return Value of property possibilities. |
89 | */ |
90 | public List<Integer> getPossibilities() { |
91 | return this.possibilities; |
92 | } |
93 | |
94 | } |