第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Hangman 游戲圖像僅限 JavaScript

Hangman 游戲圖像僅限 JavaScript

浮云間 2022-12-29 10:31:55
每次玩家未能猜出字母時(shí),我該如何實(shí)現(xiàn)這些圖像?我應(yīng)該把它放在哪里?當(dāng)玩家猜不到1個(gè)字母時(shí),會(huì)顯示劊子手1,然后是劊子手2,然后是劊子手3等等?這是我的代碼。圖像在 console.log 中 /* hangman 1 console.log(" _|_\n|   |_____\n|         |\n|_________|"); hangman 2 console.log("   _____\n  |     |\n  |\n  |\n _|_\n|   |_____\n|         |\n|_________|\n"); hangman 3 console.log("   _____\n  |     |\n  |     o\n  |     | \n _|_    \ \n|   |_____\n|         |\n|_________|\n"); hangman 4 console.log("   _____\n  |     |\n  |     o\n  |    /|\\ \n _|_    \ \n|   |_____\n|         |\n|_________|\n"); hangman 5 console.log("   _____\n  |     |\n  |     o\n  |    /|\\ \n _|_   / \\ \n|   |_____\n|         |\n|_________|\n");*/// Show player their progress | .join returned answer as a stringwhil (remainingLetters > 0 && lives > 0) {    (answerArray.join(""));    guess = readline.question(name + "'s guess (Enter 9 for lifelines or 0 to pass): ");    guess = guess.toUpperCase();    //if guess is more than 1 letter or no letter, alert player to guess 1 letter only    if (guess.length !== 1) {      console.log("Please enter 1 letter only.");    }    //if valid guess    else {      if (guesses.includes(guess)) {        console.log("\nYou have already made this guess, please try another letter!\n");      } else {        guesses.push(guess);        correctGuess = 0;        for (var j = 0; j < Word.length; j++) {          if (Word[j] == guess) {            answerArray[j] = guess;            remainingLetters--;            correctGuess = 1;          }        }       
查看完整描述

2 回答

?
慕尼黑8549860

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超11個(gè)贊

我的建議是將您的各種圖形存儲(chǔ)在一個(gè)數(shù)組中,并存儲(chǔ)當(dāng)前圖形的索引 - 從零開始。


每次用戶得到錯(cuò)誤答案時(shí),您都會(huì)獲取console.log當(dāng)前索引,然后遞增索引:


 console.log(graphicsArray[graphicsIndex++]);

下面有一個(gè)演示,使用按鈕按下來(lái)模擬錯(cuò)誤答案。試試看。


var graphicsArray = [];

graphicsArray.push(" _|_\n|   |_____\n|         |\n|_________|");

graphicsArray.push("   _____\n  |     |\n  |\n  |\n _|_\n|   |_____\n|         |\n|_________|\n");

graphicsArray.push("   _____\n  |     |\n  |     o\n  |     | \n _|_    \ \n|   |_____\n|         |\n|_________|\n");

graphicsArray.push("   _____\n  |     |\n  |     o\n  |    /|\\ \n _|_    \ \n|   |_____\n|         |\n|_________|\n");

graphicsArray.push("   _____\n  |     |\n  |     o\n  |    /|\\ \n _|_   / \\ \n|   |_____\n|         |\n|_________|\n");



var graphicsIndex = 0;


document.querySelector("#demo").onclick = () => {

    console.log(graphicsArray[graphicsIndex++]);

}

<button id="demo">press me</button>


您將在減少生命數(shù)量的代碼部分執(zhí)行此操作。


// ... //

if (correctGuess == 1) {

   console.log("\nGood job! " + guess + " is one of the letters!\n");

   console.log(JSON.stringify(answerArray) + "\n");

   console.log(JSON.stringify(alphabets) + "\n");

} else {

   lives -= 1;

   console.log("\nSorry. " + guess + " is not a part of the word.\n");

   console.log(JSON.stringify(answerArray) + "\n");

   console.log(JSON.stringify(alphabets) + "\n");

   console.log("You have " + lives + " lives remaining.\n");

   console.log(graphicsArray[graphicsIndex++]);

}

// ... //


