JSFiddle - React, Tailwind, and code Playground

by António Almeida

JavaScript

var pieces = new Array();
pieces.push(
    [0,0],
    [1,0],[2,0],[3,0],[4,0],[5,0],[6,0],
    [1,1],[2,1],[3,1],[4,1],[5,1],
    [2,2],[3,2],[4,2],
    [3,3]
);

function testStructure(ps) {
    return (
        // Lines
        ps[0][0] + ps[1][0] + ps[2][0] + ps[3][0] == 6 &&
        ps[0][1] + ps[1][1] + ps[2][1] + ps[3][1] == 6 &&
        ps[4][0] + ps[5][0] + ps[6][0] + ps[7][0] == 6 &&
        ps[4][1] + ps[5][1] + ps[6][1] + ps[7][1] == 6 &&
        
        // Columns
        ps[1][0] + ps[1][1] + ps[5][0] + ps[5][1] == 6 &&
        ps[2][0] + ps[2][1] + ps[6][0] + ps[6][1] == 6 &&
        ps[3][0] + ps[3][1] + ps[7][0] + ps[7][1] == 6 &&
        ps[4][0] + ps[4][1] + ps[8][0] + ps[8][1] == 6 &&
        
        // Diagonals
        ps[1][0] + ps[2][1] + ps[7][0] + ps[8][1] == 6 &&
        ps[4][0] + ps[3][1] + ps[6][0] + ps[5][1] == 6
        
    ) ? true : false ;
}


Array.prototype.shuffle = function () {
    for (var i = this.length - 1; i > 0; i--) {
        var j = Math.floor(Math.random() * (i + 1));
        var tmp = this[i];
        this[i] = this[j];
        this[j] = tmp;
    }
    return this;
}

/* CODE EXECUTION */

var data = pieces, ntries = 0;
do {
    ntries++;
    data.shuffle();
    for (var i = data.length - 1; i > 0; i--)
        if(Math.random() > 0.5) data[i].shuffle();
} while( !testStructure(data) && ntries < 1000)

console.log(ntries, data);