简体   繁体   中英

How do I re run the function in my Simon game?

I'm pretty confident that I have the basic logic behind my Simon game. However, I'm having issues in going to the next round. I have attempted to use an if statement and while loop but neither have worked. How should I proceed?

A brief explanation: gameColors is an array that has all the random colors that will be played out in an increasing number over several rounds. The variable sequencelength is used to increase the counter used in the user input function, whereas the variable sequenceLengthInv is the opposite of that, subtracted from gameColors.length to get sequenceLength.

random sequence displays the random sequence of colors up to the sequenceLength value.

JS:

if (sequenceLengthInv >= 0) {
   // have the game play the random sequence of colors
   for (var i = 0; i < gameColors.length - sequenceLengthInv; i++) {
       randomSequence();
   }


   //listens for the user's input
   for (var i = 0; i < color.length; i++) {
       userInput();
   }
sequenceLength++;
sequenceLengthInv--;
}

Try using for loops and using throw / reject & catch (or break ). Also, Promise s may be useful here.

async function playGame(availableColors, minColors, maxColors) {
    await wait(333);

    var colors = Array.from({ length: maxColors }, () => randomChoice(availableColors));

    try {
        for (var i = minColors; i <= maxColors; i++) {
            console.log(colors.slice(0, i));
            await playRound(colors.slice(0, i));
        }

        alert('You win!');
    } catch (e) {
        if(e !== undefined) {
            throw e;
        }

        alert('You lose!');
    }
}

My modified (and now commented) code is available here: https://repl.it/@solly_ucko/InconsequentialRoastedMonitor .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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