Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                 <body onload="generateBoard(2)">

    <div class=" max-550">
        <p>This is just a random sudoku board generator based in the difficulty introduced that i made up for my game "soduko". You can play it
            <a href="https://reymon359.github.io/ionic-soduko/" target="_blank">here</a>. And get the code   <a href="https://github.com/reymon359/web-experiments/tree/master/Sudoku%20Board%20Generator" target="_blank">here</a> Enjoy :D
        </p>
        <div style="display: inline;" id="difficultyButton">
            <button onclick="generateBoard(0)"> Beginner </button>
            <button onclick="generateBoard(1)"> Easy </button>
            <button onclick="generateBoard(2)"> Normal </button>
            <button onclick="generateBoard(3)"> Hard </button>
            <button onclick="generateBoard(4)"> Extreme </button>
        </div>
        <div class="board"><div class="row">            <div class="box">                <div class="fixed">                    <div>3</div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>9</div>                </div>            </div>            <div class="box  border-right">                <div class="fixed">                    <div>7</div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>8</div>                </div>            </div>            <div class="box">                <div>                    <div></div>                </div>            </div>            <div class="box  border-right">                <div class="fixed">                    <div>5</div>                </div>            </div>            <div class="box">                <div>                    <div></div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>4</div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>2</div>                </div>            </div>        </div><div class="row">            <div class="box">                <div class="fixed">                    <div>1</div>                </div>            </div>            <div class="box">                <div>                    <div></div>                </div>            </div>            <div class="box  border-right">                <div class="fixed">                    <div>2</div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>3</div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>9</div>                </div>            </div>            <div class="box  border-right">                <div>                    <div></div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>8</div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>6</div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>5</div>                </div>            </div>        </div><div class="row">            <div class="box border-bottom">                <div>                    <div></div>                </div>            </div>            <div class="box border-bottom">                <div class="fixed">                    <div>6</div>                </div>            </div>            <div class="box border-right border-bottom ">                <div class="fixed">                    <div>5</div>                </div>            </div>            <div class="box border-bottom">                <div class="fixed">                    <div>1</div>                </div>            </div>            <div class="box border-bottom">                <div class="fixed">                    <div>2</div>                </div>            </div>            <div class="box border-right border-bottom ">                <div class="fixed">                    <div>4</div>                </div>            </div>            <div class="box border-bottom">                <div>                    <div></div>                </div>            </div>            <div class="box border-bottom">                <div class="fixed">                    <div>7</div>                </div>            </div>            <div class="box border-bottom">                <div>                    <div></div>                </div>            </div>        </div><div class="row">            <div class="box">                <div>                    <div></div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>1</div>                </div>            </div>            <div class="box  border-right">                <div class="fixed">                    <div>3</div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>7</div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>8</div>                </div>            </div>            <div class="box  border-right">                <div>                    <div></div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>5</div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>2</div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>4</div>                </div>            </div>        </div><div class="row">            <div class="box">                <div>                    <div></div>                </div>            </div>            <div class="box">                <div>                    <div></div>                </div>            </div>            <div class="box  border-right">                <div class="fixed">                    <div>6</div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>5</div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>4</div>                </div>            </div>            <div class="box  border-right">                <div>                    <div></div>                </div>            </div>            <div class="box">                <div>                    <div></div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>1</div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>9</div>                </div>            </div>        </div><div class="row">            <div class="box border-bottom">                <div class="fixed">                    <div>5</div>                </div>            </div>            <div class="box border-bottom">                <div>                    <div></div>                </div>            </div>            <div class="box border-right border-bottom ">                <div class="fixed">                    <div>4</div>                </div>            </div>            <div class="box border-bottom">                <div class="fixed">                    <div>9</div>                </div>            </div>            <div class="box border-bottom">                <div class="fixed">                    <div>1</div>                </div>            </div>            <div class="box border-right border-bottom ">                <div class="fixed">                    <div>3</div>                </div>            </div>            <div class="box border-bottom">                <div>                    <div></div>                </div>            </div>            <div class="box border-bottom">                <div class="fixed">                    <div>8</div>                </div>            </div>            <div class="box border-bottom">                <div>                    <div></div>                </div>            </div>        </div><div class="row">            <div class="box">                <div class="fixed">                    <div>6</div>                </div>            </div>            <div class="box">                <div>                    <div></div>                </div>            </div>            <div class="box  border-right">                <div class="fixed">                    <div>1</div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>4</div>                </div>            </div>            <div class="box">                <div>                    <div></div>                </div>            </div>            <div class="box  border-right">                <div>                    <div></div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>2</div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>3</div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>8</div>                </div>            </div>        </div><div class="row">            <div class="box">                <div>                    <div></div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>5</div>                </div>            </div>            <div class="box  border-right">                <div class="fixed">                    <div>8</div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>2</div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>3</div>                </div>            </div>            <div class="box  border-right">                <div>                    <div></div>                </div>            </div>            <div class="box">                <div>                    <div></div>                </div>            </div>            <div class="box">                <div>                    <div></div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>7</div>                </div>            </div>        </div><div class="row">            <div class="box">                <div>                    <div></div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>3</div>                </div>            </div>            <div class="box  border-right">                <div class="fixed">                    <div>9</div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>6</div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>7</div>                </div>            </div>            <div class="box  border-right">                <div>                    <div></div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>4</div>                </div>            </div>            <div class="box">                <div>                    <div></div>                </div>            </div>            <div class="box">                <div class="fixed">                    <div>1</div>                </div>            </div>        </div></div>
    </div>
</body><script src="https://cdn.jsdelivr.net/gh/reymon359/web-experiments/Social%20Signature/socialsignature.min.js" crossorigin="anonymous"></script>
              
            
!

CSS

              
                *{
  font-family:Arial
}
button {
    color: #fff;
    background-color: #009688;
    font-family: Arial, Helvetica, sans-serif;
    cursor: pointer;
    padding: 3%;
    border: none;
    border-radius: 50px;
    box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 1px 5px 0 rgba(0, 0, 0, 0.12);
}

button:hover {
    background-color: #024d45;
    color: #fff;
}

button:focus {
    outline: 0;
    background-color: #024d45;
}

.max-550 {
    max-width: 500px;
    margin: auto;
    text-align: center;
}

.board {
    position: relative;
    margin-top: 5%;
    text-align: center;
    width: 100%;
    margin: auto;
    padding-top: 3%;
}

.row {
    text-align: center;
    display: -ms-flexbox;
    display: flex;
    flex-wrap: wrap;
}

.box {
    display: inline-block;
    width: 9.5%;
    text-align: center;
    padding: .5%;
}

.fixed {
    background-color: #80cbc4;
    cursor: context-menu;
    border-radius: 25%;
    padding: 30%;
}

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

.border-bottom {
    border-bottom: 2px solid;
}
              
            
!

JS

              
                /*
 * Sudoku board Generator
 * Ramon Morcillo @reymon359
 */

function generateBoard(difficulty) {
    let board = [
        [2, 8, 6, 7, 5, 4, 9, 3, 1],
        [9, 3, 1, 2, 8, 6, 7, 5, 4],
        [7, 5, 4, 9, 1, 3, 8, 6, 2],
        [8, 9, 2, 6, 7, 5, 4, 1, 3],
        [6, 7, 5, 4, 3, 1, 2, 9, 8],
        [4, 1, 3, 8, 9, 2, 6, 7, 5],
        [5, 6, 9, 3, 4, 8, 1, 2, 7],
        [3, 4, 7, 1, 2, 9, 5, 8, 6],
        [1, 2, 8, 5, 6, 7, 3, 4, 9]
    ];
    let random10 = Math.floor((Math.random() * 10) + 1);
    for (let r = 0; r < random10; r++) {
        for (let i = 0; i < 9; i++) {
            for (let j = 0; j < 9; j++) {
                board[i][j] = board[i][j] + 1;
                if (board[i][j] == 10) {
                    board[i][j] = 1;
                }
            }
        }
    }
    let min = difficulty;
    let max = min + 2;
    board.forEach((row) => {
        let toReplace = shuffleArray([0, 1, 2, 3, 4, 5, 6, 7, 8]);
        for (let i = 0; i < Math.floor(Math.random() * (max - min + 1) + min); i++) {
            row[toReplace[i]] = '';
        }
    });
    renderBoard(board);
    

}

function renderBoard(boardToRender) {
    let board = boardToRender;
    let html = '';
    for (let i = 0; i < 9; i++) {
        html += "<div class='row' >";
        for (let j = 0; j < 9; j++) {

            // if (i == 2 && j == 2) {
            //     html += "            <div class='box border-right border-bottom '  >";
            // } else if (j == 2 || j == 5) {
            //     html += "            <div class='box border-right'  >";

            if ((i == 2 && j == 2) || (i == 2 && j == 5) || (i == 5 && j == 2) || (i == 5 && j == 5)) {
                html += "            <div class='box border-right border-bottom '  >";
            } else if (i == 2 || i == 5) {
                html += "            <div class='box border-bottom'  >";
            } else if (j == 2 || j == 5) {
                html += "            <div class='box  border-right'  >";
            } else {
                html += "            <div class='box'  >";
            }

            if (board[i][j] == '') {
                html += "                <div >"
            } else {
                html += "                <div class='fixed'  >"
            }


            html += "                    <div>" + board[i][j] + "</div>"
            html += "                </div>"
            html += "            </div>"
        }
        html += "        </div>"

    }
    document.getElementsByClassName('board')[0].innerHTML = html;

}
// -------SHUFFLE ARRAY------------
function shuffleArray(array) {
    let currentIndex = array.length,
        temporaryValue, randomIndex;
    // While there remain elements to shuffle inside the array
    while (0 !== currentIndex) {
        // Pick a remaining element from the array
        randomIndex = Math.floor(Math.random() * currentIndex);
        // randomIndex is a random number between 0 and the array position
        currentIndex -= 1;
        // We get a new currentIndex and swap it with the current element.
        temporaryValue = array[currentIndex];
        // We save the currentIndex value in temporaryValue
        array[currentIndex] = array[randomIndex];
        // Now we fill the new currentIndex position with the value from the randomIndex
        array[randomIndex] = temporaryValue;
        // Lastly in the randomIndex position we put the temporaryValue we saved before
    }
    return array;
}
              
            
!
999px

Console