Ajout de classes pour l'affichage du résultat des simulation

This commit is contained in:
Lyanis SOUIDI 2023-04-28 21:41:11 +02:00
parent 0edf40fbf5
commit 4488c72f28
Signed by: Lyanis SOUIDI
GPG Key ID: 251ADD56CFE6A854
2 changed files with 113 additions and 0 deletions

View File

@ -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);
}
}

48
src/Simulation.java Normal file
View File

@ -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;
}
}