From 4488c72f2848f8a7a4d7778d0efd03aa966c7680 Mon Sep 17 00:00:00 2001 From: Lyanis Souidi Date: Fri, 28 Apr 2023 21:41:11 +0200 Subject: [PATCH] =?UTF-8?q?Ajout=20de=20classes=20pour=20l'affichage=20du?= =?UTF-8?q?=20r=C3=A9sultat=20des=20simulation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AutoSimulationView.java | 65 +++++++++++++++++++++++++++++++++++++ src/Simulation.java | 48 +++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 src/AutoSimulationView.java create mode 100644 src/Simulation.java diff --git a/src/AutoSimulationView.java b/src/AutoSimulationView.java new file mode 100644 index 0000000..32d9b44 --- /dev/null +++ b/src/AutoSimulationView.java @@ -0,0 +1,65 @@ +import javax.swing.*; +import java.awt.*; + +/** + * The view for the auto simulation + * Display directly the success rate and the average number of moves of the simulations + * @version 1.0 + * @author Amir Daouadi + * @author Lyanis Souidi + */ +public class AutoSimulationView extends JPanel { + /** + * The simulations to display + */ + private Simulation[] simulations; + /** + * The success rate of the simulations + */ + private float success = 0; + + /** + * The average number of moves of the simulations + */ + private float moves = 0; + + /** + * Constructor + * @param simulations The simulations to display + */ + public AutoSimulationView(Simulation[] simulations) { + super(); + this.simulations = simulations; + this.setOpaque(false); + this.setPreferredSize(new Dimension(700, 500)); + } + + /** + * Calculate the success rate and the average number of moves + */ + private void calculate() { + for (Simulation simulation : simulations) { + this.success += simulation.isSuccess() ? 1 : 0; + this.moves += simulation.getMoves(); + } + + this.success = (this.success / simulations.length) * 100; + + this.moves = this.moves / simulations.length; + } + + @Override + public void paintComponent(Graphics g) { + super.paintComponent(g); + calculate(); + g.setColor(Color.BLACK); + g.setFont(new Font("Arial", Font.PLAIN, 20)); + FontMetrics metrics = g.getFontMetrics(g.getFont()); + + String successStr = "Taux de réussite : " + this.success + "%"; + g.drawString(successStr, (getWidth() - metrics.stringWidth(successStr)) / 2, ((getHeight() - metrics.getHeight()) / 2 + metrics.getAscent()) - 50); + + String movesStr = "Nombre de mouvements moyen : " + this.moves; + g.drawString(movesStr, (getWidth() - metrics.stringWidth(movesStr)) / 2, ((getHeight() - metrics.getHeight()) / 2 + metrics.getAscent()) + 50); + } +} diff --git a/src/Simulation.java b/src/Simulation.java new file mode 100644 index 0000000..67e6bb6 --- /dev/null +++ b/src/Simulation.java @@ -0,0 +1,48 @@ +/** + * This class is used to store the number of moves and if the simulation was successful or not + * @version 1.0 + * @author Amir Daouadi + * @author Lyanis Souidi + */ +public class Simulation { + /** + * The number of moves of the simulation + */ + private int moves = 0; + + /** + * If the simulation has been successful or not + */ + private boolean success = false; + + /** + * Get the number of moves of the simulation + * @return The number of moves + */ + public int getMoves() { + return this.moves; + } + + /** + * Get if the simulation has been successful or not + * @return If the simulation has been successful or not + */ + public boolean isSuccess() { + return this.success; + } + + /** + * Add a move to the simulation + */ + public void addMove() { + this.moves++; + } + + /** + * Set if the simulation has been successful or not + * @param success If the simulation has been successful or not + */ + public void setSuccess(boolean success) { + this.success = success; + } +}