-4

我正在做一个井字游戏。

我将如何更改以下代码,以便可以在完成之前宣布获胜者(每个正方形都填充)并且获胜者是第一个获得 3 个匹配值的人,而不是最后一个获得它的人?

function check_squares () {
    var values = new Array(); 
    var complete = true;

    $('.square').each(function () {     
        values.push( $(this).html() );  
        //if ($(this).html() == '') complete = false;
    });

    if (complete == true) { 
        var winner = false;

        if ( values[0] == values[1] && values[1] == values[2] ) winner = values[0];
        if ( values[3] == values[4] && values[4] == values[5] ) winner = values[3];
        if ( values[6] == values[7] && values[7] == values[8] ) winner = values[6];
        if ( values[0] == values[3] && values[3] == values[6] ) winner = values[0];
        if ( values[1] == values[4] && values[4] == values[7] ) winner = values[1];
        if ( values[2] == values[5] && values[5] == values[8] ) winner = values[2];
        if ( values[0] == values[4] && values[4] == values[8] ) winner = values[0];
        if ( values[2] == values[4] && values[4] == values[6] ) winner = values[2];

        if (winner) {           
            $('#output').html('Winner: ' + winner);
        } else {        
            $('#output').html('No Winner');
        }
    } else {    
        if (player_x == true) {     
            $('#output').html('x turn to move');
        } else {        
            $('#output').html('o turn to move');
        }       
    }
}
4

1 回答 1

0

你的功能几乎是正确的。主要是,您只需要在玩家每次更改单元格的值时调用它。

我已经稍微调整了你的函数,以允许它被多次调用。我已在我更改的部分中添加了注释,以使它们更易于查找。

// Move these two variables into the global scope so that they are not reset
// by each call to check_squares and so that our click function can access them.
var complete = false;
var winner = null;

function check_squares () {
    var values = new Array(); 

    $('.square').each(function () {     
        values.push( $(this).html() );  
        //if ($(this).html() == '') complete = false;
    });

    // Debug statement to see the order of the values.
    console.log(values);

    if (!complete) {
        if ( values[0] == values[1] && values[1] == values[2] ) winner = values[0];
        if ( values[3] == values[4] && values[4] == values[5] ) winner = values[3];
        if ( values[6] == values[7] && values[7] == values[8] ) winner = values[6];
        if ( values[0] == values[3] && values[3] == values[6] ) winner = values[0];
        if ( values[1] == values[4] && values[4] == values[7] ) winner = values[1];
        if ( values[2] == values[5] && values[5] == values[8] ) winner = values[2];
        if ( values[0] == values[4] && values[4] == values[8] ) winner = values[0];
        if ( values[2] == values[4] && values[4] == values[6] ) winner = values[2];

        if (winner) {           
            $('#output').html('Winner: ' + winner);
            // Indicate that there was a winner so no more input is allowed.
            complete = true;
        } else {        
            $('#output').html('No Winner');
        }
    } else {    
        if (player_x == true) {     
            $('#output').html('x turn to move');
        } else {        
            $('#output').html('o turn to move');
        }       
    }
}

// Add a click handler to each square that checks for win conditions after each
// X or 0 is added.
$(".square").click(function() {
  if (!complete)
  {
    // This obviously needs to be changed to determine which player is going.
    $(this).html("X");
    check_squares();
  }
});

我创建了一个简单的JSFiddle来查看一个工作示例。

于 2012-05-08T20:31:14.460 回答