0

Connect4 - 游戏我对左上框的第一个筹码位置有疑问。这是放置芯片的代码:

$('canvas').click(function(){
    var clicked_id = $(this).attr('id');

    if (clicked_id>7&&$(this).hasClass('placeable')){
        var above_id = $(this).attr('id') - 7;
        var above = $('#' + above_id);
        above.attr('class','placeable');
        $(this).attr('class','');
        create_circle(clicked_id);
    } else if($(this).hasClass('placeable')){
        $(this).attr('class','');
        create_circle(clicked_id);
    }
    if (check_for_win(color, clicked_id)){
        congratulate(color);
    }
});

错误发生在 check_for_win() 函数内部,在这部分:

var left_squares = [1, 8, 15, 22, 29, 36];
var in_array = jQuery.inArray(square, left_squares);

if (in_array > -1) { //IT HAPPENS IN THIS IF
    if (check_horiz_win(color, square)) { //---------
        return true;//-----------
    }//--------
}//----
else {
    while (result = jQuery.inArray(square, left_squares) == -1) {
        square -= 1;
        if (result = jQuery.inArray(square, left_squares) !== -1) {
            if (check_horiz_win(color, square)) {
                return true;    
            }
        }
    }
}

check_horiz_win() 函数:

function check_horiz_win(color, square) {
    win_count = 0;
    for (i = square; i < square + 7; i += 1) {
        if (four_in_a_row_check()) {
            return true;
        }
    }
}

和four_in_a_row_check():

function four_in_a_row_check() {
    if ($('#' + i).hasClass(color)) {
        win_count += 1;
        if (win_count == 4) { return true; }
    } else {
        win_count = 0;
    }
}
4

1 回答 1

0

传递给 check_for_win() 函数的值是一个字符串,我只是简单地更改了:

var clicked_id = $(this).attr('id');

至:

var clicked_id = parseInt($(this).attr('id'));

在 .click() 函数中。

于 2013-05-26T17:07:43.793 回答