diff --git a/Projects/Sudoku/sudoku-web-app b/Projects/Sudoku/sudoku-web-app deleted file mode 160000 index cb77276..0000000 --- a/Projects/Sudoku/sudoku-web-app +++ /dev/null @@ -1 +0,0 @@ -Subproject commit cb772768e963864d218a9568564584415d5b8f6c diff --git a/Projects/sudoku-solver/index.html b/Projects/sudoku-solver/index.html new file mode 100644 index 0000000..46f80a0 --- /dev/null +++ b/Projects/sudoku-solver/index.html @@ -0,0 +1,126 @@ + + + + + + Sudoku + + +
+
+

Sudoku Game

+
+
+
+

Difficulty :

+ Easy + Medium + Hard + +
+
+

Theme :

+ Light + Dark +
+
+
+ +
+
+ Time Taken : + 00 : 00 : 00 +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ +
+
+ +
+
+ + + \ No newline at end of file diff --git a/Projects/sudoku-solver/script.js b/Projects/sudoku-solver/script.js new file mode 100644 index 0000000..3daf941 --- /dev/null +++ b/Projects/sudoku-solver/script.js @@ -0,0 +1,233 @@ + +// in order to extract the elements of the board on the page +var a = [[],[],[],[],[],[],[],[],[]]; +// this is used to mark the remaining spaces +var temp = [[],[],[],[],[],[],[],[],[]]; + +// initialize the a[][] on the web to make the changes +function initialize(){ + for( i = 0 ; i < 9 ; i++ ){ + for( j = 0 ; j < 9 ; j++ ){ + a[i][j] = document.getElementById( i*9 + j); + a[i][j].innerText = 0; + temp[i][j] = false; + } + } +} + +// we need a board on which we can perform backtracking +var board = [[],[],[],[],[],[],[],[],[]]; + +// in order to generate new board +var generator = document.getElementById('generate'); + +// in order to solve the puzzle +var solver = document.getElementById('solve'); + +var difficulty; + +function mode(){ + var m = document.querySelectorAll('input[name="diff"]'); + for(item of m){ + if(item.checked){ + difficulty = item; + break; + } + } +} + +console.log(a); + +// updating the board when we get the solution +function changeboard(){ + for( i = 0 ; i < 9 ; i++ ){ + for( j = 0 ; j < 9 ; j++ ){ + if(board[i][j]!=0){ + a[i][j].innerText = board[i][j]; + } + else{ + a[i][j].innerText = ''; + } + } + } +} + +function qsetColor(){ + for( i = 0 ; i < 9 ; i++ ){ + for( j = 0 ; j < 9 ; j++ ){ + if(board[i][j]!=0){ + temp[i][j] = true; + // a[i][j].style.background = "brown"; + a[i][j].style.color = "brown"; + } + } + } +} +function asetColor(){ + for( i = 0 ; i < 9 ; i++ ){ + for( j = 0 ; j < 9 ; j++ ){ + if(temp[i][j]==false){ + // a[i][j].style.background = "green"; + a[i][j].style.color = "green"; + } + } + } +} +function resetcolor(){ + for( i = 0 ; i < 9 ; i++ ){ + for( j = 0 ; j < 9 ; j++ ){ + a[i][j].style.background = "rgb(223, 203, 203)"; + } + } +} + +var seconds = 0; +var minutes = 0 ; +var hours = 0; +var displayseconds; +var displayminutes; +var displayhour; + + +var start; +function time(){ + setInterval(function timer(){ + seconds++; + if(seconds/60 ===1){ + seconds = 0; + minutes++; + } + if(minutes/60===1){ + minutes=0; + hours++; + } + + if(seconds<10){ + displayseconds = "0" + seconds.toString(); + } + else{ + displayseconds = seconds; + } + if(minutes<10){ + displayminutes = "0" + minutes.toString(); + } + else{ + displayminutes = minutes; + } + if(hours<10){ + displayhour = "0" + hours.toString(); + } + else{ + displayhour = hours; + } + + document.getElementById('timer').innerHTML = displayhour + " : " + displayminutes + " : " + displayseconds; + }, 1000); +} +function reset(){ + clearInterval(start); +} +generator.onclick = function(){ + // resetboard(); + mode(); + reset(); + start = time(); + console.log(difficulty.value); + var xhr = new XMLHttpRequest(); + if(difficulty=='easy'){ + xhr.open('GET','https://sugoku.herokuapp.com/board?difficulty=easy'); + xhr.responseType = 'json'; + xhr.send(); + xhr.onload = function(){ + var robj = xhr.response; + board = robj.board; + console.log(board); + initialize(); + resetcolor(); + qsetColor(); + changeboard(); + } + } + else if(difficulty=='medium'){ + xhr.open('GET','https://sugoku.herokuapp.com/board?difficulty=hard'); + xhr.responseType = 'json'; + xhr.send(); + xhr.onload = function(){ + var robj = xhr.response; + board = robj.board; + console.log(board); + initialize(); + resetcolor(); + qsetColor(); + changeboard(); + } + } + else{ + xhr.open('GET','https://sugoku.herokuapp.com/board?difficulty=hard'); + xhr.responseType = 'json'; + xhr.send(); + xhr.onload = function(){ + var robj = xhr.response; + board = robj.board; + console.log(board); + initialize(); + resetcolor(); + qsetColor(); + changeboard(); + } + } +} + +/*--------------------------------------------------------------------------------------------------------------------------------------------------- */ +function isPossible(board,i,j,num){ + for(row=0;row<9;row++){ + if(board[row][j]==num){ + return false; + } + } + + for(col=0;col<9;col++){ + if(board[i][col]==num){ + return false; + } + } + + var sx = i - i%3; + var sy = j - j%3; + for(r = sx; r < sx+3; r++){ + for(c = sy; c < sy+3; c++){ + if(board[r][c]==num){ + return false; + } + } + } + return true; +} + +function solveSudoku(board,i,j){ + if(i==9){ + changeboard(); + return; + } + if(j==9){ + solveSudoku(board,i+1,0); + return; + } + if(board[i][j]!=0){ + solveSudoku(board,i,j+1); + return; + } + for(var num=1;num<=9;num++){ + if(isPossible(board,i,j,num)){ + board[i][j] = num; + solveSudoku(board,i,j+1); + board[i][j] = 0; + } + } +} +solve.onclick = function(){ + solveSudoku(board,0,0); + asetColor(); +} + +/*---------------------------------------------------------------------------------------------------------------------------------------------------------------- */ \ No newline at end of file diff --git a/Projects/sudoku-solver/style.css b/Projects/sudoku-solver/style.css new file mode 100644 index 0000000..4bd073d --- /dev/null +++ b/Projects/sudoku-solver/style.css @@ -0,0 +1,132 @@ +*{ + box-sizing: border-box; + padding: 0px; + margin: 0px; + font-family: Arial, Helvetica, sans-serif; + +} +header{ + display: flex; + flex-direction: column; + align-items: center; + padding: 5px; + border: 2px solid black; + margin-bottom: 10px; +} +h1{ + font-size: 40pt; + background-color: black; + color: chartreuse; + border: 2px solid chartreuse; + border-radius: 20px; + padding: 0px 5px; + margin: 10px; +} +.heading{ + display: flex; + width: 100%; + justify-content: center; +} + +#difficulty,#time,#theme{ + flex: 1; + display: flex; + justify-content: center; + align-items: center; + background-color: black; + color: chartreuse; + padding: 20px 15px; + margin: 5px; + border-radius: 10px; + border: 2px solid chartreuse; +} + +#easy,#medium,#hard,#dark,#light,#three,#five,#ten{ + flex: 1; +} +#difficulty > h3 , #time > h3 , #theme > h3{ + padding: 10px; + font-size: 30px; +} +input[type=radio]{ + height: 30px; + width: 30px; +} +span{ + padding: 0px 10px; + font-size: 35px; +} +.container{ + display: flex; + flex-direction: column; + align-items: center; + margin-bottom: 10px; +} +.timer{ + background-color: black; + color: yellowgreen; + padding: 10px; + margin: 10px; + border-radius: 10px; + border: 2px solid yellowgreen; + font-size: 2em; +} +#timer{ + color: yellowgreen; + padding: 10px; + margin: 10px; + font-size: 1em; +} +#board{ + display: grid; + grid-template-columns: repeat(9,60px); + grid-template-rows: repeat(9,60px); + border: 2.5px solid black; + margin-bottom: 10px; +} +.cell{ + display: flex; + background-color: rgb(223, 203, 203); + border: 1px dotted black; + justify-content: center; + align-items: center; + font-size: 40px; + font-weight: bold; +} +.cell:hover{ + cursor: pointer; +} +.cell:active{ + border: 2px solid black; +} +.right{ + border-right: 2.5px solid black; +} +.bottom{ + border-bottom: 2px solid black; +} + +#generate{ + font-size: 30px; + width: 300px; + margin: 10px; + font-weight: bold; + border: 2px solid black; + padding: 5px; + border-radius: 10px; +} +#solve{ + font-size: 30px; + width: 300px; + font-weight: bold; + border: 2px solid black; + padding: 5px; + border-radius: 10px; +} + +#generate:hover,#solve:hover{ + background-color: black; + color: yellowgreen; + border-radius: 10px; + border: 2px solid yellowgreen; +} \ No newline at end of file