This commit is contained in:
Victor 2024-06-22 00:30:22 +02:00
parent e3eda3a457
commit 7d4074bded
3 changed files with 33 additions and 30 deletions

View File

@ -7,19 +7,23 @@ public class Chessboard {
public static final int SIZE = 8; public static final int SIZE = 8;
private final int[][] gameBoard = new int[SIZE][SIZE]; private final int[][] gameBoard = new int[SIZE][SIZE];
private final List<Position> queensPosition = new ArrayList<>(); private final List<Position> queensPosition = new ArrayList<>();
private static final String ANSI_RED = "\u001B[31m";
private static final String ANSI_RESET = "\u001B[0m";
public Chessboard() { public Chessboard() {
// Constructeur vide // Constructeur vide
} }
public void placeQueen(int x, int y) { public void placeQueen(int x, int y) {
gameBoard[x][y] = 1; if(VerifAccessible(x,y)){
queensPosition.add(new Position(x, y)); gameBoard[x][y] = 1;
queensPosition.add(new Position(x, y));
}
} }
public void removeQueen(int x, int y) { public void removeQueen(int x, int y) {
gameBoard[x][y] = 0; gameBoard[x][y] = 0;
queensPosition.removeIf(pos -> pos.x() == x && pos.y() == y); // Utilise removeIf pour supprimer l'objet correspondant queensPosition.removeIf(pos -> pos.x() == x && pos.y() == y);
} }
public Boolean VerifAccessible(int x, int y) { public Boolean VerifAccessible(int x, int y) {
@ -45,4 +49,22 @@ public class Chessboard {
public int getNumberOfQueen() { public int getNumberOfQueen() {
return queensPosition.size(); return queensPosition.size();
} }
public void printChessboard() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (gameBoard[j][i] == 1) {
System.out.print(ANSI_RED+"|♕|"+ANSI_RESET);
} else{
if(VerifAccessible(i, j)){
System.out.print("[ ]");
}
else{
System.out.print("[X]");
}
}
//System.out.print(" ");
}
System.out.println();
}
}
} }

View File

@ -4,8 +4,13 @@ public class EightQueensSolver {
private Chessboard chessboard = new Chessboard(); private Chessboard chessboard = new Chessboard();
public EightQueensSolver() { public EightQueensSolver() {
System.out.println("Début");
System.out.println(Solver()); System.out.println(Solver());
System.out.println("Fin");
chessboard.printChessboard();
}
public void main(String[] args){
} }
private Boolean Solver() { private Boolean Solver() {
@ -30,4 +35,4 @@ public class EightQueensSolver {
public Chessboard getChessboard() { public Chessboard getChessboard() {
return chessboard; return chessboard;
} }
} }

View File

@ -19,29 +19,5 @@ class ChessboardTest {
assertEquals(1, this.chessboard.getNumberOfQueen()); assertEquals(1, this.chessboard.getNumberOfQueen());
} }
/*@Test
void CaptureTiles(){
int Qx = 4;
int Qy = 4;
int Vx,Vy;
this.chessboard.placeQueen(Qx,Qy);
for (int i = 0; i < SIZE*SIZE; i++) {
Vx = i % SIZE;
Vy = i / SIZE;
if(Vx == Qx && Vy == Qy){
assertEquals(1, this.chessboard.gameBoard[Qx][Qy]);
continue;
}
if(Vx == Qx || Vy == Qy){
assertEquals(2, this.chessboard.gameBoard[Qx][Qy]);
continue;
}
If(Vx - Vy == Qx - Qy || Vx + Vy == Qx + Qy){
assertEquals(2, this.chessboard.gameBoard[Qx][Qy]);
continue;
}
assertEquals(0, this.chessboard.gameBoard[Qx][Qy]);
}
}*/
} }