Skip to content

Commit 2a006d3

Browse files
committed
sudoku
first implementation of sudoku with backtrack not completed yet
1 parent 3c09076 commit 2a006d3

File tree

4 files changed

+341
-0
lines changed

4 files changed

+341
-0
lines changed

Sudoku-backtrack-1/pom.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.dodecaedro</groupId>
5+
<artifactId>Sudoku-backtrack-1</artifactId>
6+
<packaging>jar</packaging>
7+
<version>1.0-SNAPSHOT</version>
8+
<name>Sudoku-backtrack-1</name>
9+
<url>http://maven.apache.org</url>
10+
<properties>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
</properties>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>junit</groupId>
17+
<artifactId>junit</artifactId>
18+
<version>4.11</version>
19+
<scope>test</scope>
20+
</dependency>
21+
22+
<dependency>
23+
<groupId>com.dodecaedro</groupId>
24+
<artifactId>Algorithms</artifactId>
25+
<version>1.0-SNAPSHOT</version>
26+
</dependency>
27+
</dependencies>
28+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.dodecaedro;
2+
3+
/**
4+
* Hello world!
5+
*
6+
*/
7+
public class App
8+
{
9+
public static void main( String[] args )
10+
{
11+
System.out.println( "Hello World!" );
12+
}
13+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/**
2+
*
3+
*/
4+
package com.dodecaedro.backtrack.sudoku;
5+
6+
import java.util.Collection;
7+
8+
import com.dodecaedro.backtrack.BacktrackNode;
9+
10+
/**
11+
* @author JM
12+
*/
13+
public class SudokuNode implements BacktrackNode {
14+
public final static int SIDE_SIZE = 9;
15+
private int[][] board = new int[SIDE_SIZE][SIDE_SIZE];
16+
17+
/* (non-Javadoc)
18+
* @see com.dodecaedro.backtrack.BacktrackNode#isLeaf()
19+
*/
20+
public boolean isLeaf() {
21+
return isAllBoardFull() || isAnyNumberRepeated();
22+
}
23+
24+
/* (non-Javadoc)
25+
* @see com.dodecaedro.backtrack.BacktrackNode#isGoal()
26+
*/
27+
public boolean isGoal() {
28+
return isAllBoardFull() && !isAnyNumberRepeated();
29+
}
30+
31+
/* (non-Javadoc)
32+
* @see com.dodecaedro.backtrack.BacktrackNode#getChildrenNodes()
33+
*/
34+
public Collection<BacktrackNode> getChildrenNodes() {
35+
// TODO Auto-generated method stub
36+
return null;
37+
}
38+
39+
public void setValuePositionXY(int value, int positionX, int positionY) {
40+
this.board[positionX][positionY]=value;
41+
}
42+
43+
public boolean isAnyNumberRepeated() {
44+
return isAnyNumberRepeatedRow() || isAnyNumberRepeatedColumn() || isAnyNumberRepeatedBlock();
45+
}
46+
47+
public boolean isAnyNumberRepeatedBlock() {
48+
49+
for (int currentBlockIndexX = 3; currentBlockIndexX < SIDE_SIZE; currentBlockIndexX += 3) {
50+
for (int currentBlockIndexY = 3; currentBlockIndexY < SIDE_SIZE; currentBlockIndexY += 3) {
51+
52+
for (int posY = currentBlockIndexY - 3; posY < currentBlockIndexY; posY++) {
53+
for (int posX = currentBlockIndexX - 3; posX < currentBlockIndexX; posX++) {
54+
55+
for (int comparePosY = currentBlockIndexY - 3; comparePosY < currentBlockIndexY; comparePosY++) {
56+
for (int comparePosX = currentBlockIndexX - 3; comparePosX < currentBlockIndexX; comparePosX++) {
57+
if (posX != comparePosX || posY != comparePosY) {
58+
if (board[posX][posY] == board[comparePosX][comparePosY]) {
59+
return true;
60+
}
61+
}
62+
}
63+
}
64+
65+
}
66+
}
67+
}
68+
}
69+
70+
return false;
71+
}
72+
73+
public boolean isAnyNumberRepeatedColumn() {
74+
for (int posY=0;posY<SIDE_SIZE;posY++) {
75+
for (int posX=0;posX<SIDE_SIZE;posX++) {
76+
for (int comparePosY=0;comparePosY<SIDE_SIZE;comparePosY++) {
77+
if (posY != comparePosY) {
78+
if (this.board[posX][posY] == this.board[posX][comparePosY]) {
79+
return true;
80+
}
81+
}
82+
}
83+
}
84+
}
85+
return false;
86+
}
87+
88+
public boolean isAnyNumberRepeatedRow() {
89+
for (int posY=0;posY<SIDE_SIZE;posY++) {
90+
for (int posX=0;posX<SIDE_SIZE;posX++) {
91+
for (int comparePosX=0;comparePosX<SIDE_SIZE;comparePosX++) {
92+
if (posX != comparePosX) {
93+
if (this.board[posX][posY] == this.board[comparePosX][posY]) {
94+
return true;
95+
}
96+
}
97+
}
98+
}
99+
}
100+
return false;
101+
}
102+
103+
public boolean isAllBoardFull() {
104+
for (int posY=0;posY<SIDE_SIZE;posY++) {
105+
for (int posX=0;posX<SIDE_SIZE;posX++) {
106+
if (board[posX][posY]==0) {
107+
return false;
108+
}
109+
}
110+
}
111+
return true;
112+
}
113+
114+
public boolean isPositionUsed(int positionX, int positionY) {
115+
return this.board[positionX][positionY] != 0;
116+
}
117+
118+
public int[][] getBoard() {
119+
return board;
120+
}
121+
122+
public void setBoard(int[][] board) {
123+
this.board = board;
124+
}
125+
126+
127+
128+
}
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package com.dodecaedro;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import com.dodecaedro.backtrack.sudoku.SudokuNode;
9+
10+
/**
11+
* Unit test for simple App.
12+
*/
13+
public class SudokuTest {
14+
private static SudokuNode goalNode;
15+
private static SudokuNode nonCompleteNode;
16+
private static SudokuNode boardWithRepeatedNumbersInRow;
17+
private static SudokuNode boardWithRepeatedNumbersInColumn;
18+
private static SudokuNode boardWithRepeatedNumbersInBlock;
19+
20+
@BeforeClass
21+
public static void setUp() {
22+
goalNode = new SudokuNode();
23+
24+
goalNode.setValuePositionXY(6, 0, 0);
25+
goalNode.setValuePositionXY(4, 1, 0);
26+
goalNode.setValuePositionXY(5, 2, 0);
27+
goalNode.setValuePositionXY(7, 3, 0);
28+
goalNode.setValuePositionXY(8, 4, 0);
29+
goalNode.setValuePositionXY(2, 5, 0);
30+
goalNode.setValuePositionXY(3, 6, 0);
31+
goalNode.setValuePositionXY(9, 7, 0);
32+
goalNode.setValuePositionXY(1, 8, 0);
33+
34+
goalNode.setValuePositionXY(2, 0, 1);
35+
goalNode.setValuePositionXY(1, 1, 1);
36+
goalNode.setValuePositionXY(3, 2, 1);
37+
goalNode.setValuePositionXY(6, 3, 1);
38+
goalNode.setValuePositionXY(9, 4, 1);
39+
goalNode.setValuePositionXY(4, 5, 1);
40+
goalNode.setValuePositionXY(5, 6, 1);
41+
goalNode.setValuePositionXY(7, 7, 1);
42+
goalNode.setValuePositionXY(8, 8, 1);
43+
44+
goalNode.setValuePositionXY(7, 0, 2);
45+
goalNode.setValuePositionXY(9, 1, 2);
46+
goalNode.setValuePositionXY(8, 2, 2);
47+
goalNode.setValuePositionXY(1, 3, 2);
48+
goalNode.setValuePositionXY(3, 4, 2);
49+
goalNode.setValuePositionXY(5, 5, 2);
50+
goalNode.setValuePositionXY(4, 6, 2);
51+
goalNode.setValuePositionXY(2, 7, 2);
52+
goalNode.setValuePositionXY(6, 8, 2);
53+
54+
goalNode.setValuePositionXY(9, 0, 3);
55+
goalNode.setValuePositionXY(3, 1, 3);
56+
goalNode.setValuePositionXY(2, 2, 3);
57+
goalNode.setValuePositionXY(4, 3, 3);
58+
goalNode.setValuePositionXY(7, 4, 3);
59+
goalNode.setValuePositionXY(6, 5, 3);
60+
goalNode.setValuePositionXY(1, 6, 3);
61+
goalNode.setValuePositionXY(8, 7, 3);
62+
goalNode.setValuePositionXY(5, 8, 3);
63+
64+
goalNode.setValuePositionXY(1, 0, 4);
65+
goalNode.setValuePositionXY(8, 1, 4);
66+
goalNode.setValuePositionXY(4, 2, 4);
67+
goalNode.setValuePositionXY(2, 3, 4);
68+
goalNode.setValuePositionXY(5, 4, 4);
69+
goalNode.setValuePositionXY(3, 5, 4);
70+
goalNode.setValuePositionXY(9, 6, 4);
71+
goalNode.setValuePositionXY(6, 7, 4);
72+
goalNode.setValuePositionXY(7, 8, 4);
73+
74+
goalNode.setValuePositionXY(5, 0, 5);
75+
goalNode.setValuePositionXY(7, 1, 5);
76+
goalNode.setValuePositionXY(6, 2, 5);
77+
goalNode.setValuePositionXY(8, 3, 5);
78+
goalNode.setValuePositionXY(1, 4, 5);
79+
goalNode.setValuePositionXY(9, 5, 5);
80+
goalNode.setValuePositionXY(2, 6, 5);
81+
goalNode.setValuePositionXY(4, 7, 5);
82+
goalNode.setValuePositionXY(3, 8, 5);
83+
84+
goalNode.setValuePositionXY(8, 0, 6);
85+
goalNode.setValuePositionXY(6, 1, 6);
86+
goalNode.setValuePositionXY(9, 2, 6);
87+
goalNode.setValuePositionXY(3, 3, 6);
88+
goalNode.setValuePositionXY(2, 4, 6);
89+
goalNode.setValuePositionXY(1, 5, 6);
90+
goalNode.setValuePositionXY(7, 6, 6);
91+
goalNode.setValuePositionXY(5, 7, 6);
92+
goalNode.setValuePositionXY(4, 8, 6);
93+
94+
goalNode.setValuePositionXY(4, 0, 7);
95+
goalNode.setValuePositionXY(2, 1, 7);
96+
goalNode.setValuePositionXY(1, 2, 7);
97+
goalNode.setValuePositionXY(5, 3, 7);
98+
goalNode.setValuePositionXY(6, 4, 7);
99+
goalNode.setValuePositionXY(7, 5, 7);
100+
goalNode.setValuePositionXY(8, 6, 7);
101+
goalNode.setValuePositionXY(3, 7, 7);
102+
goalNode.setValuePositionXY(9, 8, 7);
103+
104+
goalNode.setValuePositionXY(3, 0, 8);
105+
goalNode.setValuePositionXY(5, 1, 8);
106+
goalNode.setValuePositionXY(7, 2, 8);
107+
goalNode.setValuePositionXY(9, 3, 8);
108+
goalNode.setValuePositionXY(4, 4, 8);
109+
goalNode.setValuePositionXY(8, 5, 8);
110+
goalNode.setValuePositionXY(6, 6, 8);
111+
goalNode.setValuePositionXY(1, 7, 8);
112+
goalNode.setValuePositionXY(2, 8, 8);
113+
114+
int[][] original = goalNode.getBoard();
115+
int[][] nonCompleteArray = copyUsingForLoop(original);
116+
nonCompleteArray[1][0] = 0;
117+
118+
int[][] repeatedNumberArray = copyUsingForLoop(original);
119+
repeatedNumberArray[0][2] = 6;
120+
121+
nonCompleteNode = new SudokuNode();
122+
nonCompleteNode.setBoard(nonCompleteArray);
123+
124+
boardWithRepeatedNumbersInRow = new SudokuNode();
125+
boardWithRepeatedNumbersInRow.setBoard(repeatedNumberArray);
126+
127+
boardWithRepeatedNumbersInColumn = new SudokuNode();
128+
129+
int[][] repeatedNumberColumnArray = copyUsingForLoop(original);
130+
repeatedNumberColumnArray[2][0] = 6;
131+
boardWithRepeatedNumbersInColumn.setBoard(repeatedNumberColumnArray);
132+
133+
boardWithRepeatedNumbersInBlock = new SudokuNode();
134+
int[][] repeatedNumberBlockArray = copyUsingForLoop(original);
135+
repeatedNumberBlockArray[1][4] = 7;
136+
boardWithRepeatedNumbersInBlock.setBoard(repeatedNumberBlockArray);
137+
}
138+
139+
@Test
140+
public void testIsFull() {
141+
assertTrue(goalNode.isAllBoardFull());
142+
assertFalse(nonCompleteNode.isAllBoardFull());
143+
}
144+
145+
@Test
146+
public void testNumberRepeatedRow() {
147+
assertFalse(goalNode.isAnyNumberRepeatedRow());
148+
assertTrue(boardWithRepeatedNumbersInRow.isAnyNumberRepeatedRow());
149+
}
150+
151+
@Test
152+
public void testNumberRepeatedColumn() {
153+
assertFalse(goalNode.isAnyNumberRepeatedColumn());
154+
assertTrue(boardWithRepeatedNumbersInColumn.isAnyNumberRepeatedColumn());
155+
}
156+
157+
@Test
158+
public void testNumberRepeatedBlock() {
159+
//assertFalse(goalNode.isAnyNumberRepeatedBlock());
160+
assertTrue(boardWithRepeatedNumbersInBlock.isAnyNumberRepeatedBlock());
161+
}
162+
163+
private static int[][] copyUsingForLoop(int[][] aArray) {
164+
int[][] copy = new int[aArray.length][aArray.length];
165+
for (int idy = 0; idy < aArray.length; ++idy) {
166+
for (int idx = 0; idx < aArray.length; ++idx) {
167+
copy[idx][idy] = aArray[idx][idy];
168+
}
169+
}
170+
return copy;
171+
}
172+
}

0 commit comments

Comments
 (0)