Advertisement
Guest User

AI Sudoku

a guest
Mar 24th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.87 KB | None | 0 0
  1.  
  2. public class Sudoku {
  3.    
  4.     public static final int n = 3;
  5.     public static final int n2 = n*n;
  6.    
  7.     private byte [][] board;
  8.    
  9.     public Sudoku()
  10.     {
  11.         board = new byte[n2][n2];
  12.         for(int i = 0; i <n2; i++)
  13.         {
  14.             for(int j = 0; j <n2; j++)
  15.             {
  16.                 board[i][j] = 0;
  17.             }
  18.         }
  19.     }
  20.    
  21.     public String toString()
  22.     {
  23.         String result = "";
  24.         for(int i = 0; i <n2; i++)
  25.         {
  26.             for(int j = 0; j <n2; j++)
  27.             {
  28.                 result += board[i][j] + ",";
  29.             }
  30.             result += "\n";
  31.         }
  32.        
  33.         return result;
  34.     }
  35.    
  36.     public void fromString(String txt)
  37.     {
  38.         int k = 0;
  39.         for(int i = 0; i <n2; i++)
  40.         {
  41.             for(int j = 0; j <n2; j++, k++)
  42.             {
  43.                 board[i][j] = Byte.valueOf(txt.substring(k,k+1));
  44.             }
  45.         }
  46.        
  47.     }
  48.    
  49.     private static boolean isGroupLeegal(Byte group[])
  50.     {
  51.        
  52.         boolean [] visited = new boolean[n2];
  53.         for(int i = 0; i < n2; i++)
  54.         {
  55.             visited[i] = false;
  56.         }
  57.        
  58.         for(int i = 0; i < group.length; i++)
  59.         {
  60.             if(group[i] == 0)
  61.             {
  62.                 continue;
  63.             }
  64.             if(visited[group[i]-1]) {
  65.                 return false;
  66.             }
  67.             visited[group[i]-1] = true;
  68.         }
  69.        
  70.         return true;
  71.     }
  72.    
  73.    
  74.     public boolean isBoardCorrect()
  75.     {
  76.         Byte[] group = new Byte[n2];
  77.         //wiersze
  78.         for(int i = 0; i < n2; i++)
  79.         {
  80.             for(int j = 0; j < n2; j++)
  81.             {
  82.                 group[j]=board [i][j];
  83.             }
  84.             if(!isGroupLeegal(group))
  85.             {
  86.                 return false;
  87.             }
  88.         }
  89.        
  90.         //kolumny
  91.        
  92.         for(int i = 0; i < n2; i++)
  93.         {
  94.             for(int j = 0; j < n2; j++)
  95.             {
  96.                 group[j]=board [j][i];
  97.             }
  98.             if(!isGroupLeegal(group))
  99.             {
  100.                 return false;
  101.             }
  102.         }
  103.        
  104.        
  105.         //kwadrat
  106.        
  107.         return true;
  108.     }
  109.    
  110.     public static void main(String[] args)
  111.     {
  112.        
  113.         String sudokuValue = "003020600900305001001806400008102900700000008006708200002609500800203009005010300";
  114.         Sudoku s = new  Sudoku();
  115.         s.fromString(sudokuValue);
  116.         System.out.println(s);
  117.         System.out.println(s.isBoardCorrect());
  118.     }
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement