Almost works, Display at 7 works, doesn't stop when finishing / print solution

This commit is contained in:
Matti 2024-05-20 21:25:17 +02:00
parent 7c7a91a762
commit f53f29200e
2 changed files with 106 additions and 23 deletions

View File

@ -3,10 +3,12 @@ package part5.aufg2;
public class Demo {
public static void main(String[] args) {
Queenproblem Q = new Queenproblem();
System.out.println(Q.field[2]);
Q.setField(new int[] {0,0,2,3,4,5,0,0});
Q.setField(new int[]{0, 0, 0, 0, 0, 0, 0, 0});
Q.print();
System.out.println("=============");
Q.solve(0);
}
}

View File

@ -25,9 +25,10 @@ public class Queenproblem {
for (int i=0; i<8; i++) {
try {
int yPosition = this.field[i];
field[i][yPosition-1] = 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){
System.out.println("Ignored Index Error while making it 2D");
;
//System.out.println("Ignored Index Error while making it 2D");
}
}
@ -39,6 +40,7 @@ public class Queenproblem {
for (int[] row:field) {
System.out.println(Arrays.toString(row));
}
System.out.println();
}
public boolean checkVertical(int col){
@ -48,37 +50,116 @@ public class Queenproblem {
return true;
}
public boolean checkHorizontal(int col){
public boolean checkHorizontal(int row){
for (int colFilled : this.field) {
if (colFilled == col){
if (colFilled == row){
return false;
}
}
return true;
}
public boolean isPossible(int row, int col){
int x = --row;
int y = --col;
private void ensureLegalCords(int row, int col){
if (col<1 || col>8){
throw new RuntimeException("Invalid Col " + col);
}
if (row<1 || row>8){
throw new RuntimeException("Invalid Row " + row);
}
}
public boolean checkFallingDiagonal(int row, int col){
ensureLegalCords(row, col);
// Vertical
if (this.field[x] != 0)
return false;
// Diagonal \
for (int i = 0; i < 8; i++) {
try {
if (this.field[x+i] == y+i){
if (this.field[col-1+i] != 0) {
if (this.field[col - 1 + i] == row + i) {
return false;
}
if (this.field[x-i] == y-i){
return false;
}
}catch (IndexOutOfBoundsException e){
System.out.println("Ignored index Error with Diagonal Possibility Check");
}
; // Skip Iteration with IndexError
}
// cannot be in the same try block.
// Exceptions in the + direction stop execution of - prematurely
try {
if (this.field[col-1-i] != 0) {
if (this.field[col - 1 - i] == row - i) {
return false;
}
}
}catch (IndexOutOfBoundsException e){
; // Skip Iteration with IndexError
}
}
return true;
}
public boolean checkRisingDiagonal(int row, int col){
ensureLegalCords(row, col);
for (int i = 0; i < 8; i++) {
try {
if (this.field[col-1+i] != 0) {
if (this.field[col - 1 + i] == row - i) {
return false;
}
}
}catch (IndexOutOfBoundsException e){
;
}
try {
if (this.field[col-1-1] != 0) {
if (this.field[col - 1 - i] == row + 1) {
return false;
}
}
}catch (IndexOutOfBoundsException e){
;
}
}
return true;
}
public boolean isPossible(int row, int col){
if(checkHorizontal(row) == false){
return false;
}
if (checkVertical(col) == false) {
return false;
}
// Diagonal \
if (checkFallingDiagonal(row, col) == false){
return false;
}
if (checkRisingDiagonal(row, col) == false){
return false;
}
return true;
}
public void solve(int queensPlaced){
if (queensPlaced == 7){
this.print();
}
for (int col = 1; col < 9; col++) {
if (this.field[col-1] == 0) {
for (int row = 1; row < 9; row++) {
if (isPossible(row,col)) {
this.field[col-1] = row;
queensPlaced++;
this.solve(queensPlaced);
queensPlaced--;
this.field[col-1] = 0;
}
}
return;
}
}
}
}