簡體   English   中英

jQuery游戲的生命檢測鄰居細胞

[英]jquery game of life detect neighbour cells

我目前正在研究一個小jQuery項目。 我想用javascript / jquery / html構建Conway的生活游戲。 但是我不知道如何檢測一個單元是否有活着的鄰居。 但是我知道我必須利用數組。

到目前為止,我想到的是:

$(document).ready(function () {

var $create_grid = $('#create_grid');
var $run = $('#run');
var $reset = $('#reset');
var $random = $('#random');
var $cells = {};
var $active_cells = {};

$create_grid.click(function () {

    var width = $("[name='width']").val();
    var height = $("[name='height']").val();
    var cellsize = $("[name='cellsize']").val();
    var $table = $('#game');

    if (width.length != 0 && height.length != 0 && cellsize.length != 0) {

        for (i = 1; i <= height; i++) {
            $('table').append('<tr id="' + i + '"></tr>');
        }

        for (i = 1; i <= width; i++) {
            $('table tr').append('<td class="test" id="' + i + '"></td>');
        }
        $cells = $('table#game td');

        $cells.css('width', cellsize);
        $cells.css('height', cellsize);

    } else { alert("Please fill out all the fields!"); }

    $create_grid.hide('fast');
    $('ul.parameters').hide('fast');
    $random.css('display', 'block');
    $reset.css('display', 'block');

    //RESET CELLS
    $reset.click(function () {
        $cells.removeClass('alive');
    });

    //DRAW CELLS
    var isDown = false;

    $cells.mousedown(function () {
        isDown = true;
    })
        .mouseup(function () {
            isDown = false;
        });

    $cells.mouseover(function () {
        if (isDown) {
            $(this).toggleClass('alive');
        }
    });

    $cells.click(function () {
        $(this).toggleClass('alive');
    });
});

//RANDOM PATTERN
function shuffle(array) {
    var m = array.length, t, i;

    // While there remain elements to shuffle…
    while (m) {

        // Pick a remaining element…
        i = Math.floor(Math.random() * m--);

        // And swap it with the current element.
        t = array[m];
        array[m] = array[i];
        array[i] = t;
    }
    return array;
}

$random.click(function () {
    $(shuffle($cells).slice(0, 30)).addClass("alive");
});


//RUN SIMULATION
$run.click(function simulate() {


    //GET NEIGHBOUR CELLS
    $cells_alive = $('#game td.alive').length;


    for (var c = 1; c <= $cells_alive; c++) {

            alert(c);


        };

});

});

您分配的ID並非唯一。 所有行和所有列的ID分別來自1..n1..m 因此, 1..min(n,m)每個數字都會使用兩次。 您應該更改此設置。

您可能還希望分配一些類或數據屬性,或僅分配任何可能實際上選擇您創建的任何html元素的東西。

例如,這為所有trtd標簽設置了一些數據屬性。

    for (i = 1; i <= height; i++) {
        var elem = $('<tr></tr>');
        elem.data('column', i);
        $('table').append(elem);
    }

    for (i = 1; i <= width; i++) {
        var elem = $('<td></td>');
        elem.data('row', i);
        $('table tr').append();
    }
    $cells = $('table#game td');

    $cells.css('width', cellsize);
    $cells.css('height', cellsize);

如果您有坐標(x, y) ,則可以選擇所有鄰居,例如

$('tr[data-column='+(x-1)+'] td[data-row='+y+'], 
   tr[data-column='+(x+1)+'] td[data-row='+y+'], 
   tr[data-column='+x+'] td[data-row='+(y-1)+'], 
   tr[data-column='+x+'] td[data-row='+(y+1)+']');

(出於效率考慮,您可能要考慮使用class 。盡管我不知道這樣做是否有顯着差異。)

編輯:

這是關於data-選擇器和class選擇器性能的問題: 數據屬性css選擇器是否比類選擇器更快?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM