Guest User

solokacka

a guest
Jan 4th, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. class Test {
  2.  
  3. static final int MAX_X = 7, MAX_Y = 7; //x: 0-6, y: 0-6
  4. static String[][] arrayPos = new String[MAX_X][MAX_Y];
  5. static int[][] possibleArray = new int[MAX_X][MAX_Y];
  6. static int[] oldPositionX = new int[MAX_X];
  7. static int[] oldPositionY = new int[MAX_Y];
  8. static int[] betweenX = new int[MAX_X];
  9. static int[] betweenY = new int[MAX_Y];
  10. static int possible = 0, counter = 0;
  11.  
  12. public static void main(String args[]) {
  13.  
  14. //Lade das Spiel und speichere die einzelnen Zeichen in ein Array
  15. In.open("start.txt");
  16. for(int y = 0; y < MAX_Y; y++) {
  17.  
  18. String s = In.readLine();
  19. String[] row = s.split("");
  20.  
  21. for(int x = 0; x < MAX_X; x++) {
  22. arrayPos[x][y] = row[x];
  23. }
  24. }
  25. In.close();
  26.  
  27. //Spiele das Spiel
  28. playGame();
  29. }
  30.  
  31. static void playGame() {
  32.  
  33. //Setze den possible-Counter zurück...
  34. if(possible > 0) possible = 0;
  35.  
  36. //Zeige das neue Spielfeld an
  37. for(int y = 0; y < MAX_Y; y++) {
  38. for(int x = 0; x < MAX_X; x++) {
  39. Out.print(arrayPos[x][y]);
  40. if(x == MAX_X-1) Out.println();
  41. }
  42. }
  43.  
  44. //Setze alle Werte zurück für die nächsten Möglichkeiten...
  45. for(int y = 0; y < MAX_Y; y++) {
  46. for(int x = 0; x < MAX_X; x++) {
  47. if(possibleArray[x][y] >= 0) possibleArray[x][y] = -1;
  48. betweenX[x] = 0;
  49. betweenY[y] = 0;
  50. oldPositionX[x] = 0;
  51. oldPositionY[y] = 0;
  52. }
  53. }
  54.  
  55. //Kalkuliere die Möglichkeiten
  56. for(int y = 0; y < MAX_Y; y++) {
  57. for(int x = 0; x < MAX_X; x++) {
  58. if(arrayPos[x][y].equals(" ")) continue;
  59. if(arrayPos[x][y].equals("O")) {
  60. calcPossibleSteps(x, y);
  61. }
  62. }
  63. }
  64.  
  65. if(possible == 0) { //Wenn keine Möglichkeit mehr vorhanden, beende das Spiel...
  66. Out.println("Keine weiteren Moeglichkeiten vorhanden! Beende Spiel...");
  67. } else {
  68.  
  69. //Generiere zufällige Zahl zwischen 1 und der möglichen Anzahl Wege
  70. int rand = (int) (Math.random() * possible);
  71.  
  72. //Suche den Random Wert im possibleArray raus und verändere das Spielfeld
  73. for(int y = 0; y < MAX_Y; y++) {
  74. for(int x = 0; x < MAX_X; x++) {
  75. if(possibleArray[x][y] == -1) continue;
  76. if(possibleArray[x][y] == rand) {
  77. Out.println("Waehle Zug ID "+rand+": Von ("+x+", "+y+") zu ("+oldPositionX[x]+", "+oldPositionY[y]+")");
  78. arrayPos[x][y] = "O";
  79. arrayPos[betweenX[x]][betweenY[y]] = "O";
  80. arrayPos[oldPositionX[x]][oldPositionY[y]] = "X";
  81.  
  82. break;
  83. }
  84. }
  85. }
  86. Out.println();
  87.  
  88. for(int y = 0; y < MAX_Y; y++) {
  89. for(int x = 0; x < MAX_X; x++) {
  90. if(arrayPos[x][y].equals(" ")) Out.print(" ");
  91. else Out.print(" " +possibleArray[x][y]);
  92.  
  93. if(x == MAX_X-1) Out.println();
  94. }
  95. }
  96.  
  97. Out.println();
  98.  
  99. //Spiele erneut...
  100. playGame();
  101. }
  102. }
  103.  
  104. static void calcPossibleSteps(int x, int y) {
  105.  
  106. //Schauen, ob man von links aus ziehen kann
  107. if(x-2 >= 0 && arrayPos[x-2][y].equals("X")) {
  108. Out.println("Zug "+possible+": Von ("+(x-2)+", "+y+") zu ("+x+", "+y+")");
  109.  
  110. possibleArray[x-2][y] = possible;
  111.  
  112. betweenX[x-2] = x-1;
  113. betweenY[y] = y;
  114.  
  115. oldPositionX[x-2] = x;
  116. oldPositionY[y] = y;
  117.  
  118. possible++;
  119. }
  120. //Schauen, ob man von rechts aus ziehen kann
  121. if(x+2 < MAX_X && arrayPos[x+2][y].equals("X")) {
  122. Out.println("Zug "+possible+": Von ("+(x+2)+", "+y+") zu ("+x+", "+y+")");
  123.  
  124. possibleArray[x+2][y] = possible;
  125.  
  126. betweenX[x+2] = x+1;
  127. betweenY[y] = y;
  128.  
  129. oldPositionX[x+2] = x;
  130. oldPositionY[y] = y;
  131.  
  132. possible++;
  133. }
  134. //Schauen, ob man von oben aus ziehen kann
  135. if(y-2 >= 0 && arrayPos[x][y-2].equals("X")) {
  136. Out.println("Zug "+possible+": Von ("+x+", "+(y-2)+") zu ("+x+", "+y+")");
  137.  
  138. possibleArray[x][y-2] = possible;
  139.  
  140. betweenX[x] = x;
  141. betweenY[y-2] = y-1;
  142.  
  143. oldPositionY[y-2] = y;
  144. oldPositionX[x] = x;
  145.  
  146. possible++;
  147. }
  148. //Schauen, ob man von unten aus ziehen kann
  149. if(y+2 < MAX_Y && arrayPos[x][y+2].equals("X")) {
  150. Out.println("Zug "+possible+": Von ("+x+", "+(y+2)+") zu ("+x+", "+y+")");
  151.  
  152. possibleArray[x][y+2] = possible;
  153.  
  154. betweenX[x] = x;
  155. betweenY[y+2] = y+1;
  156.  
  157. oldPositionY[y+2] = y;
  158. oldPositionX[x] = x;
  159.  
  160. possible++;
  161. }
  162. }
  163.  
  164. }
Advertisement
Add Comment
Please, Sign In to add comment