diff --git a/TestV1/GameController.java b/TestV1/GameController.java index 0f4f206..264dc07 100644 --- a/TestV1/GameController.java +++ b/TestV1/GameController.java @@ -1,15 +1,12 @@ public class GameController { private GameView view; - private Board board; - // Constructeur qui prend GameView comme argument public GameController(GameView view) { this.view = view; - this.board = view.getBoard(); // Associer le Board à partir de la vue } - // Méthode pour démarrer le jeu public void startGame() { - view.setVisible(true); // Afficher la fenêtre + System.out.println("Bienvenue dans Dorfromantik simplifié !"); + view.showTile(); } } diff --git a/TestV1/Main.java b/TestV1/Main.java index f222a87..86515a4 100644 --- a/TestV1/Main.java +++ b/TestV1/Main.java @@ -1,8 +1,15 @@ public class Main { public static void main(String[] args) { - Board board = new Board(); - GameView view = new GameView(board); - GameController controller = new GameController(view); // Passe la vue au contrôleur - controller.startGame(); // Démarre le jeu + // Exemple : création d'une tuile avec deux terrains + Tile tile = new Tile(Terrain.MER, Terrain.FORET, 1); + + // Vue graphique + GameView view = new GameView(tile); + + // Contrôleur + GameController controller = new GameController(view); + + // Démarrer le jeu + controller.startGame(); } } diff --git a/TestV1/TestEnAttendantResolutionBug/Board.class b/TestV1/TestEnAttendantResolutionBug/Board.class deleted file mode 100644 index b1c7877..0000000 Binary files a/TestV1/TestEnAttendantResolutionBug/Board.class and /dev/null differ diff --git a/TestV1/TestEnAttendantResolutionBug/Board.java b/TestV1/TestEnAttendantResolutionBug/Board.java index de436b7..29d0800 100644 --- a/TestV1/TestEnAttendantResolutionBug/Board.java +++ b/TestV1/TestEnAttendantResolutionBug/Board.java @@ -3,24 +3,30 @@ import java.util.HashMap; import java.util.Map; public class Board { - private final Map tiles; + private Map tiles; public Board() { - this.tiles = new HashMap<>(); + tiles = new HashMap<>(); } + // Vérifie si la position est déjà occupée public boolean isPositionOccupied(Point position) { return tiles.containsKey(position); } + // Ajoute une tuile à une position donnée public void addTile(Point position, Tile tile) { - tiles.put(position, tile); + if (!isPositionOccupied(position)) { + tiles.put(position, tile); + } } + // Récupère la tuile à une position donnée public Tile getTile(Point position) { return tiles.get(position); } + // Récupère toutes les tuiles public Map getTiles() { return tiles; } diff --git a/TestV1/TestEnAttendantResolutionBug/GameController.class b/TestV1/TestEnAttendantResolutionBug/GameController.class deleted file mode 100644 index a7ec8c1..0000000 Binary files a/TestV1/TestEnAttendantResolutionBug/GameController.class and /dev/null differ diff --git a/TestV1/TestEnAttendantResolutionBug/GameController.java b/TestV1/TestEnAttendantResolutionBug/GameController.java index 5fdb271..45709d8 100644 --- a/TestV1/TestEnAttendantResolutionBug/GameController.java +++ b/TestV1/TestEnAttendantResolutionBug/GameController.java @@ -1,13 +1,11 @@ public class GameController { - private final Board board; - private final GameView view; + private GameView view; public GameController(GameView view) { - this.board = new Board(); this.view = view; } public void startGame() { - view.setVisible(true); + view.showBoard(); } } diff --git a/TestV1/TestEnAttendantResolutionBug/GameView$1.class b/TestV1/TestEnAttendantResolutionBug/GameView$1.class deleted file mode 100644 index a987ae0..0000000 Binary files a/TestV1/TestEnAttendantResolutionBug/GameView$1.class and /dev/null differ diff --git a/TestV1/TestEnAttendantResolutionBug/GameView$2.class b/TestV1/TestEnAttendantResolutionBug/GameView$2.class deleted file mode 100644 index 0bc4cc0..0000000 Binary files a/TestV1/TestEnAttendantResolutionBug/GameView$2.class and /dev/null differ diff --git a/TestV1/TestEnAttendantResolutionBug/GameView.class b/TestV1/TestEnAttendantResolutionBug/GameView.class deleted file mode 100644 index 2b3d584..0000000 Binary files a/TestV1/TestEnAttendantResolutionBug/GameView.class and /dev/null differ diff --git a/TestV1/TestEnAttendantResolutionBug/GameView.java b/TestV1/TestEnAttendantResolutionBug/GameView.java index 4073feb..3d9f377 100644 --- a/TestV1/TestEnAttendantResolutionBug/GameView.java +++ b/TestV1/TestEnAttendantResolutionBug/GameView.java @@ -4,10 +4,12 @@ import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; public class GameView extends JFrame { - private final Board board; - 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 + private Board board; + private int hexRadius = 30; // Rayon du grand hexagone + + // Dimensions calculées des hexagones + private int hexWidth = (int) (1.5 * hexRadius); // Largeur de l'hexagone + private int hexHeight = (int) (Math.sqrt(3) * hexRadius); // Hauteur de l'hexagone public GameView(Board board) { this.board = board; @@ -16,24 +18,18 @@ public class GameView extends JFrame { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); + // Gestion des clics de souris pour placer une tuile addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { - handleMouseClick(e.getPoint()); + handleMouseClick(e.getX(), e.getY()); } }); } - 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 - board.addTile(hexPosition, newTile); - repaint(); - } + public void showBoard() { + setVisible(true); + repaint(); } @Override @@ -41,54 +37,99 @@ public class GameView extends JFrame { super.paint(g); Graphics2D g2d = (Graphics2D) g; - // Dessiner toutes les tuiles placées + // Dessiner toutes les tuiles du plateau 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(g2d, position.x, position.y, tile); } } - private void drawLargeHexagon(Graphics2D g2d, int x, int y, Tile tile) { - Polygon hexagon = new Polygon(); - 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); - } + private void drawLargeHexagon(Graphics2D g2d, int centerX, int centerY, Tile tile) { + // Couleurs des triangles + Color color1 = getColorForTerrain(tile.getTerrain()); + Color color2 = color1.darker(); // Variante plus sombre pour contraste - g2d.setColor(getColorForTerrain(tile.getTerrain())); - g2d.fillPolygon(hexagon); - g2d.setColor(Color.BLACK); - g2d.drawPolygon(hexagon); + // Dessiner les 6 triangles pour former l'hexagone + for (int i = 0; i < 6; i++) { + // Dessiner chaque triangle équilatéral avec sa couleur associée + drawTriangle(g2d, centerX, centerY, i, hexRadius, i % 2 == 0 ? color1 : color2); + } + } + + private void drawTriangle(Graphics2D g2d, int centerX, int centerY, int triangleIndex, int radius, Color color) { + // Orientation de chaque triangle (décalage de 60° par rapport à chaque triangle) + double angle = Math.toRadians(60 * triangleIndex); + + // Calcul des points de chaque triangle autour du centre du grand hexagone + int x1 = centerX + (int) (Math.cos(angle) * radius); + int y1 = centerY + (int) (Math.sin(angle) * radius); + + int x2 = centerX + (int) (Math.cos(angle + Math.toRadians(60)) * radius); + int y2 = centerY + (int) (Math.sin(angle + Math.toRadians(60)) * radius); + + int x3 = centerX + (int) (Math.cos(angle + Math.toRadians(120)) * radius); + int y3 = centerY + (int) (Math.sin(angle + Math.toRadians(120)) * radius); + + // Créer un polygone pour dessiner le triangle + Polygon triangle = new Polygon(); + triangle.addPoint(centerX, centerY); // Centre de l'hexagone + triangle.addPoint(x1, y1); // Premier sommet + triangle.addPoint(x2, y2); // Deuxième sommet + triangle.addPoint(x3, y3); // Troisième sommet + + // Remplir le triangle avec la couleur + g2d.setColor(color); + g2d.fillPolygon(triangle); } 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; - }; + switch (terrain) { + case MER: + return Color.BLUE; + case CHAMP: + return Color.YELLOW; + case PRE: + return Color.GREEN; + case FORET: + return new Color(34, 139, 34); // Vert foncé + case MONTAGNE: + return Color.GRAY; + default: + return Color.WHITE; + } } - 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); - } - - private int calculateScreenX(Point position) { - int col = position.x; - return (int) (col * 1.5 * hexRadius + hexWidth / 2.0); - } - - 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); - } -} + private void handleMouseClick(int mouseX, int mouseY) { + // Calcul des coordonnées de la grille de manière à respecter la structure hexagonale + + // Calcul de la colonne et de la ligne en fonction de la taille des hexagones + int gridX = (int) (mouseX / (hexWidth * 0.75)); // Calcul de la colonne + + // Calcul de la ligne en fonction de la hauteur des hexagones + int gridY = (int) (mouseY / hexHeight); // Calcul de la ligne + + // Détecter si c'est une rangée paire ou impaire et appliquer un décalage + if (gridY % 2 != 0) { // Si la ligne est impaire + gridX += 1; // Décalage horizontal pour les rangées impaires + } + + // Calculer les coordonnées X et Y en pixels + int finalX = (int) (gridX * hexWidth * 0.75); // Calcul des coordonnées X + int finalY = gridY * hexHeight; // Calcul des coordonnées Y + + // Créer un objet Point avec les coordonnées finales + Point position = new Point(finalX, finalY); + + // Vérifier si la position est déjà occupée par une autre tuile + if (!board.isPositionOccupied(position)) { + // Créer une nouvelle tuile (ici, une tuile de forêt) + Tile newTile = new Tile(Terrain.FORET); + + // Ajouter la tuile sur le plateau + board.addTile(position, newTile); + + // Rafraîchir l'affichage + repaint(); + } + } +} \ No newline at end of file diff --git a/TestV1/TestEnAttendantResolutionBug/Main.class b/TestV1/TestEnAttendantResolutionBug/Main.class deleted file mode 100644 index 1e41647..0000000 Binary files a/TestV1/TestEnAttendantResolutionBug/Main.class and /dev/null differ diff --git a/TestV1/TestEnAttendantResolutionBug/Main.java b/TestV1/TestEnAttendantResolutionBug/Main.java index 7589352..86fb72a 100644 --- a/TestV1/TestEnAttendantResolutionBug/Main.java +++ b/TestV1/TestEnAttendantResolutionBug/Main.java @@ -1,9 +1,15 @@ public class Main { public static void main(String[] args) { + // Création d'un plateau Board board = new Board(); + + // Vue avec le plateau GameView view = new GameView(board); + + // Contrôleur GameController controller = new GameController(view); + // Démarrer le jeu controller.startGame(); } -} \ No newline at end of file +} diff --git a/TestV1/TestEnAttendantResolutionBug/Terrain.class b/TestV1/TestEnAttendantResolutionBug/Terrain.class deleted file mode 100644 index f0a67a9..0000000 Binary files a/TestV1/TestEnAttendantResolutionBug/Terrain.class and /dev/null differ diff --git a/TestV1/TestEnAttendantResolutionBug/Terrain.java b/TestV1/TestEnAttendantResolutionBug/Terrain.java index 02135c5..3cbbe46 100644 --- a/TestV1/TestEnAttendantResolutionBug/Terrain.java +++ b/TestV1/TestEnAttendantResolutionBug/Terrain.java @@ -1,3 +1,3 @@ public enum Terrain { - MER, CHAMP, FORET, PRE, MONTAGNE + MER, CHAMP, PRE, FORET, MONTAGNE, VIDE } diff --git a/TestV1/TestEnAttendantResolutionBug/Tile.class b/TestV1/TestEnAttendantResolutionBug/Tile.class deleted file mode 100644 index 8ca866b..0000000 Binary files a/TestV1/TestEnAttendantResolutionBug/Tile.class and /dev/null differ diff --git a/TestV1/TestEnAttendantResolutionBug/Tile.java b/TestV1/TestEnAttendantResolutionBug/Tile.java index 21da20c..d476f15 100644 --- a/TestV1/TestEnAttendantResolutionBug/Tile.java +++ b/TestV1/TestEnAttendantResolutionBug/Tile.java @@ -1,5 +1,5 @@ public class Tile { - private Terrain terrain; + private Terrain terrain; // Un seul terrain pour toute la tuile public Tile(Terrain terrain) { this.terrain = terrain; diff --git a/TestV1/TestEnAttendantResolutionBug/View/BoardView.java b/TestV1/TestEnAttendantResolutionBug/View/BoardView.java new file mode 100644 index 0000000..8757694 --- /dev/null +++ b/TestV1/TestEnAttendantResolutionBug/View/BoardView.java @@ -0,0 +1,61 @@ +package view; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import model.Board; +import model.Tile; +import controller.GameController; + +public class BoardView extends JPanel { + private GameController controller; + private int rows = 5; + private int cols = 5; + + public BoardView(Board board, GameController controller) { + this.controller = controller; + setLayout(new GridLayout(rows, cols, 5, 5)); + initializeBoard(); + } + + // Méthode pour assigner le contrôleur après création de l'instance + public void setController(GameController controller) { + this.controller = controller; + } + + private void initializeBoard() { + for (int i = 0; i < rows * cols; i++) { + JPanel tilePanel = new JPanel(); + tilePanel.setBackground(Color.LIGHT_GRAY); + tilePanel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); + + tilePanel.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + handleTilePlacement(tilePanel); + } + }); + + add(tilePanel); + } + } + + private void handleTilePlacement(JPanel tilePanel) { + if (controller != null) { // Assure que le contrôleur est bien assigné + Tile tile = controller.getNextTile(); + if (tile != null) { + tilePanel.setBackground(Color.GREEN); + tilePanel.add(new JLabel(tile.getType())); + revalidate(); + repaint(); + controller.placeTile(tile); + } + } + } + + public void refreshBoard() { + revalidate(); + repaint(); + } +} diff --git a/TestV1/TestEnAttendantResolutionBug/View/GameController.java b/TestV1/TestEnAttendantResolutionBug/View/GameController.java new file mode 100644 index 0000000..0b3fe7c --- /dev/null +++ b/TestV1/TestEnAttendantResolutionBug/View/GameController.java @@ -0,0 +1,31 @@ +package controller; + +import model.Game; +import model.Tile; +import view.GameView; + +public class GameController { + private Game game; + private GameView gameView; + private Tile currentTile; + + public GameController(Game game, GameView gameView) { + this.game = game; + this.gameView = gameView; + this.currentTile = null; // Initialise sans tuile au début + } + + public void selectNextTile(Tile tile) { + this.currentTile = tile; + } + + public Tile getNextTile() { + return currentTile; // Retourne la tuile actuellement sélectionnée + } + + public void placeTile(Tile tile) { + game.placeTile(tile); + gameView.update(game); + currentTile = null; // Réinitialise après placement + } +} diff --git a/TestV1/TestEnAttendantResolutionBug/View/GameView.java b/TestV1/TestEnAttendantResolutionBug/View/GameView.java new file mode 100644 index 0000000..99c91e4 --- /dev/null +++ b/TestV1/TestEnAttendantResolutionBug/View/GameView.java @@ -0,0 +1,52 @@ +package view; + +import model.Game; +import model.Tile; +import controller.GameController; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class GameView extends JFrame { + private JLabel scoreLabel; + private BoardView boardView; + private GameController controller; // Ajoute le contrôleur ici + + public GameView(Game game) { // Constructeur sans le contrôleur en paramètre + setTitle("Dorfromantik en Java"); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setSize(500, 500); + setLayout(new BorderLayout()); + + scoreLabel = new JLabel("Score : " + game.getScore()); + boardView = new BoardView(game.getBoard(), null); // Initialise BoardView sans contrôleur + + JButton nextTileButton = new JButton("Sélectionner une tuile"); + nextTileButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (controller != null) { // Vérifie que le contrôleur est bien assigné + Tile tile = new Tile("forêt", new String[]{"terre", "terre", "eau", "terre", "eau", "terre"}); + controller.selectNextTile(tile); + } + } + }); + + add(scoreLabel, BorderLayout.NORTH); + add(boardView, BorderLayout.CENTER); + add(nextTileButton, BorderLayout.SOUTH); + } + + // Nouvelle méthode setController pour permettre d'assigner le contrôleur après création + public void setController(GameController controller) { + this.controller = controller; + boardView.setController(controller); // Passe le contrôleur à BoardView + } + + public void update(Game game) { + scoreLabel.setText("Score : " + game.getScore()); + boardView.refreshBoard(); + } +} diff --git a/TestV1/TestEnAttendantResolutionBug/controller/GameController.java b/TestV1/TestEnAttendantResolutionBug/controller/GameController.java new file mode 100644 index 0000000..4b08d39 --- /dev/null +++ b/TestV1/TestEnAttendantResolutionBug/controller/GameController.java @@ -0,0 +1,20 @@ +package controller; + +import model.Game; +import model.Tile; +import view.GameView; + +public class GameController { + private Game game; + private GameView gameView; + + public GameController(Game game, GameView gameView) { + this.game = game; + this.gameView = gameView; + } + + public void placeTile(Tile tile) { + game.placeTile(tile); + gameView.update(game); + } +} diff --git a/TestV1/TestEnAttendantResolutionBug/model/Board.java b/TestV1/TestEnAttendantResolutionBug/model/Board.java new file mode 100644 index 0000000..b5e26f2 --- /dev/null +++ b/TestV1/TestEnAttendantResolutionBug/model/Board.java @@ -0,0 +1,26 @@ +package model; + +import java.util.ArrayList; +import java.util.List; + +public class Board { + private List tiles; + + public Board() { + tiles = new ArrayList<>(); + } + + public void addTile(Tile tile) { + tiles.add(tile); + } + + public List getTiles() { + return tiles; + } + + public boolean isPlacementValid(Tile tile) { + // Logique simplifiée pour vérifier si la tuile peut être placée + // Ici, tu pourrais vérifier les bords de la tuile et du plateau + return true; // Retourne true pour simplifier + } +} diff --git a/TestV1/TestEnAttendantResolutionBug/model/Game.java b/TestV1/TestEnAttendantResolutionBug/model/Game.java new file mode 100644 index 0000000..5cf832a --- /dev/null +++ b/TestV1/TestEnAttendantResolutionBug/model/Game.java @@ -0,0 +1,26 @@ +package model; + +public class Game { + private Board board; + private int score; + + public Game() { + board = new Board(); + score = 0; + } + + public Board getBoard() { + return board; + } + + public int getScore() { + return score; + } + + public void placeTile(Tile tile) { + if (board.isPlacementValid(tile)) { + board.addTile(tile); + score += 10; // Par exemple, chaque tuile ajoute 10 points + } + } +} diff --git a/TestV1/TestEnAttendantResolutionBug/model/Tile.java b/TestV1/TestEnAttendantResolutionBug/model/Tile.java new file mode 100644 index 0000000..f175519 --- /dev/null +++ b/TestV1/TestEnAttendantResolutionBug/model/Tile.java @@ -0,0 +1,19 @@ +package model; + +public class Tile { + private String type; // Par exemple : "forêt", "rivière", "champ", etc. + private String[] edges; // Les types des bords de la tuile (ex: "eau", "terre") + + public Tile(String type, String[] edges) { + this.type = type; + this.edges = edges; + } + + public String getType() { + return type; + } + + public String getEdge(int index) { + return edges[index]; + } +} diff --git a/TestV1/TestEnAttendantResolutionBug/view/BoardView.java b/TestV1/TestEnAttendantResolutionBug/view/BoardView.java new file mode 100644 index 0000000..e81065b --- /dev/null +++ b/TestV1/TestEnAttendantResolutionBug/view/BoardView.java @@ -0,0 +1,24 @@ +package view; + +import javax.swing.*; +import model.Board; +import model.Tile; +import java.awt.*; + +public class BoardView extends JPanel { + private Board board; + + public BoardView(Board board) { + this.board = board; + setLayout(new GridLayout(5, 5, 5, 5)); // Exemple de grille 5x5 pour le plateau + } + + public void refreshBoard() { + removeAll(); + for (Tile tile : board.getTiles()) { + add(new TileView(tile.getType())); + } + revalidate(); + repaint(); + } +} diff --git a/TestV1/TestEnAttendantResolutionBug/view/GameView.java b/TestV1/TestEnAttendantResolutionBug/view/GameView.java new file mode 100644 index 0000000..4028ecb --- /dev/null +++ b/TestV1/TestEnAttendantResolutionBug/view/GameView.java @@ -0,0 +1,28 @@ +package view; + +import model.Game; +import javax.swing.*; +import java.awt.*; + +public class GameView extends JFrame { + private JLabel scoreLabel; + private BoardView boardView; + + public GameView(Game game) { + setTitle("Dorfromantik en Java"); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setSize(400, 400); + setLayout(new BorderLayout()); + + scoreLabel = new JLabel("Score : " + game.getScore()); + boardView = new BoardView(game.getBoard()); + + add(scoreLabel, BorderLayout.NORTH); + add(boardView, BorderLayout.CENTER); + } + + public void update(Game game) { + scoreLabel.setText("Score : " + game.getScore()); + boardView.refreshBoard(); + } +} diff --git a/TestV1/TestEnAttendantResolutionBug/view/TileView.java b/TestV1/TestEnAttendantResolutionBug/view/TileView.java new file mode 100644 index 0000000..b5bf724 --- /dev/null +++ b/TestV1/TestEnAttendantResolutionBug/view/TileView.java @@ -0,0 +1,21 @@ +package view; + +import javax.swing.*; +import java.awt.*; + +public class TileView extends JPanel { + private String type; + + public TileView(String type) { + this.type = type; + setPreferredSize(new Dimension(50, 50)); // Taille de la tuile + setBackground(Color.LIGHT_GRAY); + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + g.setColor(Color.BLACK); + g.drawString(type, 10, 25); // Affiche le type de la tuile au centre + } +}