var rules = [[1, 2], [1, -2], [-1, 2], [-1, -2], [-2, 1], [-2, -1], [2, 1], [2, -1]]

document.addEventListener("DOMContentLoaded", () => {
    var blocks = Array.from(document.getElementsByClassName("c"));
    var matrix = [];
    for (var i = 0; i < 8; i++) {
        var rav = [];
        for (var j = 0; j < 8; j++) {
            rav.push(blocks[i * 8 + j]);
        }
        matrix.push(rav);
    }

    for (var i = 0; i < matrix.length; i++) {
        for (var j = 0; j < matrix[i].length; j++) {
            matrix[i][j].addEventListener("click", (function (ci, cj) {
                return function () {
                    var flag = true;
                    var newloc = [];
                    for (var j = 0; j < rules.length; j++) {
                        try {
                            if (matrix[ci + rules[j][0]][cj + rules[j][1]].children.length != 0) {
                                flag = false;
                                newloc = [ci + rules[j][0], cj + rules[j][1]];
                            }
                        }
                        catch { }
                    }
                    if (!flag) {
                        var paci = matrix[newloc[0]][newloc[1]].children[0].cloneNode(true);
                        matrix[newloc[0]][newloc[1]].children[0].remove();
                        this.appendChild(paci);
                    }
                }
            })(i, j));
        }
    }
});