Cleanup
This commit is contained in:
parent
9ff0ded7ea
commit
00200af26b
@ -3,11 +3,11 @@ package part5.aufg2;
|
|||||||
public class Demo {
|
public class Demo {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Queenproblem Q = new Queenproblem();
|
Queenproblem Q = new Queenproblem();
|
||||||
Q.setField(new int[]{1,0,0,0,0,0,0,0});
|
Q.solve();
|
||||||
Q.solve(0);
|
System.out.println("========\n");
|
||||||
|
Q.setField(new int[]{0,7,0,0,0,0,0,8});
|
||||||
|
Q.solve();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,15 +6,20 @@ public class Queenproblem {
|
|||||||
// 0 means empty
|
// 0 means empty
|
||||||
// 1-8 is the y position of the Queen
|
// 1-8 is the y position of the Queen
|
||||||
int[] field;
|
int[] field;
|
||||||
int[][] field2D;
|
|
||||||
public Queenproblem(){
|
public Queenproblem(){
|
||||||
this.field = new int[8];
|
this.field = new int[8];
|
||||||
|
makeBoardEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setField(int[] field) {
|
public void setField(int[] field) {
|
||||||
this.field = field;
|
this.field = field;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void makeBoardEmpty(){
|
||||||
|
this.setField(new int[] {0,0,0,0,0,0,0,0});
|
||||||
|
}
|
||||||
|
|
||||||
private int[][] make2D(){
|
private int[][] make2D(){
|
||||||
int[][] field = new int[8][8];
|
int[][] field = new int[8][8];
|
||||||
|
|
||||||
@ -26,10 +31,7 @@ public class Queenproblem {
|
|||||||
try {
|
try {
|
||||||
int yPosition = this.field[i];
|
int yPosition = this.field[i];
|
||||||
field[yPosition-1][i] = 1; // -1 to handle 0 as empty and 8 as lowest possible with actual index 7
|
field[yPosition-1][i] = 1; // -1 to handle 0 as empty and 8 as lowest possible with actual index 7
|
||||||
}catch (IndexOutOfBoundsException e){
|
}catch (IndexOutOfBoundsException e){/* System.out.println("Ignored Index Error while making it 2D") */}
|
||||||
;
|
|
||||||
//System.out.println("Ignored Index Error while making it 2D");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return field;
|
return field;
|
||||||
@ -44,10 +46,7 @@ public class Queenproblem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean checkVertical(int col){
|
public boolean checkVertical(int col){
|
||||||
if (this.field[col-1] != 0){
|
return this.field[col - 1] == 0;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean checkHorizontal(int row){
|
public boolean checkHorizontal(int row){
|
||||||
@ -77,9 +76,7 @@ public class Queenproblem {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}catch (IndexOutOfBoundsException e){
|
}catch (IndexOutOfBoundsException e){/* Skip Iteration with IndexError */}
|
||||||
; // Skip Iteration with IndexError
|
|
||||||
}
|
|
||||||
|
|
||||||
// cannot be in the same try block.
|
// cannot be in the same try block.
|
||||||
// Exceptions in the + direction stop execution of - prematurely
|
// Exceptions in the + direction stop execution of - prematurely
|
||||||
@ -89,9 +86,7 @@ public class Queenproblem {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}catch (IndexOutOfBoundsException e){
|
}catch (IndexOutOfBoundsException e){/* Skip Iteration with IndexError*/ }
|
||||||
; // Skip Iteration with IndexError
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -106,52 +101,37 @@ public class Queenproblem {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}catch (IndexOutOfBoundsException e){
|
}catch (IndexOutOfBoundsException e){}
|
||||||
;
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
if (this.field[col-1-i] != 0) {
|
if (this.field[col-1-i] != 0) {
|
||||||
if (this.field[col-1-i] == row+i) {
|
if (this.field[col-1-i] == row+i) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}catch (IndexOutOfBoundsException e){
|
}catch (IndexOutOfBoundsException e){}
|
||||||
;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
public boolean isPossible(int row, int col){
|
public boolean isPossible(int row, int col){
|
||||||
|
if(!checkHorizontal(row)){
|
||||||
if(checkHorizontal(row) == false){
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (!checkVertical(col)) {
|
||||||
if (checkVertical(col) == false) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (!checkFallingDiagonal(row, col)){
|
||||||
// Diagonal \
|
|
||||||
if (checkFallingDiagonal(row, col) == false){
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
return checkRisingDiagonal(row, col);
|
||||||
|
|
||||||
if (checkRisingDiagonal(row, col) == false){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void solve(int queensPlaced){
|
public void solve(){
|
||||||
for (int col = 1; col < 9; col++) {
|
for (int col = 1; col < 9; col++) {
|
||||||
if (this.field[col-1] == 0) {
|
if (this.field[col-1] == 0) {
|
||||||
for (int row = 1; row < 9; row++) {
|
for (int row = 1; row < 9; row++) {
|
||||||
if (isPossible(row,col)) {
|
if (isPossible(row,col)) {
|
||||||
this.field[col-1] = row;
|
this.field[col-1] = row;
|
||||||
queensPlaced++;
|
this.solve();
|
||||||
this.solve(queensPlaced);
|
|
||||||
queensPlaced--;
|
|
||||||
this.field[col-1] = 0;
|
this.field[col-1] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user