Labirinti
This commit is contained in:
127
LabirintoTest/src/MazeSolver.java
Normal file
127
LabirintoTest/src/MazeSolver.java
Normal file
@@ -0,0 +1,127 @@
|
||||
import java.util.*;
|
||||
|
||||
public class MazeSolver {
|
||||
private static final int ROWS = 15;
|
||||
private static final int COLS = 25;
|
||||
private static final char WALL = '█';
|
||||
private static final char PATH = ' ';
|
||||
private static final char START = 'S';
|
||||
private static final char END = 'E';
|
||||
private static final char SOLUTION = '·';
|
||||
private char[][] maze;
|
||||
private boolean[][] visited;
|
||||
private int startRow, startCol, endRow, endCol;
|
||||
public MazeSolver() {
|
||||
maze = new char[ROWS][COLS];
|
||||
visited = new boolean[ROWS][COLS];
|
||||
generateMaze();
|
||||
}
|
||||
private void generateMaze() {
|
||||
for (int i = 0; i < ROWS; i++) {
|
||||
for (int j = 0; j < COLS; j++) {
|
||||
maze[i][j] = WALL;
|
||||
}
|
||||
}
|
||||
Random rand = new Random();
|
||||
Stack<int[]> stack = new Stack<>();
|
||||
startRow = 1;
|
||||
startCol = 1;
|
||||
maze[startRow][startCol] = PATH;
|
||||
stack.push(new int[]{startRow, startCol});
|
||||
int[][] directions = {{-2, 0}, {2, 0}, {0, -2}, {0, 2}};
|
||||
while (!stack.isEmpty()) {
|
||||
int[] current = stack.peek();
|
||||
int row = current[0];
|
||||
int col = current[1];
|
||||
List<int[]> neighbors = new ArrayList<>();
|
||||
for (int[] dir : directions) {
|
||||
int newRow = row + dir[0];
|
||||
int newCol = col + dir[1];
|
||||
|
||||
if (newRow > 0 && newRow < ROWS - 1 &&
|
||||
newCol > 0 && newCol < COLS - 1 &&
|
||||
maze[newRow][newCol] == WALL) {
|
||||
neighbors.add(new int[]{newRow, newCol, row + dir[0]/2, col + dir[1]/2});
|
||||
}
|
||||
}
|
||||
if (!neighbors.isEmpty()) {
|
||||
int[] next = neighbors.get(rand.nextInt(neighbors.size()));
|
||||
maze[next[2]][next[3]] = PATH; // Rompi il muro
|
||||
maze[next[0]][next[1]] = PATH;
|
||||
stack.push(new int[]{next[0], next[1]});
|
||||
} else {
|
||||
stack.pop();
|
||||
}
|
||||
}
|
||||
endRow = ROWS - 2;
|
||||
endCol = COLS - 2;
|
||||
maze[startRow][startCol] = START;
|
||||
maze[endRow][endCol] = END;
|
||||
}
|
||||
|
||||
private void printMaze() {
|
||||
System.out.println("\n╔" + "═".repeat(COLS) + "╗");
|
||||
for (int i = 0; i < ROWS; i++) {
|
||||
System.out.print("║");
|
||||
for (int j = 0; j < COLS; j++) {
|
||||
System.out.print(maze[i][j]);
|
||||
}
|
||||
System.out.println("║");
|
||||
}
|
||||
System.out.println("╚" + "═".repeat(COLS) + "╝");
|
||||
}
|
||||
|
||||
private boolean solveMaze(int row, int col, List<int[]> path) {
|
||||
if (row < 0 || row >= ROWS || col < 0 || col >= COLS) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (maze[row][col] == WALL || visited[row][col]) {
|
||||
return false;
|
||||
}
|
||||
visited[row][col] = true;
|
||||
path.add(new int[]{row, col});
|
||||
if (row == endRow && col == endCol) {
|
||||
return true;
|
||||
}
|
||||
if (solveMaze(row - 1, col, path) ||
|
||||
solveMaze(row + 1, col, path) ||
|
||||
solveMaze(row, col - 1, path) ||
|
||||
solveMaze(row, col + 1, path)) {
|
||||
return true;
|
||||
}
|
||||
path.remove(path.size() - 1);
|
||||
return false;
|
||||
}
|
||||
private void markSolution(List<int[]> path) {
|
||||
for (int[] pos : path) {
|
||||
int row = pos[0];
|
||||
int col = pos[1];
|
||||
if (maze[row][col] != START && maze[row][col] != END) {
|
||||
maze[row][col] = SOLUTION;
|
||||
}
|
||||
}
|
||||
}
|
||||
public void solve() {
|
||||
System.out.println("LABIRINTO ORIGINALE:");
|
||||
printMaze();
|
||||
List<int[]> path = new ArrayList<>();
|
||||
visited = new boolean[ROWS][COLS];
|
||||
System.out.println("\nRisoluzione in corso...");
|
||||
if (solveMaze(startRow, startCol, path)) {
|
||||
markSolution(path);
|
||||
System.out.println("\nLABIRINTO RISOLTO:");
|
||||
printMaze();
|
||||
System.out.println("\nPercorso trovato! Lunghezza: " + path.size() + " passi");
|
||||
} else {
|
||||
System.out.println("\nNessuna soluzione trovata!");
|
||||
}
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
System.out.println("=== GENERATORE E RISOLUTORE DI LABIRINTI ===");
|
||||
System.out.println("Legenda: " + START + " = Inizio, " + END + " = Fine, " +
|
||||
SOLUTION + " = Soluzione");
|
||||
MazeSolver solver = new MazeSolver();
|
||||
solver.solve();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user