Skip to content

Commit

Permalink
Merge pull request Vishal-raj-1#1900 from soma2000-lang/iron
Browse files Browse the repository at this point in the history
Adding the Sudoku Project
  • Loading branch information
Rizwan-S authored Jul 19, 2021
2 parents 1c0d723 + 8364e07 commit 825fbc3
Show file tree
Hide file tree
Showing 5 changed files with 2,625 additions and 2,024 deletions.
Binary file added assets/Images/Sudoku_Solver.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
71 changes: 71 additions & 0 deletions assets/css/Sudoku_Solver.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#container {
height: auto;
width: 540px;
background-color: white;
display: flex;
flex-wrap: wrap;
justify-content: space-evenly;
align-content: space-evenly;
margin: 0 auto;
}

#generate-sudoku {
margin: auto;
display: block;
;
}

#solve {
margin: auto;
display: block;
;
}

#container div {
background-color: whitesmoke;
height: 60px;
width: 60px;
box-sizing: border-box;
font-family: sans-serif;
text-align: center;
vertical-align: middle;
line-height: 60px;
font-size: 30px;
color: red;
}

#container div:hover {
background-color: rgb(250, 250, 135);
}

.lsb {
border-left: black 2px solid;
}

.bsb {
border-bottom: black 2px solid;
}

.rsb {
border-right: black 2px solid;
}

.tsb {
border-top: black 2px solid;
}

.ldb {
border-left: black 0.6px dashed;
}

.bdb {
border-bottom: black 0.6px dashed;
}

.rdb {
border-right: black 0.6px dashed;
}

.tdb {
border-top: black 0.6px dashed;
}
197 changes: 197 additions & 0 deletions assets/js/Sudoku_Solver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
var arr = [
[],
[],
[],
[],
[],
[],
[],
[],
[]
]
var temp = [
[],
[],
[],
[],
[],
[],
[],
[],
[]
]

for (var i = 0; i < 9; i++) {
for (var j = 0; j < 9; j++) {
arr[i][j] = document.getElementById(i * 9 + j);

}
}

function initializeTemp(temp) {

for (var i = 0; i < 9; i++) {
for (var j = 0; j < 9; j++) {
temp[i][j] = false;

}
}
}


function setTemp(board, temp) {

for (var i = 0; i < 9; i++) {
for (var j = 0; j < 9; j++) {
if (board[i][j] != 0) {
temp[i][j] = true;
}

}
}
}


function setColor(temp) {

for (var i = 0; i < 9; i++) {
for (var j = 0; j < 9; j++) {
if (temp[i][j] == true) {
arr[i][j].style.color = "#DC3545";
}

}
}
}

function resetColor() {

for (var i = 0; i < 9; i++) {
for (var j = 0; j < 9; j++) {

arr[i][j].style.color = "green";


}
}
}

var board = [
[],
[],
[],
[],
[],
[],
[],
[],
[]
]


let button = document.getElementById('generate-sudoku')
let solve = document.getElementById('solve')

console.log(arr)

function changeBoard(board) {
for (var i = 0; i < 9; i++) {
for (var j = 0; j < 9; j++) {
if (board[i][j] != 0) {

arr[i][j].innerText = board[i][j]
} else
arr[i][j].innerText = ''
}
}
}


button.onclick = function() {
var xhrRequest = new XMLHttpRequest()
xhrRequest.onload = function() {
var response = JSON.parse(xhrRequest.response)
console.log(response)
initializeTemp(temp)
resetColor()

board = response.board
setTemp(board, temp)
setColor(temp)
changeBoard(board)
}
xhrRequest.open('get', 'https://sugoku.herokuapp.com/board?difficulty=easy')
//we can change the difficulty of the puzzle the allowed values of difficulty are easy, medium, hard and random
xhrRequest.send()
}

//to be completed by student, function should not return anything
// you can make a call to changeboard(board) function to update the state on the screen
//returns a boolean true of false

function isSafe(board, r, c, no) {


//not repeating in the same row or column
for (var i = 0; i < 9; i++) {
if (board[i][c] == no || board[r][i] == no) {
return false;
}
}
//subgrid
var sx = r - r % 3;
var sy = c - c % 3;

for (var x = sx; x < sx + 3; x++) {
for (var y = sy; y < sy + 3; y++) {
if (board[x][y] == no) {
return false;
}
}
}

return true;
}

function solveSudokuHelper(board, r, c) {

//base case
if (r == 9) {
changeBoard(board);
return true;
}
//other cases
if (c == 9) {
return solveSudokuHelper(board, r + 1, 0);
}
//pre-filled cell, skip it
if (board[r][c] != 0) {
return solveSudokuHelper(board, r, c + 1);
}

//there is 0 in the current location
for (var i = 1; i <= 9; i++) {

if (isSafe(board, r, c, i)) {
board[r][c] = i;
var success = solveSudokuHelper(board, r, c + 1);
if (success == true) {
return true;
}
//backtracking step
board[r][c] = 0;
}

}
return false;

}

function solveSudoku(board) {
solveSudokuHelper(board, 0, 0);
}


solve.onclick = function() {
solveSudoku(board)
}
Loading

0 comments on commit 825fbc3

Please sign in to comment.