2016-12-25 83 views
-2

對於deal()功能,每當您爲curtain1輸入1時,它都能正常工作,但是每次爲curtain2輸入2時,它只會說「您是否想再次播放,是或沒有?」但是,它不在代碼的curtain2部分。讓我們達成協議JavaScript代碼不起作用

<script type="text/javascript"> 
    alert("Welcome to the arcade filled with MANY games"); 
    var userName = prompt("What is your name").toUpperCase() 
    alert("Welcome " + userName) 
    var select = prompt("1.Start Game \n \n 2. Instructions \n \n select: ") 
    while (select != 1 && select != 2) 
     var select = prompt("1.Start Game \n \n 2. Instructions \n \n select: ") 
    if (select == 1) 
     start() 
    else if (select == 2) 
     instructions() 


    function menu() { 
     var select = prompt("1.Start Game \n \n 2. Instructions \n \n select: ") 
     while (select != 1 && select != 2) 
      var select = prompt("1.Start Game \n \n 2. Instructions \n \n select: ") 
     if (select == 1) 
      start() 
     else if (select == 2) 
      instructions() 
    } 

    function start() { 
     var option = prompt("1. Black Jack \n \n 2, MAKE A DEAL \n \n TO end type QUIT \n \n Select: ").toUpperCase() 
     while (option != 1 && option != 2) 
      var option = prompt("1. Black Jack \n \n 2, MAKE A DEAL \n \n TO end type QUIT \n \n Select: ").toUpperCase() 
     if (option == 1) 
      blackJack() 
     else if (option == 2) 
      deal(userName) 
     else if (option == "QUIT") 
      menu() 

    } 

    function blackJack() { 
     alert("Welcome to BlackJack") 
     var dealerCard = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "11"] 
     var dealer2 = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "11"] 
     var cards = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "11"] 
     var cardsTwo = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "11"] 
     var cardHit = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "11"] 
     var cardHit2 = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "11"] 
     var cardHit3 = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "11"] 
     var cardHit4 = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "11"] 
     var cardsThree = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "11"] 
     var cardsFour = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "11"] 
     var cardsFive = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "11"] 
     var ranCards = Math.floor(Math.random() * cards.length); 
     var ranTwo = Math.floor(Math.random() * cardsTwo.length); 
     var ranThree = Math.floor(Math.random() * cardsThree.length); 
     var ranFour = Math.floor(Math.random() * cardsFour.length); 
     var ranFive = Math.floor(Math.random() * cardsFive.length); 
     var ranHit = Math.floor(Math.random() * cardHit.length); 
     var ranHit2 = Math.floor(Math.random() * cardHit2.length); 
     var ranHit3 = Math.floor(Math.random() * cardHit3.length); 
     var ranHit4 = Math.floor(Math.random() * cardHit4.length); 
     var dealerRan = Math.floor(Math.random() * dealerCard.length) 
     var dealerRan2 = Math.floor(Math.random() * dealer2.length) 

     var add = (ranCards + ranTwo) 
     if (add > 21) 
      var gameOver = prompt("GAME OVERRRRRR!!! Do you want to play again? YES OR NO").toUpperCase() 
     if (gameOver == "YES") 
      blackJack() 
     else if (gameOver == "NO") 
      menu() 
     else if (add < 21) 
      firstPart(add) 


     function firstPart(add) { 
      var userCards = prompt(ranCards + " and " + ranTwo + " do you want to HIT OR STAY?").toUpperCase() 
      while (userCards != "HIT" && userCards != "STAY") 
       var userCards = prompt(ranCards + " and " + ranTwo + " do you want to HIT OR STAY?").toUpperCase() 
      if (userCards == "HIT") 
       var add2 = (add + ranHit) 
      if (add2 > 21) 
       var gameOver = prompt("Game OVERRRRRR!!! Do you want to play again? YES OR NO").toUpperCase() 
      if (gameOver == "YES") 
       blackJack() 
      else if (gameOver == "NO") 
       menu() 
      else if (add2 < 21) 
       secondPart(add2) 
      else if (userCards == "STAY") 
       var dealer = (dealerRan + dealerRan2) 
      alert("You finished with " + add + " the dealer finsihed with " + dealer) 
      if (add > dealer) 
       var gameOverWin = prompt("YOU WINNN!!! Do you want to play again? YES OR NO").toUpperCase() 
      if (gameOverWin == "YES") 
       blackJack() 
      else if (gameOverWin == "NO") 
       menu() 
      else if (add < dealer) 
       var gameOver = prompt("Game OVERRRRRR!!! Do you want to play again? YES OR NO").toUpperCase() 
      if (gameOver == "YES") 
       blackJack() 
      else if (gameOver == "NO") 
       menu() 

     } 

     function secondPart(add2) { 
      var userCards2 = prompt("You have " + add2 + " do you want to HIT OR STAY").toUpperCase() 
      while (userCards2 != "HIT" && userCards2 != "STAY") 
       var userCards2 = prompt("You have " + add2 + " do you want to HIT OR STAY").toUpperCase() 
      if (userCards2 == "HIT") 
       var add3 = (add2 + ranHit2) 
      if (add3 > 21) 
       var gameOver = prompt("Game OVERRRRRR!!! Do you want to play again? YES OR NO").toUpperCase() 
      if (gameOver == "YES") 
       blackJack() 
      else if (gameOver == "NO") 
       menu() 
      else if (add3 < 21) 
       thirdPart(add3) 

      else if (userCards2 == "STAY") 
       var dealer = (dealerRan + dealerRan2) 
      alert("You finished with " + add2 + " the dealer finsihed with " + dealer) 
      if (add > dealer) 
       var gameOverWin = prompt("YOU WINNN!!! Do you want to play again? YES OR NO").toUpperCase() 
      if (gameOverWin == "YES") 
       blackJack() 
      else if (gameOverWin == "NO") 
       menu() 
      else if (add < dealer) 
       var gameOver = prompt("Game OVERRRRRR!!! Do you want to play again? YES OR NO").toUpperCase() 
      if (gameOver == "YES") 
       blackJack() 
      else if (gameOver == "NO") 
       menu() 

     } 

     function thirdPart(add3) { 
      var userCards3 = prompt("You have " + add3 + " do you want to HIT OR STAY").toUpperCase() 
      while (userCards3 != "HIT" && userCards3 != "STAY") 
       var userCards3 = prompt("You have " + add3 + " do you want to HIT OR STAY").toUpperCase() 
      if (userCards3 == "HIT") 
       var add4 = (add3 + ranHit3) 
      if (add4 > 21) 
       var gameOver = prompt("Game OVERRRRRR!!! Do you want to play again? YES OR NO").toUpperCase() 
      if (gameOver == "YES") 
       blackJack() 
      else if (gameOver == "NO") 
       menu() 
      else if (add4 < 21) 
       fourthPart(add4) 

      else if (userCards3 == "STAY") 
       var dealer = (dealerRan + dealerRan2) 
      alert("You finished with " + add3 + " the dealer finsihed with " + dealer) 
      if (add > dealer) 
       var gameOverWin = prompt("YOU WINNN!!! Do you want to play again? YES OR NO").toUpperCase() 
      if (gameOverWin == "YES") 
       blackJack() 
      else if (gameOverWin == "NO") 
       menu() 
      else if (add < dealer) 
       var gameOver = prompt("Game OVERRRRRR!!! Do you want to play again? YES OR NO").toUpperCase() 
      if (gameOver == "YES") 
       blackJack() 
      else if (gameOver == "NO") 
       menu() 
     } 

     function fourthPart(add4) { 
      var userCards4 = prompt("You have " + add4 + " do you wan to HIT OR STAY").toUpperCase() 
      while (userCards4 != "HIT" && userCards4 != "STAY") 
       if (userCards4 == "HIT") 
        var add5 = (add4 + ranHit4) 
      if (add5 > 21) 
       var gameOver = prompt("Game OVERRRRRR!!! Do you want to play again? YES OR NO").toUpperCase() 
      if (gameOver == "YES") 
       blackJack() 
      else if (gameOver == "NO") 
       menu() 
      else if (add5 < 21) 
       fifthPart(add5) 
      else if (userCards4 == "STAY") 
       var dealer = (dealerRan + dealerRan2) 
      alert("You finished with " + add4 + " the dealer finsihed with " + dealer) 
      if (add > dealer) 
       var gameOverWin = prompt("YOU WINNN!!! Do you want to play again? YES OR NO").toUpperCase() 
      if (gameOverWin == "YES") 
       blackJack() 
      else if (gameOverWin == "NO") 
       menu() 
      else if (add < dealer) 
       var gameOver = prompt("Game OVERRRRRR!!! Do you want to play again? YES OR NO").toUpperCase() 
      if (gameOver == "YES") 
       blackJack() 
      else if (gameOver == "NO") 
       menu() 

     } 

     function fifthPart(add5) { 
      alert("YOU BEAT THE GAME!!!! YOU FINISHED WITH" + add5) 
      var gameOver = prompt("Game OVERRRRRR!!! Do you want to play again? YES OR NO").toUpperCase() 
      if (gameOver == "YES") 
       blackJack() 
      else if (gameOver == "NO") 
       menu() 
     } 

    } 

    function deal(userName) { 
     curtain1 = Array("MOTORCYLE", "ZONK", "ZONK", "COOKING TOOLS", "LA FITNESS 1 YEAR PASS", "COMPAC LAPTOP") 
     curtain2 = Array("ZONK", "ZONK", "BMW", "YEEZY'S", "IPHONE 7PLUS") 
     curtain3 = Array("FERRARI 458", "ZONK", "ZONK", "ZONK", "KFC LIFETIME MEMBERSHIP") 
     ran1 = Math.floor(Math.random() * curtain1.length) 
     ran2 = Math.floor(Math.random() * curtain2.length) 
     ran3 = Math.floor(Math.random() * curtain3.length) 
     alert("COME ON DOWN " + userName + " MAKE A DEAL WITH US! ") 
     var dealSelect = prompt("1. Curtain \n \n 2. BOX \n \n 3. Envolope \n \n Select: ") 
     if (dealSelect == 1) 
      var curtainSelect = prompt("Which curtain would you like? Curtain 1? Curtain 2? or Curtain 3?") 
     var won = curtain1[ran1] 
     if (curtainSelect == 1) 
      if (won == "ZONK") 
       alert("DUN DUN DUNHHH YOU GOT ZONKED") 
      else if (won != "ZONK") 
      alert("YOU JUST WON " + won + " CONGRATULATIONS") 
     var dealOver = prompt("DO YOU WANT TO PLAY AGAIN? YES OR NO").toUpperCase() 
     if (dealOver == "YES") 
      deal(userName) 
     else if (dealOver == "NO") 
      menu() 
     else if (curtainSelect == 2) 
      var wonOne = curtain2[ran2] 
     if (wonOne == "ZONK") 
      alert("DUN DUN DUNHHH YOU GOT ZONKED") 
     else if (wonOne != "ZONK") 
      alert("YOU JUST WON " + wonOne + " CONGRATULATIONS") 




    } 



    </script> 
+0

這似乎是一個錯誤 –

+0

你在'deal()'函數中的變量應該用'var'或'let'聲明。 – Pointy

+0

即使將「var」添加到所有變量中,它也會執行相同的操作。 – Michael

回答

0

使得SENS
把你的大括號,{ ... }多行,如果語句
,如果你不把它們,你的代碼將在if

else if (won != "ZONK") 
{ 
    alert("YOU JUST WON " + won + " CONGRATULATIONS") 
    // the rest of your code if won != "Zonk" 
後僅執行第一條指令

,因爲在這種情況下,無論curtainSelect的值是什麼,都會執行var dealOver = prompt("DO YOU WANT TO PLAY AGAIN? YES OR NO").toUpperCase(),因爲該指令不是if語句的一部分:if (curtainSelect == 1)

+0

很高興我能幫忙:) – Taki