Skip to content

Commit 8376d1e

Browse files
committed
Fix
1 parent fa9b1de commit 8376d1e

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

src/main/java/com/thealgorithms/backtracking/KnightsTour.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ private KnightsTour() {
4141
// Total number of cells the knight needs to visit
4242
static int total;
4343

44+
/**
45+
* Resets the chess board to its initial state.
46+
* Initializes the grid with boundary cells marked as -1 and internal cells as 0.
47+
* Sets the total number of cells the knight needs to visit.
48+
*/
49+
public static void resetBoard() {
50+
grid = new int[BASE][BASE];
51+
total = (BASE - 4) * (BASE - 4);
52+
for (int r = 0; r < BASE; r++) {
53+
for (int c = 0; c < BASE; c++) {
54+
if (r < 2 || r > BASE - 3 || c < 2 || c > BASE - 3) {
55+
grid[r][c] = -1; // Mark boundary cells
56+
}
57+
}
58+
}
59+
}
60+
4461
/**
4562
* Recursive method to solve the Knight's Tour problem.
4663
*

src/test/java/com/thealgorithms/backtracking/KnightsTourTest.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,12 @@ public class KnightsTourTest {
1111

1212
@BeforeEach
1313
void setUp() {
14-
// Reset the grid and total for each test
15-
KnightsTour.grid = new int[12][12];
16-
KnightsTour.total = (12 - 4) * (12 - 4);
17-
for (int r = 0; r < 12; r++) {
18-
for (int c = 0; c < 12; c++) {
19-
if (r < 2 || r > 12 - 3 || c < 2 || c > 12 - 3) {
20-
KnightsTour.grid[r][c] = -1;
21-
}
22-
}
23-
}
14+
// Call the reset method in the KnightsTour class
15+
KnightsTour.resetBoard();
2416
}
2517

2618
@Test
2719
void testGridInitialization() {
28-
// Ensure that the border squares are -1 and internal grid is 0
2920
for (int r = 0; r < 12; r++) {
3021
for (int c = 0; c < 12; c++) {
3122
if (r < 2 || r > 12 - 3 || c < 2 || c > 12 - 3) {

0 commit comments

Comments
 (0)