Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 KB | None | 0 0
  1. package pgdp.games;
  2.  
  3. public class Penguin extends MiniJava {
  4.  
  5. public static final int LAND = 0;
  6. public static final int ICE = 1;
  7. public static final int WATER = 2;
  8. public static final int SHARK = 3;
  9. public static final int FISH = 4;
  10. //Variable die angibt, dass das Feld gerade geprüft wird!
  11. //Benötigt für Abbruchbedingung der Rekursion um Schleifen und StackOverFlow zu verhindern
  12. public static final int INCHECK = 5;
  13.  
  14. public static void printWorld(int[][] world, int pinguRow, int pinguColumn) {
  15.  
  16. for(int i = 0;i<world.length;i++) {
  17. for(int j = 0;j<world[i].length;j++) {
  18.  
  19. int value = world[i][j];
  20.  
  21. if(i == pinguRow && j== pinguColumn) {
  22. if(j == 0) {
  23. //Wenn es das erste Feld der Reihe ist
  24. switch(value) {
  25. case LAND: writeConsole("L(P)");break;
  26. case ICE: writeConsole("E(P)");break;
  27. case WATER: writeConsole("W(P)");break;
  28. case SHARK: writeConsole("H(P)");break;
  29. case FISH: writeConsole("F(P)");break;
  30. }
  31. }
  32. else if(j == world[i].length-1) {
  33. //Wenn Pingu am letzten Feld einer Spalte steht
  34. switch(value) {
  35. case LAND: write("\tL(P)");break;
  36. case ICE: write("\tE(P)");break;
  37. case WATER: write("\tW(P)");break;
  38. case SHARK: write("\tH(P)");break;
  39. case FISH: write("\tF(P)");break;
  40. }
  41. }
  42. else {
  43. //Wenn Pingu das n+1 Feld der Reihe ist
  44. switch(value) {
  45. case LAND: writeConsole("\tL(P)");break;
  46. case ICE: writeConsole("\tE(P)");break;
  47. case WATER: writeConsole("\tW(P)");break;
  48. case SHARK: writeConsole("\tH(P)");break;
  49. case FISH: writeConsole("\tF(P)");break;
  50. }
  51. }
  52. }
  53. else {
  54. if(j == 0) {
  55. switch(value) { //Erstes Feld der Reihe
  56. case LAND: writeConsole("L");break;
  57. case ICE: writeConsole("E");break;
  58. case WATER: writeConsole("W");break;
  59. case SHARK: writeConsole("H");break;
  60. case FISH: writeConsole("F");break;
  61. }
  62. }
  63. else if(j == world[i].length -1) {
  64. switch(value) { //Normale Behandlung
  65. case LAND: write("\tL");break;
  66. case ICE: write("\tE");break;
  67. case WATER: write("\tW");break;
  68. case SHARK: write("\tH");break;
  69. case FISH: write("\tF");break;
  70. }
  71.  
  72. }
  73. else {
  74. switch(value) { //Normale Behandlung
  75. case LAND: writeConsole("\tL");break;
  76. case ICE: writeConsole("\tE");break;
  77. case WATER: writeConsole("\tW");break;
  78. case SHARK: writeConsole("\tH");break;
  79. case FISH: writeConsole("\tF");break;
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
  86.  
  87. public static boolean isFishReachable(int[][] world, int pinguRow, int pinguColumn){
  88. //Aufruf eines rekursiven Suchalgorithmus
  89. if(walk(world,pinguRow,pinguColumn,new int[world.length][world[0].length]) >= 1) {
  90. return true;
  91. }
  92. else {
  93. return false;
  94. }
  95. }
  96.  
  97. public static int walk(int[][]world,int walkX,int walkY,int[][]cache) {
  98.  
  99. int fish = 0;
  100.  
  101. //Pingu versucht die Map zu verlassen
  102. if(walkX < 0 || walkY < 0 || walkX >= world.length || walkY >= world[0].length) {
  103. return 0;
  104. }
  105. //Wird das Feld bereits gecheckt? (Verhindert Schleifen)
  106. else if(cache[walkX][walkY] == INCHECK) {
  107. return 0;
  108. }
  109. //Pingu kann nicht auf einem Haifeld landen
  110. else if(world[walkX][walkY] == SHARK) {
  111. return 0;
  112. }
  113. //Landet Pingu hier wird der nächste Schritt ein diagonaler sein
  114. else if(world[walkX][walkY] == ICE) {
  115. int[][]directions = new int[][] {{1,1},{1,-1},{-1,1},{-1,-1}};
  116. for(int i = 0;i<directions.length;i++) {
  117. fish+=walk(world,walkX+directions[i][0],walkY+directions[i][1],cache);
  118. cache[walkX][walkY] = INCHECK;
  119. }
  120.  
  121. //Landet Pingu hier werden die nächsten 3 Schritte diagonale sein
  122. }
  123. else if(world[walkX][walkY] == WATER) {
  124. //Landet Pingu hier hat er den Fisch gefunden und die Frage damit beantwortet mit ja
  125. int[][]directions = new int[][] {{3,3},{3,-3},{-3,3},{-3,-3}};
  126. for(int i = 0;i<directions.length;i++) {
  127. fish+=walk(world,walkX+directions[i][0],walkY+directions[i][1],cache);
  128. cache[walkX][walkY] = INCHECK;
  129. }
  130. }
  131. //Landet Pingu hier hat er den Fisch gefunden und die Rekursion wird beendet
  132. else if(world[walkX][walkY] == FISH) {
  133. fish = 1;
  134. return fish;
  135. }
  136. else {
  137. int[][]directions = new int[][] {{1,0},{0,1},{-1,0},{0,-1}};
  138. for(int i = 0;i<directions.length;i++) {
  139. fish+=walk(world,walkX+directions[i][0],walkY+directions[i][1],cache);
  140. cache[walkX][walkY] = INCHECK;
  141. }
  142. }
  143. return fish;
  144. }
  145.  
  146. /**
  147. * Gibt die Beispielwelt Nr. 1 zurück.
  148. * Modifizieren Sie diese Methode nicht.
  149. * @return Eine Beispielwelt
  150. */
  151. public static int[][] generateExampleWorldOne(){
  152. 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}};
  153. }
  154.  
  155. /**
  156. * Gibt die Beispielwelt Nr. 2 zurück.
  157. * Modifizieren Sie diese Methode nicht.
  158. * @return Eine Beispielwelt
  159. */
  160. public static int[][] generateExampleWorldTwo(){
  161. return new int[][] {{0,0,0,2}, {0,0,0,1}, {0,1,3,4}, {3,4,3,3}};
  162. }
  163.  
  164. /**
  165. * Sie können die main-Methode verwenden, um Ihr Programm zu testen.
  166. */
  167. public static void main(String[] args){
  168. int pinguRow = 0;
  169. int pinguColumn = 0;
  170. int[][] world = generateExampleWorldOne();
  171. printWorld(world, pinguRow, pinguColumn);
  172. write(""+isFishReachable(world, pinguRow, pinguColumn));
  173. }
  174.  
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement