0% found this document useful (0 votes)
54 views11 pages

Array Name Variables: Rules Void Postsudokuconstraints (Cpproblem P)

The document describes the structure of a Sudoku problem and solution using arrays and variables. It defines arrays to represent the rows, columns, and blocks of a 9x9 Sudoku grid, with each element of the grid represented by a variable. Constraints are defined to ensure each row, column, and block contains the digits 1-9 without repetition. Layouts are provided to display the problem grid for users to fill in and later show the solved solution.

Uploaded by

Theo Lo
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as XLS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views11 pages

Array Name Variables: Rules Void Postsudokuconstraints (Cpproblem P)

The document describes the structure of a Sudoku problem and solution using arrays and variables. It defines arrays to represent the rows, columns, and blocks of a 9x9 Sudoku grid, with each element of the grid represented by a variable. Constraints are defined to ensure each row, column, and block contains the digits 1-9 without repetition. Layouts are provided to display the problem grid for users to fill in and later show the solved solution.

Uploaded by

Theo Lo
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as XLS, PDF, TXT or read online on Scribd
You are on page 1/ 11

Rules void postSudokuConstraints(CpProblem p)

Array Name Variables


row[0] x[0,0] x[0,1] x[0,2] x[0,3] x[0,4] x[0,5] x[0,6] x[0,7] x[0,8]
row[1] x[1,0] x[1,1] x[1,2] x[1,3] x[1,4] x[1,5] x[1,6] x[1,7] x[1,8]
row[2] x[2,0] x[2,1] x[2,2] x[2,3] x[2,4] x[2,5] x[2,6] x[2,7] x[2,8]
row[3] x[3,0] x[3,1] x[3,2] x[3,3] x[3,4] x[3,5] x[3,6] x[3,7] x[3,8]
row[4] x[4,0] x[4,1] x[4,2] x[4,3] x[4,4] x[4,5] x[4,6] x[4,7] x[4,8]
row[5] x[5,0] x[5,1] x[5,2] x[5,3] x[5,4] x[5,5] x[5,6] x[5,7] x[5,8]
row[6] x[6,0] x[6,1] x[6,2] x[6,3] x[6,4] x[6,5] x[6,6] x[6,7] x[6,8]
row[7] x[7,0] x[7,1] x[7,2] x[7,3] x[7,4] x[7,5] x[7,6] x[7,7] x[7,8]
row[8] x[8,0] x[8,1] x[8,2] x[8,3] x[8,4] x[8,5] x[8,6] x[8,7] x[8,8]
col[0] x[0,0] x[1,0] x[2,0] x[3,0] x[4,0] x[5,0] x[6,0] x[7,0] x[8,0]
col[1] x[0,1] x[1,1] x[2,1] x[3,1] x[4,1] x[5,1] x[6,1] x[7,1] x[8,1]
col[2] x[0,2] x[1,2] x[2,2] x[3,2] x[4,2] x[5,2] x[6,2] x[7,2] x[8,2]
col[3] x[0,3] x[1,3] x[2,3] x[3,3] x[4,3] x[5,3] x[6,3] x[7,3] x[8,3]
col[4] x[0,4] x[1,4] x[2,4] x[3,4] x[4,4] x[5,4] x[6,4] x[7,4] x[8,4]
col[5] x[0,5] x[1,5] x[2,5] x[3,5] x[4,5] x[5,5] x[6,5] x[7,5] x[8,5]
col[6] x[0,6] x[1,6] x[2,6] x[3,6] x[4,6] x[5,6] x[6,6] x[7,6] x[8,6]
col[7] x[0,7] x[1,7] x[2,7] x[3,7] x[4,7] x[5,7] x[6,7] x[7,7] x[8,7]
col[8] x[0,8] x[1,8] x[2,8] x[3,8] x[4,8] x[5,8] x[6,8] x[7,8] x[8,8]
block[0,0] x[0,0] x[0,1] x[0,2] x[1,0] x[1,1] x[1,2] x[2,0] x[2,1] x[2,2]
block[0,1] x[0,3] x[0,4] x[0,5] x[1,3] x[1,4] x[1,5] x[2,3] x[2,4] x[2,5]
block[0,2] x[0,6] x[0,7] x[0,8] x[1,6] x[1,7] x[1,8] x[2,6] x[2,7] x[2,8]
block[1,0] x[3,0] x[3,1] x[3,2] x[4,0] x[4,1] x[4,2] x[5,0] x[5,1] x[5,2]
block[1,1] x[3,3] x[3,4] x[3,5] x[4,3] x[4,4] x[4,5] x[5,3] x[5,4] x[5,5]
block[1,2] x[3,6] x[3,7] x[3,8] x[4,6] x[4,7] x[4,8] x[5,6] x[5,7] x[5,8]
block[2,0] x[6,0] x[6,1] x[6,2] x[7,0] x[7,1] x[7,2] x[8,0] x[8,1] x[8,2]
block[2,1] x[6,3] x[6,4] x[6,5] x[7,3] x[7,4] x[7,5] x[8,3] x[8,4] x[8,5]
block[2,2] x[6,6] x[6,7] x[6,8] x[7,6] x[7,7] x[7,8] x[8,6] x[8,7] x[8,8]
Row Constraints

Column Constraints

Block Constraints
Layout TableLayout sudokuProblemLayout(RuleSolver s)
<h4>Sudoku Problem</h4>
sudokuProblemTable(s);

<i>Fill the grid so that each column, each row, and each of the nine
3×3 blocks contains the digits from 1 to 9, only one time each</i>

actions()

Layout TableLayout
sudokuProblemTable(RuleSolver s)
block00(s) block01(s) block02(s)
block10(s) block11(s) block12(s)
block20(s) block21(s) block22(s)

Layout TableLayout Layout TableLayout Layout TableLayout


block00(RuleSolver s) block01(RuleSolver s) block02(RuleSolver s)
dat(0,0) dat(0,1) dat(0,2) dat(0,3) dat(0,4) dat(0,5) dat(0,6)
dat(1,0) dat(1,1) dat(1,2) dat(1,3) dat(1,4) dat(1,5) dat(1,6)
dat(2,0) dat(2,1) dat(2,2) dat(2,3) dat(2,4) dat(2,5) dat(2,6)

Layout TableLayout Layout TableLayout Layout TableLayout


block10(RuleSolver s) block11(RuleSolver s) block12(RuleSolver s)
dat(3,0) dat(3,1) dat(3,2) dat(3,3) dat(3,4) dat(3,5) dat(3,6)
dat(4,0) dat(4,1) dat(4,2) dat(4,3) dat(4,4) dat(4,5) dat(4,6)
dat(5,0) dat(5,1) dat(5,2) dat(5,3) dat(5,4) dat(5,5) dat(5,6)

Layout TableLayout Layout TableLayout Layout TableLayout


block20(RuleSolver s) block21(RuleSolver s) block22(RuleSolver s)
dat(6,0) dat(6,1) dat(6,2) dat(6,3) dat(6,4) dat(6,5) dat(6,6)
dat(7,0) dat(7,1) dat(7,2) dat(7,3) dat(7,4) dat(7,5) dat(7,6)
dat(8,0) dat(8,1) dat(8,2) dat(8,3) dat(8,4) dat(8,5) dat(8,6)
Layout TableLayout
block02(RuleSolver s)
dat(0,7) dat(0,8)
dat(1,7) dat(1,8)
dat(2,7) dat(2,8)

Layout TableLayout
block12(RuleSolver s)
dat(3,7) dat(3,8)
dat(4,7) dat(4,8)
dat(5,7) dat(5,8)

Layout TableLayout
block22(RuleSolver s)
dat(6,7) dat(6,8)
dat(7,7) dat(7,8)
dat(8,7) dat(8,8)
Layout TableLayout sudokuSolutionLayout(RuleSolver s)
<h4>Sudoku Solution</h4>
sudokuSolutionTable(s);
actions()

Layout TableLayout sudokuSolutionTable(RuleSolver


s)
show00(s) show01(s) show02(s)
show10(s) show11(s) show12(s)
show20(s) show21(s) show22(s)

Layout TableLayout show00(RuleSolver s) Layout TableLayout show01(RuleSolver s)


s.show("x[0,0]") s.show("x[0,1]") s.show("x[0,2]") s.show("x[0,3]") s.show("x[0,4]")
s.show("x[1,0]") s.show("x[1,1]") s.show("x[1,2]") s.show("x[1,3]") s.show("x[1,4]")
s.show("x[2,0]") s.show("x[2,1]") s.show("x[2,2]") s.show("x[2,3]") s.show("x[2,4]")

Layout TableLayout show10(RuleSolver s) Layout TableLayout show11(RuleSolver s)


s.show("x[3,0]") s.show("x[3,1]") s.show("x[3,2]") s.show("x[3,3]") s.show("x[3,4]")
s.show("x[4,0]") s.show("x[4,1]") s.show("x[4,2]") s.show("x[4,3]") s.show("x[4,4]")
s.show("x[5,0]") s.show("x[5,1]") s.show("x[5,2]") s.show("x[5,3]") s.show("x[5,4]")

Layout TableLayout show20(RuleSolver s) Layout TableLayout show21(RuleSolver s)


s.show("x[6,0]") s.show("x[6,1]") s.show("x[6,2]") s.show("x[6,3]") s.show("x[6,4]")
s.show("x[7,0]") s.show("x[7,1]") s.show("x[7,2]") s.show("x[7,3]") s.show("x[7,4]")
s.show("x[8,0]") s.show("x[8,1]") s.show("x[8,2]") s.show("x[8,3]") s.show("x[8,4]")
ow01(RuleSolver s) Layout TableLayout show02(RuleSolver s)
s.show("x[0,5]") s.show("x[0,6]") s.show("x[0,7]") s.show("x[0,8]")
s.show("x[1,5]") s.show("x[1,6]") s.show("x[1,7]") s.show("x[1,8]")
s.show("x[2,5]") s.show("x[2,6]") s.show("x[2,7]") s.show("x[2,8]")

ow11(RuleSolver s) Layout TableLayout show12(RuleSolver s)


s.show("x[3,5]") s.show("x[3,6]") s.show("x[3,7]") s.show("x[3,8]")
s.show("x[4,5]") s.show("x[4,6]") s.show("x[4,7]") s.show("x[4,8]")
s.show("x[5,5]") s.show("x[5,6]") s.show("x[5,7]") s.show("x[5,8]")

ow21(RuleSolver s) Layout TableLayout show22(RuleSolver s)


s.show("x[6,5]") s.show("x[6,6]") s.show("x[6,7]") s.show("x[6,8]")
s.show("x[7,5]") s.show("x[7,6]") s.show("x[7,7]") s.show("x[7,8]")
s.show("x[8,5]") s.show("x[8,6]") s.show("x[8,7]") s.show("x[8,8]")
Data SudokuData data Data SudokuData data1
x0 x1 x2 x3 x4 x5 x6 x7 x8 x0 x1 x2 x3 x4
0 6 9 0 0 7 0 0 5 0 5 6 8 0

0 5 0 0 0 4 0 2 0 9 0 0 6 0

4 0 0 0 5 0 1 0 0 7 0 0 4 9

8 0 5 0 0 0 6 0 0 8 9 7 0 4

6 7 0 2 9 5 0 1 4 0 0 3 9 0

0 0 1 0 0 0 7 0 9 4 6 5 0 8

0 0 6 0 1 0 0 0 7 5 0 0 2 6

0 1 0 4 0 0 0 8 0 6 0 0 5 0

5 0 0 3 0 0 2 6 0 0 4 9 7 0

Datatype
SudokuData
String id
int x0
int x1
int x2
int x3
int x4
int x5
int x6
int x7
int x8
1
x5 x6 x7 x8
1 9 4 0

5 0 0 3

3 0 0 8

0 6 3 5

6 8 0 0

0 2 9 1

9 0 0 7

4 0 0 9

8 3 5 0
Method String processSudoku(RuleSolver solver)

problemFlowRules(solver);
sudokuNextLayout(solver);
return dialog().next;

Method void createSudokuProblem(RuleSolver s)

CpProblem p = s.newProblem();
// Create 9X9 Square of constrained variables with values from 1 to 9
CpVariable[] vars = p.addSquare("x",1, 9, 9);
postDataConstraints(p);
postSudokuConstraints(p);

Method boolean postDataConstraints(CpProblem p)

CpVariable[] x = p.getArray("x");
for (int i = 0; i < 9; i++) {
if (data[i].x0 != 0) p.add(x[i*9+0].eq(data[i].x0));
if (data[i].x1 != 0) p.add(x[i*9+1].eq(data[i].x1));
if (data[i].x2 != 0) p.add(x[i*9+2].eq(data[i].x2));
if (data[i].x3 != 0) p.add(x[i*9+3].eq(data[i].x3));
if (data[i].x4 != 0) p.add(x[i*9+4].eq(data[i].x4));
if (data[i].x5 != 0) p.add(x[i*9+5].eq(data[i].x5));
if (data[i].x6 != 0) p.add(x[i*9+6].eq(data[i].x6));
if (data[i].x7 != 0) p.add(x[i*9+7].eq(data[i].x7));
if (data[i].x8 != 0) p.add(x[i*9+8].eq(data[i].x8));
}
if (!p.postConstraints()) {
dialog().addError();
dialog().status = "Invalid data";
}
Method String launchSudoku(RuleSolver solver)

out("Launch Sudoku");
dialog().setLastAction("Problem");
return processSudoku(solver);

Method boolean solveSudokuProblem(CpProblem p)

CpVariable[] x = p.getArray("x");
if (p.solve(x, new CpSelectorMinSize(x)) != null) {
return true;
}
else {
p.log("No Solutions");
return false;
}

Method TableLayout dat(int i, int j)

int value = 0;
if (j == 0) { value = data[i].x0; }
if (j == 1) { value = data[i].x1; }
if (j == 2) { value = data[i].x2; }
if (j == 3) { value = data[i].x3; }
if (j == 4) { value = data[i].x4; }
if (j == 5) { value = data[i].x5; }
if (j == 6) { value = data[i].x6; }
if (j == 7) { value = data[i].x7; }
if (j == 8) { value = data[i].x8; }
if (value == 0)
return ask();
"" + value;
Rules void sudokuNextLayout(RuleSolver s)
AND THEN
Next Step is Execute Rules and Define Next Layout

{
ProblemLayout sudokuProblemLayout(s);
}

{
createSudokuProblem(s);
TableLayout layout;
if (solveSudokuProblem(s.cpProblem))
SolutionLayout layout = sudokuSolutionLayout(s);
else
layout = noSolutionLayout();
layout;
}

You might also like