SUDOKU 2 ???
unknown
plain_text
a year ago
2.5 kB
3
Indexable
Never
//Interfejs SudokuSolver: java Copy code public interface SudokuSolver { void solve(SudokuBoard board); } //Klasa SudokuBoard: java Copy code public class SudokuBoard { private final int[][] board; public SudokuBoard() { board = new int[9][9]; } public int[][] getBoard() { int[][] temp = new int[9][9]; for (int row = 0; row < 9; row++) { System.arraycopy(board[row], 0, temp[row], 0, 9); } return temp; } public boolean isBoardValid() { // Sprawdź czy układ liczb na planszy jest prawidłowy // Zwróć true jeśli jest poprawny, w przeciwnym razie false // Możesz użyć istniejących metod isValidMove, isSafeInRow, isSafeInColumn, isSafeInBox return true; } } //Klasa TypeSudokuSolver implementująca interfejs SudokuSolver: java Copy code import java.util.Random; public class TypeSudokuSolver implements SudokuSolver { @Override public void solve(SudokuBoard board) { solveGame(board); } private void solveGame(SudokuBoard board) { fillBoard(board, 0, 0); } private boolean fillBoard(SudokuBoard board, int row, int col) { if (row == 9 - 1 && col == 9) { return true; } if (col == 9) { row++; col = 0; } if (board.getBoard()[row][col] != 0) { return fillBoard(board, row, col + 1); } int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9}; shuffleArray(nums); for (int num : nums) { if (isValidMove(board, row, col, num)) { board.getBoard()[row][col] = num; if (fillBoard(board, row, col + 1)) { return true; } board.getBoard()[row][col] = 0; } } return false; } private boolean isValidMove(SudokuBoard board, int row, int col, int num) { // Implementacja isValidMove na podstawie danych z SudokuBoard return true; } private void shuffleArray(int[] arr) { Random rand = new Random(); for (int i = arr.length - 1; i > 0; i--) { int index = rand.nextInt(i + 1); int temp = arr[index]; arr[index] = arr[i]; arr[i] = temp; } } }