Labirinti
This commit is contained in:
30
LabirintoGUI/.gitignore
vendored
Normal file
30
LabirintoGUI/.gitignore
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
### IntelliJ IDEA ###
|
||||
out/
|
||||
!**/src/main/**/out/
|
||||
!**/src/test/**/out/
|
||||
.kotlin
|
||||
|
||||
### Eclipse ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
bin/
|
||||
!**/src/main/**/bin/
|
||||
!**/src/test/**/bin/
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
|
||||
### Mac OS ###
|
||||
.DS_Store
|
||||
8
LabirintoGUI/.idea/.gitignore
generated
vendored
Normal file
8
LabirintoGUI/.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
6
LabirintoGUI/.idea/misc.xml
generated
Normal file
6
LabirintoGUI/.idea/misc.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
8
LabirintoGUI/.idea/modules.xml
generated
Normal file
8
LabirintoGUI/.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/LabirintoGUI.iml" filepath="$PROJECT_DIR$/LabirintoGUI.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
6
LabirintoGUI/.idea/vcs.xml
generated
Normal file
6
LabirintoGUI/.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
11
LabirintoGUI/LabirintoGUI.iml
Normal file
11
LabirintoGUI/LabirintoGUI.iml
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
276
LabirintoGUI/src/MazeGame.java
Normal file
276
LabirintoGUI/src/MazeGame.java
Normal file
@@ -0,0 +1,276 @@
|
||||
import javax.swing.*;
|
||||
import javax.swing.Timer;
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.util.*;
|
||||
|
||||
public class MazeGame extends JFrame {
|
||||
private static final int ROWS = 21;
|
||||
private static final int COLS = 31;
|
||||
private static final int CELL_SIZE = 25;
|
||||
|
||||
private static final int WALL = 0;
|
||||
private static final int PATH = 1;
|
||||
private static final int END = 3;
|
||||
private static final int PLAYER = 4;
|
||||
|
||||
private final int[][] maze;
|
||||
private int playerRow, playerCol;
|
||||
private int endRow, endCol;
|
||||
private MazePanel mazePanel;
|
||||
private JLabel statusLabel;
|
||||
private JLabel movesLabel;
|
||||
private int moveCount = 0;
|
||||
|
||||
public MazeGame() {
|
||||
setTitle("Gioco del Labirinto");
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setResizable(false);
|
||||
|
||||
maze = new int[ROWS][COLS];
|
||||
generateMaze();
|
||||
|
||||
setupUI();
|
||||
|
||||
pack();
|
||||
setLocationRelativeTo(null);
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
private void setupUI() {
|
||||
setLayout(new BorderLayout());
|
||||
|
||||
// Panel superiore con informazioni
|
||||
JPanel topPanel = new JPanel(new GridLayout(2, 1));
|
||||
topPanel.setBackground(new Color(44, 62, 80));
|
||||
topPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
|
||||
|
||||
statusLabel = new JLabel("Raggiungi la bandiera! Usa le frecce per muoverti", SwingConstants.CENTER);
|
||||
statusLabel.setFont(new Font("Arial", Font.BOLD, 16));
|
||||
statusLabel.setForeground(Color.WHITE);
|
||||
|
||||
movesLabel = new JLabel("Mosse: 0", SwingConstants.CENTER);
|
||||
movesLabel.setFont(new Font("Arial", Font.PLAIN, 14));
|
||||
movesLabel.setForeground(new Color(52, 152, 219));
|
||||
|
||||
topPanel.add(statusLabel);
|
||||
topPanel.add(movesLabel);
|
||||
|
||||
// Panel del labirinto
|
||||
mazePanel = new MazePanel();
|
||||
mazePanel.setFocusable(true);
|
||||
mazePanel.addKeyListener(new KeyAdapter() {
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
handleKeyPress(e.getKeyCode());
|
||||
}
|
||||
});
|
||||
|
||||
// Panel inferiore con pulsanti
|
||||
JPanel bottomPanel = new JPanel();
|
||||
bottomPanel.setBackground(new Color(44, 62, 80));
|
||||
bottomPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
|
||||
|
||||
JButton newGameButton = new JButton("Nuovo Gioco");
|
||||
newGameButton.setFont(new Font("Arial", Font.BOLD, 14));
|
||||
newGameButton.setBackground(new Color(46, 204, 113));
|
||||
newGameButton.setForeground(Color.WHITE);
|
||||
newGameButton.setFocusPainted(false);
|
||||
newGameButton.addActionListener(e -> newGame());
|
||||
|
||||
bottomPanel.add(newGameButton);
|
||||
|
||||
add(topPanel, BorderLayout.NORTH);
|
||||
add(mazePanel, BorderLayout.CENTER);
|
||||
add(bottomPanel, BorderLayout.SOUTH);
|
||||
}
|
||||
|
||||
private void generateMaze() {
|
||||
// Inizializza con muri
|
||||
for (int i = 0; i < ROWS; i++) {
|
||||
for (int j = 0; j < COLS; j++) {
|
||||
maze[i][j] = WALL;
|
||||
}
|
||||
}
|
||||
|
||||
// Genera percorsi usando DFS
|
||||
Random rand = new Random();
|
||||
Stack<int[]> stack = new Stack<>();
|
||||
|
||||
int startRow = 1;
|
||||
int 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];
|
||||
|
||||
java.util.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;
|
||||
maze[next[0]][next[1]] = PATH;
|
||||
stack.push(new int[]{next[0], next[1]});
|
||||
} else {
|
||||
stack.pop();
|
||||
}
|
||||
}
|
||||
|
||||
// Posiziona giocatore e fine
|
||||
playerRow = 1;
|
||||
playerCol = 1;
|
||||
endRow = ROWS - 2;
|
||||
endCol = COLS - 2;
|
||||
|
||||
maze[playerRow][playerCol] = PLAYER;
|
||||
maze[endRow][endCol] = END;
|
||||
}
|
||||
|
||||
private void handleKeyPress(int keyCode) {
|
||||
int newRow = playerRow;
|
||||
int newCol = playerCol;
|
||||
|
||||
switch (keyCode) {
|
||||
case KeyEvent.VK_UP:
|
||||
newRow--;
|
||||
break;
|
||||
case KeyEvent.VK_DOWN:
|
||||
newRow++;
|
||||
break;
|
||||
case KeyEvent.VK_LEFT:
|
||||
newCol--;
|
||||
break;
|
||||
case KeyEvent.VK_RIGHT:
|
||||
newCol++;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
// Verifica se la mossa è valida
|
||||
if (newRow >= 0 && newRow < ROWS && newCol >= 0 && newCol < COLS) {
|
||||
if (maze[newRow][newCol] != WALL) {
|
||||
// Muovi il giocatore
|
||||
maze[playerRow][playerCol] = PATH;
|
||||
playerRow = newRow;
|
||||
playerCol = newCol;
|
||||
|
||||
// Verifica se ha raggiunto la fine
|
||||
if (playerRow == endRow && playerCol == endCol) {
|
||||
maze[playerRow][playerCol] = PLAYER;
|
||||
mazePanel.repaint();
|
||||
showVictory();
|
||||
} else {
|
||||
maze[playerRow][playerCol] = PLAYER;
|
||||
moveCount++;
|
||||
movesLabel.setText("Mosse: " + moveCount);
|
||||
mazePanel.repaint();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void showVictory() {
|
||||
statusLabel.setText("🎉 Complimenti! Hai vinto in " + moveCount + " mosse! 🎉");
|
||||
statusLabel.setForeground(new Color(46, 204, 113));
|
||||
|
||||
Timer timer = new Timer(3000, e -> {
|
||||
int choice = JOptionPane.showConfirmDialog(
|
||||
this,
|
||||
"Hai completato il labirinto in " + moveCount + " mosse!\nVuoi giocare di nuovo?",
|
||||
"Vittoria!",
|
||||
JOptionPane.YES_NO_OPTION,
|
||||
JOptionPane.INFORMATION_MESSAGE
|
||||
);
|
||||
|
||||
if (choice == JOptionPane.YES_OPTION) {
|
||||
newGame();
|
||||
}
|
||||
});
|
||||
timer.setRepeats(false);
|
||||
timer.start();
|
||||
}
|
||||
|
||||
private void newGame() {
|
||||
moveCount = 0;
|
||||
movesLabel.setText("Mosse: 0");
|
||||
statusLabel.setText("Raggiungi la bandiera! Usa le frecce per muoverti");
|
||||
statusLabel.setForeground(Color.WHITE);
|
||||
generateMaze();
|
||||
mazePanel.repaint();
|
||||
mazePanel.requestFocusInWindow();
|
||||
}
|
||||
|
||||
class MazePanel extends JPanel {
|
||||
public MazePanel() {
|
||||
setPreferredSize(new Dimension(COLS * CELL_SIZE, ROWS * CELL_SIZE));
|
||||
setBackground(Color.BLACK);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
|
||||
for (int i = 0; i < ROWS; i++) {
|
||||
for (int j = 0; j < COLS; j++) {
|
||||
int x = j * CELL_SIZE;
|
||||
int y = i * CELL_SIZE;
|
||||
|
||||
switch (maze[i][j]) {
|
||||
case WALL:
|
||||
g2d.setColor(new Color(52, 73, 94));
|
||||
g2d.fillRect(x, y, CELL_SIZE, CELL_SIZE);
|
||||
g2d.setColor(new Color(44, 62, 80));
|
||||
g2d.drawRect(x, y, CELL_SIZE, CELL_SIZE);
|
||||
break;
|
||||
case PATH:
|
||||
g2d.setColor(new Color(236, 240, 241));
|
||||
g2d.fillRect(x, y, CELL_SIZE, CELL_SIZE);
|
||||
break;
|
||||
case END:
|
||||
g2d.setColor(new Color(236, 240, 241));
|
||||
g2d.fillRect(x, y, CELL_SIZE, CELL_SIZE);
|
||||
// Bandiera
|
||||
g2d.setColor(new Color(231, 76, 60));
|
||||
g2d.fillRect(x + 10, y + 5, 10, 8);
|
||||
g2d.setColor(new Color(52, 73, 94));
|
||||
g2d.fillRect(x + 9, y + 5, 2, 15);
|
||||
break;
|
||||
case PLAYER:
|
||||
g2d.setColor(new Color(236, 240, 241));
|
||||
g2d.fillRect(x, y, CELL_SIZE, CELL_SIZE);
|
||||
// Giocatore (cerchio)
|
||||
g2d.setColor(new Color(52, 152, 219));
|
||||
g2d.fillOval(x + 5, y + 5, CELL_SIZE - 10, CELL_SIZE - 10);
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.fillOval(x + 10, y + 8, 4, 4);
|
||||
g2d.fillOval(x + CELL_SIZE - 14, y + 8, 4, 4);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SwingUtilities.invokeLater(MazeGame::new);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user