Connect4 - Game我在左上角的盒子中有问题,第一个。 这是放置芯片的代码:
$('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;
}
}
答案 0 :(得分:0)
传递给check_for_win()函数的值是一个字符串,我只是简单地改变了:
var clicked_id = $(this).attr('id');
为:
var clicked_id = parseInt($(this).attr('id'));
在.click()函数中。