Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.12 KB | None | 0 0
  1. package sudokupak;
  2.  
  3. public class MySudokuModel implements SudokuModel {
  4.    
  5.     private int board [][] = new int [9][9];
  6.    
  7.    
  8.     // put zeros aka 0s in all the squares
  9.     @Override
  10.     public void clear() {
  11. //      System.out.println("inside clear");
  12.         for (int row = 0; row < 9; row++) {
  13.             for (int col = 0; col < 9; col++) {
  14.                 board [row][col] = 0;
  15.         }
  16.            
  17.         }
  18.         // TODO Auto-generated method stub
  19.        
  20.     }
  21.    
  22.     // return the sudoku board with a 2D array
  23.     @Override
  24.     public int[][] getBoard() {
  25. //      System.out.println("inside getBoard");
  26.        
  27.         // TODO Auto-generated method stub
  28.         return board;
  29.     }
  30.  
  31.     // receives the sudoku board, each row ends with \n
  32.     // checks that the board is "valid" (numbers are placed properly) and that the board is 9x9
  33.     // if something is wrong it should throw a IllegalArgumentException
  34.    
  35.     //like so:
  36.     //(9 rows x 9 columns)
  37.     // 3...8....
  38.     // .........
  39.     // .........
  40.     // .........
  41.     // .........
  42.     // ......8..
  43.    
  44.     @Override
  45.     public void setBoard(String input) {
  46. //      System.out.println("inside setBoard");
  47.         // TODO Auto-generated method stub
  48.         String[] rows = input.split("[\r\n]+");
  49.         if (rows.length != 9) {
  50.             throw new IllegalArgumentException();
  51.         }
  52.         //rows
  53.         for (int row = 0; row < rows.length; row++) {
  54.            
  55.             String r = rows[row];
  56.             if (r.length() != 9) {
  57.                 throw new IllegalArgumentException();
  58.             }
  59.             //columns
  60.             for (int col = 0; col < r.length(); col++) {
  61.                 String s = String.valueOf(r.charAt(col));
  62.                 if (!s.matches("[0-9]")) {
  63.                     s = "0";
  64.                 }
  65.                 int nr = Integer.valueOf(s);
  66.                 board[row][col] = nr;
  67.             }
  68.         }
  69.     }
  70.  
  71.     // returns the board like so: return board [row][col]
  72.     @Override
  73.     public int getSquare(int row, int col) {
  74.         return board[row][col];
  75.     }
  76.  
  77.    
  78.     // check that its allowed to put the number in the square
  79.     // if yes, make the necessary changes and return true
  80.     // else do nothing and return false
  81.     @Override
  82.     public boolean setSquare(int row, int col, int val) {
  83. //      System.out.println("inside setSquare");
  84.         int r = row - row%3;
  85.         int c = col - col%3;
  86.         for(int i = r ; i< r+3 ; i++)
  87.         {
  88.             for(int j = c; j < c+3 ; j++)
  89.             {
  90.                 if(board[i][j]==val)
  91.                 {
  92. //                  System.out.println("setSquare wtf");
  93.                     return true;
  94.                 }
  95.             }
  96.              
  97.         }
  98.         return false;
  99.     }
  100.  
  101.     //check that its allowed to put the number but make no changes
  102.     @Override
  103.     public boolean isLegal(int row, int col, int val) {
  104.         return row>=0 && row<9 && col>=0 && col<9
  105.                 && val>0 && val<=9 && board[row][col]==0;
  106.     }
  107.  
  108.     // solve the sudoku puzzle
  109.     // iteratively search for a solution for a given puzzle
  110.     // if solution found, return true
  111.     // if solution not found, return false and continue loop until solution found
  112.     @Override
  113.     public boolean solve() {
  114. //      System.out.println("inside solve");
  115.         for (int row = 0; row < 9; row++) {
  116.             for (int col = 0; col < 9; col++) {
  117.              // we search an empty cell
  118.              if (board[row][col] == 0) {
  119.                // we try possible numbers
  120.                for (int number = 1; number <= 9; number++) {
  121.                  if (isLegal(row, col, number)) {
  122.                    // number ok. it respects sudoku constraints
  123.                    board[row][col] = number;
  124.  
  125.                    if (solve()) { // we start backtracking recursively
  126.                      return true;
  127.                    } else { // if not a solution, we empty the cell and we continue
  128.                      board[row][col] = 0;
  129.                    }
  130.                 }
  131.                }
  132.  
  133.                return false; // we return false
  134.               }
  135.              }
  136.             }
  137.  
  138.             return true; // sudoku solved
  139.  
  140.     }
  141.    
  142.     //copy the model
  143.     @Override
  144.     public SudokuModel copy() {
  145. //      System.out.println("inside copy");
  146.         // TODO Auto-generated method stub
  147.         return null;
  148.     }
  149.    
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement