From 7c755a5ce308bb7bbea7652d6844847638d23f07 Mon Sep 17 00:00:00 2001 From: Victor Date: Wed, 26 Jun 2024 22:43:57 +0200 Subject: [PATCH] tout ca tout ca --- .../fr/iut_fbleau/but3/dev6_2/Chessboard.java | 2 +- .../but3/dev6_2/EightQueensSolver.java | 10 ++--- .../but3/dev6_2/ChessboardTest.java | 37 +++++++++++++++++-- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/main/java/fr/iut_fbleau/but3/dev6_2/Chessboard.java b/src/main/java/fr/iut_fbleau/but3/dev6_2/Chessboard.java index ae9e29b..7959664 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev6_2/Chessboard.java +++ b/src/main/java/fr/iut_fbleau/but3/dev6_2/Chessboard.java @@ -5,7 +5,7 @@ import java.util.List; public class Chessboard { public static final int SIZE = 8; - private final int[][] gameBoard = new int[SIZE][SIZE]; + public final int[][] gameBoard = new int[SIZE][SIZE]; private final List queensPosition = new ArrayList<>(); private static final String ANSI_RED = "\u001B[31m"; private static final String ANSI_RESET = "\u001B[0m"; diff --git a/src/main/java/fr/iut_fbleau/but3/dev6_2/EightQueensSolver.java b/src/main/java/fr/iut_fbleau/but3/dev6_2/EightQueensSolver.java index 578c438..df90fc6 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev6_2/EightQueensSolver.java +++ b/src/main/java/fr/iut_fbleau/but3/dev6_2/EightQueensSolver.java @@ -1,7 +1,7 @@ package fr.iut_fbleau.but3.dev6_2; public class EightQueensSolver { - private Chessboard chessboard = new Chessboard(); + public Chessboard chessboard = new Chessboard(); public EightQueensSolver() { @@ -26,7 +26,7 @@ public class EightQueensSolver { long startTimeVictor = System.currentTimeMillis(); System.out.println("Début Victor"); - System.out.println(solver.Solver(0)); + System.out.println(solver.SolverVic(0)); System.out.println("Fin Victor"); long endTimeVictor = System.currentTimeMillis(); System.out.println("Temps d'exécution Victor: " + (endTimeVictor - startTimeVictor) + " ms"); @@ -34,7 +34,7 @@ public class EightQueensSolver { } - private Boolean Solver(int level) { + public Boolean SolverVic(int level) { if (chessboard.getNumberOfQueen() == Chessboard.SIZE) { return true; } @@ -43,7 +43,7 @@ public class EightQueensSolver { int y = i / Chessboard.SIZE; if (chessboard.VerifAccessible(x, y)) { chessboard.placeQueen(x, y); - if (!Solver(y)) { + if (!SolverVic(y)) { chessboard.removeQueen(x, y); } else { return true; @@ -53,7 +53,7 @@ public class EightQueensSolver { return false; } - private boolean SolverSim(int line){ + public boolean SolverSim(int line){ if(chessboard.getNumberOfQueen() == Chessboard.SIZE){ return true; } diff --git a/src/test/java/fr/iut_fbleau/but3/dev6_2/ChessboardTest.java b/src/test/java/fr/iut_fbleau/but3/dev6_2/ChessboardTest.java index f742bc8..fb7c4d1 100644 --- a/src/test/java/fr/iut_fbleau/but3/dev6_2/ChessboardTest.java +++ b/src/test/java/fr/iut_fbleau/but3/dev6_2/ChessboardTest.java @@ -9,10 +9,12 @@ import org.junit.jupiter.api.Test; class ChessboardTest { private Chessboard chessboard; + private EightQueensSolver solver; @BeforeEach public void beforeEach() { this.chessboard = new Chessboard(); + this.solver = new EightQueensSolver(); } @Test @@ -74,14 +76,40 @@ class ChessboardTest { return x >= 0 && x < Chessboard.SIZE && y >= 0 && y < Chessboard.SIZE; } + + void testWinningSolution(Chessboard chessboard) { + assertEquals(Chessboard.SIZE, chessboard.getNumberOfQueen(),"Devrait etre egal a "+Chessboard.SIZE); + for(int i = 0; i< Chessboard.SIZE ;i++){ + for(int j = 0; j< Chessboard.SIZE ;j++){ + if(chessboard.gameBoard[i][j] != 0){ + chessboard.removeQueen(i,j); + assertTrue(chessboard.VerifAccessible(i,j)); + chessboard.placeQueen(i,j); + } + } + } + } + @Test - void testWinningSolution() { - // Placer les reines aux positions données + void TestSolverSim() { + solver.SolverSim(0); + testWinningSolution(solver.chessboard); + } + + @Test + void TestSolverVic() { + solver.SolverVic(0); + testWinningSolution(solver.chessboard); + } + + @Test + void TestKnownedWinningSolution(){ + int[][] positions = { {0, 0}, {1, 4}, {2, 7}, {3, 5}, {4, 2}, {5, 6}, {6, 1}, {7, 3} }; - + for (int[] pos : positions) { assertTrue(this.chessboard.VerifAccessible(pos[0], pos[1]), "La position (" + pos[0] + "," + pos[1] + ") devrait être accessible."); @@ -89,7 +117,8 @@ class ChessboardTest { assertFalse(this.chessboard.VerifAccessible(pos[0], pos[1]), "La position (" + pos[0] + "," + pos[1] + ") ne devrait plus être accessible."); } + + testWinningSolution(this.chessboard); } - }