Check Horizontal & Vertical work
This commit is contained in:
parent
3aa56bd3fb
commit
7c7a91a762
@ -4,7 +4,9 @@ public class Demo {
|
||||
public static void main(String[] args) {
|
||||
Queenproblem Q = new Queenproblem();
|
||||
System.out.println(Q.field[2]);
|
||||
Q.setField(new int[] {0,1,2,3,4,5,6,7});
|
||||
Q.setField(new int[] {0,0,2,3,4,5,0,0});
|
||||
Q.print();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,8 @@ package part5.aufg2;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Queenproblem {
|
||||
// 0 means empty
|
||||
// 1-8 is the y position of the Queen
|
||||
int[] field;
|
||||
|
||||
public Queenproblem(){
|
||||
@ -21,8 +23,12 @@ public class Queenproblem {
|
||||
}
|
||||
|
||||
for (int i=0; i<8; i++) {
|
||||
int index = this.field[i];
|
||||
field[i][index] = 1;
|
||||
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
|
||||
}catch (IndexOutOfBoundsException e){
|
||||
System.out.println("Ignored Index Error while making it 2D");
|
||||
}
|
||||
}
|
||||
|
||||
return field;
|
||||
@ -35,13 +41,25 @@ public class Queenproblem {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isPossible(int x, int y){
|
||||
// Horizontal
|
||||
for (int value: this.field) {
|
||||
if (value == y){
|
||||
public boolean checkVertical(int col){
|
||||
if (this.field[col-1] != 0){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean checkHorizontal(int col){
|
||||
for (int colFilled : this.field) {
|
||||
if (colFilled == col){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean isPossible(int row, int col){
|
||||
int x = --row;
|
||||
int y = --col;
|
||||
|
||||
// Vertical
|
||||
if (this.field[x] != 0)
|
||||
@ -50,9 +68,14 @@ public class Queenproblem {
|
||||
// Diagonal \
|
||||
for (int i=0; i<8; i++){
|
||||
try{
|
||||
|
||||
if (this.field[x+i] == y+i){
|
||||
return false;
|
||||
}
|
||||
if (this.field[x-i] == y-i){
|
||||
return false;
|
||||
}
|
||||
}catch (IndexOutOfBoundsException e){
|
||||
System.out.println(e);
|
||||
System.out.println("Ignored index Error with Diagonal Possibility Check");
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user