2017-08-02 9 views
0

結果は正確だと思いますが、ループはfalseのときに実行を続けます。私は、ループを強制的に閉じるために戦闘員の健康状態をゼロにリセットしますが、両方がゼロになるまで実行を続けます。私が戻ってコードを修正することをお勧めしますが、私は非常に混乱しています。ありがとう戦闘員の体力が偽であっても、whileループは停止してはいけませんか?

function Fighter(name, health, damagePerAttack) { 
     this.name = name; 
     this.health = health; 
     this.damagePerAttack = damagePerAttack; 
     this.toString = function() { return this.name; } 
} 




function declareWinner(fighter1, fighter2, firstAttacker) { 
    var result, 
     winner; 

    if (fighter1.name == firstAttacker) { 
    firstAttacker = fighter1; 
    battlev1(); 
    } else if (fighter2.name == firstAttacker) { 
    firstAttacker = fighter2; 
    battlev2(); 
    } else { 
    console.log(`${firstAttacker} isn't fighting right now!`) 
    return 
    } 

    function resetHealth(p) { 
    p.health = 0; 
    } 

    function resultsv1() { 
    if (firstAttacker.health <= 0) { 
     result = `${fighter2.name} attacks ${firstAttacker.name}; ${firstAttacker.name} now has ${firstAttacker.health} health and is dead. ${fighter2.name} wins`; 
     winner = fighter2.name; 
     console.log(winner); 
     resetHealth(fighter2); 
    } else if (fighter2.name <= 0) { 
     result = `${firstAttacker.name} attacks ${fighter2.name}; ${fighter2.name} now has ${fighter2.health} health and is dead. ${firstAttacker.name} wins`; 
     winner = firstAttacker.name; 
     console.log(winner); 
     resetHealth(firstAttacker); 
    } 
    } 

    function resultsv2(){ 
    if (firstAttacker.health <= 0) { 
     result = `${fighter1.name} attacks ${firstAttacker.name}; ${firstAttacker.name} now has ${firstAttacker.health} health and is dead. ${fighter1.name} wins`; 
     winner = fighter1.name; 
     console.log(winner); 
     resetHealth(fighter1); 
    } else if (fighter1.name <= 0) { 
     fighter1.health = 0; 
     result = `${firstAttacker.name} attacks ${fighter2.name}; ${fighter2.name} now has ${fighter2.health} health and is dead. ${firstAttacker.name} wins`; 
     winner = firstAttacker.name 
     console.log(winner); 
     resetHealth(firstAttacker); 
    } 
    } 

    function determineLifeStatusv1() { 
    resultsv1(); 
    } 

    function determineLifeStatusv2() { 
    resultsv2(); 
    } 

    function fightv1() { 
    determineLifeStatusv1(); 
    fighter2.health -= firstAttacker.damagePerAttack; 
    result = `${firstAttacker.name} attacks ${fighter2.name}; ${fighter2.name} now has ${fighter2.health} health.`; 
    console.log(result); 
    determineLifeStatusv1(); 
    firstAttacker.health -= fighter2.damagePerAttack; 
    result = `${fighter2.name} attacks ${firstAttacker.name}; ${firstAttacker.name} now has ${firstAttacker.health} health.`; 
    console.log(result); 
    determineLifeStatusv1(); 
    } 

    function fightv2() { 
    determineLifeStatusv2() 
    fighter1.health -= firstAttacker.damagePerAttack; 
    result = `${firstAttacker.name} attacks ${fighter1.name}; ${fighter1.name} now has ${fighter1.health} health.`; 
    console.log(result); 
    determineLifeStatusv2() 
    firstAttacker.health -= fighter1.damagePerAttack; 
    result = `${fighter1.name} attacks ${firstAttacker.name}; ${firstAttacker.name} now has ${firstAttacker.health} health.`; 
    console.log(result); 
    determineLifeStatusv2() 
    } 

    function battlev1() { 
    while (firstAttacker.health > 0 && fighter2.health > 0) { 
     fightv1(); 
    } 
    } 

    function battlev2() { 
    while (fighter1.health > 0 && firstAttacker.health > 0) { 
     firstAttacker.health = 0; 
     fightv2(); 
    } 
    } 

} 

declareWinner(new Fighter("Lew", 10, 2), new Fighter("Harry", 5, 4), "Harry"); 

答えて

0

問題はあなたの関数fightv2にあります。あなたは最初の戦闘機が死んでいる場合は、機能を終了する必要があります。

function fightv2() { 
    determineLifeStatusv2() 
    fighter1.health -= firstAttacker.damagePerAttack; 
    result = `${firstAttacker.name} attacks ${fighter1.name}; ${fighter1.name} now has ${fighter1.health} health.`; 
    console.log(result); 

    // you have to quit function if fighter1 is dead, like so 
    if(fighter1.health <= 0) return; 

    determineLifeStatusv2() 
    firstAttacker.health -= fighter1.damagePerAttack; 
    result = `${fighter1.name} attacks ${firstAttacker.name}; ${firstAttacker.name} now has ${firstAttacker.health} health.`; 
    console.log(result); 
    determineLifeStatusv2() 
} 
関連する問題