2016-04-25 93 views
0

我想這樣做,當人命中循環100命中退出循環時,生命骰子滾動到0.它目前是如何循環100次。我不太清楚我需要如何解決這個問題,任何提示都會有所幫助。我的代碼如下。試圖讓循環退出,但它目前只是繼續循環100次

function addChar(fname, lname, speed, agility, wep, outfit, mood) { 
    this.fname = fname; 
    this.lname = lname; 
    this.speed = speed; 
    this.agility = agility; 
    this.wep = wep; 
    this.outfit = outfit; 
    this.mood = mood; 

    this.special = function(specialMove) { 
     specialMove(); 

    } 

    this.jumpKick = function() { 
     var jmpkckTimes = Math.floor(Math.random() * (100 - 33 + 1)) + 33; 

     document.write("He jumpkicks " + jmpkckTimes + " times. "); 
     if (jmpkckTimes > 70) { 
      document.write("He enters rage mode! "); 
     } else { 
      document.write("He does not have enough kicks for rage mode. "); 
     } 
    } 

    this.hits = function() { 
     var allHits = Math.floor(Math.random() * (100 - 33 + 1)) + 33; 
     document.write(" He gets attacked for " + allHits + " HP."); 
    } 
    this.lifes = function() { 
     var life = Math.floor(Math.random() * (3 - 0 + 1)) + 0; 
     if (life > 0) { 
      document.write(" The life dice rolls a " + life + ". You have survived! For now..."); 


     } else { 
      document.write(" The life dice rolls a " + life + ". You have died!"); 

     } 
    } 
} 
var myChar = new addChar('Johhny', 'Kicker', 10, 7, 'Ancient Greataxe', 'Barrows', 'angry'); 

document.write(myChar.fname + " " + myChar.lname + "'s speed is " + myChar.speed + "<br>"); 
document.write(myChar.fname + " " + myChar.lname + "'s agility is " + myChar.agility + "<br>"); 
document.write(myChar.fname + " " + myChar.lname + "'s weapon of choice is: " + myChar.wep + "<br>"); 
document.write(myChar.fname + " " + myChar.lname + " feels " + myChar.mood + "<br>"); 
document.write(myChar.fname + " " + myChar.lname + " attempts his special: "); 
myChar.special(myChar.jumpKick) 

for (i = 1; i < 101; i++) { 
    myChar.hits(myChar.allHits) 
    myChar.lifes(myChar.lifes) 
} 

function myOutfit() { 
    document.getElementById("demo").innerHTML = ("He is wearing " + myChar.outfit) 

} 
var start = Date.now(); 
var response = prompt("Do you think my character has what it takes?", ""); 
var end = Date.now(); 
var elapsed = (end - start)/1000; 
console.log("You took " + elapsed + " seconds" + " to type: " + response); 

回答

0

以及一般你可以擺脫福爾的藏漢迴路爲防止福爾循環的進一步執行,並繼續下一個迭代:

for (var i = 0; i < 10; i++) { 
    if (i == 4) continue; 
    if (i == 8) break; 
    console.log(i); 
} 

這將基本打印:0,1,2, 3,5,6,7
(你可以看到它那種跳過4)
(它也將在工作一段時間/ while循環)

所以你的情況,你可以檢查返回值你的一個f聯合是真或假,或做一些其他類型的條件檢查,以擺脫循環。

或類似搶布蘭德在他的回答如何寫道:

var maxTurns = 100; 

var turns = 0; 
while (myChar.isAlive && ++turns <= maxTurns) { 
    myChar.hits(); 
    myChar.lifes(); 
} 
console.log("character is: " + myChar.isAlive ? "alive" : "dead"); 
console.log("after: " + turns + " turns."); 
+0

我已經相應修改我的代碼,但我得到了相同的輸出。 –

2

你需要有一個溝通方式之外的對象,什麼對象內部發生的事情。例如,當某個函數發生某些事情時,例如lifes()或hits(),您應該爲對象上的變量賦值以保留狀態。這樣你可以從for循環訪問狀態。

例子:

this.isAlive = true; // starting condition 

this.lifes = function() { 
    var life = Math.floor(Math.random() * (3 - 0 + 1)) + 0; 
    this.isAlive = (life > 0); 
    if (this.alive) { 
     document.write('you survived'); 
    } else { 
     document.write('you died'); 
    } 

現在,在你的for循環,您可以訪問的IsAlive:

// loop until 100 attempts or you die, which ever comes first 
for (i = 1; i < 101 && myChar.isAlive; i++) { 
    myChar.hits(myChar.allHits) 
    myChar.lifes(myChar.lifes) 
} 
+0

我剛剛嘗試了您的更正,但他們似乎沒有更正解決方案。所以我需要進一步編輯? –

+1

@DrewStenger這是一個例子,而不是一個實現。您的問題是基於對象的當前狀態提前退出循環。這取決於你如何實現該邏輯。 –

+0

@DrewStenger我們不是奴才。此外,SO規則明確規定。 – GottZ