查看完整回答
反對(duì) 回復(fù) 2022-12-29
?
泛舟湖上清波郎朗

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超3個(gè)贊

使用發(fā)電機(jī)!

要更好地了解什么是生成器,您可以訪問(wèn)此處:https ://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*


/* This function generates your images */

function* hangmanGenerator() {

    yield console.log(" _|_\n|   |_____\n|         |\n|_________|");

    yield console.log("   _____\n  |     |\n  |\n  |\n _|_\n|   |_____\n|         |\n|_________|\n");

    yield console.log("   _____\n  |     |\n  |     o\n  |     | \n _|_    \ \n|   |_____\n|         |\n|_________|\n");

    yield console.log("   _____\n  |     |\n  |     o\n  |    /|\\ \n _|_    \ \n|   |_____\n|         |\n|_________|\n");

    yield console.log("   _____\n  |     |\n  |     o\n  |    /|\\ \n _|_   / \\ \n|   |_____\n|         |\n|_________|\n");

}


/* This is the istance of your image generator */

const generator = hangmanGenerator();


/* This is how you get the images out of your generator */

generator.next().value  /* Hangman 1*/

generator.next().value  /* Hangman 2*/

generator.next().value  /* Hangman 3*/

generator.next().value  /* Hangman 4*/

generator.next().value  /* Hangman 5*/

generator.next().value  /* No value because you're out of yields -> game over */


您的代碼應(yīng)如下所示:


//Your code...

//The function definition can go wherever you want in your code as long as it's before the while loop

function* hangmanGenerator() {

    yield console.log(" _|_\n|   |_____\n|         |\n|_________|");

    yield console.log("   _____\n  |     |\n  |\n  |\n _|_\n|   |_____\n|         |\n|_________|\n");

    yield console.log("   _____\n  |     |\n  |     o\n  |     | \n _|_    \ \n|   |_____\n|         |\n|_________|\n");

    yield console.log("   _____\n  |     |\n  |     o\n  |    /|\\ \n _|_    \ \n|   |_____\n|         |\n|_________|\n");

    yield console.log("   _____\n  |     |\n  |     o\n  |    /|\\ \n _|_   / \\ \n|   |_____\n|         |\n|_________|\n");

}

//As the generator definition you can istanciate the generator object wherever you need 

//as long as it's visible in the while loop

const generator = hangmanGenerator();


// Show player their progress | .join returned answer as a string

while (remainingLetters > 0 && lives > 0) {

    (answerArray.join(""));


    guess = readline.question(name + "'s guess (Enter 9 for lifelines or 0 to pass): ");

    guess = guess.toUpperCase();


    //if guess is more than 1 letter or no letter, alert player to guess 1 letter only

    if (guess.length !== 1) {

      console.log("Please enter 1 letter only.");

    }


    //if valid guess

    else {

      if (guesses.includes(guess)) {

        console.log("\nYou have already made this guess, please try another letter!\n");

      } else {

        guesses.push(guess);

        correctGuess = 0;

        for (var j = 0; j < Word.length; j++) {

          if (Word[j] == guess) {

            answerArray[j] = guess;

            remainingLetters--;

            correctGuess = 1;

          }

        }


        if (correctGuess == 1) {

          console.log("\nGood job! " + guess + " is one of the letters!\n");

          console.log(JSON.stringify(answerArray) + "\n");

          console.log(JSON.stringify(alphabets) + "\n");

        } else {

          lives -= 1;

          console.log("\nSorry. " + guess + " is not a part of the word.\n");

          console.log(JSON.stringify(answerArray) + "\n");

          console.log(JSON.stringify(alphabets) + "\n");

          console.log("You have " + lives + " lives remaining.\n");

          //HERE you show the hangman in the console

          generator.next().value;

        }

      }

    }


    if (remainingLetters == 0) {

      console.log("Congratulation! You managed to guess the word!\n");

      break;

    }

    

    if (lives == 0) {

      console.log("Game Over... You failed to guess the word. The word is " + Word + ".\n")

    }


  }


查看完整回答
反對(duì) 回復(fù) 2022-12-29
  • 2 回答
  • 0 關(guān)注
  • 115 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)