This commit is contained in:
Simon CATANESE 2024-06-26 23:40:31 +02:00
commit 6899b0db33
4 changed files with 145 additions and 6 deletions

View File

@ -21,6 +21,10 @@ public class Chessboard {
currentSize = size; currentSize = size;
} }
public int getTile(int x,int y){
return gameBoard[x][y];
}
public void placeQueen(int x, int y) { public void placeQueen(int x, int y) {
if(VerifAccessible(x,y)){ if(VerifAccessible(x,y)){
gameBoard[x][y] = 1; gameBoard[x][y] = 1;

View File

@ -66,7 +66,7 @@ public class EightQueensSolver {
int y = i / Chessboard.currentSize; int y = i / Chessboard.currentSize;
if (chessboard.VerifAccessible(x, y)) { if (chessboard.VerifAccessible(x, y)) {
chessboard.placeQueen(x, y); chessboard.placeQueen(x, y);
if (!Solver(y)) { if (!SolverVic(y)) {
chessboard.removeQueen(x, y); chessboard.removeQueen(x, y);
} else { } else {
return true; return true;

View File

@ -1,23 +1,134 @@
package fr.iut_fbleau.but3.dev6_2; package fr.iut_fbleau.but3.dev6_2;
import static org.junit.jupiter.api.Assertions.assertEquals; 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.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
class ChessboardTest { class ChessboardTest {
private Chessboard chessboard; private Chessboard chessboard;
private EightQueensSolver solver;
@BeforeEach @BeforeEach
public void beforeEach(){ public void beforeEach() {
this.chessboard = new Chessboard(); this.chessboard = new Chessboard();
this.solver = new EightQueensSolver();
} }
@Test @Test
void placeAQueen(){ void testReplaceQueen() {
this.chessboard.placeQueen(0,0); //test place une reine
assertEquals(1, this.chessboard.getNumberOfQueen()); 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);
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.");
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());
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
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);
} 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;
}
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.getTile(i,j) != 0){
chessboard.removeQueen(i,j);
assertTrue(chessboard.VerifAccessible(i,j));
chessboard.placeQueen(i,j);
}
}
}
}
@Test
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.");
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.");
}
testWinningSolution(this.chessboard);
}
}

View File

@ -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());
}
}