<commit_before>
//Width and height
var kCellCount = 9;
var kNewChessCount = 3;
var kMinMatchBallCount = 5;

var board_data = [];
var game_data;

var svg_sel;
var board_sel;
var cells_sel;
var chesses_sel;

var hud_sel;
var score_sel;

var kEmptyColorId = -1;
// svg.on('click', function() {
//     var coords = d3.mouse(this);
//     // console.log(coords);
// });

// https://en.wikipedia.org/wiki/Web_colors#HTML_color_names
var chess_colors = [
    "blueviolet",
    "deeppink",
    "limegreen",
    "steelblue",
    "rosybrown",
    "fuchsia",
    "orange",
];

function random_index(max_value) {
    var random = d3.randomUniform(0, max_value);
    var impl = function() {
        var id = parseInt(random());
        return id;
    }
    return impl();
}

function random_color_id() {
    return random_index(chess_colors.length);
}

function random_board_id() {
    return random_index(board_data.length);
}

function set_chess_color(selection) {
    selection
        .transition()
        .duration(500)
        .attr("stroke", function(d, i) {
            if (i === selected_board_id) {
                return "black";
            }
            else {
                return "none";
            }
        })
        .attr("fill", function(d, i) {
            if (d.color_id === kEmptyColorId) {
                return "lightgrey";
            }
            return chess_colors[d.color_id];
            })
        ;
}

function place_new_chesses() {
    for (var i=0;i<kNewChessCount;i++) {
       if (game_data.chess_count >= kCellCount * kCellCount) {
            // TODO: enter game_over state
            // new_game();
            // svg_sel.remove();
            // svg_sel = undefined;
            // resize();
            return;
        }

        var board_id = random_board_id();
        while (board_data[board_id].color_id !== kEmptyColorId) {
            board_id = random_board_id();
        }
        game_data.chess_count ++;
        board_data[board_id].color_id = random_color_id();    
    }
}

function check_connected_chesses(board_id) {
    function check_dir(dx, dy) {
        var center_color_id = board_data[board_id].color_id;
        var result = [];
        for (var x = board_data[board_id].i, y = board_data[board_id].j;
                x < kCellCount, y < kCellCount; x+=dx, y+=dy ) {
            var idx = y * kCellCount + x;
            if (board_data[idx].color_id != center_color_id) break;
            result.push(idx);
        }
        return result;
    }
    var results = [
        // [0, +/-1]
        check_dir(0, +1).concat(check_dir(0, -1)),
        // [+/-1, 0]
        check_dir(+1, 0).concat(check_dir(-1, 0)),
        // [+/-1, +/-1]
        check_dir(+1, +1).concat(check_dir(-1, -1)),
        // [+/-1, -/+1]
        check_dir(+1, -1).concat(check_dir(-1, +1)),
    ];
}

var selected_board_id = -1;
function update_board_data(board_id) {
    if (selected_board_id === -1) {
        if (board_data[board_id].color_id !== kEmptyColorId) {
            selected_board_id = board_id;
        }
    }
    else {
        if (board_data[board_id].color_id === kEmptyColorId) {
            // move #selected_board_id -> empty slot #board_id
            board_data[board_id].color_id = board_data[selected_board_id].color_id;
            board_data[selected_board_id].color_id = kEmptyColorId;

            check_connected_chesses(board_id);            
            place_new_chesses();

            selected_board_id = -1;
        }
        else {
            // selecting #board_id as #selected_board_id
            selected_board_id = board_id;<commit_msg>Finish check_connected_chesses()<commit_after>
//Width and height
var kCellCount = 9;
var kNewChessCount = 3;
var kMinMatchBallCount = 5;

var board_data = [];
var game_data;

var svg_sel;
var board_sel;
var cells_sel;
var chesses_sel;

var hud_sel;
var score_sel;

var kEmptyColorId = -1;
// svg.on('click', function() {
//     var coords = d3.mouse(this);
//     // console.log(coords);
// });

// https://en.wikipedia.org/wiki/Web_colors#HTML_color_names
var chess_colors = [
    "blueviolet",
    "deeppink",
    "limegreen",
    "steelblue",
    "rosybrown",
    "fuchsia",
    "orange",
];

function random_index(max_value) {
    var random = d3.randomUniform(0, max_value);
    var impl = function() {
        var id = parseInt(random());
        return id;
    }
    return impl();
}

function random_color_id() {
    return random_index(chess_colors.length);
}

function random_board_id() {
    return random_index(board_data.length);
}

function set_chess_color(selection) {
    selection
        .transition()
        .duration(500)
        .attr("stroke", function(d, i) {
            if (i === selected_board_id) {
                return "black";
            }
            else {
                return "none";
            }
        })
        .attr("fill", function(d, i) {
            if (d.color_id === kEmptyColorId) {
                return "lightgrey";
            }
            return chess_colors[d.color_id];
            })
        ;
}

function place_new_chesses() {
    for (var i=0;i<kNewChessCount;i++) {
       if (game_data.chess_count >= kCellCount * kCellCount) {
            // TODO: enter game_over state
            // new_game();
            // svg_sel.remove();
            // svg_sel = undefined;
            // resize();
            return;
        }

        var board_id = random_board_id();
        while (board_data[board_id].color_id !== kEmptyColorId) {
            board_id = random_board_id();
        }
        game_data.chess_count ++;
        board_data[board_id].color_id = random_color_id();    
    }
}

function check_connected_chesses(board_id) {
    function check_dir(dx, dy) {
        var center_color_id = board_data[board_id].color_id;
        var result = [];
        var x = board_data[board_id].i+dx, y = board_data[board_id].j+dy;
        while (x >=0 && x < kCellCount && y >= 0 && y < kCellCount) {
            var idx = y * kCellCount + x;
            // console.log("x" + x + "y" + y);
            if (board_data[idx].color_id != center_color_id) break;
            result.push(idx);
            
            x+=dx;
            y+=dy;
        }
        return result;
    }
    var results = [
        // [0, +/-1]
        check_dir(0, +1).concat(check_dir(0, -1)),
        // [+/-1, 0]
        check_dir(+1, 0).concat(check_dir(-1, 0)),
        // [+/-1, +/-1]
        check_dir(+1, +1).concat(check_dir(-1, -1)),
        // [+/-1, -/+1]
        check_dir(+1, -1).concat(check_dir(-1, +1)),
    ];

    results.forEach(function (result) {
        // console.log(result);
        if (result.length >= 4) {
            board_data[board_id].color_id = kEmptyColorId;
            for (var id of result) {
                board_data[id].color_id = kEmptyColorId;
                game_data.score++;
            }
        }
    });
}

var selected_board_id = -1;
function update_board_data(board_id) {
    if (selected_board_id === -1) {
        if (board_data[board_id].color_id !== kEmptyColorId) {
            selected_board_id = board_id;
        }
    }
    else {
        if (board_data[board_id].color_id === kEmptyColorId) {
            // move #selected_board_id -> empty slot #board_id
            board_data[board_id].color_id = board_data[selected_board_id].color_id;
            board_data[selected_board_id].color_id = kEmptyColorId;

            check_connected_chesses(board_id);            
            place_new_chesses();

            selected_board_id = -1;
        }
        else {
            // selecting #board_id as #selected_board_id
            selected_board_id = board_id;
{'fraction_non_alphanumeric': 0.13699165797705945, 'fraction_numerical': 0.01029718456725756, 'mean_word_length': 1.9353481254781943, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3757828', 'n_tokens_mistral': 2636, 'n_tokens_neox': 2553, 'n_words': 466}