View Javadoc
1   package io.github.skenvy;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   /**
7    * A collection of cells ~ can be a row, column, or box|region|block.
8    */
9   public class CellCollection {
10  
11    private final List<Cell> cells;
12  
13    /**
14     * Initialise an empty collection to reference 9 cells in a row, column, or box.
15     */
16    public CellCollection() {
17      this.cells = new ArrayList<Cell>(9);
18    }
19  
20    /**
21     * Initialise an empty collection to reference N*N cells in a row, column, or box.
22     */
23    public CellCollection(int boardSize) {
24      this.cells = new ArrayList<Cell>((boardSize * boardSize));
25    }
26  
27    /**
28     * Get a collection of the cells.
29     */
30    public List<Cell> getCells() {
31      // Return new list instance to prevent removal from underlying collection.
32      return new ArrayList<Cell>(this.cells);
33    }
34  
35    public void addCellToCollection(Cell cell) {
36      this.cells.add(cell);
37    }
38  
39    public int[] getValues() {
40      int[] values = new int[this.cells.size()];
41      for (int k = 0; k < this.cells.size(); k++) {
42        values[k] = this.cells.get(k).getValue();
43      }
44      return values;
45    }
46  
47    /**
48     * Used for cell collection related exceptions.
49     */
50    public class SudokuCellCollectionException extends Exception {
51  
52      public SudokuCellCollectionException(String string) {
53        super(string);
54      }
55  
56    }
57  
58  }