Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.lang.Math;
- class Sudoku{
- // numbers of the sudoku
- public ArrayList<Integer> numbers;
- // size of sudoku's side
- private int size;
- // constructor with ready sudoku
- Sudoku(StringBuilder strb){
- size = 4;
- numbers = new ArrayList<>();
- for(int i=0; i<size*size; i++){
- numbers.add(Character.getNumericValue(strb.charAt(i)));
- }
- }
- // constuctor with empty sudoku
- Sudoku(){
- size = 4;
- numbers = new ArrayList<>();
- // filling in the sudoku with a number -...a4a3a2a1 as index of
- // where it has no value(a1=1 if 1 is possible to be at that
- // spot and 0 if not. The same applies for a2, a3, a4 and so forth)
- StringBuilder strbNum = new StringBuilder();
- for(int i=size; i>0; i--){
- strbNum.append(Integer.toString(i));
- }
- for(int i=0; i<size*size; i++){
- numbers.add(-Integer.parseInt(strbNum.toString()));
- }
- }
- // return true if it's possible to add a number at that space
- public boolean check(int number, int place){
- if(number == -numbers.get(place)/(Math.pow(10, number-1)%10) && numbers.get(place)<0){
- return true;
- }
- return false;
- }
- // when a number has been added makes the changes to the rest of the sudoku
- public boolean optimize(){
- return false;
- }
- // checks the boxes for optimize
- private boolean checksTheBoxes(){
- int val;
- // check if any changes have been made
- boolean has_changed = false;
- // in each box it saves the four places
- ArrayList<Integer> places = new ArrayList<>();
- // first box
- places.add(0);
- places.add(1);
- places.add(4);
- places.add(5);
- for(Integer i : places){
- for(Integer j: places){
- if(i != j){
- // val at i
- int val_i = numbers.get(i);
- int val_j = numbers.get(j);
- if(val_i>0 && val_j<0){
- if(-val_j/(Math.pow(10, val_i-1)%10) == val_i){
- val_j += val_i*Math.pow(10, val_i-1);
- has_changed = true;
- }
- }
- }
- }
- }
- // second box
- places.set(0, 2);
- places.set(1, 3);
- places.set(2, 6);
- places.set(3, 7);
- for(Integer i : places){
- for(Integer j: places){
- if(i != j){
- // val at i
- int val_i = numbers.get(i);
- int val_j = numbers.get(j);
- if(val_i>0 && val_j<0){
- if(-val_j/(Math.pow(10, val_i-1)%10) == val_i){
- val_j += val_i*Math.pow(10, val_i-1);
- has_changed = true;
- }
- }
- }
- }
- }
- // third box
- places.set(0, 8);
- places.set(1, 9);
- places.set(2, 12);
- places.set(3, 13);
- for(Integer i : places){
- for(Integer j: places){
- if(i != j){
- // val at i
- int val_i = numbers.get(i);
- int val_j = numbers.get(j);
- if(val_i>0 && val_j<0){
- if(-val_j/(Math.pow(10, val_i-1)%10) == val_i){
- val_j += val_i*Math.pow(10, val_i-1);
- has_changed = true;
- }
- }
- }
- }
- }
- // fourth box
- places.set(0, 10);
- places.set(1, 11);
- places.set(2, 14);
- places.set(3, 15);
- for(Integer i : places){
- for(Integer j: places){
- if(i != j){
- // val at i
- int val_i = numbers.get(i);
- int val_j = numbers.get(j);
- if(val_i>0 && val_j<0){
- if(-val_j/(Math.pow(10, val_i-1)%10) == val_i){
- val_j += val_i*Math.pow(10, val_i-1);
- has_changed = true;
- }
- }
- }
- }
- }
- return has_changed;
- }
- // checks the rows for optimize
- private boolean rows(){
- int val;
- // check if any changes have been made
- boolean has_changed = false;
- // in each box it saves the four places
- ArrayList<Integer> places = new ArrayList<>();
- for(int k=0; k<4; k++){
- places.add(k);
- places.add(k+1);
- places.add(k+2);
- places.add(k+3);
- for(Integer i : places){
- for(Integer j: places){
- if(i != j){
- // val at i
- int val_i = numbers.get(i);
- int val_j = numbers.get(j);
- if(val_i>0 && val_j<0){
- if(-val_j/(Math.pow(10, val_i-1)%10) == val_i){
- val_j += val_i*Math.pow(10, val_i-1);
- has_changed = true;
- }
- }
- }
- }
- }
- }
- }
- }
- class SudokuLogic{
- public Sudoku sudoku;
- SudokuLogic(StringBuilder strb){
- sudoku = new Sudoku(strb);
- }
- SudokuLogic(){
- sudoku = new Sudoku();
- }
- // printing the sudoku
- public void printSudoku(){
- for(int i=0; i<4; i++){
- for(int j=0; j<4; j++){
- if(sudoku.numbers.get(i*4 + j)>0){
- System.out.print(Integer.toString(sudoku.numbers.get(i*4 + j)) + " ");
- }
- else{
- System.out.print("_ ");
- }
- }
- System.out.println();
- }
- }
- // to enter a number
- public void enterNumber(int number, int place){
- if(sudoku.check(number, place)){
- sudoku.numbers.set(place, number);
- // checks if sudoku has not been optimize(means it has no more optimization to take)
- boolean flag;
- do{
- flag = false;
- flag = sudoku.optimize();
- } while(flag == true);
- }
- }
- }
- public class Main {
- public static void main(String[] args) throws Exception {
- // Your code here!
- StringBuilder strb = new StringBuilder("1234123412341234");
- SudokuLogic sudokuLogic = new SudokuLogic();
- sudokuLogic.printSudoku();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement