From e172cdd45b02d65fb03bd860fdb0b7ef37ddf553 Mon Sep 17 00:00:00 2001 From: Victor Date: Wed, 26 Jun 2024 22:05:52 +0200 Subject: [PATCH 1/4] tout ca tout ca --- .../but3/dev6_2/EightQueensSolver.java | 16 ++-- .../but3/dev6_2/ChessboardTest.java | 81 +++++++++++++++++-- .../steps/EightQueensSolverStepsTest.java | 24 ++++++ 3 files changed, 108 insertions(+), 13 deletions(-) create mode 100644 src/test/java/fr/iut_fbleau/but3/dev6_2/steps/EightQueensSolverStepsTest.java 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 70f7f56..578c438 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 @@ -9,28 +9,28 @@ public class EightQueensSolver { - public void main(String[] args){ + public static void main(String[] args){ + EightQueensSolver solver = new EightQueensSolver(); - // Début de la résolution par méthode simplifiée long startTimeSimSim = System.currentTimeMillis(); System.out.println("Début SimSim"); - System.out.println(SolverSim(0)); + System.out.println(solver.SolverSim(0)); System.out.println("Fin SimSim"); long endTimeSimSim = System.currentTimeMillis(); System.out.println("Temps d'exécution SimSim: " + (endTimeSimSim - startTimeSimSim) + " ms"); - chessboard.printChessboard(); + solver.chessboard.printChessboard(); - chessboard = new Chessboard(); // Réinitialisation du plateau + solver.chessboard = new Chessboard(); - // Début de la résolution par méthode classique + long startTimeVictor = System.currentTimeMillis(); System.out.println("Début Victor"); - System.out.println(Solver(0)); + System.out.println(solver.Solver(0)); System.out.println("Fin Victor"); long endTimeVictor = System.currentTimeMillis(); System.out.println("Temps d'exécution Victor: " + (endTimeVictor - startTimeVictor) + " ms"); - chessboard.printChessboard(); + solver.chessboard.printChessboard(); } 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 d1c8384..5caa59c 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 @@ -1,6 +1,8 @@ package fr.iut_fbleau.but3.dev6_2; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -9,15 +11,84 @@ class ChessboardTest { private Chessboard chessboard; @BeforeEach - public void beforeEach(){ + public void beforeEach() { this.chessboard = new Chessboard(); } @Test - void placeAQueen(){ - this.chessboard.placeQueen(0,0); - assertEquals(1, this.chessboard.getNumberOfQueen()); + void testReplaceQueen() { + //test place une reine + this.chessboard.placeQueen(0, 0); + assertEquals(1, this.chessboard.getNumberOfQueen(), "Il devrait y avoir 1 reine sur l'échiquier."); + assertFalse(this.chessboard.VerifAccessible(0, 0), "La position (0,0) ne devrait pas être accessible."); + + // Déplacer la reine + this.chessboard.removeQueen(0, 0); + this.chessboard.placeQueen(5, 3); + assertEquals(1, this.chessboard.getNumberOfQueen(), "Il devrait y avoir 1 reine sur l'échiquier."); + assertTrue(this.chessboard.VerifAccessible(0, 0), "La position (0,0) devrait être accessible."); + assertFalse(this.chessboard.VerifAccessible(5, 3), "La position (1,1) ne devrait pas être accessible."); } + @Test + void placeMultipleQueens() { + this.chessboard.placeQueen(0, 0); + this.chessboard.placeQueen(1, 2); + this.chessboard.placeQueen(2, 4); + assertEquals(3, this.chessboard.getNumberOfQueen()); + } + + @Test + void removeQueen() { + this.chessboard.placeQueen(0, 0); + this.chessboard.removeQueen(0, 0); + assertEquals(0, this.chessboard.getNumberOfQueen()); + } + + @Test + void verifAccessible() { + int middle = Chessboard.SIZE / 2; + this.chessboard.placeQueen(middle, middle); -} \ No newline at end of file + for (int i = 0; i < Chessboard.SIZE; i++) { + assertFalse(this.chessboard.VerifAccessible(i, i)); + assertFalse(this.chessboard.VerifAccessible(middle, i)); + assertFalse(this.chessboard.VerifAccessible(i, middle)); + if (isValidPosition(middle + i, middle + i)) { + assertFalse(this.chessboard.VerifAccessible(middle + i, middle + i)); + } + if (isValidPosition(middle - i, middle - i)) { + assertFalse(this.chessboard.VerifAccessible(middle - i, middle - i)); + } + if (isValidPosition(middle + i, middle - i)) { + assertFalse(this.chessboard.VerifAccessible(middle + i, middle - i)); + } + if (isValidPosition(middle - i, middle + i)) { + assertFalse(this.chessboard.VerifAccessible(middle - i, middle + i)); + } + } + } + + private boolean isValidPosition(int x, int y) { + return x >= 0 && x < Chessboard.SIZE && y >= 0 && y < Chessboard.SIZE; + } + + @Test + void testWinningSolution() { + // Placer les reines aux positions données + 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."); + this.chessboard.placeQueen(pos[0], pos[1]); + assertFalse(this.chessboard.VerifAccessible(pos[0], pos[1]), + "La position (" + pos[0] + "," + pos[1] + ") ne devrait plus être accessible."); + } + } + + +} diff --git a/src/test/java/fr/iut_fbleau/but3/dev6_2/steps/EightQueensSolverStepsTest.java b/src/test/java/fr/iut_fbleau/but3/dev6_2/steps/EightQueensSolverStepsTest.java new file mode 100644 index 0000000..f790141 --- /dev/null +++ b/src/test/java/fr/iut_fbleau/but3/dev6_2/steps/EightQueensSolverStepsTest.java @@ -0,0 +1,24 @@ +package fr.iut_fbleau.but3.dev6_2.steps; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import fr.iut_fbleau.but3.dev6_2.Chessboard; + +class ChessboardTest { + + private Chessboard chessboard; + + @BeforeEach + public void beforeEach(){ + this.chessboard = new Chessboard(); + } + + @Test + void placeAQueen(){ + this.chessboard.placeQueen(0,0); + assertEquals(1, this.chessboard.getNumberOfQueen()); + } + +} \ No newline at end of file From de585edfb75ff18e2823a00785d1544886361758 Mon Sep 17 00:00:00 2001 From: Victor Date: Wed, 26 Jun 2024 22:07:00 +0200 Subject: [PATCH 2/4] tout ca tout ca --- src/test/java/fr/iut_fbleau/but3/dev6_2/ChessboardTest.java | 1 + 1 file changed, 1 insertion(+) 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 5caa59c..f742bc8 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 @@ -24,6 +24,7 @@ class ChessboardTest { // Déplacer la reine this.chessboard.removeQueen(0, 0); + assertEquals(0, this.chessboard.getNumberOfQueen(), "Il devrait y avoir 0 reine sur l'échiquier."); this.chessboard.placeQueen(5, 3); assertEquals(1, this.chessboard.getNumberOfQueen(), "Il devrait y avoir 1 reine sur l'échiquier."); assertTrue(this.chessboard.VerifAccessible(0, 0), "La position (0,0) devrait être accessible."); From 7c755a5ce308bb7bbea7652d6844847638d23f07 Mon Sep 17 00:00:00 2001 From: Victor Date: Wed, 26 Jun 2024 22:43:57 +0200 Subject: [PATCH 3/4] 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); } - } From c0bdf8543432030611d248e3e0791165f486947c Mon Sep 17 00:00:00 2001 From: Victor Date: Wed, 26 Jun 2024 22:49:23 +0200 Subject: [PATCH 4/4] tout ca tout ca --- .../java/fr/iut_fbleau/but3/dev6_2/Chessboard.java | 6 +++++- .../fr/iut_fbleau/but3/dev6_2/ChessboardTest.java | 12 +++++++++++- 2 files changed, 16 insertions(+), 2 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 7959664..d9a4d73 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; - public final int[][] gameBoard = new int[SIZE][SIZE]; + private 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"; @@ -14,6 +14,10 @@ public class Chessboard { // Constructeur vide } + public int getTile(int x,int y){ + return gameBoard[x][y]; + } + public void placeQueen(int x, int y) { if(VerifAccessible(x,y)){ gameBoard[x][y] = 1; 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 fb7c4d1..e7ecfa0 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 @@ -39,6 +39,16 @@ class ChessboardTest { this.chessboard.placeQueen(1, 2); this.chessboard.placeQueen(2, 4); assertEquals(3, this.chessboard.getNumberOfQueen()); + int cpt=0; + //double test to assure getNumberOfQueen works + for(int i = 0; i< Chessboard.SIZE ;i++){ + for(int j = 0; j< Chessboard.SIZE ;j++){ + if(this.chessboard.getTile(i, j) == 1){ + cpt += 1; +; } + } + } + assertEquals(3, cpt); } @Test @@ -81,7 +91,7 @@ class ChessboardTest { 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){ + if(chessboard.getTile(i,j) != 0){ chessboard.removeQueen(i,j); assertTrue(chessboard.VerifAccessible(i,j)); chessboard.placeQueen(i,j);