Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pgdp.games;
- public class Penguin extends MiniJava {
- public static final int LAND = 0;
- public static final int ICE = 1;
- public static final int WATER = 2;
- public static final int SHARK = 3;
- public static final int FISH = 4;
- //Variable die angibt, dass das Feld gerade geprüft wird!
- //Benötigt für Abbruchbedingung der Rekursion um Schleifen und StackOverFlow zu verhindern
- public static final int INCHECK = 5;
- public static void printWorld(int[][] world, int pinguRow, int pinguColumn) {
- for(int i = 0;i<world.length;i++) {
- for(int j = 0;j<world[i].length;j++) {
- int value = world[i][j];
- if(i == pinguRow && j== pinguColumn) {
- if(j == 0) {
- //Wenn es das erste Feld der Reihe ist
- switch(value) {
- case LAND: writeConsole("L(P)");break;
- case ICE: writeConsole("E(P)");break;
- case WATER: writeConsole("W(P)");break;
- case SHARK: writeConsole("H(P)");break;
- case FISH: writeConsole("F(P)");break;
- }
- }
- else if(j == world[i].length-1) {
- //Wenn Pingu am letzten Feld einer Spalte steht
- switch(value) {
- case LAND: write("\tL(P)");break;
- case ICE: write("\tE(P)");break;
- case WATER: write("\tW(P)");break;
- case SHARK: write("\tH(P)");break;
- case FISH: write("\tF(P)");break;
- }
- }
- else {
- //Wenn Pingu das n+1 Feld der Reihe ist
- switch(value) {
- case LAND: writeConsole("\tL(P)");break;
- case ICE: writeConsole("\tE(P)");break;
- case WATER: writeConsole("\tW(P)");break;
- case SHARK: writeConsole("\tH(P)");break;
- case FISH: writeConsole("\tF(P)");break;
- }
- }
- }
- else {
- if(j == 0) {
- switch(value) { //Erstes Feld der Reihe
- case LAND: writeConsole("L");break;
- case ICE: writeConsole("E");break;
- case WATER: writeConsole("W");break;
- case SHARK: writeConsole("H");break;
- case FISH: writeConsole("F");break;
- }
- }
- else if(j == world[i].length -1) {
- switch(value) { //Normale Behandlung
- case LAND: write("\tL");break;
- case ICE: write("\tE");break;
- case WATER: write("\tW");break;
- case SHARK: write("\tH");break;
- case FISH: write("\tF");break;
- }
- }
- else {
- switch(value) { //Normale Behandlung
- case LAND: writeConsole("\tL");break;
- case ICE: writeConsole("\tE");break;
- case WATER: writeConsole("\tW");break;
- case SHARK: writeConsole("\tH");break;
- case FISH: writeConsole("\tF");break;
- }
- }
- }
- }
- }
- }
- public static boolean isFishReachable(int[][] world, int pinguRow, int pinguColumn){
- //Aufruf eines rekursiven Suchalgorithmus
- if(walk(world,pinguRow,pinguColumn,new int[world.length][world[0].length]) >= 1) {
- return true;
- }
- else {
- return false;
- }
- }
- public static int walk(int[][]world,int walkX,int walkY,int[][]cache) {
- int fish = 0;
- //Pingu versucht die Map zu verlassen
- if(walkX < 0 || walkY < 0 || walkX >= world.length || walkY >= world[0].length) {
- return 0;
- }
- //Wird das Feld bereits gecheckt? (Verhindert Schleifen)
- else if(cache[walkX][walkY] == INCHECK) {
- return 0;
- }
- //Pingu kann nicht auf einem Haifeld landen
- else if(world[walkX][walkY] == SHARK) {
- return 0;
- }
- //Landet Pingu hier wird der nächste Schritt ein diagonaler sein
- else if(world[walkX][walkY] == ICE) {
- int[][]directions = new int[][] {{1,1},{1,-1},{-1,1},{-1,-1}};
- for(int i = 0;i<directions.length;i++) {
- fish+=walk(world,walkX+directions[i][0],walkY+directions[i][1],cache);
- cache[walkX][walkY] = INCHECK;
- }
- //Landet Pingu hier werden die nächsten 3 Schritte diagonale sein
- }
- else if(world[walkX][walkY] == WATER) {
- //Landet Pingu hier hat er den Fisch gefunden und die Frage damit beantwortet mit ja
- int[][]directions = new int[][] {{3,3},{3,-3},{-3,3},{-3,-3}};
- for(int i = 0;i<directions.length;i++) {
- fish+=walk(world,walkX+directions[i][0],walkY+directions[i][1],cache);
- cache[walkX][walkY] = INCHECK;
- }
- }
- //Landet Pingu hier hat er den Fisch gefunden und die Rekursion wird beendet
- else if(world[walkX][walkY] == FISH) {
- fish = 1;
- return fish;
- }
- else {
- int[][]directions = new int[][] {{1,0},{0,1},{-1,0},{0,-1}};
- for(int i = 0;i<directions.length;i++) {
- fish+=walk(world,walkX+directions[i][0],walkY+directions[i][1],cache);
- cache[walkX][walkY] = INCHECK;
- }
- }
- return fish;
- }
- /**
- * Gibt die Beispielwelt Nr. 1 zurück.
- * Modifizieren Sie diese Methode nicht.
- * @return Eine Beispielwelt
- */
- public static int[][] generateExampleWorldOne(){
- return new int[][] {{2,3,3,3,3,3}, {3,0,3,3,4,3}, {3,3,3,3,3,1}, {3,3,3,0,1,3}, {3,3,3,3,3,3}};
- }
- /**
- * Gibt die Beispielwelt Nr. 2 zurück.
- * Modifizieren Sie diese Methode nicht.
- * @return Eine Beispielwelt
- */
- public static int[][] generateExampleWorldTwo(){
- return new int[][] {{0,0,0,2}, {0,0,0,1}, {0,1,3,4}, {3,4,3,3}};
- }
- /**
- * Sie können die main-Methode verwenden, um Ihr Programm zu testen.
- */
- public static void main(String[] args){
- int pinguRow = 0;
- int pinguColumn = 0;
- int[][] world = generateExampleWorldOne();
- printWorld(world, pinguRow, pinguColumn);
- write(""+isFishReachable(world, pinguRow, pinguColumn));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement