forked from Vishal-raj-1/Awesome-JavaScript-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/vishal-raj-1/awesome-javasc…
…ript-projects into johncent
- Loading branch information
Showing
13 changed files
with
3,336 additions
and
2,054 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.