diff --git a/TestV1/GameController.java b/TestV1/GameController.java index 264dc07..0f4f206 100644 --- a/TestV1/GameController.java +++ b/TestV1/GameController.java @@ -1,12 +1,15 @@ 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() { - System.out.println("Bienvenue dans Dorfromantik simplifié !"); - view.showTile(); + view.setVisible(true); // Afficher la fenêtre } } diff --git a/TestV1/Main.java b/TestV1/Main.java index 86515a4..f222a87 100644 --- a/TestV1/Main.java +++ b/TestV1/Main.java @@ -1,15 +1,8 @@ public class Main { public static void main(String[] args) { - // 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(); + 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 } } diff --git a/TestV1/TestEnAttendantResolutionBug/Board.class b/TestV1/TestEnAttendantResolutionBug/Board.class new file mode 100644 index 0000000..b1c7877 Binary files /dev/null and b/TestV1/TestEnAttendantResolutionBug/Board.class differ diff --git a/TestV1/TestEnAttendantResolutionBug/Board.java b/TestV1/TestEnAttendantResolutionBug/Board.java index 29d0800..de436b7 100644 --- a/TestV1/TestEnAttendantResolutionBug/Board.java +++ b/TestV1/TestEnAttendantResolutionBug/Board.java @@ -3,30 +3,24 @@ import java.util.HashMap; import java.util.Map; public class Board { - private Map tiles; + private final Map tiles; public Board() { - tiles = new HashMap<>(); + this.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) { - if (!isPositionOccupied(position)) { - tiles.put(position, tile); - } + 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 new file mode 100644 index 0000000..a7ec8c1 Binary files /dev/null and b/TestV1/TestEnAttendantResolutionBug/GameController.class differ diff --git a/TestV1/TestEnAttendantResolutionBug/GameController.java b/TestV1/TestEnAttendantResolutionBug/GameController.java index 45709d8..5fdb271 100644 --- a/TestV1/TestEnAttendantResolutionBug/GameController.java +++ b/TestV1/TestEnAttendantResolutionBug/GameController.java @@ -1,11 +1,13 @@ public class GameController { - private GameView view; + private final Board board; + private final GameView view; public GameController(GameView view) { + this.board = new Board(); this.view = view; } public void startGame() { - view.showBoard(); + view.setVisible(true); } } diff --git a/TestV1/TestEnAttendantResolutionBug/GameView$1.class b/TestV1/TestEnAttendantResolutionBug/GameView$1.class new file mode 100644 index 0000000..a987ae0 Binary files /dev/null and b/TestV1/TestEnAttendantResolutionBug/GameView$1.class differ diff --git a/TestV1/TestEnAttendantResolutionBug/GameView$2.class b/TestV1/TestEnAttendantResolutionBug/GameView$2.class new file mode 100644 index 0000000..0bc4cc0 Binary files /dev/null and b/TestV1/TestEnAttendantResolutionBug/GameView$2.class differ diff --git a/TestV1/TestEnAttendantResolutionBug/GameView.class b/TestV1/TestEnAttendantResolutionBug/GameView.class new file mode 100644 index 0000000..2b3d584 Binary files /dev/null and b/TestV1/TestEnAttendantResolutionBug/GameView.class differ diff --git a/TestV1/TestEnAttendantResolutionBug/GameView.java b/TestV1/TestEnAttendantResolutionBug/GameView.java index 3d9f377..4073feb 100644 --- a/TestV1/TestEnAttendantResolutionBug/GameView.java +++ b/TestV1/TestEnAttendantResolutionBug/GameView.java @@ -4,12 +4,10 @@ import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; public class GameView extends JFrame { - 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 + 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 public GameView(Board board) { this.board = board; @@ -18,18 +16,24 @@ 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.getX(), e.getY()); + handleMouseClick(e.getPoint()); } }); } - public void showBoard() { - setVisible(true); - repaint(); + 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(); + } } @Override @@ -37,99 +41,54 @@ public class GameView extends JFrame { super.paint(g); Graphics2D g2d = (Graphics2D) g; - // Dessiner toutes les tuiles du plateau + // Dessiner toutes les tuiles placées for (Point position : board.getTiles().keySet()) { Tile tile = board.getTile(position); - drawLargeHexagon(g2d, position.x, position.y, tile); + int x = calculateScreenX(position); + int y = calculateScreenY(position); + drawLargeHexagon(g2d, x, y, tile); } } - 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 - - // Dessiner les 6 triangles pour former l'hexagone + private void drawLargeHexagon(Graphics2D g2d, int x, int y, Tile tile) { + Polygon hexagon = new Polygon(); 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); + 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 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); + g2d.setColor(getColorForTerrain(tile.getTerrain())); + g2d.fillPolygon(hexagon); + g2d.setColor(Color.BLACK); + g2d.drawPolygon(hexagon); } private Color getColorForTerrain(Terrain terrain) { - 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; - } + 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; + }; } - 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 + 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); + } +} diff --git a/TestV1/TestEnAttendantResolutionBug/Main.class b/TestV1/TestEnAttendantResolutionBug/Main.class new file mode 100644 index 0000000..1e41647 Binary files /dev/null and b/TestV1/TestEnAttendantResolutionBug/Main.class differ diff --git a/TestV1/TestEnAttendantResolutionBug/Main.java b/TestV1/TestEnAttendantResolutionBug/Main.java index 86fb72a..7589352 100644 --- a/TestV1/TestEnAttendantResolutionBug/Main.java +++ b/TestV1/TestEnAttendantResolutionBug/Main.java @@ -1,15 +1,9 @@ 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 new file mode 100644 index 0000000..f0a67a9 Binary files /dev/null and b/TestV1/TestEnAttendantResolutionBug/Terrain.class differ diff --git a/TestV1/TestEnAttendantResolutionBug/Terrain.java b/TestV1/TestEnAttendantResolutionBug/Terrain.java index 3cbbe46..02135c5 100644 --- a/TestV1/TestEnAttendantResolutionBug/Terrain.java +++ b/TestV1/TestEnAttendantResolutionBug/Terrain.java @@ -1,3 +1,3 @@ public enum Terrain { - MER, CHAMP, PRE, FORET, MONTAGNE, VIDE + MER, CHAMP, FORET, PRE, MONTAGNE } diff --git a/TestV1/TestEnAttendantResolutionBug/Tile.class b/TestV1/TestEnAttendantResolutionBug/Tile.class new file mode 100644 index 0000000..8ca866b Binary files /dev/null and b/TestV1/TestEnAttendantResolutionBug/Tile.class differ diff --git a/TestV1/TestEnAttendantResolutionBug/Tile.java b/TestV1/TestEnAttendantResolutionBug/Tile.java index d476f15..21da20c 100644 --- a/TestV1/TestEnAttendantResolutionBug/Tile.java +++ b/TestV1/TestEnAttendantResolutionBug/Tile.java @@ -1,5 +1,5 @@ public class Tile { - private Terrain terrain; // Un seul terrain pour toute la tuile + private Terrain terrain; public Tile(Terrain terrain) { this.terrain = terrain; diff --git a/TestV1/TestEnAttendantResolutionBug/View/BoardView.java b/TestV1/TestEnAttendantResolutionBug/View/BoardView.java deleted file mode 100644 index 8757694..0000000 --- a/TestV1/TestEnAttendantResolutionBug/View/BoardView.java +++ /dev/null @@ -1,61 +0,0 @@ -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 deleted file mode 100644 index 0b3fe7c..0000000 --- a/TestV1/TestEnAttendantResolutionBug/View/GameController.java +++ /dev/null @@ -1,31 +0,0 @@ -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 deleted file mode 100644 index 99c91e4..0000000 --- a/TestV1/TestEnAttendantResolutionBug/View/GameView.java +++ /dev/null @@ -1,52 +0,0 @@ -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 deleted file mode 100644 index 4b08d39..0000000 --- a/TestV1/TestEnAttendantResolutionBug/controller/GameController.java +++ /dev/null @@ -1,20 +0,0 @@ -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 deleted file mode 100644 index b5e26f2..0000000 --- a/TestV1/TestEnAttendantResolutionBug/model/Board.java +++ /dev/null @@ -1,26 +0,0 @@ -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 deleted file mode 100644 index 5cf832a..0000000 --- a/TestV1/TestEnAttendantResolutionBug/model/Game.java +++ /dev/null @@ -1,26 +0,0 @@ -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 deleted file mode 100644 index f175519..0000000 --- a/TestV1/TestEnAttendantResolutionBug/model/Tile.java +++ /dev/null @@ -1,19 +0,0 @@ -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 deleted file mode 100644 index e81065b..0000000 --- a/TestV1/TestEnAttendantResolutionBug/view/BoardView.java +++ /dev/null @@ -1,24 +0,0 @@ -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 deleted file mode 100644 index 4028ecb..0000000 --- a/TestV1/TestEnAttendantResolutionBug/view/GameView.java +++ /dev/null @@ -1,28 +0,0 @@ -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 deleted file mode 100644 index b5bf724..0000000 --- a/TestV1/TestEnAttendantResolutionBug/view/TileView.java +++ /dev/null @@ -1,21 +0,0 @@ -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 - } -}