手机游戏获胜消息问题 - 警报框

时间:2012-12-12 19:10:52

标签: javascript cordova mobile tic-tac-toe

我为大学项目制作了一个简单的TicTacToe游戏,它使用HTML和JavaScript。 在桌面上查看时工作正常,但在移动设备上存在问题。 在移动设备上,最终获胜的作品在您获胜时不会显示,但其他所有内容都像警报框一样。

您可以在此处试用 - http://stuffmadeby.me/test/tictactoe/ - 如果在移动智能手机上查看此页面。

有什么方法可以解决这个问题吗?

奇怪的是它如何在桌面上运行,而不是在移动设备上,因为我想使用PhoneGap将其打包成一个简单的应用程序。

    $(document).ready(function(e) {
    $('#messages').hide();
    var currentPlayer=1;
    square=new Array(3)
    square[0]=new Array(3);
    square[1]=new Array(3);
    square[2]=new Array(3);

    var Ximg = $('<img />').attr('src','images/x.png');
    var Oimg = $('<img />').attr('src','images/o.png');

    samego=false;
    endgame=false;

    $('.square').click(function() {
        samego=false;
        input=$(this).attr('id');
        arrayindex=input.split("");
        x=arrayindex[0];
        y=arrayindex[1];
            if(square[x][y]=='X'||square[x][y]=='O') {
                $('#messages').html("That square has already been filled! try again.");
                samego=true;
            } else {
                currentPlayer==1 ? square[x][y]='X' : square[x][y]='O';
                currentPlayer==1 ? $('#'+input).html('<img src="images/x.png"/>') :
                $('#'+input).html('<img src="images/o.png"/>');
            }
            checkStatus();
    }); /* ends .square click function */

    function checkStatus() {
        if(
            square[0][0]=='X'&&square[0][1]=='X'&&square[0][2]=='X'||
            square[0][0]=='X'&&square[1][1]=='X'&&square[2][2]=='X'||
            square[1][0]=='X'&&square[1][1]=='X'&&square[1][2]=='X'||
            square[2][0]=='X'&&square[2][1]=='X'&&square[2][2]=='X'||
            square[0][1]=='X'&&square[1][1]=='X'&&square[2][1]=='X'||
            square[2][0]=='X'&&square[1][1]=='X'&&square[0][2]=='X'||
            square[0][2]=='X'&&square[1][2]=='X'&&square[2][2]=='X'||
            square[0][0]=='X'&&square[1][0]=='X'&&square[2][0]=='X'
    )
    {
        $('#messages').show().html("Player 1 wins!");
        alert("Player 1 wins! Click to reset!");
        reinitialise();
        endgame=true;
    }
        if(
            square[0][0]=='O'&&square[0][1]=='O'&&square[0][2]=='O'||
            square[0][0]=='O'&&square[1][1]=='O'&&square[2][2]=='O'||
            square[1][0]=='O'&&square[1][1]=='O'&&square[1][2]=='O'||
            square[2][0]=='O'&&square[2][1]=='O'&&square[2][2]=='O'||
            square[0][1]=='O'&&square[1][1]=='O'&&square[2][1]=='O'||
            square[2][0]=='O'&&square[1][1]=='O'&&square[0][2]=='O'||
            square[0][2]=='O'&&square[1][2]=='O'&&square[2][2]=='O'||
            square[0][0]=='O'&&square[1][0]=='O'&&square[2][0]=='O'
    )
    {
        $('#messages').html("Player 2 wins!");
        alert("Player 2 wins! Click to reset!");
        reinitialise();
        endgame=true;
    }

    var filled=0;
    for(x=0;x<3;x++) {
        for(y=0;y<3;y++) {
            if(square[x][y]=='X'||square[x][y]=='O') {
                filled++;
            }
        }
    } /* ends var filled */

    if(!samego&&!endgame) {
        currentPlayer==1 ? currentPlayer=2 : currentPlayer=1;
    } /* ends if */

    if(filled==9) {
        $('#messages').html("It's a draw!");
        alert("It's a draw! Click to reset!");
        reinitialise();
    } /* ends filled==9 */

    endgame=false;
    $('#player').html('Current Player = Player '+currentPlayer);

    } /* ends function check status */

    function reinitialise() {
        $('#messages').hide();
        $('#messages').html('');    
        square[0][0]=''; $('#00').html('');
        square[0][1]=''; $('#01').html('');
        square[0][2]=''; $('#02').html('');
        square[1][0]=''; $('#10').html('');
        square[1][1]=''; $('#11').html('');
        square[1][2]=''; $('#12').html('');
        square[2][0]=''; $('#20').html('');
        square[2][1]=''; $('#21').html('');
        square[2][2]=''; $('#22').html('');
        currentPlayer=1;
    } /* ends function reintialise */

}); /* ends whole function */

0 个答案:

没有答案