Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Test {
- static final int MAX_X = 7, MAX_Y = 7; //x: 0-6, y: 0-6
- static String[][] arrayPos = new String[MAX_X][MAX_Y];
- static int[][] possibleArray = new int[MAX_X][MAX_Y];
- static int[] oldPositionX = new int[MAX_X];
- static int[] oldPositionY = new int[MAX_Y];
- static int[] betweenX = new int[MAX_X];
- static int[] betweenY = new int[MAX_Y];
- static int possible = 0, counter = 0;
- public static void main(String args[]) {
- //Lade das Spiel und speichere die einzelnen Zeichen in ein Array
- In.open("start.txt");
- for(int y = 0; y < MAX_Y; y++) {
- String s = In.readLine();
- String[] row = s.split("");
- for(int x = 0; x < MAX_X; x++) {
- arrayPos[x][y] = row[x];
- }
- }
- In.close();
- //Spiele das Spiel
- playGame();
- }
- static void playGame() {
- //Setze den possible-Counter zurück...
- if(possible > 0) possible = 0;
- //Zeige das neue Spielfeld an
- for(int y = 0; y < MAX_Y; y++) {
- for(int x = 0; x < MAX_X; x++) {
- Out.print(arrayPos[x][y]);
- if(x == MAX_X-1) Out.println();
- }
- }
- //Setze alle Werte zurück für die nächsten Möglichkeiten...
- for(int y = 0; y < MAX_Y; y++) {
- for(int x = 0; x < MAX_X; x++) {
- if(possibleArray[x][y] >= 0) possibleArray[x][y] = -1;
- betweenX[x] = 0;
- betweenY[y] = 0;
- oldPositionX[x] = 0;
- oldPositionY[y] = 0;
- }
- }
- //Kalkuliere die Möglichkeiten
- for(int y = 0; y < MAX_Y; y++) {
- for(int x = 0; x < MAX_X; x++) {
- if(arrayPos[x][y].equals(" ")) continue;
- if(arrayPos[x][y].equals("O")) {
- calcPossibleSteps(x, y);
- }
- }
- }
- if(possible == 0) { //Wenn keine Möglichkeit mehr vorhanden, beende das Spiel...
- Out.println("Keine weiteren Moeglichkeiten vorhanden! Beende Spiel...");
- } else {
- //Generiere zufällige Zahl zwischen 1 und der möglichen Anzahl Wege
- int rand = (int) (Math.random() * possible);
- //Suche den Random Wert im possibleArray raus und verändere das Spielfeld
- for(int y = 0; y < MAX_Y; y++) {
- for(int x = 0; x < MAX_X; x++) {
- if(possibleArray[x][y] == -1) continue;
- if(possibleArray[x][y] == rand) {
- Out.println("Waehle Zug ID "+rand+": Von ("+x+", "+y+") zu ("+oldPositionX[x]+", "+oldPositionY[y]+")");
- arrayPos[x][y] = "O";
- arrayPos[betweenX[x]][betweenY[y]] = "O";
- arrayPos[oldPositionX[x]][oldPositionY[y]] = "X";
- break;
- }
- }
- }
- Out.println();
- for(int y = 0; y < MAX_Y; y++) {
- for(int x = 0; x < MAX_X; x++) {
- if(arrayPos[x][y].equals(" ")) Out.print(" ");
- else Out.print(" " +possibleArray[x][y]);
- if(x == MAX_X-1) Out.println();
- }
- }
- Out.println();
- //Spiele erneut...
- playGame();
- }
- }
- static void calcPossibleSteps(int x, int y) {
- //Schauen, ob man von links aus ziehen kann
- if(x-2 >= 0 && arrayPos[x-2][y].equals("X")) {
- Out.println("Zug "+possible+": Von ("+(x-2)+", "+y+") zu ("+x+", "+y+")");
- possibleArray[x-2][y] = possible;
- betweenX[x-2] = x-1;
- betweenY[y] = y;
- oldPositionX[x-2] = x;
- oldPositionY[y] = y;
- possible++;
- }
- //Schauen, ob man von rechts aus ziehen kann
- if(x+2 < MAX_X && arrayPos[x+2][y].equals("X")) {
- Out.println("Zug "+possible+": Von ("+(x+2)+", "+y+") zu ("+x+", "+y+")");
- possibleArray[x+2][y] = possible;
- betweenX[x+2] = x+1;
- betweenY[y] = y;
- oldPositionX[x+2] = x;
- oldPositionY[y] = y;
- possible++;
- }
- //Schauen, ob man von oben aus ziehen kann
- if(y-2 >= 0 && arrayPos[x][y-2].equals("X")) {
- Out.println("Zug "+possible+": Von ("+x+", "+(y-2)+") zu ("+x+", "+y+")");
- possibleArray[x][y-2] = possible;
- betweenX[x] = x;
- betweenY[y-2] = y-1;
- oldPositionY[y-2] = y;
- oldPositionX[x] = x;
- possible++;
- }
- //Schauen, ob man von unten aus ziehen kann
- if(y+2 < MAX_Y && arrayPos[x][y+2].equals("X")) {
- Out.println("Zug "+possible+": Von ("+x+", "+(y+2)+") zu ("+x+", "+y+")");
- possibleArray[x][y+2] = possible;
- betweenX[x] = x;
- betweenY[y+2] = y+1;
- oldPositionY[y+2] = y;
- oldPositionX[x] = x;
- possible++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment