Initial Commit

This commit is contained in:
2012-01-22 16:14:32 -05:00
commit f5e306f67f
155 changed files with 5097 additions and 0 deletions

6
Java/Sudoku/.classpath Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="output" path="bin"/>
</classpath>

17
Java/Sudoku/.project Normal file
View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Sudoku</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@@ -0,0 +1,12 @@
#Tue Dec 07 18:50:24 EST 2010
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.6

BIN
Java/Sudoku/bin/Main.class Normal file

Binary file not shown.

Binary file not shown.

53
Java/Sudoku/src/Main.java Normal file
View File

@@ -0,0 +1,53 @@
/**
* @author Twenty Codes, LLC
* @author ricky barrette
* @date Dec 7, 2010
*/
/**
* Driver class to test my sudoku class
* @author ricky barrette
*/
public class Main {
int[][] puzzle = new int[][] {
{0, 8, 0, 4, 0, 2, 0, 6, 0},
{0, 3, 4, 0, 0, 0, 9, 1, 0},
{9, 6, 0, 0, 0, 0, 0, 8, 4},
{0, 0, 0, 2, 1, 6, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 3, 5, 7, 0, 0, 0},
{8, 4, 0, 0, 0, 0, 0, 7, 5},
{0, 2, 6, 0, 0, 0, 1, 3, 0},
{0, 9, 0, 7, 0, 1, 0, 4, 0},
};
int[][] puzzle2 = new int[][] {
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
};
public Main(){
Sudoku s = new Sudoku(puzzle2);
System.out.println(s.solve());
System.out.println(s.toString());
}
/**
*
* @param args
* @author ricky barrette
*/
public static void main (String[] args){
new Main();
}
}

170
Java/Sudoku/src/Sudoku.java Normal file
View File

@@ -0,0 +1,170 @@
/**
* @author Twenty Codes, LLC
* @author ricky barrette
* @date Dec 7, 2010
*/
/**
* This will be a sudoku puzzle
* @author ricky barrette
*/
public class Sudoku {
private int[][] mPuzzle;
private final boolean DEBUG = true;
/**
* Creates a new Sudoku
* @author ricky barrette
* @param puzzle
*/
public Sudoku(int[][] puzzle) {
mPuzzle = puzzle;
}
/**
* creates a copy of the maze that we can modify and discard
* @return a copy of the maze
*/
private int[][] makeTempMaze() {
int[][] m = new int[mPuzzle.length][mPuzzle[0].length];
for (int i = 0; i < m.length; i++) {
for (int index = 0; index < m[0].length; index++) {
m[i][index] = mPuzzle[i][index];
}
}
return m;
}
/**
* solves the puzzle
* @return true if the puzzle is solvable
* @author ricky barrette
*/
public boolean solve(){
return solve(0,0, mPuzzle);
}
/**
* Recursively solves a sudoku puzzle
* @return true if the puzzle is solvable
* @author ricky barrette
*/
private boolean solve(int row, int column, int[][] temp){
/*
* move on to the next row, if we have no more columns
*/
if (column == 9) {
column = 0;
if (++row == 9)
return true;
}
/*
* if this spot is filled in, then lets move on
*/
if (temp[row][column] != 0)
solve(row, column +1, temp);
/*
* we are going to try every number, to find one that is valid.
*/
for(int i = 1; i <= 9; ++i){
if(isValid(i, row, column, temp)){
temp[row][column] = i;
if(solve(row, column +1, temp))
return true;
}
}
/*
* reset on backtrack
*/
temp[row][column] = 0;
/*
* XXX This is only for testing comment out for production
* this allows us to watch the maze being solved
*/
if (DEBUG) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(puzzleToString(temp));
}
return false;
}
/**
* checks to see if the value is a valid answer for this location
* @param newValue in question
* @param row of newValue
* @param column of newValue
* @param temp puzzle
* @return true if the newValue is a valid answer
* @author ricky barrette
*/
private boolean isValid(int newValue, int row, int column, int[][] temp) {
/*
* row
*/
for (int i = 0; i < 9; ++i)
if (newValue == temp[i][column])
return false;
/*
* column
*/
for (int i = 0; i < 9; ++i)
if (newValue == temp[row][i])
return false;
int boxRowOffset = (row / 3)*3;
int boxColOffset = (column / 3)*3;
/*
* box
*/
for (int i = 0; i < 3; ++i)
for (int index = 0; index < 3; ++index)
if (newValue == temp[boxRowOffset+i][boxColOffset+index])
return false;
/*
* no violations, so it's legal
*/
return true;
}
/**
* converts the maze into a human readable string
* @param maze
* @return string for of the maze
*/
private String puzzleToString(int[][] puzzle){
StringBuffer s = new StringBuffer();
s.append("\n");
for (int i = 0; i < puzzle.length; i++) {
for (int index = 0; index < puzzle[0].length; index++) {
s.append(puzzle[i][index] + " ");
}
s.append("\n");
}
return s.toString();
}
/**
* returns a human readable String of this maze
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString(){
return puzzleToString(mPuzzle);
}
}