sudoku game using html CSS and JavaScript

Introduction
Hello friends, you all are welcome to today’s beautiful project. Today we have made a beautiful project for you which is going to be very amazing. This project is a game which is very easy to create. If you are also learning coding, then this project will be absolutely perfect for you because with the help of this project you will get very good knowledge of coding which will help you in your job. You will get to learn a lot from this project, like when you create this project, you may face many errors, when you solve them then you will You will get knowledge of coding and you will also learn to solve errors. To create this game we have used html css and javascript technology, so now let’s understand about our code that how we have created this project.
HTML (index.html)
Get Discount on Top Educational Courses
First of all we have to understand the code of HTML because to create a game we first have to create the structure of the game with the help of HTML, without the structure of HTML we cannot create the game, this code which you are seeing is the code of HTML with the help of which we have created the structure of our game.
- <!DOCTYPE html> This tag comes first, which tells that this code is of html.
- Then in the second our title comes in which you can add the name related to your project <title>Sudoku Game</title>
- Then to create the game we have made a box <div class=”sudoku-bk”> and inside this box we have created another box <div class=”sudoku-game”></div>
Friends, we have written some HTML code but to create this game, the importance of JavaScript is very important, so now we have created the structure with the help of HTML, but now we have to design it too, so now let’s understand the CSS code.
Sudoku Game
$(document).ready(function () {
$(".sudoku-game").sudoku({});
});
CSS (Style.css)
Friends this is the code of CSS with the help of which we have designed our game, without CSS you cannot design any of your projects, you have to use CSS, with the help of CSS you can make your project look good, due to which the look and feel of the project looks good, so let’s understand the CSS code now.
- First of all we have used this font in our body you can use any other font also font-family: ‘Montserrat’, sans-serif; and we have kept the body height something like this height: 100%;
- We have also kept the width and height of the game box at 100%. We have kept the height and width of the box same width: 100%; and at the top we have used padding padding-top: 70px; and in the background of the game we have used png image background:url(“sudoku.png”) center 0px no-repeat;
- To create the game, we have created rows which makes it easier to create the game and in this we have used position: absolute; and in this we have also applied z-index:1;
5. We’ve used position: relative; in the game row and added display: block; - We have kept the width of the game’s columns as width: 11.11%; and min-height:45px; and have made it float left and have used this color in the border border-color: #CCC;
- . Friends, we have also used animation which makes our game look good, for which we have used CSS keyframes @-moz-keyframes pulse{
40%{ background:rgba(100,100,100,0.04); }
}
*, *:before, *:after {
box-sizing: border-box;
}
body {
background-attachment: fixed;
background-size: contain;
-moz-user-select: -moz-none;
-ms-user-select: none;
-webkit-user-select: none;
user-select: none;
font-family: 'Montserrat', sans-serif;
line-height: 1.5;
font-size: 12px;
font-weight: 400;
}
html, body {
height: 100%;
}
.sdk-row:after {
content: ".";
visibility: hidden;
display: block;
height: 0;
clear: both; }
.sudoku-bk {
width: 100%;
height: 100%;
padding-top: 70px;
background:url("sudoku.png") center 0px no-repeat;
}
.sdk-game {
width: 100%;
height: 100%;
}
.sdk-table {
max-width: 416px;
margin: auto;
display: block;
position: relative;
border: #AAA solid 2px;
border-radius: 6px;
font-size: 2em;
color: #777;
-webkit-transition: all 2s ease;
-moz-transition: all 2s ease;
-ms-transition: all 2s ease;
-o-transition: all 2s ease;
transition: all 2s ease;
}
.sdk-table-bk {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
z-index:1;
}
.sdk-row {
display: block;
position: relative;
width: 100%;
z-index: 2;
}
.sdk-col {
width: 11.11%;
min-height:45px;
position: relative;
float: left;
border-color: #CCC;
border-style: dashed;
border-width: 2px 2px 0 0;
}
.sdk-row.sdk-border {
border-bottom: #AAA solid 2px;
}
.sdk-col.sdk-border {
border-right: #AAA solid 2px;
}
.sdk-table .sdk-row .sdk-col:last-child {
border-right: none;
}
.sdk-table .sdk-row:first-child .sdk-col,
.sdk-table .sdk-row:nth-child(4) .sdk-col,
.sdk-table .sdk-row:nth-child(7) .sdk-col {
border-top: none;
}
.sdk-row:first-child .sdk-col:nth-child(n+4):nth-child(-n+6),
.sdk-row:nth-child(2) .sdk-col:nth-child(n+4):nth-child(-n+6),
.sdk-row:nth-child(3) .sdk-col:nth-child(n+4):nth-child(-n+6),
.sdk-row:nth-child(7) .sdk-col:nth-child(n+4):nth-child(-n+6),
.sdk-row:nth-child(8) .sdk-col:nth-child(n+4):nth-child(-n+6),
.sdk-row:nth-child(9) .sdk-col:nth-child(n+4):nth-child(-n+6),
.sdk-row:nth-child(4) .sdk-col:nth-child(n+1):nth-child(-n+3),
.sdk-row:nth-child(5) .sdk-col:nth-child(n+1):nth-child(-n+3),
.sdk-row:nth-child(6) .sdk-col:nth-child(n+1):nth-child(-n+3),
.sdk-row:nth-child(4) .sdk-col:nth-child(n+7):nth-child(-n+9),
.sdk-row:nth-child(5) .sdk-col:nth-child(n+7):nth-child(-n+9),
.sdk-row:nth-child(6) .sdk-col:nth-child(n+7):nth-child(-n+9)
{
background-color: rgba(0,0,0,0.04);
}
.sdk-col.sdk-selected {
background-color: rgba(100,100,100,0.2) !important;
-webkit-animation: .8s .1s pulse infinite linear;
-moz-animation: .8s .1s pulse infinite linear;
-o-animation: .8s .1s pulse infinite linear;
animation: .8s .1s pulse infinite linear;
}
@-moz-keyframes pulse{
40%{ background:rgba(100,100,100,0.04); }
}
@-webkit-keyframes pulse{
40%{ background:rgba(100,100,100,0.04); }
}
@-o-keyframes pulse {
40%{ background:rgba(100,100,100,0.04); }
}
@keyframes pulse {
40%{ background:rgba(100,100,100,0.04); }
}
.sdk-solution {
padding: 4px;
text-align: center;
}
.sdk-helper {
background-color: rgba(100,100,100,0.2);
color: #000;
}
.sdk-picker {
max-width: 416px;
margin: auto;
margin-top: 50px;
display: block;
position: relative;
background: #f9f9f9;
box-shadow: 0 0 1px rgba(0, 0, 0, 0.2), 0 2px 4px rgba(0, 0, 0, 0.1);
border-radius: 5px;
margin-bottom: 20px;
text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.1);
padding: 20px;
-webkit-transition: all 2s ease;
-moz-transition: all 2s ease;
-ms-transition: all 2s ease;
-o-transition: all 2s ease;
transition: all 2s ease;
}
.sdk-btn {
display: block;
width: 100%;
text-align: center;
padding: 12px 28px;
border-radius: 3px;
background: #707070;
color: rgba(255, 255, 255, 0.9);
font-size: 2em;
transition: background .3s ease, color .3s ease;
text-decoration: none;
margin-bottom: 5px;
}
.sdk-btn:hover{
color: rgba(243, 94, 94, 1);
}
.sdk-ans-container {
max-width: 416px;
margin: auto;
margin-top: 10px;
display: block;
position: relative;
}
.sdk-ans-container .sdk-btn {
display:inline-block;
width: 11.11%;
padding: 5px;
}
.sdk-no-show {
opacity: 0;
}
@media only screen and (max-width: 600px) {
.sdk-table,
.sdk-ans-container .sdk-btn {
font-size: 1.5em;
}
.sdk-col {
min-height:36px;
}
}
Javascript (Script.js)
Now comes the most important thing about the javascript code. This code is the most important code for our game. Without it, our game is useless. Unless we link the javascript code to the html, our game will not work because with html we can create a framework. Then with the help of css, we can design the UI of our project well. By doing this, our project can only look good, but to make it work, we have to take the help of javascript. Without javascript, you cannot make it.
We have created three different levels in this game. In which there is easy, medium and hard and in this we have created a function with the help of javascript in which user can select the level init: function (options)
(function( $ ){
var methods = {
init : function( options ) {
return this.each(function() {
var settings = {
levels : [
{level: "Easy", numbers: 70},
{level: "Medium", numbers: 30},
{level: "Hard", numbers: 20}
]
};
var defaults = {
matrix : [],
domMatrix : [],
numOfRows : 9,
numOfCols : 9,
level : 40,
selected : null,
selectedSolution : null,
anwerTracker : {
"1" : 9,
"2" : 9,
"3" : 9,
"4" : 9,
"5" : 9,
"6" : 9,
"7" : 9,
"8" : 9,
"9" : 9
}
}
if ( options ) {
$.extend( settings, options );
}
var $this = $(this);
$this.addClass('sdk-game');
//creates the sudoku number grid
$this.createMatrix = function() {
var matrix = new Array();
for(var rowCounter=0;rowCounter<9;rowCounter++){
matrix[rowCounter] = new Array();
for(var colCounter=0;colCounter9) number = number % 9;
if(number==0) number=9;
matrix[rowCounter][colCounter] = number;
}
}
// Switching rows
for(var no=0;no<9;no+=3){
for(var no2=0;no2<3;no2++){
row1 = Math.floor(Math.random()*3);
row2 = Math.floor(Math.random()*3);
while(row2==row1){
row2 = Math.floor(Math.random()*3);
}
row1 = row1 + no;
row2 = row2 + no;
var tmpMatrix = new Array();
tmpMatrix = matrix[row1];
matrix[row1] = matrix[row2];
matrix[row2] = tmpMatrix;
}
}
// Switching columns
for(var no=0;no<9;no+=3){
for(var no2=0;no2<3;no2++){
col1 = Math.floor(Math.random()*3);
col2 = Math.floor(Math.random()*3);
while(col2==col1){
col2 = Math.floor(Math.random()*3);
}
col1 = col1 + no;
col2 = col2 + no;
var tmpMatrix = new Array();
for(var no3=0;no3<matrix.length;no3++){
tmpMatrixValue = matrix[no3][col1];
matrix[no3][col1] = matrix[no3][col2];
matrix[no3][col2] = tmpMatrixValue;
}
}
}
return matrix;
};
// create the playable table
$this.createTable = function() {
//array to hold the dom reference to the table matrix so that we dont have to travers dom all the time
defaults.domMatrix = [];
//create table
defaults.table = $("");
//add rows and columns to table
for (var row=0;row<defaults.numOfRows;row++) {
defaults.domMatrix[row] = [];
var tempRow = $("");
//set solid border after 3rd and 6th row
if (row == 2 || row == 5) tempRow.addClass("sdk-border");
for (var col=0;col<defaults.numOfCols;col++) {
defaults.domMatrix[row][col] = $("");
//set solid border after 3rd and 6th column
if (col == 2 || col == 5) defaults.domMatrix[row][col].addClass("sdk-border");
//add columns to rows
tempRow.append(defaults.domMatrix[row][col]);
}
//add rows to table
defaults.table.append(tempRow);
}
//add extra div in here for background decoration
defaults.table.append("");
//add table to screen
$this.append(defaults.table);
//populate table with random number depending on the level difficulty
var items = defaults.level;
while (items > 0) {
var row = Math.floor(Math.random() * (8 - 0 + 1)) + 0;
var col = Math.floor(Math.random() * (8 - 0 + 1)) + 0;
if (defaults.domMatrix[row][col].children().length == 0) {
defaults.domMatrix[row][col].append(""+ defaults.matrix[row][col] +"");
defaults.anwerTracker[defaults.matrix[row][col].toString()]--;
items--;
}
}
//click even when clicking on cells
defaults.table.find(".sdk-col").click(function () {
//remove any helper styling
$this.find(".sdk-solution").removeClass("sdk-helper");
$this.find(".sdk-col").removeClass("sdk-selected");
if ($(this).children().length == 0) {
//select this
defaults.domMatrix[$(this).attr("data-row")][$(this).attr("data-col")].addClass("sdk-selected");
defaults.selected = defaults.domMatrix[$(this).attr("data-row")][$(this).attr("data-col")];
defaults.selectedSolution = defaults.matrix[$(this).attr("data-row")][$(this).attr("data-col")]
} else {
//add helper style
$this.highlightHelp(parseInt($(this).text()));
}
});
//add answers choices to screen
$this.answerPicker();
//remove the no show class to do a small fadein animation with css
setTimeout(function () {
defaults.table.removeClass("sdk-no-show");
}, 300);
};
//add answer picker to screen
$this.answerPicker = function() {
//make a answer container
var answerContainer = $("");
//add answer buttons to container
for (var a in defaults.anwerTracker) {
//check if need to show button else we add it for space reason but dont pick up clicks from it
if (defaults.anwerTracker[a] > 0) {
answerContainer.append(""+a+"");
} else {
answerContainer.append(""+a+"");
}
}
answerContainer.find(".sdk-btn").click(function () {
//only listen to clicks if it is shown
if (!$(this).hasClass("sdk-no-show") && defaults.selected != null && defaults.selected.children().length == 0 ) {
//check if it is the answer
if ( defaults.selectedSolution == parseInt($(this).text()) ) {
//decrease answer tracker
defaults.anwerTracker[$(this).text()]--;
//if answer tracker is 0 hide that button
if (defaults.anwerTracker[$(this).text()] == 0) {
$(this).addClass("sdk-no-show");
}
//remove highlighter
$this.find(".sdk-col").removeClass("sdk-selected");
//add the answer to screen
defaults.selected.append(""+ defaults.selectedSolution +"");
}
}
});
$this.append(answerContainer);
};
//add highlight help
$this.highlightHelp = function(number) {
//loop through dom matrix to find filled in number that match the number we clicked on
for (var row=0;row<defaults.numOfRows;row++) {
for (var col=0;col<defaults.numOfCols;col++) {
if ( parseInt(defaults.domMatrix[row][col].text()) == number ) {
defaults.domMatrix[row][col].find(".sdk-solution").addClass("sdk-helper");
}
}
}
};
// create difficulty picker
$this.createDiffPicker = function() {
//level picker container
var picker = $("");
//loop through all levels possible and add buttons to the picker container
$(settings.levels).each(function (e) {
picker.append(""+this.level+"");
});
//add it to screen
$this.append(picker);
//click event for the level select buttons
picker.find(".sdk-btn").click(function () {
picker.addClass("sdk-no-show");
defaults.level = parseInt($(this).attr("data-level"));
//wait for animation to complete to continue on
setTimeout(function () {
// remove the picker from the DOM
picker.remove();
// add the playable table to screen.
$this.createTable();
}, 2000);
});
//remove the no show class to do a small fadein animation with css
setTimeout(function () {
picker.removeClass("sdk-no-show");
}, 500);
};
defaults.matrix = $this.createMatrix();
$this.createDiffPicker();
});
}
};
$.fn.sudoku = function( method ) {
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.sudoku' );
}
};
})( jQuery );
Output:

Disclaimer: The code provided in this post is sourced from GitHub and is distributed under the MIT License. All credits for the original code go to the respective owner. We have shared this code for educational purposes only. Please refer to the original repository for detailed information and any additional usage rights or restrictions.
sudoku game using html CSS and JavaScript Introduction Hello friends, you all are welcome to today’s beautiful project. Today we have made …
Drawing Chhatrapati Shivaji Maharaj Using Python Chhatrapati Shivaji Maharaj, the legendary Maratha warrior and founder of the Maratha Empire, is an inspiration …
Resume Builder Application using Java With Source Code Graphical User Interface [GUI] Introduction: The Resume Builder Application is a powerful and user-friendly …
Encryption Tool using java with complete source Code GUI Introduction: The Encryption Tool is a Java-based GUI application designed to help users …