当var == 0时,如何显示一个警报?

时间:2016-05-18 17:27:46

标签: javascript function if-statement

我目前正在尝试制作一款简单的卡片对比游戏,但我遇到了一些问题。什么时候var playerLife或computerLife == 0我想显示一条警告信息并刷新新游戏的页面。如果向下滚动,您可以看到我正在尝试使用if语句执行此操作,但它似乎不起作用。我是新手,所以欢迎所有帮助和提示。此外,如果我此刻的代码可以更好,我应该喜欢听到它。谢谢你的时间。

var cards = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,
            1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8,8, 9, 9, 10, 10];
var shuffledCards = shuffle(cards);
var playerDeck = shuffledCards.slice(0,20);
var computerDeck = shuffledCards.slice(20);
var playerlife = 25;
var computerLife = 25;

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;
}

function getTopCard(deck) {
    return deck[0];
}

function dealCards() {
    var playerTopCard = getTopCard(playerDeck);
    var computerTopCard = getTopCard(computerDeck);
    var cardImage = "<img src='http://i1194.photobucket.com/albums/aa365/Sarah_Dunlap/card_back.png'/>";

    whoWon();
    console.log('--------------------');

    document.getElementById("card1").innerHTML = "<div class='cardNumber'>" + playerTopCard + '</div>' + cardImage;
    document.getElementById("card2").innerHTML = "<div class='cardNumber'>" + computerTopCard + '</div>' + cardImage;
    document.getElementById("scoreComputer").innerHTML = computerLife;
    document.getElementById("scorePlayer").innerHTML = playerlife;
}

function whoWon() {
    var playerTopCard = getTopCard(playerDeck);
    var computerTopCard = getTopCard(computerDeck);

    if (playerTopCard > computerTopCard) {
        console.log('Player Won! ' + playerTopCard + '/' + computerTopCard);
        computerLife = computerLife - (playerTopCard - computerTopCard);
        playerDeck.shift();
        computerDeck.shift();
        console.log('Player Life - ' + playerlife);
        console.log('Computer Life - ' + computerLife);
    } else if (playerTopCard < computerTopCard) {
        console.log('Computer Won! ' + playerTopCard + '/' + computerTopCard);
        playerlife = playerlife - (computerTopCard - playerTopCard);
        playerDeck.shift();
        computerDeck.shift();
        console.log('Player Life - ' + playerlife);
        console.log('Computer Life - ' + computerLife);
    } else {
        console.log('Tie! ' + playerTopCard + '/' + computerTopCard);
        playerDeck.shift();
        computerDeck.shift();
        console.log('Player Life - ' + playerlife);
        console.log('Computer Life - ' + computerLife);
    }
}

shuffle(cards);
console.log('playerDeck - ' + playerDeck);
console.log('computerDeck - ' + computerDeck);

document.getElementById("click").addEventListener("click", dealCards);

(function gameEnd () {
    if (playerlife <=0 || computerLife <=0) {
        alert('DONE');
    }
}());

1 个答案:

答案 0 :(得分:0)

你应该考虑改变你的代码:

// new function
function checkIsGameOver () {
    if (playerlife <=0 || computerLife <=0) {
        alert('DONE');
    }
}
function whoWon() {
    var playerTopCard = getTopCard(playerDeck);
    var computerTopCard = getTopCard(computerDeck);

    if (playerTopCard > computerTopCard) {
        console.log('Player Won! ' + playerTopCard + '/' + computerTopCard);
        computerLife = computerLife - (playerTopCard - computerTopCard);
        // added code here... 
        checkIsGameOver();
        playerDeck.shift();
        computerDeck.shift();
        console.log('Player Life - ' + playerlife);
        console.log('Computer Life - ' + computerLife);
    } else if (playerTopCard < computerTopCard) {
        console.log('Computer Won! ' + playerTopCard + '/' + computerTopCard);
        playerlife = playerlife - (computerTopCard - playerTopCard);
        // added code here...
        checkIsGameOver();
        playerDeck.shift();
        computerDeck.shift();
        console.log('Player Life - ' + playerlife);
        console.log('Computer Life - ' + computerLife);
    } else {
       console.log('Tie! ' + playerTopCard + '/' + computerTopCard);
       playerDeck.shift();
       computerDeck.shift();
       console.log('Player Life - ' + playerlife);
       console.log('Computer Life - ' + computerLife);
    }
}