Compare commits

..

No commits in common. "4286d01786a7a881c1a81f2166c39bd2dc629bc8" and "b0b248ca9d7fce89be433d37366a6cc50832e572" have entirely different histories.

3 changed files with 1 additions and 194 deletions

View File

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

View File

@ -1,23 +0,0 @@
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

@ -1,170 +0,0 @@
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();
}
}