Created
June 15, 2011 09:58
-
-
Save Feh/1026822 to your computer and use it in GitHub Desktop.
Labyrinth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Assignment 9, exercise 3 | |
* Class representing a labyrinth | |
* Two constructors are already implemented, also a method to print out the labyrinth and its solution | |
* | |
* @author Lisa Julia Nebel Julius Plenz | |
*/ | |
// TODO Take a look how this class works. | |
// TODO Look at the docstrings, document your own functions | |
// TODO Implement two functions to solve the Labyrinth. You will find the stubs below | |
// (solve and solveRightHandRule) | |
// TODO Implement the third constructor that generates a labyrinth with given size and given probability | |
// test the solvability of 1000 labyrinths of size 10 by 10 with the parameters p=0.8 and p=0.9 | |
// (p stands for the probability of any entry in the matrix being 1) | |
public class Labyrinth{ | |
int[][] labyrinth; // labyrinth as defined in the assignment | |
int[][] solution; // solution to the labyrinth | |
Boolean solved; // Is the labyrinth already solved? | |
/** | |
* Construct a Labyrinth with default size | |
*/ | |
public Labyrinth(){ | |
this.labyrinth=makeRandomLabyrinth(4,0.8); | |
this.solved=false; | |
} | |
/** | |
* Construct a Labyrinth with given size and given "1"-probability | |
* Not yet implemented! | |
*/ | |
public Labyrinth(int size, double p){ | |
this.labyrinth = makeRandomLabyrinth(size,p); | |
} | |
public Labyrinth(int[][]labyrinth){ | |
this.labyrinth=labyrinth; | |
this.solved=false; | |
} | |
/** | |
* @param labyrinth size and probability for "1" | |
* | |
* @return a labyrinth (int[][]) | |
*/ | |
public static int[][] makeRandomLabyrinth(int size, double p){ | |
int[][] labyrinth=new int[size][size]; | |
for (int i1=0; i1<size; i1++) { | |
for (int i2=0; i2<size; i2++) { | |
double r = Math.random(); | |
labyrinth[i1][i2]=r<p?1:0; //Question: If r<p, then [i1][i2] = 1, else: then [i1][i2] = 0 | |
} | |
} | |
return labyrinth; | |
} | |
/** | |
* Prints out the Labyrinth and its solution, if possible! | |
*/ | |
public void printLabyrinth(){ // | |
for (int i1=0; i1<this.labyrinth.length; i1++) { | |
for (int i2=0; i2<this.labyrinth[i1].length; i2++) { | |
System.out.print(this.labyrinth[i1][i2]); | |
} | |
System.out.println(); | |
} | |
System.out.println(); | |
if(this.solved){ | |
for (int i1=0; i1<this.labyrinth.length; i1++) { | |
for (int i2=0; i2<this.labyrinth[i1].length; i2++) { | |
System.out.print(this.solution[i1][i2]); | |
} | |
System.out.println(); | |
} | |
} | |
} | |
public boolean solve(){ //Solve the labyrinth. Return true if the labyrinth is solvable, otherwise return false. change the solution attribute to the solution. | |
this.labyrinth = labyrinth; | |
if (labyrinth[0][0]==1){ | |
int i1 = 0; | |
int i2 = 0; | |
if (i1!= (this.labyrinth.length-1) && i2!=(this.labyrinth.length-1)){ | |
if (labyrinth[i1-1][i2]==1 && i1>0){ | |
labyrinth[i1-1][i2]=8; | |
i1 = i1-1; | |
} | |
else if (labyrinth[i1+1][i2]==1 && (i1+1)<this.labyrinth.length){ | |
labyrinth[i1+1][i2]=8; | |
i1 = i1+1; | |
} | |
else if (labyrinth[i1][i2-1]==1 && i2>0){ | |
labyrinth[i1][i2-1]=8; | |
i2 = i2-1; | |
} | |
else if (labyrinth[i1][i2+1]==1 && (i2+1)<this.labyrinth.length){ | |
labyrinth[i1][i2+1]=8; | |
i2 = i2+1; | |
} | |
else { | |
labyrinth[i1][i2]=0; | |
if (labyrinth[i1-1][i2]==8 && i1>0){ | |
i1 = i1-1; | |
} | |
else if (labyrinth[i1+1][i2]==8 && (i1+1)<this.labyrinth.length){ | |
i1 = i1+1; | |
} | |
else if (labyrinth[i1][i2-1]==8 && i2>0){ | |
i2 = i2-1; | |
} | |
else if (labyrinth[i1][i2+1]==8 && (i2+1)<this.labyrinth.length){ | |
i2 = i2+1; | |
} | |
} | |
} | |
} | |
return false; | |
} | |
public boolean solveRightHandRule(){ //implement the right-hand-rule explained in the assignment | |
return false; | |
} | |
public static void main(String args[]){ | |
System.out.println("Aufgabe 3a:"); | |
int[][] testLab={{1,0,1,1},{1,1,1,1},{0,1,0,1},{0,1,0,1}}; | |
Labyrinth L1=new Labyrinth(testLab); | |
L1.solve(); | |
L1.printLabyrinth(); | |
Labyrinth L2=new Labyrinth(); | |
L2.solve(); | |
L2.printLabyrinth(); | |
System.out.println("Aufgabe 3b:"); | |
Labyrinth L3=new Labyrinth(testLab); | |
L3.solveRightHandRule(); | |
L3.printLabyrinth(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Labyrinth.solve(Labyrinth.java:88)
at Labyrinth.main(Labyrinth.java:137)