Skip to content

Commit

Permalink
initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
f4r4z committed Mar 15, 2018
0 parents commit 3e79e35
Show file tree
Hide file tree
Showing 2 changed files with 203 additions and 0 deletions.
114 changes: 114 additions & 0 deletions Sudoku.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
public class Sudoku {

private int[][] board;

//constructor
public Sudoku (int[][] board) {
this.board = board;
}

//get board
public int[][] getBoard() {
return board;
}


//solve method helper
public void solve() {
solve(0,0);
}

//solves the next position and returns true if it is possible
private boolean nextPos(int row, int col) {

if (col < 8) {
return solve(row, col + 1);
} else {
return solve(row + 1, 0);
}
}

//solves the sudoku
private boolean solve(int row, int col) {

//base case
if(row > 8) {
return true;
}

if(board[row][col] != 0) {
if(check(row, col, board[row][col])) {
return nextPos(row, col);
}
}


int num = board[row][col];

//checks each number from 1 to 9
for(int i = 1; i < 10; i++) {

if(check(row, col, i)) {

board[row][col] = i;
if(nextPos(row, col)) {
return true;
}



}

//clears the previous positions
board[row][col] = 0;

}


return false;
}

//checks for each scenario
public boolean check(int row, int col, int x) {

//checks for rows and column
for(int i = 0; i < board.length; i++) {
if(i != row && board[i][col] == x) {
return false;
}

if(i != col && board[row][i] == x) {
return false;
}
}


int squareRow = (row/3) * 3;
int squareCol = (col/3) * 3;


//checks for 3x3
for(int i = squareRow; i < squareRow+3; i++) {
for(int j = squareCol; j < squareCol+3; j++) {
if((j != col || i != row) && board[i][j] == x) {
return false;
}
}
}

return true;
}



//prints the board
public void print() {
for(int r = 0; r < board.length; r++) {
for (int c = 0; c < board[r].length; c++)
System.out.print(board[r][c] + " ");
System.out.println();
}
}


}
89 changes: 89 additions & 0 deletions SudokuClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;

public class SudokuClient {
public static void main(String[] args) throws MalformedURLException {



//World's hardest sudoku
int[][] board = {
{8,0,0,0,0,0,0,0,0},
{0,0,3,6,0,0,0,0,0},
{0,7,0,0,9,0,2,0,0},
{0,5,0,0,0,7,0,0,0},
{0,0,0,0,4,5,7,0,0},
{0,0,0,1,0,0,0,3,0},
{0,0,1,0,0,0,0,6,8},
{0,0,8,5,0,0,0,1,0},
{0,9,0,0,0,0,4,0,0},
};

Sudoku test = new Sudoku(board);

test.solve();
test.print();


//Project Euler Problem 96
URL file = new URL("https://projecteuler.net/project/resources/p096_sudoku.txt");
int sum = 0;

//whole text file
String s = "";

try {
Scanner scanner = new Scanner(file.openStream());


while(scanner.hasNextLine()) {

s += scanner.nextLine() + "\n";


}

} catch (IOException e) {
System.out.println(e);
}

//Array of lines of the text
String[] lines = s.split(System.getProperty("line.separator"));

//Goes over each line
for(int i = 0; i < lines.length; i++) {

int[][] sudoku96 = new int[9][9];

//If line is not a title, stores it in a 2D array
if(lines[i].contains("Grid")) {
for(int r = 0; r < sudoku96.length; r++) {
for(int c = 0; c < sudoku96[r].length; c++) {

sudoku96[r][c] = Character.getNumericValue(lines[i+ r + 1].charAt(c));


}
}

//Sudoku class
Sudoku sudokuSolver = new Sudoku(sudoku96);
sudokuSolver.solve();
sudoku96 = sudokuSolver.getBoard();
String digits = sudoku96[0][0] + "" + sudoku96[0][1] + "" + sudoku96[0][2];
sum += Integer.parseInt(digits);

}



}

System.out.println();
System.out.println(sum);

}
}

0 comments on commit 3e79e35

Please sign in to comment.