2016-10-07 2 views
-5

Ich bin neu im Programmieren und versuche mich selbst zu unterrichten.Java: Wie führst du dieses Programm aus?

Ich habe diesen Sudoku-Lösungsalgorithmus online gefunden, aber ich kann es anscheinend nicht ausführen - ich denke, ich muss eine Hauptmethode erstellen, aber ich weiß nicht, welche Parameter darin eingefügt werden und wie andere Methoden aufgerufen werden.

Kann mir jemand ein Beispiel zeigen, was ich tun kann? Vielen Dank! :)

public class Sudoku { 
    private static int mBoard[][]; 
    private static int mBoardSize = 9; 
    private static int mBoxSize = 3; 
    private boolean mRowSubset[][]; 
    private boolean mColSubset[][]; 
    private boolean mBoxSubset[][]; 

    public Sudoku(int board[][]) { 
    mBoard = board; 
    mBoardSize = mBoard.length; 
    mBoxSize = (int) Math.sqrt(mBoardSize); 
    } 

    public void initSubsets() { 
    mRowSubset = new boolean[mBoardSize][mBoardSize]; 
    mColSubset = new boolean[mBoardSize][mBoardSize]; 
    mBoxSubset = new boolean[mBoardSize][mBoardSize]; 
    for (int i = 0; i < mBoard.length; i++) { 
     for (int j = 0; j < mBoard.length; j++) { 
     int value = mBoard[i][j]; 
     if (value != 0) { 
      setSubsetValue(i, j, value, true); 
     } 
     } 
    } 
    } 

    private void setSubsetValue(int i, int j, int value, boolean present) { 
    mRowSubset[i][value - 1] = present; 
    mColSubset[j][value - 1] = present; 
    mBoxSubset[computeBoxNo(i, j)][value - 1] = present; 
    } 

    public boolean solve() { 
    return solve(0, 0); 
    } 

    public boolean solve(int i, int j) { 
    if (i == mBoardSize) { 
     i = 0; 
     if (++j == mBoardSize) { 
     return true; 
     } 
    } 
    if (mBoard[i][j] != 0) { 
     return solve(i + 1, j); 
    } 
    for (int value = 1; value <= mBoardSize; value++) { 
     if (isValid(i, j, value)) { 
     mBoard[i][j] = value; 
     setSubsetValue(i, j, value, true); 
     if (solve(i + 1, j)) { 
      return true; 
     } 
     setSubsetValue(i, j, value, false); 
     } 
    } 

    mBoard[i][j] = 0; 
    return false; 
    } 

    private boolean isValid(int i, int j, int val) { 
    val--; 
    boolean isPresent = mRowSubset[i][val] || mColSubset[j][val] || mBoxSubset[computeBoxNo(i, j)][val]; 
    return !isPresent; 
    } 

    private int computeBoxNo(int i, int j) { 
    int boxRow = i/mBoxSize; 
    int boxCol = j/mBoxSize; 
    return boxRow * mBoxSize + boxCol; 
    } 

    public void print() { 
    for (int i = 0; i < mBoardSize; i++) { 
     if (i % mBoxSize == 0) { 
     System.out.println(" -----------------------"); 
     } 
     for (int j = 0; j < mBoardSize; j++) { 
     if (j % mBoxSize == 0) { 
      System.out.print("| "); 
     } 
     System.out.print(mBoard[i][j] != 0 ? ((Object) (Integer.valueOf(mBoard[i][j]))) : " "); 
     System.out.print(' '); 
     } 

     System.out.println("|"); 
    } 

    System.out.println(" -----------------------"); 
    } 

    public static void main(String[] args) { 
    Sudoku sudoku = new Sudoku(mBoard); 

    } 
} 
+0

Ich habe noch nicht - sollte ich es im Hauptverfahren nicht initialisiert werden? – Mimi

+4

Sie sind neu in der Programmierung und haben angefangen, etwas über den AI-Algorithmus zu lernen, der Sudoku löst? –

+2

Wenn du so Anfänger bist, warum nimmst du ein komplexes Programm wie Sudoku anstelle einer Art Hallo Welt Programm? –

Antwort

0

Sie können so etwas versuchen (Sie sollten Werte ändern, Größe?):

public static void main(String[] args) 
{ 
    //list of rows, where each row has 4 values 
    int[][] board = new int[][] { {2,3,1,1}, {1,2,1,1},{2,3,3,3}, {4,2,3,3}}; 
    Sudoku sudoku = new Sudoku(board); 
    sudoku.solve(); 
    sudoku.print(); 
} 
Verwandte Themen