Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Sudoku solver into a User Interface (UI) application#756

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Ansari-Shamaila wants to merge732 commits intocodemistic:revert-356-patch-1
base:revert-356-patch-1
Choose a base branch
Loading
fromAnsari-Shamaila:main

Conversation

Ansari-Shamaila
Copy link

How It Works:
UI Creation: The SudokuUI class creates a 9x9 grid using JTextField for user input.
Input Parsing: The parseInput() method reads the values from the text fields and converts them to an integer array (board).
Sudoku Solving: When the "Solve" button is clicked, the solveSudoku() method is triggered, which solves the puzzle using the SudokuOptimized class and displays the solution in the UI.
Output Display: After solving, the solution is displayed in the grid, and a message dialog informs the user if the puzzle was solved or unsolvable.

import javax.swing.;
import java.awt.
;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SudokuUI extends JFrame {

private static final int SIZE = 9;  // Size of the Sudoku gridprivate JTextField[][] grid = new JTextField[SIZE][SIZE];  // 9x9 JTextFields for the gridprivate int[][] board = new int[SIZE][SIZE];  // Sudoku boardpublic SudokuUI() {    setTitle("Sudoku Solver");    setSize(600, 600);    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    setLayout(new BorderLayout());    // Create the Sudoku grid panel    JPanel gridPanel = new JPanel(new GridLayout(SIZE, SIZE));    for (int row = 0; row < SIZE; row++) {        for (int col = 0; col < SIZE; col++) {            grid[row][col] = new JTextField();            grid[row][col].setHorizontalAlignment(JTextField.CENTER);            grid[row][col].setFont(new Font("Arial", Font.BOLD, 20));            grid[row][col].setBorder(BorderFactory.createLineBorder(Color.BLACK));            gridPanel.add(grid[row][col]);        }    }    // Button panel with "Solve" button    JPanel buttonPanel = new JPanel();    JButton solveButton = new JButton("Solve");    solveButton.setFont(new Font("Arial", Font.BOLD, 16));    solveButton.addActionListener(new ActionListener() {        @Override        public void actionPerformed(ActionEvent e) {            solveSudoku();        }    });    buttonPanel.add(solveButton);    // Add panels to the frame    add(gridPanel, BorderLayout.CENTER);    add(buttonPanel, BorderLayout.SOUTH);}// Parse the Sudoku grid from the UI into the board arrayprivate void parseInput() {    for (int row = 0; row < SIZE; row++) {        for (int col = 0; col < SIZE; col++) {            String text = grid[row][col].getText();            if (text.isEmpty()) {                board[row][col] = 0;  // Empty cells are represented as 0            } else {                board[row][col] = Integer.parseInt(text);            }        }    }}// Display the solved Sudoku on the UIprivate void displaySolution() {    for (int row = 0; row < SIZE; row++) {        for (int col = 0; col < SIZE; col++) {            grid[row][col].setText(String.valueOf(board[row][col]));        }    }}// Solve the Sudoku using the existing solver algorithmprivate void solveSudoku() {    parseInput();  // Parse the UI input into the board array    // Create an instance of the Sudoku solver with the current board    SudokuOptimized solver = new SudokuOptimized(board);    if (solver.solve()) {        displaySolution();  // Display the solution on the UI        JOptionPane.showMessageDialog(this, "Sudoku Solved!");    } else {        JOptionPane.showMessageDialog(this, "Unsolvable Sudoku");    }}public static void main(String[] args) {    // Create and display the Sudoku UI    SwingUtilities.invokeLater(new Runnable() {        @Override        public void run() {            SudokuUI sudokuUI = new SudokuUI();            sudokuUI.setVisible(true);        }    });}

}

class SudokuOptimized {

private int[][] board;public static final int EMPTY = 0;public static final int SIZE = 9;public SudokuOptimized(int[][] board) {    this.board = board;}// Check if a number can be placed at a specific cellprivate boolean isSafe(int row, int col, int num) {    int boxRowStart = row - row % 3;    int boxColStart = col - col % 3;    for (int i = 0; i < SIZE; i++) {        if (board[row][i] == num || board[i][col] == num ||             board[boxRowStart + i / 3][boxColStart + i % 3] == num) {            return false;        }    }    return true;}// Find the next empty cell (used for backtracking)private int[] findNextEmptyCell() {    for (int row = 0; row < SIZE; row++) {        for (int col = 0; col < SIZE; col++) {            if (board[row][col] == EMPTY) {                return new int[] {row, col};            }        }    }    return null;}// Solve the Sudoku using backtrackingpublic boolean solve() {    int[] emptyCell = findNextEmptyCell();    if (emptyCell == null) return true;  // No empty cell, puzzle solved    int row = emptyCell[0];    int col = emptyCell[1];    for (int num = 1; num <= SIZE; num++) {        if (isSafe(row, col, num)) {            board[row][col] = num;            if (solve()) return true;            board[row][col] = EMPTY;        }    }    return false;}

}
Output:
sudoku

Aarush2k1and others added30 commitsOctober 5, 2022 15:59
…TriangleminFallingPathSumTriangle.cpp created
- Added more operators- Added more comments- Fixed Typos
github-actionsbotand others added30 commitsJanuary 12, 2024 20:26
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

35 participants
@Ansari-Shamaila@Aarush2k1@thisabhijeet@AbhishekSrivastava-23@gantavyamalviya@lokesh-7977@souravkhan12@namratabose32@kokodeshinu@jashanwarraich@ayushkedia05@tiger3768@nilesh05apr@sohamdata@Ansh-Kushwaha@JYunth@KBhushan07@Ultimateutkarsh11@Bluetoothworks@SegFault03@ola-nishant@divyaa1511@MidhaShrey@vishwajeet-hogale@NarpatAanjana@vaibhav0726@GaurishIIITNR@codeX1616@AmanDekate1@arpitghura@PritomKarmokar@jayesh-kumavat@aishalxgupta@vaibhav0806@ShreyasiDebnath

[8]ページ先頭

©2009-2025 Movatter.jp