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