Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.00 KB | None | 0 0
  1. import java.util.*;
  2. import java.lang.Math;
  3.  
  4. class Sudoku{
  5.     // numbers of the sudoku
  6.     public ArrayList<Integer> numbers;
  7.    
  8.     // size of sudoku's side
  9.     private int size;
  10.    
  11.     // constructor with ready sudoku
  12.     Sudoku(StringBuilder strb){
  13.         size = 4;
  14.  
  15.         numbers = new ArrayList<>();
  16.         for(int i=0; i<size*size; i++){
  17.             numbers.add(Character.getNumericValue(strb.charAt(i)));
  18.         }
  19.     }
  20.    
  21.     // constuctor with empty sudoku
  22.     Sudoku(){
  23.         size = 4;
  24.        
  25.         numbers = new ArrayList<>();
  26.         // filling in the sudoku with a number -...a4a3a2a1 as index of
  27.         // where it has no value(a1=1 if 1 is possible to be at that
  28.         // spot and 0 if not. The same applies for a2, a3, a4 and so forth)
  29.         StringBuilder strbNum = new StringBuilder();
  30.         for(int i=size; i>0; i--){
  31.             strbNum.append(Integer.toString(i));
  32.         }
  33.        
  34.         for(int i=0; i<size*size; i++){
  35.             numbers.add(-Integer.parseInt(strbNum.toString()));
  36.         }
  37.     }
  38.    
  39.     // return true if it's possible to add a number at that space
  40.     public boolean check(int number, int place){
  41.         if(number == -numbers.get(place)/(Math.pow(10, number-1)%10) && numbers.get(place)<0){
  42.             return true;
  43.         }
  44.         return false;
  45.     }
  46.    
  47.     // when a number has been added makes the changes to the rest of the sudoku
  48.     public boolean optimize(){
  49.         return false;
  50.     }
  51.    
  52.     // checks the boxes for optimize
  53.     private boolean checksTheBoxes(){
  54.         int val;
  55.        
  56.         // check if any changes have been made
  57.         boolean has_changed = false;
  58.         // in each box it saves the four places
  59.         ArrayList<Integer> places = new ArrayList<>();
  60.        
  61.         // first box
  62.         places.add(0);
  63.         places.add(1);
  64.         places.add(4);
  65.         places.add(5);
  66.         for(Integer i : places){
  67.             for(Integer j: places){
  68.                 if(i != j){
  69.                     // val at i
  70.                     int val_i = numbers.get(i);
  71.                     int val_j = numbers.get(j);
  72.                     if(val_i>0 && val_j<0){
  73.                         if(-val_j/(Math.pow(10, val_i-1)%10) == val_i){
  74.                             val_j += val_i*Math.pow(10, val_i-1);
  75.                             has_changed = true;
  76.                         }
  77.                     }
  78.                 }
  79.             }
  80.         }
  81.  
  82.         // second box
  83.         places.set(0, 2);
  84.         places.set(1, 3);
  85.         places.set(2, 6);
  86.         places.set(3, 7);
  87.         for(Integer i : places){
  88.             for(Integer j: places){
  89.                 if(i != j){
  90.                     // val at i
  91.                     int val_i = numbers.get(i);
  92.                     int val_j = numbers.get(j);
  93.                     if(val_i>0 && val_j<0){
  94.                         if(-val_j/(Math.pow(10, val_i-1)%10) == val_i){
  95.                             val_j += val_i*Math.pow(10, val_i-1);
  96.                             has_changed = true;
  97.                         }
  98.                     }
  99.                 }
  100.             }
  101.         }
  102.  
  103.         // third box
  104.         places.set(0, 8);
  105.         places.set(1, 9);
  106.         places.set(2, 12);
  107.         places.set(3, 13);
  108.         for(Integer i : places){
  109.             for(Integer j: places){
  110.                 if(i != j){
  111.                     // val at i
  112.                     int val_i = numbers.get(i);
  113.                     int val_j = numbers.get(j);
  114.                     if(val_i>0 && val_j<0){
  115.                         if(-val_j/(Math.pow(10, val_i-1)%10) == val_i){
  116.                             val_j += val_i*Math.pow(10, val_i-1);
  117.                             has_changed = true;
  118.                         }
  119.                     }
  120.                 }
  121.             }
  122.         }
  123.  
  124.         // fourth box
  125.         places.set(0, 10);
  126.         places.set(1, 11);
  127.         places.set(2, 14);
  128.         places.set(3, 15);
  129.         for(Integer i : places){
  130.             for(Integer j: places){
  131.                 if(i != j){
  132.                     // val at i
  133.                     int val_i = numbers.get(i);
  134.                     int val_j = numbers.get(j);
  135.                     if(val_i>0 && val_j<0){
  136.                         if(-val_j/(Math.pow(10, val_i-1)%10) == val_i){
  137.                             val_j += val_i*Math.pow(10, val_i-1);
  138.                             has_changed = true;
  139.                         }
  140.                     }
  141.                 }
  142.             }
  143.         }
  144.        
  145.         return has_changed;
  146.     }
  147.    
  148.     // checks the rows for optimize
  149.     private boolean rows(){
  150.         int val;
  151.        
  152.         // check if any changes have been made
  153.         boolean has_changed = false;
  154.         // in each box it saves the four places
  155.         ArrayList<Integer> places = new ArrayList<>();
  156.        
  157.         for(int k=0; k<4; k++){
  158.    
  159.             places.add(k);
  160.             places.add(k+1);
  161.             places.add(k+2);
  162.             places.add(k+3);
  163.             for(Integer i : places){
  164.                 for(Integer j: places){
  165.                     if(i != j){
  166.                         // val at i
  167.                         int val_i = numbers.get(i);
  168.                         int val_j = numbers.get(j);
  169.                         if(val_i>0 && val_j<0){
  170.                             if(-val_j/(Math.pow(10, val_i-1)%10) == val_i){
  171.                                 val_j += val_i*Math.pow(10, val_i-1);
  172.                                 has_changed = true;
  173.                             }
  174.                         }
  175.                     }
  176.                 }
  177.             }
  178.    
  179.         }
  180.     }
  181.  
  182. }
  183.  
  184. class SudokuLogic{
  185.    
  186.     public Sudoku sudoku;
  187.     SudokuLogic(StringBuilder strb){
  188.          sudoku = new Sudoku(strb);
  189.     }
  190.    
  191.     SudokuLogic(){
  192.         sudoku = new Sudoku();
  193.     }
  194.    
  195.     // printing the sudoku
  196.     public void printSudoku(){
  197.         for(int i=0; i<4; i++){
  198.             for(int j=0; j<4; j++){
  199.                 if(sudoku.numbers.get(i*4 + j)>0){
  200.                     System.out.print(Integer.toString(sudoku.numbers.get(i*4 + j)) + "  ");
  201.                 }
  202.                 else{
  203.                     System.out.print("_  ");
  204.                 }
  205.             }
  206.             System.out.println();
  207.         }
  208.     }
  209.    
  210.     // to enter a number
  211.     public void enterNumber(int number, int place){
  212.         if(sudoku.check(number, place)){
  213.             sudoku.numbers.set(place, number);
  214.            
  215.             // checks if sudoku has not been optimize(means it has no more optimization to take)
  216.             boolean flag;
  217.             do{
  218.                 flag = false;
  219.                 flag = sudoku.optimize();
  220.             } while(flag == true);
  221.         }
  222.     }
  223.    
  224.    
  225. }
  226.  
  227. public class Main {
  228.     public static void main(String[] args) throws Exception {
  229.         // Your code here!
  230.         StringBuilder strb = new StringBuilder("1234123412341234");
  231.         SudokuLogic sudokuLogic = new SudokuLogic();
  232.        
  233.         sudokuLogic.printSudoku();
  234.     }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement