简体   繁体   English

如何随机化JS然后重复?

[英]How to randomize JS and then repeat?

I have my code to the point where it will randomize the deck to the point where it will never pull the same card.我的代码可以将牌组随机化到永远不会拉出同一张牌的程度。 Once a card has been drawn it will remove it from the array.抽出一张牌后,它会将其从阵列中移除。 The catch is, once all of the cards has been drawn it goes to "Null" and i'd rather it just re-randomizes the deck and repeats back to the start again.问题是,一旦所有的牌都被抽出来,它就会变成“空”,我宁愿它只是重新随机化牌组并再次重复开始。

Is there anyway of doing this?反正有这样做吗?

var cards = [
    'clubs-ace',
    'clubs-2',
    'clubs-3',
    'clubs-4',
    'clubs-5',
    'clubs-6',
    'clubs-7',
    'clubs-8',
    'clubs-9',
    'clubs-10',
    'clubs-jack',
    'clubs-queen',
    'clubs-king',
    'diamonds-ace',
    'diamonds-2',
    'diamonds-3',
    'diamonds-4',
    'diamonds-5',
    'diamonds-6',
    'diamonds-7',
    'diamonds-8',
    'diamonds-9',
    'diamonds-10',
    'diamonds-jack',
    'diamonds-queen',
    'diamonds-king',
    'hearts-ace',
    'hearts-2',
    'hearts-3',
    'hearts-4',
    'hearts-5',
    'hearts-6',
    'hearts-7',
    'hearts-8',
    'hearts-9',
    'hearts-10',
    'hearts-jack',
    'hearts-queen',
    'hearts-king',
    'spades-ace',
    'spades-2',
    'spades-3',
    'spades-4',
    'spades-5',
    'spades-6',
    'spades-7',
    'spades-8',
    'spades-9',
    'spades-10',
    'spades-jack',
    'spades-queen',
    'spades-king'
];
var kingsCup = 0;
var gameComplete = false;

function drawCard() {
    if (gameComplete == false) {
        var randomNumber = Math.floor(Math.random() * (cards.length - 1));
        cards = cards.filter((e, i) => i !== randomNumber);
        console.log(cards.length);
        console.log(cards[randomNumber]);
        document.getElementById("cards").src = 'assets/js/games/cards/' + cards[randomNumber] + '.png';

        switch(cards[randomNumber]) {
            case "clubs-ace":
                document.getElementById("task").innerHTML = "Waterfall!";
            break;
            case "clubs-2":
                document.getElementById("task").innerHTML = "Pick someone to drink."
            break;
            case "clubs-3":
                document.getElementById("task").innerHTML = "Take a drink.";
            break;
            case "clubs-4":
                document.getElementById("task").innerHTML = "Last to touch the floor drinks.";
            break;
            case "clubs-5":
                document.getElementById("task").innerHTML = "All guys drink.";
            break;
            case "clubs-6":
                document.getElementById("task").innerHTML = "All chicks drink.";
            break;
            case "clubs-7":
                document.getElementById("task").innerHTML = "Last to reach for the sky drinks.";
            break;
            case "clubs-8":
                document.getElementById("task").innerHTML = "Pick a mate that will drink anytime you drink.";
            break;
            case "clubs-9":
                document.getElementById("task").innerHTML = "Rhyme";
            break;
            case "clubs-10":
                document.getElementById("task").innerHTML = "Categories";
            break;
            case "clubs-jack":
                document.getElementById("task").innerHTML = "Everyone must drink.";
            break;
            case "clubs-queen":
                document.getElementById("task").innerHTML = "You're now the question master.";
            break;
            case "clubs-king":
                kingsCup = kingsCup + 1;
                if (kingsCup < 4) {
                    document.getElementById("task").innerHTML = "KING! Add a dash of your drink to the kings cup!";
                }
                else {
                    gameComplete = true;
                    document.getElementById("task").innerHTML = "GAME OVER! You must down the kings cup!";
                }
            break;
            case "diamonds-ace":
                document.getElementById("task").innerHTML = "Waterfall!";
            break;
            case "diamonds-2":
                document.getElementById("task").innerHTML = "Pick someone to drink."
            break;
            case "diamonds-3":
                document.getElementById("task").innerHTML = "Take a drink.";
            break;
            case "diamonds-4":
                document.getElementById("task").innerHTML = "Last to touch the floor drinks.";
            break;
            case "diamonds-5":
                document.getElementById("task").innerHTML = "All guys drink.";
            break;
            case "diamonds-6":
                document.getElementById("task").innerHTML = "All chicks drink.";
            break;
            case "diamonds-7":
                document.getElementById("task").innerHTML = "Last to reach for the sky drinks.";
            break;
            case "diamonds-8":
                document.getElementById("task").innerHTML = "Pick a mate that will drink anytime you drink.";
            break;
            case "diamonds-9":
                document.getElementById("task").innerHTML = "Rhyme";
            break;
            case "diamonds-10":
                document.getElementById("task").innerHTML = "Categories";
            break;
            case "diamonds-jack":
                document.getElementById("task").innerHTML = "Everyone must drink.";
            break;
            case "diamonds-queen":
                document.getElementById("task").innerHTML = "You're now the question master.";
            break;
            case "diamonds-king":
                kingsCup = kingsCup + 1;
                if (kingsCup < 4) {
                    document.getElementById("task").innerHTML = "KING! Add a dash of your drink to the kings cup!";
                }
                else {
                    gameComplete = true;
                    document.getElementById("task").innerHTML = "GAME OVER! You must down the kings cup!";
                }
            break;
            case "hearts-ace":
                document.getElementById("task").innerHTML = "Waterfall!";
            break;
            case "hearts-2":
                document.getElementById("task").innerHTML = "Pick someone to drink.";
            break;
            case "hearts-3":
                document.getElementById("task").innerHTML = "Take a drink.";
            break;
            case "hearts-4":
                document.getElementById("task").innerHTML = "Last to touch the floor drinks.";
            break;
            case "hearts-5":
                document.getElementById("task").innerHTML = "All guys drink.";
            break;
            case "hearts-6":
                document.getElementById("task").innerHTML = "All chicks drink.";
            break;
            case "hearts-7":
                document.getElementById("task").innerHTML = "Last to reach for the sky drinks.";
            break;
            case "hearts-8":
                document.getElementById("task").innerHTML = "Pick a mate that will drink anytime you drink.";
            break;
            case "hearts-9":
                document.getElementById("task").innerHTML = "Rhyme";
            break;
            case "hearts-10":
                document.getElementById("task").innerHTML = "Categories";
            break;
            case "hearts-jack":
                document.getElementById("task").innerHTML = "Everyone must drink.";
            break;
            case "hearts-queen":
                document.getElementById("task").innerHTML = "You're now the question master.";
            break;
            case "hearts-king":
                kingsCup = kingsCup + 1;
                if (kingsCup < 4) {
                    document.getElementById("task").innerHTML = "KING! Add a dash of your drink to the kings cup!";
                }
                else {
                    gameComplete = true;
                    document.getElementById("task").innerHTML = "GAME OVER! You must down the kings cup!";
                }
            break;
            case "spades-ace":
                document.getElementById("task").innerHTML = "Waterfall!";
            break;
            case "spades-2":
                document.getElementById("task").innerHTML = "Pick someone to drink.";
            break;
            case "spades-3":
                document.getElementById("task").innerHTML = "Take a drink.";
            break;
            case "spades-4":
                document.getElementById("task").innerHTML = "Last to touch the floor drinks.";
            break;
            case "spades-5":
                document.getElementById("task").innerHTML = "All guys drink.";
            break;
            case "spades-6":
                document.getElementById("task").innerHTML = "All chicks drink.";
            break;
            case "spades-7":
                document.getElementById("task").innerHTML = "Last to reach for the sky drinks.";
            break;
            case "spades-8":
                document.getElementById("task").innerHTML = "Pick a mate that will drink anytime you drink.";
            break;
            case "spades-9":
                document.getElementById("task").innerHTML = "Rhyme";
            break;
            case "spades-10":
                document.getElementById("task").innerHTML = "Categories";
            break;
            case "spades-jack":
                document.getElementById("task").innerHTML = "Everyone must drink.";
            break;
            case "spades-queen":
                document.getElementById("task").innerHTML = "You're now the question master.";
            break;
            case "spades-king":
                kingsCup = kingsCup + 1;
                if (kingsCup < 4) {
                    document.getElementById("task").innerHTML = "KING! Add a dash of your drink to the kings cup!";
                }
                else {
                    gameComplete = true;
                    document.getElementById("task").innerHTML = "GAME OVER! You must down the kings cup!";
                }
            break;
        }
        document.getElementById("kingCount").innerHTML = 4 - kingsCup + " Kings Remaining";
    }

}

