Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sudokupak;
- public class MySudokuModel implements SudokuModel {
- private int board [][] = new int [9][9];
- // put zeros aka 0s in all the squares
- @Override
- public void clear() {
- // System.out.println("inside clear");
- for (int row = 0; row < 9; row++) {
- for (int col = 0; col < 9; col++) {
- board [row][col] = 0;
- }
- }
- // TODO Auto-generated method stub
- }
- // return the sudoku board with a 2D array
- @Override
- public int[][] getBoard() {
- // System.out.println("inside getBoard");
- // TODO Auto-generated method stub
- return board;
- }
- // receives the sudoku board, each row ends with \n
- // checks that the board is "valid" (numbers are placed properly) and that the board is 9x9
- // if something is wrong it should throw a IllegalArgumentException
- //like so:
- //(9 rows x 9 columns)
- // 3...8....
- // .........
- // .........
- // .........
- // .........
- // ......8..
- @Override
- public void setBoard(String input) {
- // System.out.println("inside setBoard");
- // TODO Auto-generated method stub
- String[] rows = input.split("[\r\n]+");
- if (rows.length != 9) {
- throw new IllegalArgumentException();
- }
- //rows
- for (int row = 0; row < rows.length; row++) {
- String r = rows[row];
- if (r.length() != 9) {
- throw new IllegalArgumentException();
- }
- //columns
- for (int col = 0; col < r.length(); col++) {
- String s = String.valueOf(r.charAt(col));
- if (!s.matches("[0-9]")) {
- s = "0";
- }
- int nr = Integer.valueOf(s);
- board[row][col] = nr;
- }
- }
- }
- // returns the board like so: return board [row][col]
- @Override
- public int getSquare(int row, int col) {
- return board[row][col];
- }
- // check that its allowed to put the number in the square
- // if yes, make the necessary changes and return true
- // else do nothing and return false
- @Override
- public boolean setSquare(int row, int col, int val) {
- // System.out.println("inside setSquare");
- int r = row - row%3;
- int c = col - col%3;
- for(int i = r ; i< r+3 ; i++)
- {
- for(int j = c; j < c+3 ; j++)
- {
- if(board[i][j]==val)
- {
- // System.out.println("setSquare wtf");
- return true;
- }
- }
- }
- return false;
- }
- //check that its allowed to put the number but make no changes
- @Override
- public boolean isLegal(int row, int col, int val) {
- return row>=0 && row<9 && col>=0 && col<9
- && val>0 && val<=9 && board[row][col]==0;
- }
- // solve the sudoku puzzle
- // iteratively search for a solution for a given puzzle
- // if solution found, return true
- // if solution not found, return false and continue loop until solution found
- @Override
- public boolean solve() {
- // System.out.println("inside solve");
- for (int row = 0; row < 9; row++) {
- for (int col = 0; col < 9; col++) {
- // we search an empty cell
- if (board[row][col] == 0) {
- // we try possible numbers
- for (int number = 1; number <= 9; number++) {
- if (isLegal(row, col, number)) {
- // number ok. it respects sudoku constraints
- board[row][col] = number;
- if (solve()) { // we start backtracking recursively
- return true;
- } else { // if not a solution, we empty the cell and we continue
- board[row][col] = 0;
- }
- }
- }
- return false; // we return false
- }
- }
- }
- return true; // sudoku solved
- }
- //copy the model
- @Override
- public SudokuModel copy() {
- // System.out.println("inside copy");
- // TODO Auto-generated method stub
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement