Observer Pattern Applied: Solving Sudoku, Easy Ones

public class Cell {
private List values = new ArrayList();
private int row;
private int col;
// Add all possible entries (1 to 9) in each cell
public Cell(int row, int col) {
this.row = row;
this.col = col;
for (int n = 1; n <= 9; n++) {
values.add(new Integer(n));
}
}
...
}
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
boolean isSame = (i == row) && (j == col);
boolean isSameLine = (i == row) || (j == col);
boolean isMinor = (i/3 == row/3) && (j/3 == col/3);
if (!isSame && (isSameLine || isMinor)) {
// logic to add observers goes here

}
}
}
public class Cell extends Observable implements Observer { 

// add the known value… and notify observers
public void setValue(int value) {

super.notifyObservers(new Integer(value));
}

// Observe and remove the entry set in the observable
public void update(Observable o, Object arg) {
values.remove(arg)

}
}
package my.apps.sudoku;import java.util.Observable;
import java.util.Observer;
import java.util.ArrayList;
import java.util.List;
public class Cell extends Observable implements Observer {
private List values = new ArrayList();
private boolean isSolved = false;
private int row;
private int col;

// Add all possible entries (1 to 9) in each cell
public Cell(int row, int col) {
this.row = row;
this.col = col;
for (int n = 1; n <= 9; n++) {
values.add(new Integer(n));
}
}

// add cells that are in the same line or same box as observers
public synchronized void addObserver(Cell[][] cells) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
boolean isSame = (i == row) && (j == col);
boolean isSameLine = (i == row) || (j == col);
boolean isSecondary = (i/3 == row/3) && (j/3 == col/3);
if (!isSame && (isSameLine || isSecondary)) {
super.addObserver(cells[i][j]);
}
}
}
}
// add the known value after clearing and notify observers
public void setValue(int value) {
values.clear();
values.add(new Integer(value));
isSolved = true;
super.setChanged();
super.notifyObservers(new Integer(value));
}
// Observe and remove the entry set in the observable
public void update(Observable o, Object arg) {
values.remove(arg);
if (!isSolved && values.size() == 1) {
Integer value = (Integer)values.get(0);
setValue(value.intValue());
}
}

// A cell is solved if the it has just one value
public int getValue() {
if (values.size() == 1) {
return ((Integer)values.get(0)).intValue();
}
return 0;
}
}
package my.apps.sudoku;import java.util.Observable; 
import java.util.Observer;
import java.util.ArrayList;
import java.util.List;
public class Sudoku {
private Cell [][] cells = new Cell[9][9];
public Sudoku() {
// initialize the cell
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
cells[i][j] = new Cell(i,j);
}
}
// add observers
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
cells[i][j].addObserver(cells);
}
}
}
// set known values
public void setup(int [][] puzzle) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (puzzle[i][j]!= 0) {
cells[i][j].setValue(puzzle[i][j]);
}
}
}
}
public int getCellValue(int i, int j) {
return cells[i][j].getValue();
}

public static void main(String [] args) {
int [][] puzzle = {
{0, 6, 2, 3, 0, 0, 9, 0, 0},
{0, 0, 0, 0, 8, 0, 0, 0, 7},
{8, 0, 0, 0, 0, 5, 0, 0, 4},
{0, 0, 5, 0, 2, 0, 0, 0, 6},
{0, 3, 0, 9, 4, 6, 0, 5, 0},
{4, 0, 0, 0, 5, 0, 7, 0, 0},
{7, 0, 0, 6, 0, 0, 0, 0, 3},
{5, 0, 0, 0, 3, 0, 0, 0, 0},
{0, 0, 3, 0, 0, 7, 8, 9, 0}
};
Sudoku sudoku = new Sudoku();
sudoku.setup(puzzle);
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
System.out.print(sudoku.getCellValue(i, j) + "|");
}
System.out.println();
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store