I have the game ending when the final king is drawn, but I'd to keep it going without refreshing the page.当最终的国王被抽出时,我已经结束了游戏,但我希望在不刷新页面的情况下继续进行。

[Edit: Full source code added] [编辑:添加了完整的源代码]

At the beginning, lets create a new array under the cards array called currentDeck首先,让我们在cards数组下创建一个名为currentDeck的新数组

var currentDeck = [...cards];

Then in the drawCard() function lets replace the cards array with the currentDeck array we've just created.然后在drawCard() function 中让我们用我们刚刚创建的currentDeck数组替换cards数组。 This will allow us to remove cards from the deck without touching the list of all cards we store as cards .这将允许我们从卡组中移除卡片,而无需触及我们存储为cards的所有卡片列表。

When the game is over, you can add an else statement where gameComplete would be true which resets the currentDeck array to = the cards array just like we did before.当游戏结束时,您可以添加一个 else 语句,其中gameComplete为真,它将currentDeck数组重置为 = cards数组,就像我们之前所做的那样。

var currentDeck = [...cards];

cards should never be modified永远不要修改cards

Here's a solution that uses rando.js 's randoSequence function to simplify the random shuffling.这是一个使用rando.jsrandoSequence function 来简化随机洗牌的解决方案。 It also uses aternary operator and the javascript pop() method.它还使用三元运算符和 javascript pop()方法。 It'll shuffled the cards and store the shuffled stuff in shuffled .它将cards并将洗好的东西存储在shuffled中。 Then it'll remove one at a time from shuffled as it draws cards and refill the shuffled variable with a new shuffled version of the cards values when it runs out.然后它会在抽牌时一次从shuffled中删除一个,并在shuffled变量用完时用新的 shuffled 版本的cards值重新填充。

 var cards = ["a", "b", "c"], shuffled = []; var drawCard = () => (shuffled = shuffled.length? shuffled: randoSequence(cards)).pop().value; //Log the results to show it works... for(var i = 0; i < 10; i++) console.log(drawCard());
 <script src="https://randojs.com/1.0.0.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM