Compare commits
10 Commits
4ca42b835c
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 33bc3d76c6 | |||
| 89421ac1c4 | |||
| 241fe7271f | |||
| c6a896f0e2 | |||
| 80c5ea412e | |||
| 16f1d7af9e | |||
| ce29e454cc | |||
| 70e46cc65c | |||
| cc9f2f7ebf | |||
|
|
3054032532 |
30
Alberi1/.gitignore
vendored
Normal file
30
Alberi1/.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
Alberi1/.idea/.gitignore
generated
vendored
Normal file
8
Alberi1/.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
Alberi1/.idea/misc.xml
generated
Normal file
6
Alberi1/.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
Alberi1/.idea/modules.xml
generated
Normal file
8
Alberi1/.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$/Alberi1.iml" filepath="$PROJECT_DIR$/Alberi1.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
6
Alberi1/.idea/vcs.xml
generated
Normal file
6
Alberi1/.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
Alberi1/Alberi1.iml
Normal file
11
Alberi1/Alberi1.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>
|
||||||
123
Alberi1/src/Main.java
Normal file
123
Alberi1/src/Main.java
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
import static java.lang.Math.max;
|
||||||
|
|
||||||
|
class Nodo{
|
||||||
|
static java.util.Random rnd = new java.util.Random();
|
||||||
|
int dato;
|
||||||
|
Nodo left, right;
|
||||||
|
public Nodo(int dato, Nodo left, Nodo right) {
|
||||||
|
this.dato = dato;
|
||||||
|
this.left = left;
|
||||||
|
this.right = right;
|
||||||
|
}
|
||||||
|
static void stampa(Nodo n, int livello, String lr) {
|
||||||
|
if(n==null) return;
|
||||||
|
for(int i=0;i<livello-1;i++)
|
||||||
|
System.out.print("\t");
|
||||||
|
if(livello!=0) System.out.print("+------->");
|
||||||
|
System.out.println(n.dato+lr);
|
||||||
|
stampa(n.left, livello+1,"L");
|
||||||
|
stampa(n.right, livello+1,"R");
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
0) fissate l'intestazione della funzione e
|
||||||
|
pensate che esista già e che funzioni
|
||||||
|
1) caso base, trovare un caso in cui la risposta
|
||||||
|
è ovvia e non serve computazione
|
||||||
|
2) cosa fare col dato attuale e cosa fare con i rimanenti
|
||||||
|
|
||||||
|
*/
|
||||||
|
static int somma(Nodo albero) {
|
||||||
|
if(albero==null) return 0;
|
||||||
|
return albero.dato+somma(albero.left)+somma(albero.right);
|
||||||
|
}
|
||||||
|
static int conta(Nodo albero) {//conta quanti sono i nodi
|
||||||
|
if(albero==null) return 0;
|
||||||
|
return 1+conta(albero.left)+conta(albero.right);
|
||||||
|
}
|
||||||
|
static int contaFoglie(Nodo albero) {//conta quanti sono le foglie (nodi senza figli)
|
||||||
|
if(albero==null) return 0;
|
||||||
|
if (albero.left == null && albero.right == null) {
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
return contaFoglie(albero.left) + contaFoglie(albero.right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static Nodo insOrd(int val, Nodo albero) { //Inserimento ordinato
|
||||||
|
if(albero==null) return new Nodo(val, null, null);
|
||||||
|
if(val>albero.dato) return
|
||||||
|
new Nodo(albero.dato, albero.left, insOrd(val, albero.right));
|
||||||
|
return new Nodo(albero.dato, insOrd(val, albero.left), albero.right);
|
||||||
|
}
|
||||||
|
static boolean trova(Nodo albero, int valore) { //Trova un valore
|
||||||
|
if(albero==null) return false;
|
||||||
|
else if(albero.dato==valore) return true;
|
||||||
|
else return trova(albero.left, valore) || trova(albero.right, valore);
|
||||||
|
}
|
||||||
|
static String valoriNelLivello(Nodo albero, int livello){ //Trova un valore in un livello
|
||||||
|
if(albero==null) return "";
|
||||||
|
else if(livello==0) return albero.dato + " ";
|
||||||
|
else return valoriNelLivello(albero.left, livello-1) + valoriNelLivello(albero.right, livello-1);
|
||||||
|
}
|
||||||
|
static int altezza(Nodo albero){ //Trova l'altezza dell'albero
|
||||||
|
if(albero==null) return 0;
|
||||||
|
else if (albero.left == null && albero.right == null) return 1;
|
||||||
|
return 1 + max(altezza(albero.left), altezza(albero.right));
|
||||||
|
}
|
||||||
|
static boolean uguali(Nodo a, Nodo b) { //Controlla che i due alberi siano uguali
|
||||||
|
if (a == null && b == null) return true;
|
||||||
|
if (a == null || b == null) return false;
|
||||||
|
if (a.dato != b.dato) return false;
|
||||||
|
return uguali(a.left, b.left) && uguali(a.right, b.right);
|
||||||
|
}
|
||||||
|
static int profondita(Nodo albero, int val) { //Restituisce la profondità massima
|
||||||
|
if(albero==null) return -1;
|
||||||
|
else if(albero.left == null && albero.right == null && albero.dato != val) return -1;
|
||||||
|
else if(albero.dato == val) return 1;
|
||||||
|
int pl = profondita(albero.left,val);
|
||||||
|
int pr = profondita(albero.right,val);
|
||||||
|
int m = max(pl, pr);
|
||||||
|
if (m == -1) return -1;
|
||||||
|
return 1 + m;
|
||||||
|
}
|
||||||
|
static int contaFiglio(Nodo albero) { //Conta i nodi che hanno un solo figlio
|
||||||
|
int count = 0;
|
||||||
|
if(albero==null) return count;
|
||||||
|
else if ((albero.left == null && albero.right != null) || (albero.left != null && albero.right == null)) count = 1;
|
||||||
|
return count + contaFiglio(albero.left) + contaFiglio(albero.right);
|
||||||
|
}
|
||||||
|
static int contaNelLivello(Nodo albero, int livello) { //Conta i nodi nel livello
|
||||||
|
if(albero==null) return 0;
|
||||||
|
else if (livello == 0) return 1;
|
||||||
|
return contaNelLivello(albero.left, livello-1) + contaNelLivello(albero.right, livello-1);
|
||||||
|
}
|
||||||
|
static Nodo crea(int altezza) { //Crea un albero con altezza determinata
|
||||||
|
if (altezza == 0) return null;
|
||||||
|
else if (altezza == 1) return new Nodo(rnd.nextInt(41) + 10,null,null);
|
||||||
|
Nodo left = crea(altezza - 1);
|
||||||
|
Nodo right = crea(altezza - 1);
|
||||||
|
return new Nodo(rnd.nextInt(41) + 10, left, right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Nodo n1 = new Nodo(10, null, null);
|
||||||
|
Nodo n2 = new Nodo(5, null, null);
|
||||||
|
Nodo n3 = new Nodo(30, null, null);
|
||||||
|
|
||||||
|
n1.left = n2;
|
||||||
|
n1.right = n3;
|
||||||
|
|
||||||
|
// Nodo.stampa(n1,0);
|
||||||
|
|
||||||
|
Nodo n4 = Nodo.insOrd(23, n1);
|
||||||
|
n4 = Nodo.insOrd(1, n4);
|
||||||
|
Nodo.stampa(n4,0,"");
|
||||||
|
System.out.println("Nodi: " + Nodo.conta(n4));
|
||||||
|
System.out.println("Foglie: " + Nodo.contaFoglie(n4));
|
||||||
|
System.out.println("Somma: " + Nodo.somma(n4));
|
||||||
|
System.out.println("Trovato 24? " + Nodo.trova(n4,24));
|
||||||
|
}
|
||||||
|
}
|
||||||
30
CacciaLaTalpa/.gitignore
vendored
Normal file
30
CacciaLaTalpa/.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
CacciaLaTalpa/.idea/.gitignore
generated
vendored
Normal file
8
CacciaLaTalpa/.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
CacciaLaTalpa/.idea/misc.xml
generated
Normal file
6
CacciaLaTalpa/.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_25" default="true" project-jdk-name="25" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
8
CacciaLaTalpa/.idea/modules.xml
generated
Normal file
8
CacciaLaTalpa/.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$/CacciaLaTalpa.iml" filepath="$PROJECT_DIR$/CacciaLaTalpa.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
11
CacciaLaTalpa/CacciaLaTalpa.iml
Normal file
11
CacciaLaTalpa/CacciaLaTalpa.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>
|
||||||
287
CacciaLaTalpa/src/CacciaAllaTalpa.java
Normal file
287
CacciaLaTalpa/src/CacciaAllaTalpa.java
Normal file
@@ -0,0 +1,287 @@
|
|||||||
|
import javax.swing.*; // Importa componenti GUI (JFrame, JButton, JLabel, ecc.)
|
||||||
|
import java.awt.*; // Importa layout e colori (BorderLayout, GridLayout, Color)
|
||||||
|
import java.awt.event.*; // Importa gestione eventi (ActionListener, ActionEvent)
|
||||||
|
import java.util.Random; // Importa classe per numeri casuali
|
||||||
|
|
||||||
|
// Classe principale che estende JFrame (la finestra dell'applicazione)
|
||||||
|
public class CacciaAllaTalpa extends JFrame {
|
||||||
|
// === COSTANTI ===
|
||||||
|
private static final int NUM_BUCHI = 6; // Numero totale di buchi
|
||||||
|
private static int TEMPO_TALPA = 2000; // Millisecondi che la talpa rimane visibile
|
||||||
|
|
||||||
|
// === COMPONENTI GUI ===
|
||||||
|
private JButton[] buchi; // Array di 6 bottoni che rappresentano i buchi
|
||||||
|
private JLabel lblPunteggio; // Etichetta per mostrare il punteggio
|
||||||
|
private JLabel lblVite; // Etichetta per mostrare le vite rimaste
|
||||||
|
|
||||||
|
// === VARIABILI DI GIOCO ===
|
||||||
|
private int punteggio = 0; // Punteggio corrente del giocatore
|
||||||
|
private int vite = 5; // Vite rimanenti (inizi con 5)
|
||||||
|
private int posizioneCorrente = -1; // Indice del buco dove si trova la talpa (-1 = nessuna talpa)
|
||||||
|
private Timer timerTalpa; // Timer per spostare la talpa ogni 2 secondi
|
||||||
|
private Random random; // Generatore di numeri casuali
|
||||||
|
private boolean giocoAttivo = true; // Flag per sapere se il gioco è in corso
|
||||||
|
|
||||||
|
// === COSTRUTTORE ===
|
||||||
|
// Qui viene inizializzata tutta l'interfaccia grafica
|
||||||
|
public CacciaAllaTalpa() {
|
||||||
|
// Imposta il titolo della finestra
|
||||||
|
setTitle("Caccia alla Talpa");
|
||||||
|
|
||||||
|
// Chiudi l'applicazione quando chiudi la finestra
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
|
||||||
|
// BorderLayout: divide la finestra in 5 zone (NORTH, SOUTH, EAST, WEST, CENTER)
|
||||||
|
// I numeri (10, 10) sono gli spazi orizzontali e verticali tra le zone
|
||||||
|
setLayout(new BorderLayout(10, 10));
|
||||||
|
|
||||||
|
// Impedisci il ridimensionamento della finestra
|
||||||
|
setResizable(false);
|
||||||
|
|
||||||
|
// Inizializza il generatore di numeri casuali
|
||||||
|
random = new Random();
|
||||||
|
|
||||||
|
// === PANNELLO SUPERIORE (Punteggio e Vite) ===
|
||||||
|
// GridLayout(1, 2): 1 riga, 2 colonne
|
||||||
|
// 20 = spazio orizzontale tra le colonne, 0 = nessuno spazio verticale
|
||||||
|
JPanel panelInfo = new JPanel(new GridLayout(1, 2, 20, 0));
|
||||||
|
|
||||||
|
// BorderFactory.createEmptyBorder(top, left, bottom, right)
|
||||||
|
// Crea un margine interno di 15 pixel su tutti i lati
|
||||||
|
panelInfo.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
|
||||||
|
|
||||||
|
// Colore verde per il pannello superiore
|
||||||
|
panelInfo.setBackground(new Color(34, 139, 34));
|
||||||
|
|
||||||
|
// === LABEL PUNTEGGIO ===
|
||||||
|
lblPunteggio = new JLabel("Punteggio: 0", SwingConstants.CENTER);
|
||||||
|
lblPunteggio.setFont(new Font("Arial", Font.BOLD, 20)); // Font grande e grassetto
|
||||||
|
lblPunteggio.setForeground(Color.WHITE); // Testo bianco
|
||||||
|
|
||||||
|
// === LABEL VITE ===
|
||||||
|
lblVite = new JLabel("Vite: 5", SwingConstants.CENTER);
|
||||||
|
lblVite.setFont(new Font("Arial", Font.BOLD, 20));
|
||||||
|
lblVite.setForeground(Color.WHITE);
|
||||||
|
|
||||||
|
// Aggiungi le due label al pannello
|
||||||
|
panelInfo.add(lblPunteggio);
|
||||||
|
panelInfo.add(lblVite);
|
||||||
|
|
||||||
|
// === PANNELLO CENTRALE (I 6 buchi) ===
|
||||||
|
// GridLayout(2, 3): 2 righe, 3 colonne = 6 celle totali
|
||||||
|
// 15, 15 = spazi tra le celle (orizzontale, verticale)
|
||||||
|
JPanel panelBuchi = new JPanel(new GridLayout(2, 3, 15, 15));
|
||||||
|
panelBuchi.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
|
||||||
|
panelBuchi.setBackground(new Color(139, 69, 19)); // Colore marrone (terra)
|
||||||
|
|
||||||
|
// Crea l'array di 6 bottoni
|
||||||
|
buchi = new JButton[NUM_BUCHI];
|
||||||
|
|
||||||
|
// Ciclo per creare tutti i 6 buchi
|
||||||
|
for (int i = 0; i < NUM_BUCHI; i++) {
|
||||||
|
// IMPORTANTE: final per usare 'i' dentro la lambda
|
||||||
|
final int index = i;
|
||||||
|
|
||||||
|
// Crea un nuovo bottone
|
||||||
|
buchi[i] = new JButton();
|
||||||
|
|
||||||
|
// Imposta dimensione fissa del bottone (120x120 pixel)
|
||||||
|
buchi[i].setPreferredSize(new Dimension(120, 120));
|
||||||
|
|
||||||
|
// Font grande per l'emoji della talpa
|
||||||
|
buchi[i].setFont(new Font("Arial", Font.BOLD, 40));
|
||||||
|
|
||||||
|
// Colore di sfondo marrone scuro
|
||||||
|
buchi[i].setBackground(new Color(101, 67, 33));
|
||||||
|
|
||||||
|
// Rimuovi il bordo tratteggiato quando il bottone ha il focus
|
||||||
|
buchi[i].setFocusPainted(false);
|
||||||
|
|
||||||
|
// Bordo nero spesso 3 pixel attorno al bottone
|
||||||
|
buchi[i].setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
|
||||||
|
|
||||||
|
// === EVENT LISTENER ===
|
||||||
|
// Quando clicchi il bottone, chiama clickBuco(index)
|
||||||
|
// 'e' è l'evento (ActionEvent) che contiene info sul click
|
||||||
|
buchi[i].addActionListener(e -> clickBuco(index));
|
||||||
|
|
||||||
|
// Aggiungi il bottone al pannello
|
||||||
|
panelBuchi.add(buchi[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// === AGGIUNGI I PANNELLI ALLA FINESTRA ===
|
||||||
|
// BorderLayout.NORTH = zona superiore
|
||||||
|
add(panelInfo, BorderLayout.NORTH);
|
||||||
|
// BorderLayout.CENTER = zona centrale (prende tutto lo spazio rimanente)
|
||||||
|
add(panelBuchi, BorderLayout.CENTER);
|
||||||
|
|
||||||
|
// pack() ridimensiona la finestra per contenere tutti i componenti
|
||||||
|
pack();
|
||||||
|
|
||||||
|
// Centra la finestra sullo schermo (null = centro dello schermo)
|
||||||
|
setLocationRelativeTo(null);
|
||||||
|
|
||||||
|
// === TIMER PRINCIPALE ===
|
||||||
|
// Timer che ogni 2 secondi (TEMPO_TALPA) chiama spostaOPerdiVita()
|
||||||
|
// 'e' è l'ActionEvent generato dal timer
|
||||||
|
timerTalpa = new Timer(TEMPO_TALPA, e -> spostaOPerdiVita());
|
||||||
|
|
||||||
|
// === AVVIO DEL GIOCO ===
|
||||||
|
// Timer che aspetta 1 secondo prima di iniziare il gioco
|
||||||
|
Timer startTimer = new Timer(1000, e -> {
|
||||||
|
mostraTalpa(); // Mostra la prima talpa
|
||||||
|
timerTalpa.start(); // Avvia il timer principale
|
||||||
|
});
|
||||||
|
startTimer.setRepeats(false); // Esegui solo una volta
|
||||||
|
startTimer.start(); // Avvia questo timer
|
||||||
|
}
|
||||||
|
|
||||||
|
// === METODO: MOSTRA LA TALPA ===
|
||||||
|
// Questo metodo nasconde la talpa precedente e la mostra in una nuova posizione casuale
|
||||||
|
private void mostraTalpa() {
|
||||||
|
// Se il gioco non è attivo, esci subito dal metodo
|
||||||
|
if (!giocoAttivo) return;
|
||||||
|
|
||||||
|
// Nascondi la talpa dalla posizione precedente
|
||||||
|
if (posizioneCorrente >= 0) {
|
||||||
|
buchi[posizioneCorrente].setText(""); // Rimuovi l'emoji
|
||||||
|
// Ripristina il colore originale del buco
|
||||||
|
buchi[posizioneCorrente].setBackground(new Color(101, 67, 33));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Genera una nuova posizione casuale (da 0 a 5)
|
||||||
|
posizioneCorrente = random.nextInt(NUM_BUCHI);
|
||||||
|
|
||||||
|
// Mostra la talpa nella nuova posizione
|
||||||
|
buchi[posizioneCorrente].setText("🐭"); // Emoji della talpa
|
||||||
|
// Schiarisci leggermente il colore per evidenziare la talpa
|
||||||
|
buchi[posizioneCorrente].setBackground(new Color(139, 90, 43));
|
||||||
|
}
|
||||||
|
|
||||||
|
// === METODO: GESTIONE CLICK SU UN BUCO ===
|
||||||
|
// Parametro: index = quale buco è stato cliccato (da 0 a 5)
|
||||||
|
private void clickBuco(int index) {
|
||||||
|
// Se il gioco non è attivo, non fare nulla
|
||||||
|
if (!giocoAttivo) return;
|
||||||
|
|
||||||
|
// Controlla se hai cliccato sul buco giusto (dove c'è la talpa)
|
||||||
|
if (index == posizioneCorrente) {
|
||||||
|
// === HAI COLPITO LA TALPA! ===
|
||||||
|
punteggio++; // Incrementa il punteggio
|
||||||
|
// Aggiorna la label del punteggio
|
||||||
|
lblPunteggio.setText("Punteggio: " + punteggio);
|
||||||
|
|
||||||
|
// Feedback visivo: colora di verde il buco
|
||||||
|
buchi[index].setBackground(Color.GREEN);
|
||||||
|
|
||||||
|
// Resetta il timer principale (ricomincia il conto di 2 secondi)
|
||||||
|
timerTalpa.restart();
|
||||||
|
|
||||||
|
// Timer per mostrare subito una nuova talpa dopo 200ms
|
||||||
|
// Questo da il tempo di vedere il verde prima che la talpa si sposti
|
||||||
|
Timer feedbackTimer = new Timer(200, e -> mostraTalpa());
|
||||||
|
feedbackTimer.setRepeats(false); // Esegui una sola volta
|
||||||
|
feedbackTimer.start();
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// === HAI MANCATO! ===
|
||||||
|
// Feedback visivo: colora di rosso il buco sbagliato
|
||||||
|
buchi[index].setBackground(Color.RED);
|
||||||
|
|
||||||
|
// Timer per ripristinare il colore originale dopo 300ms
|
||||||
|
Timer resetColor = new Timer(300, e ->
|
||||||
|
buchi[index].setBackground(new Color(101, 67, 33)));
|
||||||
|
resetColor.setRepeats(false);
|
||||||
|
resetColor.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// === METODO: SPOSTA LA TALPA O PERDI UNA VITA ===
|
||||||
|
// Questo metodo viene chiamato dal timer ogni 2 secondi
|
||||||
|
private void spostaOPerdiVita() {
|
||||||
|
if (!giocoAttivo) return;
|
||||||
|
|
||||||
|
// Se arrivi qui, significa che i 2 secondi sono passati
|
||||||
|
// e il giocatore NON ha cliccato sulla talpa
|
||||||
|
vite--; // Perdi una vita
|
||||||
|
|
||||||
|
// Aggiorna la label delle vite
|
||||||
|
lblVite.setText("Vite: " + vite);
|
||||||
|
|
||||||
|
// Controlla se hai finito le vite
|
||||||
|
if (vite <= 0) {
|
||||||
|
fineGioco(); // Game Over
|
||||||
|
} else {
|
||||||
|
mostraTalpa(); // Mostra la talpa in una nuova posizione
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// === METODO: FINE DEL GIOCO ===
|
||||||
|
private void fineGioco() {
|
||||||
|
giocoAttivo = false; // Ferma il gioco
|
||||||
|
timerTalpa.stop(); // Ferma il timer principale
|
||||||
|
|
||||||
|
// Nascondi l'ultima talpa
|
||||||
|
if (posizioneCorrente >= 0) {
|
||||||
|
buchi[posizioneCorrente].setText("");
|
||||||
|
buchi[posizioneCorrente].setBackground(new Color(101, 67, 33));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mostra un dialog con il punteggio finale
|
||||||
|
// JOptionPane.showConfirmDialog mostra una finestra con bottoni SI/NO
|
||||||
|
int scelta = JOptionPane.showConfirmDialog(
|
||||||
|
this, // Finestra padre
|
||||||
|
"Game Over!\nPunteggio finale: " + punteggio + "\n\nVuoi giocare ancora?",
|
||||||
|
"Fine Gioco", // Titolo del dialog
|
||||||
|
JOptionPane.YES_NO_OPTION // Tipo di bottoni
|
||||||
|
);
|
||||||
|
|
||||||
|
// Controlla quale bottone ha cliccato l'utente
|
||||||
|
if (scelta == JOptionPane.YES_OPTION) {
|
||||||
|
resetGioco(); // Ricomincia il gioco
|
||||||
|
} else {
|
||||||
|
System.exit(0); // Chiudi l'applicazione
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// === METODO: RESET DEL GIOCO ===
|
||||||
|
// Ripristina tutte le variabili e ricomincia una nuova partita
|
||||||
|
private void resetGioco() {
|
||||||
|
// Azzera le variabili di gioco
|
||||||
|
punteggio = 0;
|
||||||
|
vite = 5;
|
||||||
|
posizioneCorrente = -1;
|
||||||
|
giocoAttivo = true;
|
||||||
|
|
||||||
|
// Aggiorna le label
|
||||||
|
lblPunteggio.setText("Punteggio: 0");
|
||||||
|
lblVite.setText("Vite: 5");
|
||||||
|
|
||||||
|
// Ripulisci tutti i buchi
|
||||||
|
for (JButton buco : buchi) {
|
||||||
|
buco.setText(""); // Rimuovi eventuali talpe
|
||||||
|
buco.setBackground(new Color(101, 67, 33)); // Colore originale
|
||||||
|
}
|
||||||
|
|
||||||
|
// Aspetta 1 secondo e poi riavvia il gioco
|
||||||
|
Timer startTimer = new Timer(500, e -> {
|
||||||
|
mostraTalpa(); // Mostra la prima talpa
|
||||||
|
timerTalpa.restart(); // Riavvia il timer principale
|
||||||
|
});
|
||||||
|
startTimer.setRepeats(false);
|
||||||
|
startTimer.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
// === METODO MAIN ===
|
||||||
|
// Punto di ingresso del programma
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// SwingUtilities.invokeLater() esegue il codice nel thread della GUI
|
||||||
|
// È la best practice per creare interfacce Swing
|
||||||
|
SwingUtilities.invokeLater(() -> {
|
||||||
|
// Crea una nuova istanza del gioco
|
||||||
|
CacciaAllaTalpa gioco = new CacciaAllaTalpa();
|
||||||
|
// Rendi visibile la finestra
|
||||||
|
gioco.setVisible(true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
30
DisegnoPazzo/.gitignore
vendored
Normal file
30
DisegnoPazzo/.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
DisegnoPazzo/.idea/.gitignore
generated
vendored
Normal file
8
DisegnoPazzo/.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
DisegnoPazzo/.idea/misc.xml
generated
Normal file
6
DisegnoPazzo/.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_25" default="true" project-jdk-name="25" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
8
DisegnoPazzo/.idea/modules.xml
generated
Normal file
8
DisegnoPazzo/.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$/DisegnoPazzo.iml" filepath="$PROJECT_DIR$/DisegnoPazzo.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
6
DisegnoPazzo/.idea/vcs.xml
generated
Normal file
6
DisegnoPazzo/.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
DisegnoPazzo/DisegnoPazzo.iml
Normal file
11
DisegnoPazzo/DisegnoPazzo.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>
|
||||||
111
DisegnoPazzo/src/CerchioTrascinabile.java
Normal file
111
DisegnoPazzo/src/CerchioTrascinabile.java
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.*;
|
||||||
|
import java.awt.geom.Ellipse2D;
|
||||||
|
|
||||||
|
public class CerchioTrascinabile extends JFrame {
|
||||||
|
|
||||||
|
public CerchioTrascinabile() {
|
||||||
|
setTitle("Cerchio Trascinabile");
|
||||||
|
setSize(600, 500);
|
||||||
|
setLayout(null);
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
setLocationRelativeTo(null);
|
||||||
|
PannelloCerchio pannello = new PannelloCerchio();
|
||||||
|
JButton btnCancella = new JButton("Cancella");
|
||||||
|
btnCancella.addActionListener(e -> pannello.cancellaCerchio());
|
||||||
|
JPanel bottomPanel = new JPanel();
|
||||||
|
bottomPanel.add(btnCancella);
|
||||||
|
setLayout(new BorderLayout());
|
||||||
|
add(pannello, BorderLayout.CENTER);
|
||||||
|
add(bottomPanel, BorderLayout.SOUTH);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SwingUtilities.invokeLater(() -> {
|
||||||
|
new CerchioTrascinabile().setVisible(true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class PannelloCerchio extends JPanel {
|
||||||
|
private Cerchio cerchio;
|
||||||
|
private boolean dragging = false;
|
||||||
|
|
||||||
|
public PannelloCerchio() {
|
||||||
|
setBackground(Color.WHITE);
|
||||||
|
|
||||||
|
addMouseListener(new MouseAdapter() {
|
||||||
|
@Override
|
||||||
|
public void mousePressed(MouseEvent e) {
|
||||||
|
if (cerchio == null) {
|
||||||
|
cerchio = new Cerchio(e.getX(), e.getY());
|
||||||
|
repaint();
|
||||||
|
} else if (cerchio.contiene(e.getX(), e.getY())) {
|
||||||
|
dragging = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseReleased(MouseEvent e) {
|
||||||
|
dragging = false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
addMouseMotionListener(new MouseMotionAdapter() {
|
||||||
|
@Override
|
||||||
|
public void mouseDragged(MouseEvent e) {
|
||||||
|
if (dragging && cerchio != null) {
|
||||||
|
cerchio.muovi(e.getX(), e.getY());
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void cancellaCerchio() {
|
||||||
|
cerchio = null;
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void paintComponent(Graphics g) {
|
||||||
|
super.paintComponent(g);
|
||||||
|
if (cerchio != null) {
|
||||||
|
Graphics2D g2d = (Graphics2D) g;
|
||||||
|
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
||||||
|
RenderingHints.VALUE_ANTIALIAS_ON);
|
||||||
|
cerchio.disegna(g2d);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Cerchio {
|
||||||
|
private int x, y;
|
||||||
|
private static final int RAGGIO = 40;
|
||||||
|
|
||||||
|
public Cerchio(int x, int y) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void muovi(int nuovoX, int nuovoY) {
|
||||||
|
this.x = nuovoX;
|
||||||
|
this.y = nuovoY;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean contiene(int px, int py) {
|
||||||
|
int dx = px - x;
|
||||||
|
int dy = py - y;
|
||||||
|
return dx * dx + dy * dy <= RAGGIO * RAGGIO;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void disegna(Graphics2D g2d) {
|
||||||
|
g2d.setColor(new Color(70, 130, 180));
|
||||||
|
g2d.fillOval(x - RAGGIO, y - RAGGIO, RAGGIO * 2, RAGGIO * 2);
|
||||||
|
|
||||||
|
g2d.setColor(new Color(30, 90, 140));
|
||||||
|
g2d.setStroke(new BasicStroke(2));
|
||||||
|
g2d.drawOval(x - RAGGIO, y - RAGGIO, RAGGIO * 2, RAGGIO * 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
30
LabirintoTest/.gitignore
vendored
Normal file
30
LabirintoTest/.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
LabirintoTest/.idea/.gitignore
generated
vendored
Normal file
8
LabirintoTest/.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
LabirintoTest/.idea/misc.xml
generated
Normal file
6
LabirintoTest/.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
LabirintoTest/.idea/modules.xml
generated
Normal file
8
LabirintoTest/.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$/LabirintoTest.iml" filepath="$PROJECT_DIR$/LabirintoTest.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
6
LabirintoTest/.idea/vcs.xml
generated
Normal file
6
LabirintoTest/.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
LabirintoTest/LabirintoTest.iml
Normal file
11
LabirintoTest/LabirintoTest.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>
|
||||||
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
30
Prova1/.gitignore
vendored
Normal file
30
Prova1/.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
Prova1/.idea/.gitignore
generated
vendored
Normal file
8
Prova1/.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
Prova1/.idea/misc.xml
generated
Normal file
6
Prova1/.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
Prova1/.idea/modules.xml
generated
Normal file
8
Prova1/.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$/Prova1.iml" filepath="$PROJECT_DIR$/Prova1.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
11
Prova1/Prova1.iml
Normal file
11
Prova1/Prova1.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>
|
||||||
152
Prova1/src/CirclePanelDemo.java
Normal file
152
Prova1/src/CirclePanelDemo.java
Normal file
@@ -0,0 +1,152 @@
|
|||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.*;
|
||||||
|
|
||||||
|
public class CirclePanelDemo {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SwingUtilities.invokeLater(() -> new CirclePanelDemo().createAndShowGUI());
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ----------------------------- */
|
||||||
|
/* 1. Finestra principale */
|
||||||
|
/* ----------------------------- */
|
||||||
|
private void createAndShowGUI() {
|
||||||
|
JFrame frame = new JFrame("Circle – click, drag & delete");
|
||||||
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
frame.setLayout(new BorderLayout());
|
||||||
|
|
||||||
|
CirclePanel circlePanel = new CirclePanel();
|
||||||
|
frame.add(circlePanel, BorderLayout.CENTER);
|
||||||
|
|
||||||
|
JButton clearBtn = new JButton("Cancella cerchio");
|
||||||
|
clearBtn.addActionListener(e -> circlePanel.clearCircle());
|
||||||
|
JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
|
||||||
|
btnPanel.add(clearBtn);
|
||||||
|
frame.add(btnPanel, BorderLayout.SOUTH);
|
||||||
|
|
||||||
|
frame.setSize(600, 400);
|
||||||
|
frame.setLocationRelativeTo(null); // centra la finestra
|
||||||
|
frame.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ----------------------------- */
|
||||||
|
/* 2. JPanel che disegna il cerchio */
|
||||||
|
/* ----------------------------- */
|
||||||
|
private static class CirclePanel extends JPanel {
|
||||||
|
|
||||||
|
private final int RADIUS = 30; // raggio fisso
|
||||||
|
|
||||||
|
private int circleX = -1; // centro X (-1 → nessun cerchio)
|
||||||
|
private int circleY = -1; // centro Y
|
||||||
|
|
||||||
|
/* variabili per il drag */
|
||||||
|
private boolean dragging = false;
|
||||||
|
private int offsetX, offsetY;
|
||||||
|
|
||||||
|
CirclePanel() {
|
||||||
|
setBackground(Color.WHITE);
|
||||||
|
|
||||||
|
MouseAdapter ma = new MouseAdapter() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mousePressed(MouseEvent e) {
|
||||||
|
int mx = e.getX();
|
||||||
|
int my = e.getY();
|
||||||
|
|
||||||
|
/* Se il cerchio esiste */
|
||||||
|
if (circleX != -1 && circleY != -1) {
|
||||||
|
|
||||||
|
/* Se il click è dentro il cerchio → inizia drag */
|
||||||
|
if (isInsideCircle(mx, my)) {
|
||||||
|
dragging = true;
|
||||||
|
offsetX = mx - circleX;
|
||||||
|
offsetY = my - circleY;
|
||||||
|
} else {
|
||||||
|
// Click fuori dal cerchio: non fare nulla
|
||||||
|
dragging = false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
/* Nessun cerchio → creiamo un nuovo */
|
||||||
|
setCircle(mx, my);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseReleased(MouseEvent e) {
|
||||||
|
dragging = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseDragged(MouseEvent e) {
|
||||||
|
if (dragging && circleX != -1) {
|
||||||
|
int mx = e.getX();
|
||||||
|
int my = e.getY();
|
||||||
|
|
||||||
|
setCircle(mx - offsetX, my - offsetY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
addMouseListener(ma);
|
||||||
|
addMouseMotionListener(ma);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------------------------------- */
|
||||||
|
/* 3. Utilità */
|
||||||
|
/* ---------------------------------- */
|
||||||
|
|
||||||
|
/** Verifica se il punto (x,y) è dentro il cerchio */
|
||||||
|
private boolean isInsideCircle(int x, int y) {
|
||||||
|
if (circleX == -1 || circleY == -1) return false;
|
||||||
|
int dx = x - circleX;
|
||||||
|
int dy = y - circleY;
|
||||||
|
return dx * dx + dy * dy <= RADIUS * RADIUS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Imposta il centro del cerchio e ridisegna */
|
||||||
|
private void setCircle(int x, int y) {
|
||||||
|
circleX = x;
|
||||||
|
circleY = y;
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Cancella il cerchio (se presente) */
|
||||||
|
public void clearCircle() {
|
||||||
|
circleX = -1;
|
||||||
|
circleY = -1;
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------------------------------- */
|
||||||
|
/* 4. Disegno del pannello */
|
||||||
|
/* ---------------------------------- */
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void paintComponent(Graphics g) {
|
||||||
|
super.paintComponent(g);
|
||||||
|
|
||||||
|
if (circleX == -1 || circleY == -1)
|
||||||
|
return; // nessun cerchio da disegnare
|
||||||
|
|
||||||
|
Graphics2D g2 = (Graphics2D) g.create();
|
||||||
|
try {
|
||||||
|
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
||||||
|
RenderingHints.VALUE_ANTIALIAS_ON);
|
||||||
|
|
||||||
|
int topLeftX = circleX - RADIUS;
|
||||||
|
int topLeftY = circleY - RADIUS;
|
||||||
|
|
||||||
|
g2.setColor(Color.RED);
|
||||||
|
g2.fillOval(topLeftX, topLeftY, 2 * RADIUS, 2 * RADIUS);
|
||||||
|
|
||||||
|
// bordo bianco
|
||||||
|
g2.setStroke(new BasicStroke(2f));
|
||||||
|
g2.setColor(Color.WHITE);
|
||||||
|
g2.drawOval(topLeftX, topLeftY, 2 * RADIUS, 2 * RADIUS);
|
||||||
|
} finally {
|
||||||
|
g2.dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
30
SparaBalle/.gitignore
vendored
Normal file
30
SparaBalle/.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
SparaBalle/.idea/.gitignore
generated
vendored
Normal file
8
SparaBalle/.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
SparaBalle/.idea/misc.xml
generated
Normal file
6
SparaBalle/.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_25" default="true" project-jdk-name="25" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
8
SparaBalle/.idea/modules.xml
generated
Normal file
8
SparaBalle/.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$/SparaBalle.iml" filepath="$PROJECT_DIR$/SparaBalle.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
6
SparaBalle/.idea/vcs.xml
generated
Normal file
6
SparaBalle/.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
SparaBalle/SparaBalle.iml
Normal file
11
SparaBalle/SparaBalle.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>
|
||||||
202
SparaBalle/src/FallingBalls.java
Normal file
202
SparaBalle/src/FallingBalls.java
Normal file
@@ -0,0 +1,202 @@
|
|||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.*;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
class GameObject{
|
||||||
|
private static final Random rnd = new Random();
|
||||||
|
protected int x, y;
|
||||||
|
protected int speed;
|
||||||
|
protected int diam = rnd.nextInt(10, 30);
|
||||||
|
|
||||||
|
GameObject(int maxW) {
|
||||||
|
reset(maxW);
|
||||||
|
}
|
||||||
|
|
||||||
|
void reset(int maxW) {
|
||||||
|
x = rnd.nextInt(Math.max(1, maxW - diam));
|
||||||
|
y = rnd.nextInt(50);
|
||||||
|
speed = 2 + rnd.nextInt(6);
|
||||||
|
}
|
||||||
|
|
||||||
|
void update(int panelH, int panelW) {
|
||||||
|
y += speed;
|
||||||
|
if (y > panelH) reset(panelW);
|
||||||
|
}
|
||||||
|
|
||||||
|
void draw(Graphics g) {
|
||||||
|
g.fillOval(x, y, diam, diam);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Pallina extends GameObject{
|
||||||
|
public Pallina(int maxW) {
|
||||||
|
super(maxW);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Astronave extends GameObject{
|
||||||
|
|
||||||
|
Astronave() {
|
||||||
|
super(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void update(int coordXMouse, int coordYMouse) {
|
||||||
|
x = coordXMouse;
|
||||||
|
y = coordYMouse;
|
||||||
|
}
|
||||||
|
|
||||||
|
void draw(Graphics g) {
|
||||||
|
g.fillRect(x, y, diam, 50);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Proiettile extends GameObject{
|
||||||
|
public Proiettile(int x, int y) {
|
||||||
|
super(0);
|
||||||
|
super.x = x;
|
||||||
|
super.y = y;
|
||||||
|
speed = -10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ButtonHandler implements ActionListener{
|
||||||
|
|
||||||
|
private FallingBalls panel;
|
||||||
|
private JButton btn;
|
||||||
|
|
||||||
|
public ButtonHandler(FallingBalls panel, JButton btn) {
|
||||||
|
this.panel = panel;
|
||||||
|
this.btn = btn;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
panel.paused = !panel.paused;
|
||||||
|
btn.setText(panel.paused ? "Play" : "Pause");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class FallingBalls extends JPanel implements MouseMotionListener, MouseListener {
|
||||||
|
|
||||||
|
static final int N_BALLS = 6;
|
||||||
|
Pallina[] balls = new Pallina[N_BALLS];
|
||||||
|
Astronave astro = new Astronave();
|
||||||
|
Proiettile p;
|
||||||
|
|
||||||
|
boolean paused = false;
|
||||||
|
|
||||||
|
public FallingBalls() {
|
||||||
|
setBounds(0,0,500, 400);
|
||||||
|
setBackground(Color.WHITE);
|
||||||
|
|
||||||
|
for (int i = 0; i < N_BALLS; i++)
|
||||||
|
balls[i] = new Pallina(500);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void paintComponent(Graphics g) {
|
||||||
|
super.paintComponent(g);
|
||||||
|
g.setColor(Color.RED);
|
||||||
|
for (int i=0; i<balls.length; i++)
|
||||||
|
balls[i].draw(g);
|
||||||
|
astro.draw(g);
|
||||||
|
if(p!=null) p.draw(g);
|
||||||
|
}
|
||||||
|
|
||||||
|
void update() {
|
||||||
|
if(!paused)
|
||||||
|
for (Pallina b : balls) b.update(500,400);
|
||||||
|
if(p!=null) p.update(500,400);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
JFrame f = new JFrame("Falling Balls");
|
||||||
|
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
f.setBounds(0, 0, 500, 600);
|
||||||
|
f.setLayout(null);
|
||||||
|
|
||||||
|
FallingBalls panel = new FallingBalls();
|
||||||
|
panel.setBounds(0, 0, 500, 400);
|
||||||
|
f.add(panel);
|
||||||
|
panel.addMouseMotionListener(panel);
|
||||||
|
panel.addMouseListener(panel);
|
||||||
|
|
||||||
|
JButton btn = new JButton("Pause");
|
||||||
|
btn.setBounds(0, 400, 500, 80);
|
||||||
|
|
||||||
|
|
||||||
|
ButtonHandler bh = new ButtonHandler(panel, btn);
|
||||||
|
btn.addActionListener(bh);
|
||||||
|
/*
|
||||||
|
btn.addActionListener(ev -> {
|
||||||
|
panel.paused = !panel.paused;
|
||||||
|
btn.setText(panel.paused ? "Play" : "Pause");
|
||||||
|
});
|
||||||
|
*/
|
||||||
|
|
||||||
|
f.add(btn);
|
||||||
|
|
||||||
|
|
||||||
|
f.setVisible(true);
|
||||||
|
|
||||||
|
while(true) {
|
||||||
|
panel.update();
|
||||||
|
panel.repaint();
|
||||||
|
try {
|
||||||
|
Thread.sleep(16);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseDragged(MouseEvent e) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseMoved(MouseEvent e) {
|
||||||
|
astro.x = e.getX();
|
||||||
|
astro.y = e.getY();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseClicked(MouseEvent e) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mousePressed(MouseEvent e) {
|
||||||
|
p = new Proiettile(e.getX(), e.getY());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseReleased(MouseEvent e) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseEntered(MouseEvent e) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseExited(MouseEvent e) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
30
TrisMiniMax/.gitignore
vendored
Normal file
30
TrisMiniMax/.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
TrisMiniMax/.idea/.gitignore
generated
vendored
Normal file
8
TrisMiniMax/.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
TrisMiniMax/.idea/misc.xml
generated
Normal file
6
TrisMiniMax/.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
TrisMiniMax/.idea/modules.xml
generated
Normal file
8
TrisMiniMax/.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$/TrisMiniMax.iml" filepath="$PROJECT_DIR$/TrisMiniMax.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
6
TrisMiniMax/.idea/vcs.xml
generated
Normal file
6
TrisMiniMax/.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
TrisMiniMax/TrisMiniMax.iml
Normal file
11
TrisMiniMax/TrisMiniMax.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>
|
||||||
221
TrisMiniMax/src/TrisGame.java
Normal file
221
TrisMiniMax/src/TrisGame.java
Normal file
@@ -0,0 +1,221 @@
|
|||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.*;
|
||||||
|
|
||||||
|
public class TrisGame extends JFrame {
|
||||||
|
private final JButton[][] buttons = new JButton[3][3];
|
||||||
|
private final char[][] board = new char[3][3];
|
||||||
|
private final char humanPlayer = 'X';
|
||||||
|
private final char aiPlayer = 'O';
|
||||||
|
private final JLabel statusLabel;
|
||||||
|
private boolean gameOver = false;
|
||||||
|
|
||||||
|
public TrisGame() {
|
||||||
|
setTitle("Tris - Minimax AI");
|
||||||
|
setSize(400, 450);
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
setLayout(new BorderLayout());
|
||||||
|
|
||||||
|
// Pannello di gioco
|
||||||
|
JPanel gamePanel = new JPanel(new GridLayout(3, 3, 5, 5));
|
||||||
|
gamePanel.setBackground(Color.BLACK);
|
||||||
|
|
||||||
|
// Inizializza la board e i bottoni
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
for (int j = 0; j < 3; j++) {
|
||||||
|
board[i][j] = ' ';
|
||||||
|
buttons[i][j] = new JButton("");
|
||||||
|
buttons[i][j].setFont(new Font("Arial", Font.BOLD, 60));
|
||||||
|
buttons[i][j].setFocusPainted(false);
|
||||||
|
buttons[i][j].setBackground(Color.WHITE);
|
||||||
|
|
||||||
|
final int row = i;
|
||||||
|
final int col = j;
|
||||||
|
|
||||||
|
buttons[i][j].addActionListener(e -> playerMove(row, col));
|
||||||
|
gamePanel.add(buttons[i][j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pannello di stato
|
||||||
|
JPanel statusPanel = new JPanel();
|
||||||
|
statusLabel = new JLabel("Il tuo turno! (X)");
|
||||||
|
statusLabel.setFont(new Font("Arial", Font.BOLD, 16));
|
||||||
|
statusPanel.add(statusLabel);
|
||||||
|
|
||||||
|
// Bottone reset
|
||||||
|
JButton resetButton = new JButton("Nuova Partita");
|
||||||
|
resetButton.addActionListener(e -> resetGame());
|
||||||
|
statusPanel.add(resetButton);
|
||||||
|
|
||||||
|
add(gamePanel, BorderLayout.CENTER);
|
||||||
|
add(statusPanel, BorderLayout.SOUTH);
|
||||||
|
|
||||||
|
setLocationRelativeTo(null);
|
||||||
|
setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void playerMove(int row, int col) {
|
||||||
|
if (gameOver || board[row][col] != ' ') return;
|
||||||
|
|
||||||
|
// Mossa del giocatore
|
||||||
|
board[row][col] = humanPlayer;
|
||||||
|
buttons[row][col].setText("X");
|
||||||
|
buttons[row][col].setForeground(Color.BLUE);
|
||||||
|
|
||||||
|
if (checkWinner(humanPlayer)) {
|
||||||
|
statusLabel.setText("Hai vinto! 🎉");
|
||||||
|
gameOver = true;
|
||||||
|
highlightWinner(humanPlayer);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isBoardFull()) {
|
||||||
|
statusLabel.setText("Pareggio!");
|
||||||
|
gameOver = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mossa dell'AI
|
||||||
|
statusLabel.setText("L'AI sta pensando...");
|
||||||
|
Timer timer = new Timer(500, e -> {
|
||||||
|
aiMove();
|
||||||
|
|
||||||
|
if (checkWinner(aiPlayer)) {
|
||||||
|
statusLabel.setText("L'AI ha vinto!");
|
||||||
|
gameOver = true;
|
||||||
|
highlightWinner(aiPlayer);
|
||||||
|
} else if (isBoardFull()) {
|
||||||
|
statusLabel.setText("Pareggio!");
|
||||||
|
gameOver = true;
|
||||||
|
} else {
|
||||||
|
statusLabel.setText("Il tuo turno! (X)");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
timer.setRepeats(false);
|
||||||
|
timer.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void aiMove() {
|
||||||
|
int[] bestMove = findBestMove();
|
||||||
|
board[bestMove[0]][bestMove[1]] = aiPlayer;
|
||||||
|
buttons[bestMove[0]][bestMove[1]].setText("O");
|
||||||
|
buttons[bestMove[0]][bestMove[1]].setForeground(Color.RED);
|
||||||
|
}
|
||||||
|
|
||||||
|
private int[] findBestMove() {
|
||||||
|
int bestScore = Integer.MIN_VALUE;
|
||||||
|
int[] bestMove = new int[2];
|
||||||
|
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
for (int j = 0; j < 3; j++) {
|
||||||
|
if (board[i][j] == ' ') {
|
||||||
|
board[i][j] = aiPlayer;
|
||||||
|
int score = minimax(0, false);
|
||||||
|
board[i][j] = ' ';
|
||||||
|
|
||||||
|
if (score > bestScore) {
|
||||||
|
bestScore = score;
|
||||||
|
bestMove[0] = i;
|
||||||
|
bestMove[1] = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return bestMove;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int minimax(int depth, boolean isMaximizing) {
|
||||||
|
if (checkWinner(aiPlayer)) return 10 - depth;
|
||||||
|
if (checkWinner(humanPlayer)) return depth - 10;
|
||||||
|
if (isBoardFull()) return 0;
|
||||||
|
|
||||||
|
int bestScore;
|
||||||
|
if (isMaximizing) {
|
||||||
|
bestScore = Integer.MIN_VALUE;
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
for (int j = 0; j < 3; j++) {
|
||||||
|
if (board[i][j] == ' ') {
|
||||||
|
board[i][j] = aiPlayer;
|
||||||
|
int score = minimax(depth + 1, false);
|
||||||
|
board[i][j] = ' ';
|
||||||
|
bestScore = Math.max(score, bestScore);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
bestScore = Integer.MAX_VALUE;
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
for (int j = 0; j < 3; j++) {
|
||||||
|
if (board[i][j] == ' ') {
|
||||||
|
board[i][j] = humanPlayer;
|
||||||
|
int score = minimax(depth + 1, true);
|
||||||
|
board[i][j] = ' ';
|
||||||
|
bestScore = Math.min(score, bestScore);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return bestScore;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean checkWinner(char player) {
|
||||||
|
// Righe e colonne
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
if (board[i][0] == player && board[i][1] == player && board[i][2] == player) return true;
|
||||||
|
if (board[0][i] == player && board[1][i] == player && board[2][i] == player) return true;
|
||||||
|
}
|
||||||
|
// Diagonali
|
||||||
|
if (board[0][0] == player && board[1][1] == player && board[2][2] == player) return true;
|
||||||
|
return board[0][2] == player && board[1][1] == player && board[2][0] == player;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void highlightWinner(char player) {
|
||||||
|
Color winColor = new Color(144, 238, 144);
|
||||||
|
|
||||||
|
// Righe
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
if (board[i][0] == player && board[i][1] == player && board[i][2] == player) {
|
||||||
|
for (int j = 0; j < 3; j++) buttons[i][j].setBackground(winColor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Colonne
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
if (board[0][i] == player && board[1][i] == player && board[2][i] == player) {
|
||||||
|
for (int j = 0; j < 3; j++) buttons[j][i].setBackground(winColor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Diagonali
|
||||||
|
if (board[0][0] == player && board[1][1] == player && board[2][2] == player) {
|
||||||
|
for (int i = 0; i < 3; i++) buttons[i][i].setBackground(winColor);
|
||||||
|
}
|
||||||
|
if (board[0][2] == player && board[1][1] == player && board[2][0] == player) {
|
||||||
|
for (int i = 0; i < 3; i++) buttons[i][2-i].setBackground(winColor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isBoardFull() {
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
for (int j = 0; j < 3; j++) {
|
||||||
|
if (board[i][j] == ' ') return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void resetGame() {
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
for (int j = 0; j < 3; j++) {
|
||||||
|
board[i][j] = ' ';
|
||||||
|
buttons[i][j].setText("");
|
||||||
|
buttons[i][j].setBackground(Color.WHITE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
gameOver = false;
|
||||||
|
statusLabel.setText("Il tuo turno! (X)");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SwingUtilities.invokeLater(TrisGame::new);
|
||||||
|
}
|
||||||
|
}
|
||||||
30
VerificaBlu/.gitignore
vendored
Normal file
30
VerificaBlu/.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
VerificaBlu/.idea/.gitignore
generated
vendored
Normal file
8
VerificaBlu/.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
VerificaBlu/.idea/misc.xml
generated
Normal file
6
VerificaBlu/.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
VerificaBlu/.idea/modules.xml
generated
Normal file
8
VerificaBlu/.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$/VerificaBlu.iml" filepath="$PROJECT_DIR$/VerificaBlu.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
6
VerificaBlu/.idea/vcs.xml
generated
Normal file
6
VerificaBlu/.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
VerificaBlu/VerificaBlu.iml
Normal file
11
VerificaBlu/VerificaBlu.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>
|
||||||
110
VerificaBlu/src/DisegnaFigura.java
Normal file
110
VerificaBlu/src/DisegnaFigura.java
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.*;
|
||||||
|
import java.awt.geom.Ellipse2D;
|
||||||
|
|
||||||
|
public class DisegnaFigura {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SwingUtilities.invokeLater(DisegnaFigura::creaGUI);
|
||||||
|
}
|
||||||
|
private static void creaGUI() {
|
||||||
|
JFrame f = new JFrame("DisegnoSuperPazzoSgravato");
|
||||||
|
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
f.setLayout(new BorderLayout()); // layout dei brordi per rendere meno storpia la finestra
|
||||||
|
JPanel ctrl = new JPanel();
|
||||||
|
|
||||||
|
JTextField wtf = new JTextField(5);
|
||||||
|
JTextField htf = new JTextField(5);
|
||||||
|
|
||||||
|
JButton btnSq = new JButton("Quadrato");
|
||||||
|
JButton btnCirc = new JButton("Cerchio");
|
||||||
|
|
||||||
|
ctrl.add(new JLabel("Larghezza:"));
|
||||||
|
ctrl.add(wtf);
|
||||||
|
ctrl.add(Box.createHorizontalStrut(10)); // Un pochetto di spaziatura per rendere il tutto più leggibile
|
||||||
|
ctrl.add(new JLabel("Altezza:"));
|
||||||
|
ctrl.add(htf);
|
||||||
|
ctrl.add(Box.createHorizontalStrut(20)); // Un pochetto di spaziatura per rendere il tutto più leggibile
|
||||||
|
ctrl.add(btnSq);
|
||||||
|
ctrl.add(btnCirc);
|
||||||
|
|
||||||
|
f.add(ctrl, BorderLayout.NORTH);
|
||||||
|
DrawPanel dp = new DrawPanel(wtf, htf);
|
||||||
|
f.add(dp, BorderLayout.CENTER);
|
||||||
|
// la freccetta è una lambda per rendere più snello il codice: e -> indica cosa eseguire in caso che quella azione sia verificata
|
||||||
|
btnSq.addActionListener(e -> dp.setFigura("quadrato"));
|
||||||
|
btnCirc.addActionListener(e -> dp.setFigura("cerchio"));
|
||||||
|
|
||||||
|
f.pack(); //ridimnezionamento
|
||||||
|
f.setLocationRelativeTo(null);
|
||||||
|
f.setVisible(true); // Rendere visibile la finestra
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class DrawPanel extends JPanel {
|
||||||
|
private String prossima = null;
|
||||||
|
private final JTextField wtf, htf; // Campi di testo di larghezza e altezza
|
||||||
|
|
||||||
|
DrawPanel(JTextField wtf, JTextField htf) {
|
||||||
|
this.wtf = wtf;
|
||||||
|
this.htf = htf;
|
||||||
|
setBackground(Color.WHITE); // Sfondo bianco perchè si
|
||||||
|
// Evento del click click click click click del mouse
|
||||||
|
addMouseListener(new MouseAdapter() {
|
||||||
|
@Override
|
||||||
|
public void mouseClicked(MouseEvent e) {
|
||||||
|
if (prossima == null) return; // Non fae niente se l'utentew non ha selezionato la modalità
|
||||||
|
|
||||||
|
int w = parseInt(wtf.getText(), 50); // Traforma la stringa ad intero, se è vuota prende 50 come valore
|
||||||
|
int h = parseInt(htf.getText(), 50); // Traforma la stringa ad intero, se è vuota prende 50 come valore
|
||||||
|
int x = e.getX(); // x del mouse
|
||||||
|
int y = e.getY(); // y del mouse
|
||||||
|
|
||||||
|
if (prossima.equals("quadrato")) {
|
||||||
|
addShape(new Rectangle(x, y, w, h)); // fai un rettangolo/quadrato
|
||||||
|
} else {
|
||||||
|
addShape(new Ellipse2D.Double(x, y, w, h)); //altrimenti disegna un ellisse
|
||||||
|
}
|
||||||
|
prossima = null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// Modo più carinino per impostrare òla figura
|
||||||
|
void setFigura(String fig) {
|
||||||
|
this.prossima = fig;
|
||||||
|
}
|
||||||
|
// Forme nella finestra
|
||||||
|
private final java.util.List<Shape> shapes = new java.util.ArrayList<>();
|
||||||
|
private void addShape(Shape s) {
|
||||||
|
shapes.add(s);
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
protected void paintComponent(Graphics g) {
|
||||||
|
super.paintComponent(g);
|
||||||
|
Graphics2D g2 = (Graphics2D) g.create();
|
||||||
|
try {
|
||||||
|
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
||||||
|
RenderingHints.VALUE_ANTIALIAS_ON); // In teoria con l'antialiasing non dovrebbe essere spigolosa la figura
|
||||||
|
// Imposta il colore delle figrure per capire meglio se si tratta di un cerchio o di un rettangolo
|
||||||
|
for (Shape s : shapes) {
|
||||||
|
if (s instanceof Rectangle)
|
||||||
|
g2.setColor(Color.BLUE);
|
||||||
|
else
|
||||||
|
g2.setColor(Color.RED);
|
||||||
|
g2.fill(s); // ripempie la figura col colorew selezionato
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
g2.dispose(); // liberazioniamo le risorse una volta finizionato il disegno della figura
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Coinvertire una stringa a intero
|
||||||
|
private static int parseInt(String txt, int defaultVal) {
|
||||||
|
try {
|
||||||
|
return Integer.parseInt(txt.trim());
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
return defaultVal;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user