2016-04-09 15 views
0

私は中世のテーマのゲームでTeetotum(歴史的なバージョンに基づいています)というミニゲームを作成し、ブラウザを応答不能にするスクリプトを作成しました。私はこれが私のループのためであると仮定しています。部品がループから脱出できないのですが、何度も何度も自分のコードをチェックしています。ブラウザをクラッシュさせるためのループを使用したJavascriptゲーム

誰も私のブラウザがクラッシュする理由を教えていただけますか?そして、それでも問題を解決する方法やそれを動作させる方法を教えてもらえますか?

function minigame(game, previous) { 
var minigameDiv = document.getElementById('minigames'); 
show('minigames'); 
minigameDiv.innerHTML = "<br>"; 
    if (game == "teetotum") { 
    minigameDiv.innerHTML += "Teetotum is a game of chance, commonly played with 2 or more players. You spin the top, if it falls on 1, then you take 5 Shrill from the pot. If it falls on 2, no action is taken. If it falls on 3, you add 5 Shrill to the pot. Lastly, if it falls on 4, you win the whole pot.<br><br>"; 
    minigameDiv.innerHTML += "If you would like to play, set the pot below with the amount you want and how many players you want to play with. <br><br><br>" 
    minigameDiv.innerHTML += "<select id='potAmount'> <option selected='selected' value='10'>10 Shrill</option> <option value='25'>25 Shrill</option> <option value='50'>50 Shrill</option></select>Pot Amount<br>" 
    minigameDiv.innerHTML += "<select id='playerCount'> <option selected='selected' value='2'>2 Players</option> <option value='3'>3 Players</option> <option value='4'>4 Players</option></select>Player Count<br><br>"  
    minigameDiv.innerHTML += "<a class='button' onclick='javascript:Teetotum(\""+previous+"\");'>Confirm Settings & Begin</a> <br> <a class='button' onclick='javascript:show(\""+previous+"\");'>Go Back to the Tavern</a>"; 
} 
} 


function Teetotum(goBack) { 
playerCount = document.getElementById("playerCount").value; 
totalPlayer = 1; 
potAmount = document.getElementById("potAmount").value; 
var minigameDiv = document.getElementById('minigames'); 
    if (shrillAmount >= potAmount) { 
    shrillAmount = shrillAmount - potAmount; 
    checkMoney(); 
    for (; potAmount > 0;) { 
    minigameDiv.innerHTML = "You spin the top..."; 
    var randomNumber = Math.ceil(Math.random() * 4); 
     setTimeout(function() { 
     if (randomNumber == 1) { 
      potAmount = potAmount - 5; 
      shrillAmount = shrillAmount + 5; 
      minigameDiv.innerHTML = "It lands on a 1! You take 5 Shrill from the pot. The pot is "+potAmount+"." 
     } 
     else if (randomNumber == 2) { 
      minigameDiv.innerHTML = "It lands on a 2. Nothing happens. The pot is "+potAmount+"." 
     } 
     else if (randomNumber == 3 && shrillAmount >= 5) { 
      potAmount = potAmount + 5; 
      shrillAmount = shrillAmount - 5; 
      minigameDiv.innerHTML = "It lands on a 3! You lose 5 Shrill into the pot. The pot is "+potAmount+"." 
     } 
     else if (randomNumber == 4) { 
      shrillAmount = shrillAmount + potAmount; 
      potAmount = potAmount - potAmount; 
      minigameDiv.innerHTML = "It lands on a 4! You win the whole pot." 
     } 
     if (potAmount > 0) { 
      minigameDiv.innerHTML += "<br><br>The next player(s) will now take their turn." 
       setTimeout(function() { 
       for (playerCount = playerCount; playerCount > totalPlayer && potAmount > 0; playerCount--) { 
        minigameDiv.innerHTML = ('Player '+playerCount+' spins the top...'); 
        var randomAINumber = Math.ceil(Math.random() * 4); 
         setTimeout(function() { 
         if (randomAINumber == 1) { 
          potAmount = potAmount - 5; 
          minigameDiv.innerHTML = "It lands on a 1. Player "+playerCount+" takes 5 Shrill from the pot. The pot is "+potAmount+"." 
         } 
         else if (randomAINumber == 2) { 
          minigameDiv.innerHTML = "It lands on a 2. Nothing happens. The pot is "+potAmount+"." 
         } 
         else if (randomAINumber == 3) { 
          potAmount = potAmount + 5; 
          minigameDiv.innerHTML = "It lands on a 3. Player "+playerCount+" puts 5 Shrill into the pot. The pot is "+potAmount+"." 
         } 
         else if (randomAINumber == 4) { 
          shrillAmount = shrillAmount + potAmount; 
          potAmount = potAmount - potAmount; 
          minigameDiv.innerHTML = "It lands on a 4! Player "+playerCount+" wins the pot." 
         } 
         if (potAmount <= 0) { 
          minigameDiv.innerHTML += "<br><br>The game is now finished <br><br><br> <a class='button' onclick='javascript:show(\""+previous+"\");'>Go Back to the Tavern</a>" 
         } 
        }, 2000); 
       } 
     }, 3000); 
     } 
     if (potAmount <= 0) { 
      minigameDiv.innerHTML += "<br><br>The game is now finished <br><br><br> <a class='button' onclick='javascript:show(\""+previous+"\");'>Go Back to the Tavern</a>" 
     } 
     }, 3000); 
    } 
} 
else { 
    ui.log('You do not have enough Shrill to do that.'); 
} 
} 

お手数をおかけしていただき、ありがとうございます。 :D

+0

コンソールにエラーが表示されますか?アプリケーションをデバッグしようとしましたが、ブラウザにはデバッガが組み込まれています。 –

+0

コンソールにエラーはありません。私はブラウザのデバッガを使ってみようとしています – Timble

答えて

2

コードの問題は、実際にはポットが決して減少しないということです。あなたは複数のコードの中にそれを減らすすべてのコードを配置しましたが、ループが終了するまで実行されません。

あなたのロジックにsetTimeoutを使用する必要がある場合は、ここではループを記述する方法は次のとおりです。

var loopFunction = function() { 
     var randomNumber = Math.random() * 4; 
     if (randomNumber == 1) { 
     } 
     ... 
     if (potAmount > 0) { 
      setTimeout(loopFunction, 3000); 
     } 
} 

前回が終了した後に次の繰り返しにのみ起動します。この方法では。

+0

私は実際にこれについて考えていたので、あなたの答えはそれを確認した。ありがとう!私は今それを書き直す作業をします。 – Timble

1

potAmountがなくなるまでゲームを継続したい場合は、while()ループを試してみてください。

while(potAmount > 0){ 
    //your code here 
} 
+0

ありがとうございます、今すぐ使えるかどうかわかります – Timble

+0

while()ループやループを使用すると、ブラウザがフリーズしてクラッシュします。 (ゲームの設定を確認すると、タイムアウトは実行されません)。 – Timble

+1

OPが持つ 'for(; potAmount> 0;)'ループはwhileループに相当します。本当の問題は、potAmount変数が、setTimeoutに渡される関数内でのみ更新されるということです。これは、ループの後に*実行されない関数なので、ループは決して終了しません。 – nnnnnn

関連する問題