creation de la base de donne et l'affichage des score
This commit is contained in:
96
linea/linea/GestionBDD.java
Normal file
96
linea/linea/GestionBDD.java
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
package linea;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class GestionBDD {
|
||||||
|
|
||||||
|
private static final String URL = "jdbc:sqlite:score.db";
|
||||||
|
|
||||||
|
//connexion bdd
|
||||||
|
private static Connection connecter() throws SQLException {
|
||||||
|
return DriverManager.getConnection(URL);
|
||||||
|
}
|
||||||
|
|
||||||
|
//creation de la table score a ca premiere execution
|
||||||
|
public static void creerTableSiAbsente() {
|
||||||
|
|
||||||
|
String sql = """
|
||||||
|
CREATE TABLE IF NOT EXISTS scores (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
score INTEGER NOT NULL,
|
||||||
|
date TEXT NOT NULL
|
||||||
|
);
|
||||||
|
""";
|
||||||
|
|
||||||
|
try (Connection connexion = connecter();
|
||||||
|
Statement statement = connexion.createStatement()) {
|
||||||
|
|
||||||
|
statement.execute(sql);
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ajouter un score a la fin de la partie
|
||||||
|
public static void ajouterScore(int score) {
|
||||||
|
|
||||||
|
String sql = "INSERT INTO scores(score, date) VALUES(?, ?)";
|
||||||
|
|
||||||
|
try (Connection connexion = connecter();
|
||||||
|
PreparedStatement requete = connexion.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
requete.setInt(1, score);
|
||||||
|
requete.setString(2, java.time.LocalDate.now().toString());
|
||||||
|
|
||||||
|
requete.executeUpdate();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Récupérer tous les scores
|
||||||
|
public static List<Integer> recupererTousLesScores() {
|
||||||
|
|
||||||
|
List<Integer> listeScores = new ArrayList<>();
|
||||||
|
|
||||||
|
String sql = "SELECT score FROM scores ORDER BY date DESC";
|
||||||
|
|
||||||
|
try (Connection connexion = connecter();
|
||||||
|
Statement statement = connexion.createStatement();
|
||||||
|
ResultSet resultat = statement.executeQuery(sql)) {
|
||||||
|
|
||||||
|
while (resultat.next()) {
|
||||||
|
listeScores.add(resultat.getInt("score"));
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return listeScores;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Récupérer le meilleur score
|
||||||
|
public static int recupererMeilleurScore() {
|
||||||
|
|
||||||
|
String sql = "SELECT MAX(score) AS meilleur FROM scores";
|
||||||
|
|
||||||
|
try (Connection connexion = connecter();
|
||||||
|
Statement statement = connexion.createStatement();
|
||||||
|
ResultSet resultat = statement.executeQuery(sql)) {
|
||||||
|
|
||||||
|
if (resultat.next()) {
|
||||||
|
return resultat.getInt("meilleur");
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -130,6 +130,45 @@ public class Jeu implements KeyListener, ActionListener{
|
|||||||
fenetre.setVisible(true);
|
fenetre.setVisible(true);
|
||||||
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
|
||||||
|
// 🔹 Création du bouton Voir Scores
|
||||||
|
javax.swing.JButton boutonScores = new javax.swing.JButton("Voir mes scores");
|
||||||
|
boutonScores.setBackground(Color.BLACK); // Couleur fond
|
||||||
|
boutonScores.setForeground(Color.WHITE); // Couleur texte
|
||||||
|
//boutonScores.setFont(new Font("Arial", Font.BOLD, 14)); // Police
|
||||||
|
|
||||||
|
// Position du bouton
|
||||||
|
boutonScores.setBounds(300, 20, 160, 30);
|
||||||
|
|
||||||
|
// Action quand on clique
|
||||||
|
boutonScores.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
|
||||||
|
java.util.List<Integer> scores = GestionBDD.recupererTousLesScores();
|
||||||
|
int meilleurScore = GestionBDD.recupererMeilleurScore();
|
||||||
|
|
||||||
|
StringBuilder message = new StringBuilder();
|
||||||
|
message.append("Meilleur score : ").append(meilleurScore).append("\n\n");
|
||||||
|
message.append("Tous les scores :\n");
|
||||||
|
|
||||||
|
for (int score : scores) {
|
||||||
|
message.append(score).append("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
javax.swing.JOptionPane.showMessageDialog(
|
||||||
|
fenetre,
|
||||||
|
message.toString(),
|
||||||
|
"Mes Scores",
|
||||||
|
javax.swing.JOptionPane.INFORMATION_MESSAGE
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// affichier le bouton à l'écran
|
||||||
|
ecran.setLayout(null);
|
||||||
|
ecran.add(boutonScores);
|
||||||
|
|
||||||
|
|
||||||
// Démarrage du timer, qui rythmera l'animation
|
// Démarrage du timer, qui rythmera l'animation
|
||||||
horloge = new Timer(40, this);
|
horloge = new Timer(40, this);
|
||||||
horloge.start();
|
horloge.start();
|
||||||
@@ -166,6 +205,8 @@ public class Jeu implements KeyListener, ActionListener{
|
|||||||
this.horloge.stop(); // 1. Arrêter le temps
|
this.horloge.stop(); // 1. Arrêter le temps
|
||||||
this.ecran.partiePerdue = true; // 2. Signaler à l'écran
|
this.ecran.partiePerdue = true; // 2. Signaler à l'écran
|
||||||
this.ecran.repaint(); // 3. Forcer l'affichage du texte
|
this.ecran.repaint(); // 3. Forcer l'affichage du texte
|
||||||
|
|
||||||
|
GestionBDD.ajouterScore((int)this.score);// enregistrement du score dans la base de donne
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
labScore.setText("<html><h3>score : " + this.score + "</h3></html>");
|
labScore.setText("<html><h3>score : " + this.score + "</h3></html>");
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ public class LineaAppli {
|
|||||||
//-------------------------------------------------------------------------
|
//-------------------------------------------------------------------------
|
||||||
public static void main(String[] arg) {
|
public static void main(String[] arg) {
|
||||||
|
|
||||||
|
GestionBDD.creerTableSiAbsente();
|
||||||
|
|
||||||
Jeu jeu = new Jeu();
|
Jeu jeu = new Jeu();
|
||||||
|
|
||||||
jeu.demarrer();
|
jeu.demarrer();
|
||||||
|
|||||||
Reference in New Issue
Block a user