This commit is contained in:
Victor 2024-06-21 23:38:58 +02:00
parent 0b8f7c5a76
commit e57fa2c36f
3 changed files with 52 additions and 1 deletions

View File

@ -10,14 +10,36 @@ public class Chessboard {
private final List<Position> queensPosition = new ArrayList<>(); private final List<Position> queensPosition = new ArrayList<>();
public Chessboard(){ public Chessboard(){
} }
public void placeQueen(int x, int y){ public void placeQueen(int x, int y){
gameBoard[x][y] = 1; gameBoard[x][y] = 1;
queensPosition.add(new Position(x,y)); queensPosition.add(new Position(x,y));
} }
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){
return false;
}
if(gameBoard[x][i] != 0){
return false;
}
if (DigonaleAscendante >= 0 && DigonaleAscendante < SIZE && gameBoard[i][DigonaleAscendante] == 1) {
return false;
}
if (DigonaleDescendante >= 0 && DigonaleAscendante < SIZE && gameBoard[i][DigonaleDescendante] == 1) {
return false;
}
}
return true;
}
public int getNumberOfQueen() { public int getNumberOfQueen() {
return queensPosition.size(); return queensPosition.size();
} }

View File

@ -4,9 +4,12 @@ public class EightQueensSolver {
private Chessboard chessboard = new Chessboard(); private Chessboard chessboard = new Chessboard();
public EightQueensSolver(){ public EightQueensSolver(){
} }
public Chessboard getChessboard() { public Chessboard getChessboard() {
return chessboard; return chessboard;
} }

View File

@ -19,4 +19,30 @@ class ChessboardTest {
this.chessboard.placeQueen(0,0); this.chessboard.placeQueen(0,0);
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]);
}
}
} }