thecoderenroute

Untitled

Apr 22nd, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.11 KB | None | 0 0
  1. import jdk.swing.interop.SwingInterOpUtils;
  2.  
  3. import java.util.*;
  4. import java.math.*;
  5.  
  6. public class Board {
  7.  
  8.     int[][] board = new int[9][9];
  9.  
  10.     public void createBoard() {
  11.         /*for(int i=0;i<9;i++){
  12.             for(int j=0;j<9;j++){
  13.                 System.out.println("\n\nStarted adding for "+i+", "+j);
  14.                 add(i,j);
  15.                 System.out.println("Added value "+board[i][j]+" for "+i+", "+j+" Successfully!");
  16.             }
  17.         }
  18.         System.out.println("Board Set!");
  19.         */
  20.         for(int i=1;i<=9;i++){
  21.             System.out.println("Adding: "+i);
  22.             for(int j=0;j<9;j++){
  23.                 System.out.println("Adding "+i+" for Box no.: "+j);
  24.                 add(i,j);
  25.                 System.out.println("Successfully Added "+i+" for Box no.: "+j);
  26.                 printBoard();
  27.             }
  28.  
  29.         }
  30.     }
  31.  
  32.     public void add(int val, int boxNo) {
  33.         int tries =0;
  34.         int row,col;
  35.         while(true){
  36.             row = (boxNo/3)*3 + (int)(Math.random()*3);
  37.             col = (boxNo%3)*3 + (int)(Math.random()*3);
  38.             if(check(row,col,val)){
  39.                 board[row][col] = val;
  40.                 break;
  41.             }
  42.             tries++;
  43.             if(tries>100){
  44.                 System.exit(0);
  45.                 break;
  46.             }
  47.         }
  48.         /*
  49.         while (true) {
  50.             int val = getRandomValue();
  51.             System.out.println("Trying value: "+val);
  52.             if(check(row,col,val)){
  53.                 board[row][col] = val;
  54.                 System.out.println(val+" successfully added");
  55.                 break;
  56.             }
  57.             System.out.println("Try no.: "+tries);
  58.             tries++;
  59.             if(tries >1000){
  60.                 System.exit(0);
  61.             }
  62.         }*/
  63.     }
  64.  
  65.   /*  public int getRandomValue() {
  66.         while (true) {
  67.             int val = (int) Math.floor((Math.random() * 10));
  68.             if (val > 0 && val <= 9) {
  69.                 return val;
  70.             }
  71.         }
  72.     }*/
  73.  
  74.     public boolean check(int row, int col, int val) {
  75.         System.out.println("Row: ");
  76.         boolean toAddRow = checker(val,rowDigits(row));
  77.         System.out.println("Col: ");
  78.         boolean toAddCol = checker(val,colDigits(col));
  79.         //System.out.println("Empty: ");
  80.         boolean isEmpty = (board[row][col] == 0);
  81.         //System.out.println("Box: ");
  82.         //boolean toAddBox = checker(val,boxDigits(row,col));
  83.         boolean toAdd = toAddRow && toAddCol && isEmpty;// && toAddBox;
  84.         System.out.println("Row: "+toAddRow +" Col:" +toAddCol+" Empty: "+isEmpty);
  85.         System.out.println("Finally, Returning " +toAdd);
  86.         return (toAdd);
  87.     }
  88.  
  89.     public boolean checker(int val, int[] list) {
  90.         for(int k =0;k<9;k++){
  91.             System.out.print(list[k]+" ");
  92.         }
  93.         System.out.println("");
  94.         for(int i :list){
  95.             if(i == val){
  96.                 //System.out.println("Returning false, i.e. it contains and we cannot add");
  97.                 return false;
  98.             }
  99.         }
  100.         //System.out.println("Checker returning true i.e. does not contain adn we can add");
  101.         return true;
  102.     }
  103.  
  104.     public int[] boxDigits(int row, int col) {
  105.         int boxNum = boxNo(row, col);
  106.         int startRow = (boxNum / 3) * 3;
  107.         int startCol = (boxNum % 3) * 3;
  108.         int[] box = new int[9];
  109.         int k = 0;
  110.         for (int i = startRow; i < startRow + 3; i++) {
  111.             for (int j = startCol; j < startCol + 3; j++) {
  112.                 box[k] = board[i][j];
  113.                 k++;
  114.             }
  115.         }
  116.         return box;
  117.     }
  118.  
  119.     public int[] rowDigits(int row) {
  120.         return board[row];
  121.     }
  122.     public int boxNo(int row, int col) {
  123.         int boxNum = (row / 3) * 3 + (col / 3);
  124.         return boxNum;
  125.     }
  126.  
  127.     public int[] colDigits(int col) {
  128.         int[] colVals = new int[9];
  129.         for (int i = 0; i < 9; i++) {
  130.             colVals[i] = board[i][col];
  131.         }
  132.         return colVals;
  133.     }
  134.  
  135.  
  136.     public int[][] getBoard(){
  137.         return board;
  138.     }
  139.  
  140.     public void printBoard(){
  141.         System.out.print(" \t");
  142.         for(int i=0;i<9;i++){
  143.             System.out.print(i+" ");
  144.             if((i+1)%3 == 0){
  145.                 System.out.print("| ");
  146.             }
  147.         }
  148.         System.out.println("\n");
  149.         for(int i=0;i<9;i++){
  150.             System.out.print(i+"\t");
  151.  
  152.             for(int j=0;j<9;j++){
  153.                 System.out.print(board[i][j]+" ");
  154.                 if((j+1)%3 == 0){
  155.                     System.out.print("| ");
  156.                 }
  157.             }
  158.             if((i+1)%3 == 0){
  159.                 System.out.print("\n_____________________________");
  160.             }
  161.             System.out.println("");
  162.         }
  163.     }
  164.  
  165.     public static void main(String[] args){
  166.  
  167.         Board b = new Board();
  168.         System.out.println("Process started");
  169.         b.createBoard();
  170.         b.printBoard();
  171.         System.out.println("\n\n---------------------------------\n\n");
  172.         //System.out.println(b.getBoard().toString());
  173.     }
  174. }
Add Comment
Please, Sign In to add comment