This commit is contained in:
Victor 2024-06-22 00:11:15 +02:00
parent 0d42134d88
commit e3eda3a457
5 changed files with 38 additions and 19 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

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

View File

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