0

我正在尝试学习 JavaScript,我正在尝试创建一个基本的比较游戏,我将卡号放在一个数组中,我随机调用其中 2 个,并且我想比较 2 个随机调用中的哪一个有更高的价值?请帮我。我已经设法编写程序来随机处理 2 张牌,我只需要有关如何比较抽出的 2 张牌的指导。

谢谢你。

{
// This is my attempt to create a basic game for a simplified version of ta siau
//where players choose a card, and compares it with dealer's card. Bigger card wins the round

var cards = [2,3,4,5,6,7,8,9,10,"jack","queen","king","ace"]; //available cards in the deck
confirm("start game now?");

var playerCard = cards[Math.floor(Math.random() * cards.length)]; //choose a random card for player

console.log(playerCard);

var dealerCard = cards[Math.floor(Math.random() * cards.length)]; //choose a random card for dealer

console.log(dealerCard);
}
4

4 回答 4

4

尝试这个:

if (cards.indexOf(playerCard) > cards.indexOf(dealerCard)) {
   alert("Player wins!");
}
else {
   alert("Dealer wins");
}
于 2013-01-09T15:40:20.700 回答
2

有什么问题?

var playerVal=Math.floor(Math.random() * cards.length);
var playerCard = cards[playerVal];

var dealerVal=Math.floor(Math.random() * cards.length);
var dealerCard = cards[dealerVal]; 

        if(playerVal>dealerVal){
        //player wins
    }

卡是隔离的。

于 2013-01-09T15:36:21.303 回答
0

你应该努力提高你的接受率,但你检查值与其他值的主要方法是if

if(playerCard === dealerCard){
    console.log('cards are the same');
} else {
    console.log('cards are different');
}

注意===. 在大多数编程语言中,您使用==. 在 JavaScript 中,你可以这样做,但奇怪的事情[] == false会评估为真。===需要严格遵守,所以[] === false确实是错误的。

于 2013-01-09T15:36:18.480 回答
0

您应该操作卡片索引

var playerCardIndex = Math.floor(Math.random() * cards.length)
    var playerCard = cards[playerCardIndex];

    var dealerCardIndex = Math.floor(Math.random() * cards.length)
    var dealerCard = cards[dealerCardIndex]

    if (playerCardIndex > dealerCardIndex) {...}
于 2013-01-09T15:40:31.543 回答