Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added a sudoku solver project and deleted the blank project folder #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Projects/Sudoku/sudoku-web-app
Submodule sudoku-web-app deleted from cb7727
126 changes: 126 additions & 0 deletions Projects/sudoku-solver/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sudoku</title>
<link rel="stylesheet" href="style.css">
</head>
<header>
<div id="Name">
<h1>Sudoku Game</h1>
</div>
<div class="heading">
<div id="difficulty">
<h3>Difficulty : </h3>
<input type="radio" value="easy" name="diff" id="easy" checked> <span> Easy </span>
<input type="radio" value="medium" name="diff" id="medium"> <span> Medium </span>
<input type="radio" value="hard" name="diff" id="hard"> <span> Hard </span>

</div>
<div id="theme">
<h3>Theme : </h3>
<input type="radio" value = "light" onClick = "document.bgColor='white'" name="theme" checked id="light" > <span> Light </span>
<input type="radio" value = "dark" onClick = "document.bgColor='#333333'" name="theme" id="dark"> <span> Dark </span>
</div>
</div>
</header>
<body id="bcolor">
<div class="container">
<div class="timer">
Time Taken :
<span id="timer">00 : 00 : 00</span>
</div>
<div id="board">
<div id="0" class="cell"></div>
<div id="1" class="cell"></div>
<div id="2" class="cell right"></div>
<div id="3" class="cell"></div>
<div id="4" class="cell"></div>
<div id="5" class="cell right" ></div>
<div id="6" class="cell"></div>
<div id="7" class="cell"></div>
<div id="8" class="cell right"></div>
<div id="9" class="cell"></div>
<div id="10" class="cell"></div>
<div id="11" class="cell right"></div>
<div id="12" class="cell"></div>
<div id="13" class="cell"></div>
<div id="14" class="cell right"></div>
<div id="15" class="cell"></div>
<div id="16" class="cell"></div>
<div id="17" class="cell right"></div>
<div id="18" class="cell bottom"></div>
<div id="19" class="cell bottom"></div>
<div id="20" class="cell right bottom"></div>
<div id="21" class="cell bottom"></div>
<div id="22" class="cell bottom"></div>
<div id="23" class="cell right bottom"></div>
<div id="24" class="cell bottom"></div>
<div id="25" class="cell bottom"></div>
<div id="26" class="cell right bottom"></div>
<div id="27" class="cell"></div>
<div id="28" class="cell"></div>
<div id="29" class="cell right"></div>
<div id="30" class="cell"></div>
<div id="31" class="cell"></div>
<div id="32" class="cell right" ></div>
<div id="33" class="cell"></div>
<div id="34" class="cell"></div>
<div id="35" class="cell right"></div>
<div id="36" class="cell"></div>
<div id="37" class="cell"></div>
<div id="38" class="cell right"></div>
<div id="39" class="cell"></div>
<div id="40" class="cell"></div>
<div id="41" class="cell right" ></div>
<div id="42" class="cell"></div>
<div id="43" class="cell"></div>
<div id="44" class="cell right" ></div>
<div id="45" class="cell bottom"></div>
<div id="46" class="cell bottom"></div>
<div id="47" class="cell right bottom"></div>
<div id="48" class="cell bottom"></div>
<div id="49" class="cell bottom"></div>
<div id="50" class="cell right bottom"></div>
<div id="51" class="cell bottom"></div>
<div id="52" class="cell bottom"></div>
<div id="53" class="cell right bottom"></div>
<div id="54" class="cell"></div>
<div id="55" class="cell"></div>
<div id="56" class="cell right"></div>
<div id="57" class="cell"></div>
<div id="58" class="cell"></div>
<div id="59" class="cell right"></div>
<div id="60" class="cell"></div>
<div id="61" class="cell"></div>
<div id="62" class="cell right"></div>
<div id="63" class="cell"></div>
<div id="64" class="cell"></div>
<div id="65" class="cell right"></div>
<div id="66" class="cell"></div>
<div id="67" class="cell"></div>
<div id="68" class="cell right"></div>
<div id="69" class="cell"></div>
<div id="70" class="cell"></div>
<div id="71" class="cell right"></div>
<div id="72" class="cell"></div>
<div id="73" class="cell"></div>
<div id="74" class="cell right"></div>
<div id="75" class="cell"></div>
<div id="76" class="cell"></div>
<div id="77" class="cell right"></div>
<div id="78" class="cell"></div>
<div id="79" class="cell"></div>
<div id="80" class="cell right"></div>
</div>
<div id="generator">
<button id="generate">Generate New Puzzle</button>
</div>
<div id="solver">
<button id="solve">Solve The Puzzle</button>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
233 changes: 233 additions & 0 deletions Projects/sudoku-solver/script.js
Original file line number Diff line number Diff line change
@@ -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();
}

/*---------------------------------------------------------------------------------------------------------------------------------------------------------------- */
Loading