From e3eda3a4576e16d4e9a16f2b41a4c5dbc78f1b71 Mon Sep 17 00:00:00 2001 From: Victor Date: Sat, 22 Jun 2024 00:11:15 +0200 Subject: [PATCH] changes --- .../iut_fbleau/but3/dev6_2/Chessboard.class | Bin 0 -> 2020 bytes .../but3/dev6_2/EightQueensSolver.class | Bin 0 -> 1124 bytes bin/fr/iut_fbleau/but3/dev6_2/Position.class | Bin 0 -> 1260 bytes .../fr/iut_fbleau/but3/dev6_2/Chessboard.java | 32 ++++++++++-------- .../but3/dev6_2/EightQueensSolver.java | 25 +++++++++++--- 5 files changed, 38 insertions(+), 19 deletions(-) create mode 100644 bin/fr/iut_fbleau/but3/dev6_2/Chessboard.class create mode 100644 bin/fr/iut_fbleau/but3/dev6_2/EightQueensSolver.class create mode 100644 bin/fr/iut_fbleau/but3/dev6_2/Position.class diff --git a/bin/fr/iut_fbleau/but3/dev6_2/Chessboard.class b/bin/fr/iut_fbleau/but3/dev6_2/Chessboard.class new file mode 100644 index 0000000000000000000000000000000000000000..8a66e1a7c9492f6e74390e3bdb2d5f2ca780780c GIT binary patch literal 2020 zcmbVMO>-MX5Pc)9ELmDd@`q$>Nlxr2SQ5cX91sLK#Z0}6k{{h8U&#s8%!h{r;nd$E7*KfLe`o}*Weh**? z?+4+5*AGR74}A=gyL^x5nq1$`tyJ%dn$6HRr|FtK&)`jG*8K=Dq#9;UYumRQ)u!O> zT(xaq%GJeQ{`PFHct==P)!=5`k07~hZI$}rhpJ*g%Kh8CC6-)sm|@t7*0!}~ZqYRP zep$2ZAWp$6eTGyFV}#+kAfdDhN*ui^|xyfQ9L6bI>z8j zmr9xS5aLMqkyJ5((-i7BF!AU0Izu$=M(jyTA+tF|37k|hg|iHbEi6jalmzWz*J$fC z$vL-bin>|S?%=NSgOg<3KU zsskm)zsH+xu|jaM^k#>L!?|-M?~WeYHb+=O+c@VYRy#$v5TEvGq`N4xny_HJTcv3YH0neTRB zBiq7ularq3$GZ#@PrgdC(9H!VsbU^# ze=o-HG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C$}=d#zykZSoP7~5@d`u#01K5C AdH?_b literal 0 HcmV?d00001 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 9d9b672..c1645b5 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 @@ -4,36 +4,38 @@ import java.util.ArrayList; import java.util.List; public class Chessboard { - private static final int SIZE = 8; + public static final int SIZE = 8; private final int[][] gameBoard = new int[SIZE][SIZE]; - private final List queensPosition = new ArrayList<>(); - public Chessboard(){ - + public Chessboard() { + // Constructeur vide } - - - public void placeQueen(int x, int y){ + public void placeQueen(int x, int y) { gameBoard[x][y] = 1; - queensPosition.add(new Position(x,y)); + queensPosition.add(new Position(x, y)); } - public Boolean VerifAccessible(int x, int y){ + public void removeQueen(int x, int y) { + gameBoard[x][y] = 0; + queensPosition.removeIf(pos -> pos.x() == x && pos.y() == y); // Utilise removeIf pour supprimer l'objet correspondant + } + + public Boolean VerifAccessible(int x, int y) { for (int i = 0; i < SIZE; i++) { - int DigonaleAscendante = y - (x - i); - int DigonaleDescendante = y + (x - i); - if(gameBoard[i][y] != 0){ + int diagonaleAscendante = y - (x - i); + int diagonaleDescendante = y + (x - i); + if (gameBoard[i][y] != 0) { return false; } - if(gameBoard[x][i] != 0){ + if (gameBoard[x][i] != 0) { return false; } - if (DigonaleAscendante >= 0 && DigonaleAscendante < SIZE && gameBoard[i][DigonaleAscendante] == 1) { + if (diagonaleAscendante >= 0 && diagonaleAscendante < SIZE && gameBoard[i][diagonaleAscendante] == 1) { return false; } - if (DigonaleDescendante >= 0 && DigonaleAscendante < SIZE && gameBoard[i][DigonaleDescendante] == 1) { + if (diagonaleDescendante >= 0 && diagonaleDescendante < SIZE && gameBoard[i][diagonaleDescendante] == 1) { return false; } } 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 35f9311..076e8cd 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 @@ -3,12 +3,29 @@ package fr.iut_fbleau.but3.dev6_2; public class EightQueensSolver { private Chessboard chessboard = new Chessboard(); - public EightQueensSolver(){ - F - + public EightQueensSolver() { + System.out.println(Solver()); + } - + private Boolean Solver() { + if (chessboard.getNumberOfQueen() == 8) { + return true; + } + for (int i = 0; i < Chessboard.SIZE * Chessboard.SIZE; i++) { + int x = i % Chessboard.SIZE; + int y = i / Chessboard.SIZE; + if (chessboard.VerifAccessible(x, y)) { + chessboard.placeQueen(x, y); + if (!Solver()) { + chessboard.removeQueen(x, y); + } else { + return true; + } + } + } + return false; + } public Chessboard getChessboard() { return chessboard;