Coding a Java Sudoku Solver - Java Programming

Поделиться
HTML-код
  • Опубликовано: 5 янв 2025

Комментарии • 6

  • @willtollefson
    @willtollefson  Год назад +9

    Here's the full source code for convenience 🙂
    import java.util.stream.IntStream;
    public class SudokuSolver {
    private static final int BOX_SIZE = 3;
    private static final int GRID_SIZE = BOX_SIZE * BOX_SIZE;
    public static void main(String[] args) {
    int[][] board = {
    {0, 0, 0, 6, 0, 0, 4, 0, 0},
    {7, 0, 0, 0, 0, 3, 6, 0, 0},
    {0, 0, 0, 0, 9, 1, 0, 8, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 5, 0, 1, 8, 0, 0, 0, 3},
    {0, 0, 0, 3, 0, 6, 0, 4, 5},
    {0, 4, 0, 2, 0, 0, 0, 6, 0},
    {9, 0, 3, 0, 0, 0, 0, 0, 0},
    {0, 2, 0, 0, 0, 0, 1, 0, 0}
    };
    if (solve(board)) {
    System.out.println("Yay!");
    printResult(board);
    } else {
    System.out.println("Oops :(");
    }
    }
    private static void printResult(int[][] board) {
    for (int row = 0; row < GRID_SIZE; row++) {
    if ((row % BOX_SIZE == 0) && (row != 0)) {
    System.out.println("-----------------------------");
    }
    for (int col = 0; col < GRID_SIZE; col++) {
    if ((col % BOX_SIZE == 0) && (col != 0)) {
    System.out.print("|");
    }
    final int cellValue = board[row][col];
    System.out.print(" ");
    if (cellValue == 0) {
    System.out.print(" ");
    } else {
    System.out.print(cellValue);
    }
    System.out.print(" ");
    }
    System.out.println();
    }
    }
    private static boolean allowedInRow(int[][] board, int row, int number) {
    return IntStream.range(0, GRID_SIZE)
    .noneMatch(col -> board[row][col] == number);
    }
    private static boolean allowedInCol(int[][] board, int col, int number) {
    return IntStream.range(0, GRID_SIZE)
    .noneMatch(row -> board[row][col] == number);
    }
    private static boolean allowedInBox(int[][] board, int row, int col, int number) {
    final int boxCol = col - (col % BOX_SIZE);
    final int boxRow = row - (row % BOX_SIZE);
    for (int i = 0; i < BOX_SIZE; i++) {
    for (int j = 0; j < BOX_SIZE; j++) {
    if (board[boxRow + i][boxCol + j] == number) {
    return false;
    }
    }
    }
    return true;
    }
    private static boolean isAllowed(int[][] board, int row, int col, int number) {
    return allowedInRow(board, row, number) &&
    allowedInCol(board, col, number) &&
    allowedInBox(board, row, col, number);
    }
    private static boolean solve(int[][] board) {
    for (int row = 0; row < GRID_SIZE; row++) {
    for (int col = 0; col < GRID_SIZE; col++) {
    if (board[row][col] == 0) {
    for (int num = 1; num

  • @DenaTollefson
    @DenaTollefson Год назад +4

    Wow Will what a fantastic tutorial on how to write a Java program to solve a Sudoku puzzle! Awesome!

  • @pablopronsky7364
    @pablopronsky7364 Год назад +4

    Amazing!!!!! I'll try to make the sudoku on my own now haha
    Hope you are doing great, Professor!
    Pablo.

    • @willtollefson
      @willtollefson  Год назад +4

      Hahaha sounds good, Pablo! I'm doing well here in the US. Hope its going well in Argentina!

    • @pablopronsky7364
      @pablopronsky7364 Год назад +4

      @@willtollefson Tomorrow (sunday) we have elections and we decide between North Korea and the Free World. Hope everything goes well for us!
      Thanks for asking Professor! Have a great week!!!!