Skip to content

Commit

Permalink
[JAVA-22175] Sudoku for-loop to avoid side effects (eugenp#17183)
Browse files Browse the repository at this point in the history
  • Loading branch information
GaetanoPiazzolla authored Aug 5, 2024
1 parent 42c1ba7 commit 4dabff8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class BacktrackingAlgorithm {
private static final int MIN_VALUE = 1;
private static final int MAX_VALUE = 9;

private static int[][] board = {
private static final int[][] board = {
{8, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 3, 6, 0, 0, 0, 0, 0},
{0, 7, 0, 0, 9, 0, 2, 0, 0},
Expand All @@ -39,7 +39,7 @@ private void printBoard() {
}
}

private boolean solve(int[][] board) {
public boolean solve(int[][] board) {
for (int row = BOARD_START_INDEX; row < BOARD_SIZE; row++) {
for (int column = BOARD_START_INDEX; column < BOARD_SIZE; column++) {
if (board[row][column] == NO_VALUE) {
Expand Down Expand Up @@ -81,14 +81,22 @@ private boolean subsectionConstraint(int[][] board, int row, int column) {

private boolean columnConstraint(int[][] board, int column) {
boolean[] constraint = new boolean[BOARD_SIZE];
return IntStream.range(BOARD_START_INDEX, BOARD_SIZE)
.allMatch(row -> checkConstraint(board, row, constraint, column));
for (int row = BOARD_START_INDEX; row < BOARD_SIZE; row++) {
if (!checkConstraint(board, row, constraint, column)) {
return false;
}
}
return true;
}

private boolean rowConstraint(int[][] board, int row) {
boolean[] constraint = new boolean[BOARD_SIZE];
return IntStream.range(BOARD_START_INDEX, BOARD_SIZE)
.allMatch(column -> checkConstraint(board, row, constraint, column));
for (int column = BOARD_START_INDEX; column < BOARD_SIZE; column++) {
if (!checkConstraint(board, row, constraint, column)) {
return false;
}
}
return true;
}

private boolean checkConstraint(int[][] board, int row, boolean[] constraint, int column) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class DancingLinksAlgorithm {
private static final int MAX_VALUE = 9;
private static final int COVER_START_INDEX = 1;

private static int[][] board = {
private static final int[][] board = {
{8, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 3, 6, 0, 0, 0, 0, 0},
{0, 7, 0, 0, 9, 0, 2, 0, 0},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.baeldung.algorithms.sudoku;

import org.junit.Assert;
import org.junit.Test;

public class BacktrackingAlgoritmUnitTest {

private static final int[][] board = {
{8, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 3, 6, 0, 0, 0, 0, 0},
{0, 7, 0, 0, 9, 0, 2, 0, 0},
{0, 5, 0, 0, 0, 7, 0, 0, 0},
{0, 0, 0, 0, 4, 5, 7, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 3, 0},
{0, 0, 1, 0, 0, 0, 0, 6, 8},
{0, 0, 8, 5, 0, 0, 0, 1, 0},
{0, 9, 0, 0, 0, 0, 4, 0, 0}
};

@Test
public void testSudokuBacktrackingAlgorithm() {
BacktrackingAlgorithm solver = new BacktrackingAlgorithm();
solver.solve(board);
Assert.assertArrayEquals(board[0], new int[] {8,1,2,7,5,3,6,4,9});
}

}

0 comments on commit 4dabff8

Please sign in to comment.