Advertisement
Guest User

Sudoku Thing

a guest
Mar 29th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.17 KB | None | 0 0
  1. package sudokuGenerator;
  2. import java.util.*;
  3. import java.util.Scanner;
  4. public class SudokuBoard {
  5.  
  6.     private ArrayList<ArrayList<SudokuSquare>> board = new ArrayList<ArrayList<SudokuSquare>>(9);
  7.     private int arrayWidth = 9;
  8.     private int arrayLength = 9;
  9.     public SudokuBoard()
  10.     {
  11.  
  12.         fillFirst(board);
  13.         fillWithNum(board);
  14.     }
  15.     private boolean infringesRow(ArrayList<ArrayList<SudokuSquare>> board, SudokuSquare a)
  16.     {
  17.         for (int i = 0; i < board.get(0).size(); i ++)
  18.         {
  19.             if (a.getColumn() == i)
  20.                 continue;
  21.             if (board.get(a.getRow()).get(i).getValue() == a.getValue())
  22.                 return true;
  23.  
  24.         }
  25.         return false;
  26.     }
  27.     private boolean infringesColumn(ArrayList<ArrayList<SudokuSquare>> board, SudokuSquare a){
  28.         for (int i = 0; i < board.size(); i ++)
  29.         {
  30.             if (a.getRow() == i)
  31.                 continue;
  32.             if (board.get(i).get(a.getColumn()).getValue() == a.getValue())
  33.                 return true;
  34.         }
  35.         return false;
  36.     }
  37.     private boolean infringesSquare(ArrayList<ArrayList<SudokuSquare>> board,SudokuSquare a){
  38.         for (int i = 0; i < board.size(); i ++ )
  39.         {
  40.             for (int j = 0; j < board.get(0).size(); j++)
  41.             {
  42.                 if (a.getColumn() == j && a.getRow() == i)
  43.                     continue;
  44.                 if (board.get(i).get(j).getSquare() == a.getSquare() && board.get(i).get(j).getValue() == a.getValue())
  45.                     return true;
  46.             }
  47.         }
  48.         return false;
  49.     }
  50.     private boolean infringesConditions(ArrayList<ArrayList<SudokuSquare>> board,SudokuSquare a )
  51.     {
  52.         if (infringesRow(board, a) == true)
  53.             return true;
  54.         else if (infringesColumn(board, a) == true)
  55.             return true;
  56.         else if (infringesSquare(board,a) == true)
  57.             return true;
  58.  
  59.         return false;
  60.     }
  61.     private void resetRow(ArrayList<ArrayList<SudokuSquare>> board, int row)
  62.     {
  63.         for (int i = 0; i < board.get(row).size(); i ++)
  64.         {
  65.             board.get(row).get(i).setValue(-1);
  66.         }
  67.     }
  68.     public void fillFirst(ArrayList<ArrayList<SudokuSquare>> board)
  69.     {
  70.  
  71.         for (int i = 0; i < arrayLength; i++)
  72.         {
  73.             board.add(i, new ArrayList<SudokuSquare>(9));
  74.             for (int j = 0; j < arrayWidth; j++)
  75.             {
  76.                 board.get(i).add(j, new SudokuSquare());
  77.                 board.get(i).get(j).setValue(-1);;
  78.                 board.get(i).get(j).setColumn(j);
  79.                 board.get(i).get(j).setRow(i);
  80.  
  81.                 if (i <= 2 && j <= 2)
  82.                     board.get(i).get(j).setSquare(0);
  83.                 else if (i <= 2 && j <= 5)
  84.                     board.get(i).get(j).setSquare(1);
  85.                 else if (i <= 2 && j <= 8)
  86.                     board.get(i).get(j).setSquare(2);
  87.                 else if (i <= 5 && j <= 2)
  88.                     board.get(i).get(j).setSquare(3);
  89.                 else if (i <= 5 && j <= 5)
  90.                     board.get(i).get(j).setSquare(4);
  91.                 else if (i <= 5 && j <= 8)
  92.                     board.get(i).get(j).setSquare(5);
  93.                 else if (i <= 8 && j <= 2)
  94.                     board.get(i).get(j).setSquare(6);
  95.                 else if (i <= 8 && j <= 5)
  96.                     board.get(i).get(j).setSquare(7);
  97.                 else if (i <= 8 && j <= 8)
  98.                     board.get(i).get(j).setSquare(8);
  99.             }
  100.         }
  101.  
  102.     }
  103.     private void fillWithNum(ArrayList<ArrayList<SudokuSquare>> board)
  104.     {
  105.  
  106.         int counter = 0;
  107.         int counter2 = 0;
  108.         boolean shouldBreak = false;
  109.         boolean meetsConditions = true;
  110.         for (int i = 0; i < board.size();i++)
  111.         {
  112.             conditionalLoop:
  113.                 for (int j = 0; j < board.get(0).size(); j ++)
  114.                 {
  115.                     while(meetsConditions == true){
  116.  
  117.                         board.get(i).get(j).setValue( (int) (Math.random()*9 + 1));
  118.                         meetsConditions = infringesConditions(board, board.get(i).get(j));
  119.                         counter++;
  120.                         //counter greater than x, where x can be reduced, at the cost of a bit of time
  121.                         if (counter >= 50){
  122.                             shouldBreak = true;
  123.                             meetsConditions = false;
  124.                         }
  125.  
  126.                     }
  127.                     meetsConditions = true;
  128.                     counter = 0;
  129.                     if (shouldBreak == true && i >= 0)
  130.                     {
  131.                         resetRow(board,i);
  132.                         counter2++;
  133.                         i--;
  134.                         if (counter2 >= 10 && i >= 0)
  135.                         {
  136.                             i--;
  137.                             counter2 = 0;
  138.                             break conditionalLoop;
  139.                         }
  140.                         shouldBreak = false;
  141.                         break conditionalLoop;
  142.                     }
  143.                 }
  144.         }
  145.     }
  146.     /*public String toString()
  147.     {
  148.         String output = "";
  149.         for (int i = 0; i < this.board.size(); i ++)
  150.         {
  151.             for (int j = 0; j < this.board.get(0).size(); j++)
  152.             {
  153.                 output += "" + (this.board.get(i).get(j).getValue()) + "  ";
  154.             }
  155.             output += "\n";
  156.         }
  157.         return output;
  158.     }
  159.     public int getLength() {return board.size();}
  160.     public int getLength(int a) {return board.get(0).size();}
  161.     public int getElement(int a, int b){return board.get(a).get(b).getValue();}
  162. */
  163.      @Override
  164. public String toString() {
  165.     StringBuilder s = new StringBuilder();
  166.     s.append("+-------+-------+-------+\n");
  167.     for (int z = 0; z < 9; z++) {
  168.         s.append("| ");
  169.        
  170.         for (int i = 0; i < 3; i++) {
  171.             for (int j = 0; j < 3; j++) {
  172.                 int num = board.get(z).get(i*3 + j).getValue();
  173.                 s.append(num == 0 ? "_" : num);
  174.                
  175.                 if (j != 2) {
  176.                     s.append(" ");
  177.                 }
  178.             }
  179.            
  180.             if (i != 2) {
  181.                 s.append(" | ");
  182.             }
  183.         }
  184.         s.append(" |");
  185.         if (z != 8) {
  186.             s.append("\n" + ((z+1) % 3 == 0 ? "+-------+-------+-------+\n" : ""));
  187.         }
  188.     }
  189.     s.append("\n+-------+-------+-------+");
  190.     return s.toString();
  191.    
  192. }
  193.      public static void main(String[] args) {
  194.         long startTime = System.nanoTime();
  195.         //your code that you run
  196.        
  197.        
  198.         SudokuBoard board = new SudokuBoard();
  199.        
  200.         System.out.println(board);
  201.         long endTime = System.nanoTime();
  202.         double duration = (endTime - startTime)/100000000.0;
  203.         System.out.println("Seconds taken: " + duration);
  204.  
  205.        
  206.        
  207.    
  208.     }
  209. }
  210. ////////////////////////////////////////////////////////////////
  211. package sudokuGenerator;
  212.  
  213. public class SudokuSquare {
  214.     private int value;
  215.     private int row;
  216.     private int column;
  217.     private int square;
  218.    
  219.     public SudokuSquare()
  220.     {
  221.        
  222.     }
  223.     public SudokuSquare(int a)
  224.     {
  225.         value = a;
  226.     }
  227.     public String toString()
  228.     {
  229.         return "" + value;
  230.     }
  231.     public void setValue(int a){value = a;}
  232.     public void setRow(int a){row = a;}
  233.     public void setColumn(int a){column = a;}
  234.     public void setSquare(int a){square = a;}
  235.    
  236.     public int getValue(){return value;}
  237.     public int getRow(){return row;}
  238.     public int getColumn(){return column;}
  239.     public int getSquare(){return square;}
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement