Compare commits

...

4 Commits

Author SHA1 Message Date
Matti
4286d01786 checkRising is the filthy Criminal 2024-05-20 21:54:30 +02:00
Matti
f53f29200e Almost works, Display at 7 works, doesn't stop when finishing / print solution 2024-05-20 21:25:17 +02:00
Matti
7c7a91a762 Check Horizontal & Vertical work 2024-05-20 17:10:12 +02:00
Matti
3aa56bd3fb Recursive Queenproblem 2024-05-20 15:52:14 +02:00
3 changed files with 194 additions and 1 deletions

23
src/part5/aufg2/Demo.java Normal file
View File

@ -0,0 +1,23 @@
package part5.aufg2;
public class Demo {
public static void main(String[] args) {
Queenproblem Q = new Queenproblem();
Q.setField(new int[]{2,4,6,8,1,3,5,0});
Q.print();
System.out.println("============= ALL");
System.out.println(Q.isPossible(7,8));
System.out.println("============= Horiz");
System.out.println(Q.checkHorizontal(7));
System.out.println("============= Vert");
System.out.println(Q.checkVertical(8));
System.out.println("============= Rising");
System.out.println(Q.checkRisingDiagonal(7,8));
System.out.println("============= Falling");
System.out.println(Q.checkFallingDiagonal(7,8));
}
}

View File

@ -0,0 +1,170 @@
package part5.aufg2;
import java.util.Arrays;
public class Queenproblem {
// 0 means empty
// 1-8 is the y position of the Queen
int[] field;
int[][] field2D;
public Queenproblem(){
this.field = new int[8];
}
public void setField(int[] field) {
this.field = field;
}
private int[][] make2D(){
int[][] field = new int[8][8];
for (int[] row : field ){
Arrays.fill(row, 0);
}
for (int i=0; i<8; i++) {
try {
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
}catch (IndexOutOfBoundsException e){
;
//System.out.println("Ignored Index Error while making it 2D");
}
}
return field;
}
public void print(){
int[][] field = make2D();
for (int[] row:field) {
System.out.println(Arrays.toString(row));
}
System.out.println();
}
public boolean checkVertical(int col){
if (this.field[col-1] != 0){
return false;
}
return true;
}
public boolean checkHorizontal(int row){
for (int colFilled : this.field) {
if (colFilled == row){
return false;
}
}
return true;
}
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);
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){
; // 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){
field2D = make2D();
if (queensPlaced == 6){
System.out.println("solved 6");
}
for (int col = 1; col < 9; col++) {
if(col == 8) {
System.out.println("solving the last one");
}
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;
}
}
this.print();
}
}

View File

@ -1,4 +1,4 @@
package part5; package part5.aufg2;
// I know this is not recursive // I know this is not recursive
// Shut up // Shut up