resolution problème de tuiles

This commit is contained in:
Lenny FOULOU
2024-11-19 21:50:14 +01:00
parent e7f3da2d68
commit 42c52e011c
4 changed files with 59 additions and 46 deletions

View File

@@ -3,24 +3,30 @@ package model;
public class Tile {
private final Terrain terrain1;
private final Terrain terrain2;
private final int terrain1Sides; // Nombre de côtés attribués à terrain1 (1, 2 ou 3)
// Constructeur pour une tuile avec un ou deux terrains
public Tile(Terrain terrain1, Terrain terrain2) {
public Tile(Terrain terrain1, Terrain terrain2, int terrain1Sides) {
this.terrain1 = terrain1;
this.terrain2 = terrain2;
this.terrain1Sides = terrain1Sides;
}
// Getter pour terrain1
public Terrain getTerrain1() {
return terrain1;
}
// Getter pour terrain2
public Terrain getTerrain2() {
return terrain2;
}
// Vérifie si la tuile contient un seul terrain
public int getTerrain1Sides() {
return terrain1Sides;
}
public int getTerrain2Sides() {
return 6 - terrain1Sides; // Le reste des côtés
}
public boolean hasSingleTerrain() {
return terrain2 == null;
}

View File

@@ -9,14 +9,23 @@ public class TileGenerator {
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;
if (random.nextBoolean()) { // 50% chance d'avoir un seul terrain
return new Tile(terrain1, null, 6); // Toutes les faces au même terrain
}
return new Tile(terrain1, terrain2);
Terrain terrain2;
do {
terrain2 = Terrain.values()[random.nextInt(Terrain.values().length)];
} while (terrain2 == terrain1); // Assurer deux terrains différents
int terrain1Sides = switch (random.nextInt(3)) { // 0, 1 ou 2 -> 1+5, 2+4, 3+3
case 0 -> 1;
case 1 -> 2;
default -> 3;
};
return new Tile(terrain1, terrain2, terrain1Sides);
}
}

View File

@@ -17,6 +17,7 @@ public class GameView extends JFrame {
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, long seed) {
this.board = board;
this.tileGenerator = new TileGenerator(seed); // Initialisation avec la seed
@@ -55,45 +56,42 @@ public class GameView extends JFrame {
}
}
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);
public void drawLargeHexagon(Graphics g, int x, int y, Tile tile) {
int r = hexRadius;
int h = (int) (Math.sqrt(3) / 2 * r);
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()) {
g.setColor(getColorForTerrain(tile.getTerrain1()));
g.fillPolygon(xPoints, yPoints, 6);
} else {
// Deux terrains : appliquer la répartition spécifique
int terrain1Sides = tile.getTerrain1Sides();
Color color1 = getColorForTerrain(tile.getTerrain1());
Color color2 = getColorForTerrain(tile.getTerrain2());
for (int i = 0; i < 6; i++) {
int next = (i + 1) % 6; // Prochain point
if (i < terrain1Sides) {
g.setColor(color1);
} else {
g.setColor(color2);
}
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
new int[]{x, xPoints[i], xPoints[next]},
new int[]{y, yPoints[i], yPoints[next]},
3
);
}
// Contour de l'hexagone
g.setColor(Color.BLACK);
g.drawPolygon(xPoints, yPoints, 6);
}
g.setColor(Color.BLACK);
g.drawPolygon(xPoints, yPoints, 6);
}
// Dans la classe GameView.java

Binary file not shown.