Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.5 kB
2
Indexable
Never
package sudoku;

import java.util.Random;

public class Sudoku implements SudokuSolver {
    private  int [][] board;
    private int count=0;
    //private int [][] grid=new int[9][9];
    
    public Sudoku() {
    	board=new int[9][9];
	}
  

	@Override
	public boolean solve() {
		return solver(this.board);
	}


	
	@Override
	public void add(int row, int col, int digit) {
		if(isValid(digit,board,row,col)) {
			board[row][col]=digit;
		}
		// TODO Auto-generated method stub
		
	}

	@Override
	public void remove(int row, int col) {
		if (row < 9 && row >= 0 && col < 9 && col >= 0) {
		board[row][col]=0;
	} else {
		throw new IllegalArgumentException("Value too big");
		}
	}

	@Override
	public int get(int row, int col) {
		if (row < 9 && row >= 0 && col < 9 && col >= 0) {
		return board[row][col];
	}else {
		return 0;
		}
	}

	
	@Override
	public boolean isValid(int number, int[][] board,int row, int col) {
		
		return !isColumnTaken(number, board, col)&&  
				!isRowTaken(number, board, row) &&
				!isNumberInBox(number, board, row, col);
	}
	
	private static boolean isColumnTaken(int number,int [][]board, int col) {
		for(int row=0;row<board.length;row++) {
			if(board[row][col]==number) {
				return true;
			}
			
		}
		return false;	
	}
	
	private static boolean isRowTaken(int number,int [][]board, int row) {
		for(int col=0;col<board.length;col++) {
			if(board[row][col]==number) {
				return true;
			}
			
		}
		return false;
		
	}
	//finds a left corner pos in the each box
	private static boolean isNumberInBox(int number,int [][]board, int row,int col) {
		int boxRow=row-row%3;
		int boxCol=col-col%3;
		for(int i = boxRow;i<boxRow+3;i++) {
			for(int j=boxCol;j<boxCol+3;j++) {
				if(board[i][j]==number) {
					return true;
				}
			}
		} return false;
	}
   
	private boolean solver(int[][] board) {
		// this.print();

		for (int row = 0; row < 9; row++) {
			for (int col = 0; col < 9; col++) {
				if (board[row][col] == 0) {
					for (int number = 1; number <= 9; number++) {
						if (isValid(number, board, row, col)) {
							// add(row,col,number);
							board[row][col] = number;

							if (solver(board)) {
								return true;
							} else {
								board[row][col] = 0;

							}
						}
					}
					return false;
				} else {

					boolean valid = isValid(get(row, col), board, row, col);
					if (valid) {
						return solver(board);
					} else {
						return false;
					}

				}
			}
		}

		return true;

	}
	@Override
	public void clear() {
		for(int i=0;i<9;i++) {
			for(int j=0;j<9;j++) {
				board[i][j]=0;
			}
		}
	}


	public  void newGame() {
		int row,col,num;
		Random rand=new Random();
		for(int i=0;i<10;i++) {
		 row=rand.nextInt(9);
		 col=rand.nextInt(9);
		 num=rand.nextInt(9)+1;
		 while(!isValid(num,board,row,col)) {
			 row=rand.nextInt(9);
			 col=rand.nextInt(9);
			 num=rand.nextInt(9)+1;
		 }
		 add(row,col,num);
		
	}}
	
	public void setMatrix(int[][] m) {
		//clear();
		
		for(int i=0;i<m.length;i++) {
			for(int j=0;j<m.length;j++) {
				board[i][j]=m[i][j];
				
			}
		}
		// TODO Auto-generated method stub
		
	}
	@Override
	public int[][] getMatrix() {
		// TODO Auto-generated method stub
		return board;
	}

	public  void print() {
		
		for(int i=0;i<9;i++) {
			
			if(i%3==0 && i!=0) {
				
				System.out.println("-----------");
				
			}
		for(int j=0;j<9;j++) {
				if(j%3==0&&j!=0) {
				System.out.print("|");
				
				
			}
				
			System.out.print(board[i][j]);
			}
		System.out.println();
		}
	}
	




	


	

}