Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests, enhance class & function documentation for `KnightsTour.jav… #5591

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix
  • Loading branch information
Hardvan committed Oct 6, 2024
commit 8376d1e273c3abdfff92196764a732169f9da4f7
17 changes: 17 additions & 0 deletions src/main/java/com/thealgorithms/backtracking/KnightsTour.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ private KnightsTour() {
// Total number of cells the knight needs to visit
static int total;

/**
* Resets the chess board to its initial state.
* Initializes the grid with boundary cells marked as -1 and internal cells as 0.
* Sets the total number of cells the knight needs to visit.
*/
public static void resetBoard() {
grid = new int[BASE][BASE];
total = (BASE - 4) * (BASE - 4);
for (int r = 0; r < BASE; r++) {
for (int c = 0; c < BASE; c++) {
if (r < 2 || r > BASE - 3 || c < 2 || c > BASE - 3) {
grid[r][c] = -1; // Mark boundary cells
}
}
}
}

/**
* Recursive method to solve the Knight's Tour problem.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,12 @@ public class KnightsTourTest {

@BeforeEach
void setUp() {
// Reset the grid and total for each test
KnightsTour.grid = new int[12][12];
KnightsTour.total = (12 - 4) * (12 - 4);
for (int r = 0; r < 12; r++) {
for (int c = 0; c < 12; c++) {
if (r < 2 || r > 12 - 3 || c < 2 || c > 12 - 3) {
KnightsTour.grid[r][c] = -1;
}
}
}
// Call the reset method in the KnightsTour class
KnightsTour.resetBoard();
}

@Test
void testGridInitialization() {
// Ensure that the border squares are -1 and internal grid is 0
for (int r = 0; r < 12; r++) {
for (int c = 0; c < 12; c++) {
if (r < 2 || r > 12 - 3 || c < 2 || c > 12 - 3) {
Expand Down
Loading