1 package io.github.skenvy;
2
3
4
5
6 public class Cell {
7
8
9
10
11 private final int initialValue;
12
13
14
15
16
17 private int value;
18
19
20
21
22 private final boolean[] possibleValues;
23
24
25
26
27 private CellCollection row;
28
29
30
31
32 private CellCollection column;
33
34
35
36
37 private CellCollection box;
38
39
40
41
42 public Cell() {
43 this.initialValue = 0;
44 this.value = 0;
45 this.possibleValues = initialisePossibleValues(3);
46 }
47
48
49
50
51
52
53
54
55 public Cell(int initialValue) throws SudokuCellInvalidInitialValueException {
56 this.initialValue = initialValue;
57 this.value = initialValue;
58 this.possibleValues = initialisePossibleValues(3);
59 if (initialValue < 0 || initialValue > 9) {
60 throw new SudokuCellInvalidInitialValueException(initialValue, 3);
61 }
62 }
63
64
65
66
67
68
69
70
71
72 public Cell(int initialValue, int boardSize) throws SudokuCellInvalidInitialValueException {
73 this.initialValue = initialValue;
74 this.value = initialValue;
75 this.possibleValues = initialisePossibleValues(boardSize);
76 if (initialValue < 0 || initialValue > (boardSize * boardSize)) {
77 throw new SudokuCellInvalidInitialValueException(initialValue, boardSize);
78 }
79 }
80
81
82
83
84
85
86
87
88
89 public Cell(int initialValue, int boardSize, CellCollection row, CellCollection column, CellCollection box) throws SudokuCellInvalidInitialValueException {
90 this.initialValue = initialValue;
91 this.value = initialValue;
92 this.possibleValues = initialisePossibleValues(boardSize);
93 if (initialValue < 0 || initialValue > (boardSize * boardSize)) {
94 throw new SudokuCellInvalidInitialValueException(initialValue, boardSize);
95 }
96 this.row = row;
97 this.column = column;
98 this.box = box;
99 }
100
101 public int getValue() {
102 return this.value;
103 }
104
105 public void setValue(int value) throws SudokuCellCantSetValueOfPredeterminedCellException {
106 if (this.wasCellPredetermined()) {
107 throw new SudokuCellCantSetValueOfPredeterminedCellException();
108 }
109 this.value = value;
110 }
111
112
113
114
115 boolean wasCellPredetermined() {
116 return (this.initialValue != 0);
117 }
118
119
120
121
122 boolean isCellFilled() {
123 return (this.value != 0);
124 }
125
126 boolean[] initialisePossibleValues(int boardSize) {
127 boolean[] possibilities = new boolean[(boardSize * boardSize)];
128 for (int i = 0; i < (boardSize * boardSize); i++) {
129 possibilities[i] = true;
130 }
131 return possibilities;
132 }
133
134
135
136
137 public class SudokuCellException extends Exception {
138 public SudokuCellException(String string) {
139 super(string);
140 }
141 }
142
143 public final class SudokuCellInvalidInitialValueException extends SudokuCellException {
144 public SudokuCellInvalidInitialValueException(int initialValue, int boardSize) {
145 super("Invalid initial value. Board size of " + boardSize + " allows for values from 1 to " + ((boardSize * boardSize)) + "; was given initial value of " + initialValue);
146 }
147 }
148
149 public class SudokuCellCantSetValueOfPredeterminedCellException extends SudokuCellException {
150 public SudokuCellCantSetValueOfPredeterminedCellException() {
151 super("You cannot change the value of a predetermined cell.");
152 }
153 }
154 }