Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import jdk.swing.interop.SwingInterOpUtils;
- import java.util.*;
- import java.math.*;
- public class Board {
- int[][] board = new int[9][9];
- public void createBoard() {
- /*for(int i=0;i<9;i++){
- for(int j=0;j<9;j++){
- System.out.println("\n\nStarted adding for "+i+", "+j);
- add(i,j);
- System.out.println("Added value "+board[i][j]+" for "+i+", "+j+" Successfully!");
- }
- }
- System.out.println("Board Set!");
- */
- for(int i=1;i<=9;i++){
- System.out.println("Adding: "+i);
- for(int j=0;j<9;j++){
- System.out.println("Adding "+i+" for Box no.: "+j);
- add(i,j);
- System.out.println("Successfully Added "+i+" for Box no.: "+j);
- printBoard();
- }
- }
- }
- public void add(int val, int boxNo) {
- int tries =0;
- int row,col;
- while(true){
- row = (boxNo/3)*3 + (int)(Math.random()*3);
- col = (boxNo%3)*3 + (int)(Math.random()*3);
- if(check(row,col,val)){
- board[row][col] = val;
- break;
- }
- tries++;
- if(tries>100){
- System.exit(0);
- break;
- }
- }
- /*
- while (true) {
- int val = getRandomValue();
- System.out.println("Trying value: "+val);
- if(check(row,col,val)){
- board[row][col] = val;
- System.out.println(val+" successfully added");
- break;
- }
- System.out.println("Try no.: "+tries);
- tries++;
- if(tries >1000){
- System.exit(0);
- }
- }*/
- }
- /* public int getRandomValue() {
- while (true) {
- int val = (int) Math.floor((Math.random() * 10));
- if (val > 0 && val <= 9) {
- return val;
- }
- }
- }*/
- public boolean check(int row, int col, int val) {
- System.out.println("Row: ");
- boolean toAddRow = checker(val,rowDigits(row));
- System.out.println("Col: ");
- boolean toAddCol = checker(val,colDigits(col));
- //System.out.println("Empty: ");
- boolean isEmpty = (board[row][col] == 0);
- //System.out.println("Box: ");
- //boolean toAddBox = checker(val,boxDigits(row,col));
- boolean toAdd = toAddRow && toAddCol && isEmpty;// && toAddBox;
- System.out.println("Row: "+toAddRow +" Col:" +toAddCol+" Empty: "+isEmpty);
- System.out.println("Finally, Returning " +toAdd);
- return (toAdd);
- }
- public boolean checker(int val, int[] list) {
- for(int k =0;k<9;k++){
- System.out.print(list[k]+" ");
- }
- System.out.println("");
- for(int i :list){
- if(i == val){
- //System.out.println("Returning false, i.e. it contains and we cannot add");
- return false;
- }
- }
- //System.out.println("Checker returning true i.e. does not contain adn we can add");
- return true;
- }
- public int[] boxDigits(int row, int col) {
- int boxNum = boxNo(row, col);
- int startRow = (boxNum / 3) * 3;
- int startCol = (boxNum % 3) * 3;
- int[] box = new int[9];
- int k = 0;
- for (int i = startRow; i < startRow + 3; i++) {
- for (int j = startCol; j < startCol + 3; j++) {
- box[k] = board[i][j];
- k++;
- }
- }
- return box;
- }
- public int[] rowDigits(int row) {
- return board[row];
- }
- public int boxNo(int row, int col) {
- int boxNum = (row / 3) * 3 + (col / 3);
- return boxNum;
- }
- public int[] colDigits(int col) {
- int[] colVals = new int[9];
- for (int i = 0; i < 9; i++) {
- colVals[i] = board[i][col];
- }
- return colVals;
- }
- public int[][] getBoard(){
- return board;
- }
- public void printBoard(){
- System.out.print(" \t");
- for(int i=0;i<9;i++){
- System.out.print(i+" ");
- if((i+1)%3 == 0){
- System.out.print("| ");
- }
- }
- System.out.println("\n");
- for(int i=0;i<9;i++){
- System.out.print(i+"\t");
- for(int j=0;j<9;j++){
- System.out.print(board[i][j]+" ");
- if((j+1)%3 == 0){
- System.out.print("| ");
- }
- }
- if((i+1)%3 == 0){
- System.out.print("\n_____________________________");
- }
- System.out.println("");
- }
- }
- public static void main(String[] args){
- Board b = new Board();
- System.out.println("Process started");
- b.createBoard();
- b.printBoard();
- System.out.println("\n\n---------------------------------\n\n");
- //System.out.println(b.getBoard().toString());
- }
- }
Add Comment
Please, Sign In to add comment