Skip to content

Instantly share code, notes, and snippets.

Created November 6, 2012 19:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4026779 to your computer and use it in GitHub Desktop.
Save anonymous/4026779 to your computer and use it in GitHub Desktop.
public class Grid {
int gridCells[][] = new int[9][9];
//initialise the grid
public void grid() {
this.initialise();
}
public int getGridCells(int c1, int c2) {
return gridCells[c1][c2];
}
public void setGridCells(int c1, int c2, int setTo) {
this.gridCells[c1][c2] = setTo;
}
//sets all spaces to 0
public void initialise() {
for(int counter = 0; counter <= 8; counter++){
for(int counter2 = 0; counter2 <= 8; counter2++){
gridCells[counter][counter2] = 0;
}
}
}
public boolean columnCheck(int i, int j) {
for(int counter = 0; counter <= 8; counter++){
if(this.getGridCells(i, counter) == j){
return true;
}
}
return false;
}
public boolean rowCheck(int i, int j) {
for(int counter = 0; counter <= 8; counter++){
if(this.getGridCells(counter, i) == j){
return true;
}
}
return false;
}
public boolean sectionCheck(int i, int j, int k) {//define the correct section for the coordinates
int firstColumn, lastColumn, firstRow, lastRow;
if(i > 2){
if(i < 6 ){
firstColumn = 3;
lastColumn = 5;
}
else{
firstColumn = 6;
lastColumn = 8;
}
}
else{
firstColumn = 0;
lastColumn = 2;
}
if(j > 2){
if(j < 6 ){
firstRow = 3;
lastRow = 5;
}
else{
firstRow = 6;
lastRow = 8;
}
}
else{
firstRow = 0;
lastRow = 2;
}
for(int counter = firstColumn; counter <= lastColumn; counter++){
for(int counter2 = firstRow; counter2 <= lastRow; counter2++){
if( this.getGridCells(counter, counter2) == k){
return true;
}
}
}
return false;
}
public boolean runAllTests(int i, int j, int k) {
if(this.columnCheck(i, k) | this.rowCheck(j, k) | this.sectionCheck(i, j, k)){//if there is amatching number return true
return true;
}
return false;
}
public boolean runInverseAllTests(int i, int j, int k) {
if(this.columnCheck(i, k ) & this.rowCheck(j, k) & this.sectionCheck(i, j, k) ){//if there isn't a matching number in one of the tests return true
return true;
}
return false;
}
package org.johnscarrott.sudukoSolver;
public class Solve {
//array of grids
Grid allGrids[] = new Grid[11];
public void solve() {
}
public void initialiseAllGrids() {
for(int counter = 0; counter <= 10; counter++){
allGrids[counter] = new Grid();
}
}
//fills arrays with options of cells where a number could go
public void allGridTest(int i) {
this.allGrids[i].initialise();
for(int counter = 0; counter <= 8; counter++){
for(int counter2 = 0; counter2 <= 8; counter2++){
if(!this.allGrids[10].runAllTests(counter, counter2, i) & this.allGrids[10].getGridCells(counter, counter2) == 0){
this.allGrids[i].setGridCells(counter, counter2, 1);
}
}
}
}//runs alltests on every grid array to say where there is only one availaable option for a number and fills it in.
public void solveInteration() {
for(int counter = 1; counter <= 9; counter++ )
{
this.allGridTest(counter);//generates 1 0 grids
for(int counter3 = 0; counter3 <= 8; counter3++){
for(int counter4 = 0; counter4 <= 8; counter4++){
if(this.allGrids[counter].getGridCells(counter3, counter4) == 1){//checks if cell is equal to 1 //need to remove test cell from test
this.allGrids[counter].setGridCells(counter3, counter4, 0);
if(!this.allGrids[counter].runInverseAllTests(counter3, counter4, 1)){//if the cell one is the only 1 in cell row or section add to grid 10
this.allGrids[10].setGridCells(counter3, counter4, counter);
this.allGrids[counter].setGridCells(counter3, counter4, 1);
}
this.allGrids[counter].setGridCells(counter3, counter4, 1);
}
}
}
}
}
public static void main(String[] args) {
Solve testRun = new Solve();
int tempGrid[][] = new int[9][9];
testRun.initialiseAllGrids();
setExampleGrid(testRun.allGrids[0]);
for(int counter = 0; counter <= 8; counter++){
System.out.print("\n");
for(int counter2 = 0; counter2 <= 8; counter2++){
System.out.print(" ");
System.out.print(testRun.allGrids[0].getGridCells(counter2, counter)) ;
tempGrid[counter][counter2] = testRun.allGrids[10].getGridCells(counter, counter2);
}
}
testRun.allGrids[10] = testRun.allGrids[0];//similar to solve iter test but iterates more and check for completeness
//tempGrid = testRun.allGrids[10];
for(int counter = 0; counter <= 8; counter++){
for(int counter2 = 0; counter2 <= 8; counter2++){
if(testRun.allGrids[10].getGridCells(counter2, counter) == 0){
testRun.solveInteration();
//if(testRun.allGrids[10].equals(tempGrid)){
//}
//testvar++;
}
}
}
System.out.print("\n");
for(int counter = 0; counter <= 8; counter++){
System.out.print("\n");
for(int counter2 = 0; counter2 <= 8; counter2++){
System.out.print(" ");
System.out.print(testRun.allGrids[10].getGridCells(counter2, counter)) ;
}
}
}
private static void setExampleGrid(Grid testGrid) {
testGrid.setGridCells(1, 0, 1);
testGrid.setGridCells(5, 0, 4);
testGrid.setGridCells(8, 0, 2);
testGrid.setGridCells(0, 1, 5);
testGrid.setGridCells(5, 1, 2);
testGrid.setGridCells(7, 1, 4);
testGrid.setGridCells(3, 2, 9);
testGrid.setGridCells(4, 2, 8);
testGrid.setGridCells(6, 2, 1);
testGrid.setGridCells(2, 3, 9);
testGrid.setGridCells(3, 3, 3);
testGrid.setGridCells(6, 3, 4);
testGrid.setGridCells(8, 3, 6);
testGrid.setGridCells(2, 4, 6);
testGrid.setGridCells(6, 4, 7);
testGrid.setGridCells(7, 4, 8);
testGrid.setGridCells(0, 5, 1);
testGrid.setGridCells(1, 5, 3);
testGrid.setGridCells(7, 5, 9);
testGrid.setGridCells(2, 6, 4);
testGrid.setGridCells(3, 6, 5);
testGrid.setGridCells(4, 6, 6);
testGrid.setGridCells(1, 7, 8);
testGrid.setGridCells(4, 7, 2);
testGrid.setGridCells(5, 7, 9);
testGrid.setGridCells(7, 7, 6);
testGrid.setGridCells(0, 8, 7);
testGrid.setGridCells(3, 8, 4);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment