public
anonymous / Class Grid
Created

  • Download Gist
Class Grid
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
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;
}
Class Solve
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
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);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.