possibilte de placer les tuiles sur le plateau + ajout d'une seed + probleme de variation de tuiles
This commit is contained in:
@@ -1,18 +0,0 @@
|
||||
SRC_DIR = src
|
||||
BUILD_DIR = build
|
||||
MAIN_CLASS = controller.Main
|
||||
|
||||
SOURCES = $(shell find $(SRC_DIR) -name "*.java")
|
||||
CLASSES = $(patsubst $(SRC_DIR)/%.java, $(BUILD_DIR)/%.class, $(SOURCES))
|
||||
|
||||
all: $(CLASSES)
|
||||
|
||||
$(BUILD_DIR)/%.class: $(SRC_DIR)/%.java
|
||||
mkdir -p $(dir $@)
|
||||
javac -d $(BUILD_DIR) $<
|
||||
|
||||
run: all
|
||||
java -cp $(BUILD_DIR) $(MAIN_CLASS)
|
||||
|
||||
clean:
|
||||
rm -rf $(BUILD_DIR)
|
@@ -2,20 +2,23 @@ package controller;
|
||||
|
||||
import model.Board;
|
||||
import view.GameView;
|
||||
import model.Tile;
|
||||
import utils.Database;
|
||||
import utils.TileRepository;
|
||||
import java.sql.SQLException;
|
||||
import model.TileGenerator;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Classe principale pour démarrer l'application.
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
// Initialisation du modèle et de la vue
|
||||
Board board = new Board(); // Créer une instance du modèle (Board)
|
||||
GameView view = new GameView(board); // Créer une instance de la vue (GameView)
|
||||
|
||||
// Création du contrôleur avec Board et GameView
|
||||
GameController controller = new GameController(board, view);
|
||||
|
||||
// Démarrage du jeu
|
||||
controller.startGame();
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
long seed = System.currentTimeMillis();
|
||||
Board board = new Board();
|
||||
GameView gameView = new GameView(board, seed);
|
||||
gameView.setVisible(true);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@@ -1,33 +1,27 @@
|
||||
package model;
|
||||
|
||||
/**
|
||||
* Classe représentant une tuile du jeu.
|
||||
*/
|
||||
public class Tile {
|
||||
private final Terrain terrain;
|
||||
private final Terrain terrain1;
|
||||
private final Terrain terrain2;
|
||||
|
||||
/**
|
||||
* Constructeur pour une tuile.
|
||||
*
|
||||
* @param terrain Le type de terrain de la tuile.
|
||||
*/
|
||||
public Tile(Terrain terrain) {
|
||||
this.terrain = terrain;
|
||||
// Constructeur pour une tuile avec un ou deux terrains
|
||||
public Tile(Terrain terrain1, Terrain terrain2) {
|
||||
this.terrain1 = terrain1;
|
||||
this.terrain2 = terrain2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtient le terrain de la tuile.
|
||||
*
|
||||
* @return Le terrain de la tuile.
|
||||
*/
|
||||
public Terrain getTerrain() {
|
||||
return terrain;
|
||||
// Getter pour terrain1
|
||||
public Terrain getTerrain1() {
|
||||
return terrain1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Tile{" +
|
||||
"terrain=" + terrain +
|
||||
'}';
|
||||
// Getter pour terrain2
|
||||
public Terrain getTerrain2() {
|
||||
return terrain2;
|
||||
}
|
||||
|
||||
// Vérifie si la tuile contient un seul terrain
|
||||
public boolean hasSingleTerrain() {
|
||||
return terrain2 == null;
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,22 @@
|
||||
package model;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class TileGenerator {
|
||||
private final Random random;
|
||||
|
||||
public TileGenerator(long seed) {
|
||||
this.random = new Random(seed);
|
||||
}
|
||||
|
||||
// Générer une tuile avec un ou deux terrains
|
||||
public Tile generateRandomTile() {
|
||||
// Générer un terrain aléatoire
|
||||
Terrain terrain1 = Terrain.values()[random.nextInt(Terrain.values().length)];
|
||||
|
||||
// Décider si la tuile a un seul terrain ou deux
|
||||
Terrain terrain2 = random.nextBoolean() ? Terrain.values()[random.nextInt(Terrain.values().length)] : null;
|
||||
|
||||
return new Tile(terrain1, terrain2);
|
||||
}
|
||||
}
|
63
TestV1/TestEnAttendantResolutionBug/src/utils/Database.java
Normal file
63
TestV1/TestEnAttendantResolutionBug/src/utils/Database.java
Normal file
@@ -0,0 +1,63 @@
|
||||
package utils;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class Database {
|
||||
private static final String URL = "jdbc:mariadb://dwarves.iut-fbleau.fr/foulou";
|
||||
private static final String LOGIN = "foulou";
|
||||
private static final String PASSWORD = "foulou";
|
||||
|
||||
private Connection database;
|
||||
|
||||
public Database() throws SQLException {
|
||||
try {
|
||||
// Chargement explicite du driver
|
||||
System.out.println("Tentative de chargement du driver MariaDB...");
|
||||
Class.forName("org.mariadb.jdbc.Driver");
|
||||
System.out.println("Driver MariaDB chargé avec succès.");
|
||||
|
||||
try {
|
||||
// Connexion à la base de données
|
||||
System.out.println("Tentative de connexion à la base de données...");
|
||||
this.database = DriverManager.getConnection(URL, LOGIN, PASSWORD);
|
||||
System.out.println("Connexion réussie !");
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Échec de la connexion à la base de données : " + e.getMessage());
|
||||
throw new SQLException("Erreur de connexion : " + e.getMessage(), e);
|
||||
}
|
||||
} catch (ClassNotFoundException e) {
|
||||
System.err.println("Erreur : Le driver MariaDB n'a pas été trouvé dans le classpath.");
|
||||
throw new SQLException("Driver MariaDB introuvable dans le classpath", e);
|
||||
}
|
||||
}
|
||||
|
||||
public Connection getDatabase() {
|
||||
return this.database;
|
||||
}
|
||||
|
||||
public void close() {
|
||||
try {
|
||||
if (this.database != null && !this.database.isClosed()) {
|
||||
this.database.close();
|
||||
System.out.println("Connexion fermée.");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Erreur lors de la fermeture de la base de données : " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
// Crée une instance de la classe Database et teste la connexion
|
||||
Database db = new Database();
|
||||
System.out.println("Connexion à la base de données réussie !");
|
||||
|
||||
// Fermer la connexion après test
|
||||
db.close();
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Erreur lors de la connexion : " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,34 @@
|
||||
package utils;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import model.Tile;
|
||||
|
||||
|
||||
public class TileRepository {
|
||||
|
||||
private final Database database;
|
||||
|
||||
public TileRepository(Database database) {
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
public void saveTile(Tile tile) throws SQLException {
|
||||
String query = "INSERT INTO tiles (terrain1, terrain2) VALUES (?, ?)";
|
||||
|
||||
try (Connection connection = database.getDatabase();
|
||||
PreparedStatement statement = connection.prepareStatement(query)) {
|
||||
|
||||
statement.setString(1, tile.getTerrain1().toString());
|
||||
|
||||
if (tile.getTerrain2() != null) {
|
||||
statement.setString(2, tile.getTerrain2().toString());
|
||||
} else {
|
||||
statement.setNull(2, java.sql.Types.VARCHAR);
|
||||
}
|
||||
|
||||
statement.executeUpdate();
|
||||
}
|
||||
}
|
||||
}
|
@@ -7,16 +7,19 @@ import java.awt.event.MouseEvent;
|
||||
import model.Board;
|
||||
import model.Tile;
|
||||
import model.Terrain;
|
||||
import model.TileGenerator;
|
||||
|
||||
|
||||
public class GameView extends JFrame {
|
||||
private final Board board;
|
||||
private final TileGenerator tileGenerator;
|
||||
private final int hexRadius = 60;
|
||||
private final int hexWidth = (int) (Math.sqrt(3) * hexRadius); // Largeur d'un hexagone
|
||||
private final int hexHeight = 2 * hexRadius; // Hauteur d'un hexagone
|
||||
|
||||
public GameView(Board board) {
|
||||
public GameView(Board board, long seed) {
|
||||
this.board = board;
|
||||
this.tileGenerator = new TileGenerator(seed); // Initialisation avec la seed
|
||||
setTitle("Dorfromantik - Plateau");
|
||||
setSize(800, 800);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
@@ -31,12 +34,10 @@ public class GameView extends JFrame {
|
||||
}
|
||||
|
||||
private void handleMouseClick(Point clickPoint) {
|
||||
// Calcul de la position en coordonnées hexagonales
|
||||
Point hexPosition = calculateHexCoordinates(clickPoint);
|
||||
|
||||
// Vérifie si la position est libre
|
||||
if (!board.isPositionOccupied(hexPosition)) {
|
||||
Tile newTile = new Tile(Terrain.FORET); // Exemple : tuile de forêt
|
||||
Tile newTile = tileGenerator.generateRandomTile();
|
||||
board.addTile(hexPosition, newTile);
|
||||
repaint();
|
||||
}
|
||||
@@ -45,79 +46,104 @@ public class GameView extends JFrame {
|
||||
@Override
|
||||
public void paint(Graphics g) {
|
||||
super.paint(g);
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
|
||||
// Dessiner toutes les tuiles placées
|
||||
|
||||
for (Point position : board.getTiles().keySet()) {
|
||||
Tile tile = board.getTile(position);
|
||||
int x = calculateScreenX(position);
|
||||
int y = calculateScreenY(position);
|
||||
drawLargeHexagon(g2d, x, y, tile);
|
||||
drawLargeHexagon(g, x, y, tile);
|
||||
}
|
||||
}
|
||||
|
||||
private void drawLargeHexagon(Graphics2D g2d, int x, int y, Tile tile) {
|
||||
Polygon hexagon = new Polygon();
|
||||
Point center = new Point(x, y); // Centre de l'hexagone
|
||||
|
||||
// Calculer les points des sommets de l'hexagone
|
||||
for (int i = 0; i < 6; i++) {
|
||||
double angle = Math.toRadians(60 * i);
|
||||
int px = x + (int) (Math.cos(angle) * hexRadius);
|
||||
int py = y + (int) (Math.sin(angle) * hexRadius);
|
||||
hexagon.addPoint(px, py);
|
||||
public void drawLargeHexagon(Graphics g, int x, int y, Tile tile) {
|
||||
int r = hexRadius;
|
||||
int h = (int) (Math.sqrt(3) / 2 * r);
|
||||
|
||||
// Coordonnées des sommets
|
||||
int[] xPoints = {x + r, x + r / 2, x - r / 2, x - r, x - r / 2, x + r / 2};
|
||||
int[] yPoints = {y, y + h, y + h, y, y - h, y - h};
|
||||
|
||||
if (tile.hasSingleTerrain()) {
|
||||
// Une seule couleur : remplir tout l'hexagone
|
||||
g.setColor(getColorForTerrain(tile.getTerrain1()));
|
||||
g.fillPolygon(xPoints, yPoints, 6);
|
||||
} else {
|
||||
// Deux terrains : Diviser l'hexagone en deux moitiés
|
||||
Color color1 = getColorForTerrain(tile.getTerrain1());
|
||||
Color color2 = getColorForTerrain(tile.getTerrain2());
|
||||
|
||||
// Premier terrain (moitié supérieure)
|
||||
g.setColor(color1);
|
||||
g.fillPolygon(
|
||||
new int[]{xPoints[0], xPoints[1], xPoints[2], xPoints[3]},
|
||||
new int[]{yPoints[0], yPoints[1], yPoints[2], yPoints[3]},
|
||||
4
|
||||
);
|
||||
|
||||
// Deuxième terrain (moitié inférieure)
|
||||
g.setColor(color2);
|
||||
g.fillPolygon(
|
||||
new int[]{xPoints[3], xPoints[4], xPoints[5], xPoints[0]},
|
||||
new int[]{yPoints[3], yPoints[4], yPoints[5], yPoints[0]},
|
||||
4
|
||||
);
|
||||
}
|
||||
|
||||
// Dessiner l'intérieur de l'hexagone en divisant en triangles
|
||||
for (int i = 0; i < 6; i++) {
|
||||
// Trouver les coordonnées des points du côté de l'hexagone
|
||||
int x1 = hexagon.xpoints[i];
|
||||
int y1 = hexagon.ypoints[i];
|
||||
int x2 = hexagon.xpoints[(i + 1) % 6]; // Le point suivant (modulo 6)
|
||||
int y2 = hexagon.ypoints[(i + 1) % 6];
|
||||
|
||||
// Dessiner un triangle avec le centre de l'hexagone
|
||||
Polygon triangle = new Polygon();
|
||||
triangle.addPoint(center.x, center.y); // Ajouter le centre
|
||||
triangle.addPoint(x1, y1); // Ajouter le premier sommet
|
||||
triangle.addPoint(x2, y2); // Ajouter le deuxième sommet
|
||||
|
||||
g2d.setColor(getColorForTerrain(tile.getTerrain()));
|
||||
g2d.fillPolygon(triangle); // Remplir le triangle
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawPolygon(triangle); // Dessiner les contours du triangle
|
||||
}
|
||||
|
||||
// Dessiner l'extérieur de l'hexagone
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawPolygon(hexagon);
|
||||
// Contour de l'hexagone
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawPolygon(xPoints, yPoints, 6);
|
||||
}
|
||||
|
||||
|
||||
private Color getColorForTerrain(Terrain terrain) {
|
||||
return switch (terrain) {
|
||||
case MER -> Color.BLUE;
|
||||
case CHAMP -> Color.YELLOW;
|
||||
case FORET -> new Color(34, 139, 34);
|
||||
case PRE -> Color.GREEN;
|
||||
case MONTAGNE -> Color.GRAY;
|
||||
};
|
||||
|
||||
|
||||
// Dans la classe GameView.java
|
||||
private Color getColorForTerrain(Terrain terrain) {
|
||||
if (terrain == null) {
|
||||
return Color.GRAY; // Retourner une couleur par défaut si le terrain est null
|
||||
}
|
||||
|
||||
private Point calculateHexCoordinates(Point clickPoint) {
|
||||
int col = (int) Math.round((clickPoint.x - hexWidth / 2.0) / (1.5 * hexRadius));
|
||||
int row = (int) Math.round((clickPoint.y - hexHeight / 2.0 - (col % 2) * hexHeight / 4.0) / hexHeight);
|
||||
return new Point(col, row);
|
||||
// Mappage des couleurs pour chaque terrain
|
||||
switch (terrain) {
|
||||
case MER:
|
||||
return new Color(0, 0, 255); // Bleu pour MER
|
||||
case CHAMP:
|
||||
return new Color(0, 255, 0); // Vert pour CHAMP
|
||||
case FORET:
|
||||
return new Color(34, 139, 34); // Vert foncé pour FORET
|
||||
case PRE:
|
||||
return new Color(255, 255, 0); // Jaune pour PRE
|
||||
case MONTAGNE:
|
||||
return new Color(139, 69, 19); // Marron pour MONTAGNE
|
||||
default:
|
||||
return Color.GRAY; // Couleur par défaut pour les cas non gérés
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private Point calculateHexCoordinates(Point clickPoint) {
|
||||
// Rayon et espacement vertical/hexagonal
|
||||
int r = hexRadius;
|
||||
double h = Math.sqrt(3) * r / 2; // Hauteur effective entre deux lignes
|
||||
|
||||
// Calcul approximatif de la colonne et de la ligne
|
||||
int col = (int) Math.round((double) clickPoint.x / (1.5 * r));
|
||||
int row = (int) Math.round((double) (clickPoint.y - (col % 2) * h) / (Math.sqrt(3) * r));
|
||||
|
||||
return new Point(col, row);
|
||||
}
|
||||
|
||||
|
||||
private int calculateScreenX(Point position) {
|
||||
int col = position.x;
|
||||
return (int) (col * 1.5 * hexRadius + hexWidth / 2.0);
|
||||
return col * (int) (1.5 * hexRadius); // Espacement horizontal
|
||||
}
|
||||
|
||||
|
||||
private int calculateScreenY(Point position) {
|
||||
int col = position.x;
|
||||
int row = position.y;
|
||||
return (int) (row * hexHeight + (col % 2) * hexHeight / 2.0 + hexHeight / 2.0);
|
||||
return row * (int) (Math.sqrt(3) * hexRadius) + (col % 2) * (int) (Math.sqrt(3) / 2 * hexRadius);
|
||||
// Décalage vertical pour les colonnes impaires
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user