Coding a Java Sudoku Solver - Java Programming

Поделиться
HTML-код
  • Опубликовано: 16 ноя 2023
  • In this video, we'll write a program that can solve Sudoku puzzles using a recursive backtracking algorithm. Hope you like the video!
    My name is Will Tollefson! I'm here to teach you key skills that will help you land your first programming job or stay current and competitive for those promotions in your current job. Subscribe to my channel and I'll help you grow in your programming skills, and make your career soar!
    Join me to learn computer programming in an easy to learn way, with a teacher who isn't talking over your head. Or sounds like this. But instead explains things in a way you can understand.
    I'm a software engineer in industry who has been coding for over a decade now and my motto is Never Stop Learning. I can give you insight into skills that employers actually care about as in addition to being a programmer I also have hired software engineers.
    Ring that notification bell so you never miss fresh content that just might help you with your current programming bug.
    I have experience with various types of programming such as mobile phone apps, scientific computational modeling, and embedded communications systems, to name a few, and I've worked on teams ranging from one to over one hundred programmers.
    The content I'm offering you is free, self paced, and practical for computer programming jobs. I'll teach you skills you need to be competitive.
    Let's dive into programming concepts like algorithms and data structures that are mostly language agnostic.
    If you've got ideas of topics or a programming dilemma you want me to cover, drop me a comment! I'll be efficient with your time so without further ado, let's get to it!

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

  • @willtollefson
    @willtollefson  8 месяцев назад +8

    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 8 месяцев назад +4

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

  • @pablopronsky7364
    @pablopronsky7364 8 месяцев назад +4

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

    • @willtollefson
      @willtollefson  8 месяцев назад +4

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

    • @pablopronsky7364
      @pablopronsky7364 8 месяцев назад +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!!!!