Tile par rapport a la base de donnée + fix rotation tuile

This commit is contained in:
Vincent
2024-10-26 22:31:34 +02:00
parent f5c3fa6149
commit 70acc18573
42 changed files with 189 additions and 135 deletions

View File

@@ -1,76 +1,41 @@
package model;
import java.util.Random;
public class Tile {
private int id; // Ajoute l'attribut id
private TerrainType[] terrains; // 2 terrains maximum par tuile
private int segmentsForTerrain1; // Nombre de segments pour le premier terrain
private static final Random random = new Random();
private int segmentsForTerrain1;
private int rotation;
public Tile() {
this.terrains = new TerrainType[2]; // Seulement deux terrains
generateTerrains();
assignSegments();
this.rotation = 0; // Rotation initiale à 0
// Constructeur modifié pour inclure l'ID
public Tile(int id, TerrainType terrain1, TerrainType terrain2, int segmentsForTerrain1) {
this.id = id;
this.terrains = new TerrainType[]{terrain1, terrain2};
this.segmentsForTerrain1 = segmentsForTerrain1;
this.rotation = 0;
}
// Méthode pour tourner la tuile dans le sens des aiguilles d'une montre
public void rotateClockwise() {
rotation = (rotation + 1) % 6; // Modulo 6 pour garder une rotation entre 0 et 5
rotation = (rotation + 1) % 6;
}
// Méthode pour obtenir la rotation actuelle
public void rotateCounterClockwise() {
rotation = (rotation + 5) % 6; // Tourner dans le sens inverse, équivalent à -1 dans un modulo 6
}
public int getRotation() {
return rotation;
}
// Génère deux terrains aléatoires pour la tuile
private void generateTerrains() {
terrains[0] = generateRandomTerrain();
terrains[1] = generateRandomTerrain();
// Assure que les deux terrains sont différents
while (terrains[0] == terrains[1]) {
terrains[1] = generateRandomTerrain();
}
}
// Assigner le nombre de segments pour chaque terrain avec plus de diversité
private void assignSegments() {
// Terrain 1 occupe entre 1 et 5 segments, le reste pour le terrain 2
this.segmentsForTerrain1 = random.nextInt(5) + 1;
}
// Génère un terrain aléatoire avec plus de variété dans les probabilités
private TerrainType generateRandomTerrain() {
int rand = random.nextInt(100);
if (rand < 15) {
return TerrainType.MER; // 15% MER
} else if (rand < 30) {
return TerrainType.CHAMP; // 15% CHAMP
} else if (rand < 50) {
return TerrainType.PRE; // 20% PRE
} else if (rand < 75) {
return TerrainType.FORET; // 25% FORET
} else {
return TerrainType.MONTAGNE; // 25% MONTAGNE
}
public int getId() {
return id;
}
public TerrainType getTerrain(int index) {
if (index >= 0 && index < 2) {
return terrains[index];
}
return null;
return index >= 0 && index < 2 ? terrains[index] : null;
}
public int getSegmentsForTerrain(int index) {
if (index == 0) {
return segmentsForTerrain1; // Nombre de segments pour le premier terrain
} else {
return 6 - segmentsForTerrain1; // Le reste pour le second terrain
}
return index == 0 ? segmentsForTerrain1 : 6 - segmentsForTerrain1;
}
}

View File

@@ -0,0 +1,44 @@
package model;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class TileDatabaseManager {
private static final String DB_URL = "jdbc:mariadb://dwarves.iut-fbleau.fr/akagundu";
private static final String USER = "akagundu";
private static final String PASSWORD = "dersim62Lodek";
public List<Tile> getTilesBySeries(int idSeries) {
List<Tile> tiles = new ArrayList<>();
String query = "SELECT id, couleur1, couleur2, chiffre FROM Tuile WHERE id_serie = ? ORDER BY id ASC";
try (Connection cnx = DriverManager.getConnection(DB_URL, USER, PASSWORD);
PreparedStatement pst = cnx.prepareStatement(query)) {
pst.setInt(1, idSeries);
try (ResultSet rs = pst.executeQuery()) {
while (rs.next()) {
int id = rs.getInt("id"); // Récupère l'ID de la tuile
String couleur1 = rs.getString("couleur1");
String couleur2 = rs.getString("couleur2");
int segmentsForTerrain1 = rs.getInt("chiffre");
System.out.println("Récupération de la tuile avec ID : " + id); // Message de débogage
// Crée la tuile avec l'ID et les autres paramètres
Tile tile = new Tile(id, TerrainType.valueOf(couleur1),
couleur2 != null ? TerrainType.valueOf(couleur2) : null,
segmentsForTerrain1);
tiles.add(tile);
}
}
} catch (SQLException e) {
System.err.println("Database error: " + e.getMessage());
}
return tiles;
}
}