首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >杀龙,守旧之道

杀龙,守旧之道
EN

Code Review用户
提问于 2017-05-10 22:41:06
回答 2查看 140关注 0票数 3

下面的程序是否以最有效的方式编写?我刚开始使用Java脚本,我只是尝试做一些基本的事情,这样我就可以在我学到更多东西的时候进行练习。

我写它的方式,这个程序确实工作(除了它一直想要在它结束后再次运行?)但还有什么更好的方法可以让我做到这一点吗?

代码语言:javascript
复制
var charName = prompt("Hello Lost Stranger, Tell us your name!");
alert("Welcome " + charName + " we have been waiting for someone who can challenge this dragon!");
alert("A rather small but still intimidating dragon appears; it does not look happy.");

var charHP = 3000;
var mobHP = 3000;
var userAttack = "What type of attack will you use?(Choices: Sword, Charge, Spell)";

var diceOne = Math.floor(Math.random() * 100) + 1;
var diceTwo = Math.floor(Math.random() * 100) + 1;
var diceThree = Math.floor(Math.random() * 100) + 1;


function report() {
    alert("You have " + charHP + " remaining and the dragon has " + mobHP + " remaining.");
}


function attack() {
    function report() {
        alert("You have " + charHP + " remaining and the dragon has " + mobHP + " remaining.");
    }
    var attackType = prompt(userAttack);
    function evaluateAttack() {
        if (attackType.toLowerCase() === "sword") {
            mobHP = mobHP - diceOne - diceTwo - diceThree;
            alert("You slash at the dragon three times drawing blood!");
        } else if (attackType.toLowerCase() === "charge") {
            mobHP = mobHP - (3 * diceOne);
            alert("You charge the dragon at full speed drawing blood!");
        } else if (attackType.toLowerCase() === "spell") {
            mobHP = mobHP - (4 * diceTwo);
            alert("You close your eyes and summon the fires of hell on the dragon");
        }
    }
    if (mobHP > 0) {
        report();
        evaluateAttack();
    } else {
        alert("The dragon is dead! You have won!");
    }
}

do {
    attack();
} while (mobHP > 0);
EN

回答 2

Code Review用户

发布于 2017-05-10 23:34:44

好的,下面的内容并不一定是提高代码的效率(从执行速度上说),只是几个快速的观察/指针。

  1. 应该避免重复代码,比如两个相同的report()函数。
  2. 您的if..else if..else if语句可能更适合作为开关()/case。在某些条件下,switch()可能比if/else-if/..(取决于javascript引擎等)我应该注意到,开发人员已经为“切换-如果-战争-如果战争已经有一段时间了。”https://stackoverflow.com/questions/2922948/javascript-switch-vs-if-else-if-else
  3. 你的骰子值看起来是固定的。它们不会改变)。也许这与预期的一样,但我认为缺少一个rollDice()函数。
  4. 闭包(嵌套函数,或“函数中的函数”,如果您愿意)可能是初学者的一个困难的概念.它可以引入范围可变的问题,这些问题很难理解/处理。免责声明:我并不是说闭包不好,它们是强大的--但为了您当前的目的,可能是过分了,仅此而已。
票数 6
EN

Code Review用户

发布于 2017-05-18 21:53:40

我建议RPG能够很好地实现面向对象的编程。

下面的初始代码会更长,但是如果你想进一步开发这个游戏,那么OO风格会更好,更灵活。我们能明白为什么接近尾声..。

我们将构建我们的PlayerCharacter(PC)的功能,以保持(或失去)各种攻击方式,并封装命中点等。

代码语言:javascript
复制
function pCharacter(heroName, charHP) {
    this.name = heroName;
    this.HP = charHP;

    // keep the attack options local to the PC: name, number of dice, dice size, attack text
    var attackMethods = [];
    attackMethods["sword"] = [2, 100, "You slash twice with your sword at the _monster_"]; 
    attackMethods["charge"] = [1, 200, "You charge the _monster_ with all your might"];
    attackMethods["spell"] = [3, 100, "You summon the fires of hell against the _monster_"];

    // so that we can add attack options later when our PC gets more powerful
    this.addAttackMethod = function(name, numDice, diceRange, desc) {
        attackMethods[name] = [numDice, diceRange, desc];
    };

    // to return the current attack choices to the alert boxes
    // we may add a new attack option later, or lose a weapon to a monster that can disarm us!!
    this.getAttackMethods = function() {
        var str = "";
        for (var prop in attackMethods) {
            if (attackMethods.hasOwnProperty(prop)) {
                str += ", " + prop;
            } 
        }
        str = str.substring(2, str.length);
        return str;
    };

    /*
    keeping the attack as a PC method which takes the monster as a parameter
    then we pass damage to the monster to process.
    We could also tweak easily so that certain attacks don't damage certain monsters so much
    if we decided to pass the attack option back to the sufferDamage method of the monster.
    */
    this.attack = function(attackMethod, monster) {
        var damage = 0;
        for (var i = 1; i <= attackMethods[attackMethod][0]; i++) {
            damage += Math.floor(Math.random() * attackMethods[attackMethod][1]) + 1;
        }
        monster.sufferDamage(damage); //pass damage for monster to process
        alert(attackMethods[attackMethod][2].replace("_monster_", monster.type));
    };
    this.sufferDamage = function(damage) {
        this.HP -= damage; // when you're ready to take damage from monsters!
    };
}

//this constructor allows us to set up new monsters easily
function monster(monsterType, mobHP) {
    this.type = monsterType;
    this.HP = mobHP;

    this.sufferDamage = function(damage) {
        this.HP -= damage;
    };
    this.attack = function(PC) {
    // fill out this function when you are ready to make the monsters attack the Hero
    }
}

var charName = prompt("Hello Lost Stranger, Tell us your name!");
var hero = new pCharacter(charName, 1000);
var dragon = new monster("baby dragon", 500);

alert("Welcome " + hero.name + " we have been waiting for someone who can challenge this dragon!");
alert("A rather small but still intimidating dragon appears; it does not look happy.");

function attack(hero, monsterInstance) {
    // make attackgeneric function to have a battle round between the hero and a monster
    let report = function() {
        alert("You have " + hero.HP + " remaining and the " + 
              monsterInstance.type + " has " + monsterInstance.HP + " remaining.");
    };

    var userAttack = "What type of attack will you use?(Choices: " +
                      hero.getAttackMethods() + ")"; //get current attack options
    var attackType = prompt(userAttack);

    hero.attack(attackType, monsterInstance);

    if (monsterInstance.HP > 0) {
        report();
    } else {
        alert("The " + monsterInstance.type + " is dead! You have won!");
    }
}

do {
    attack(hero, dragon);
} while (dragon.HP > 0);

你的程序在这里结束-下面你可以看到它是多么容易继续游戏与新的怪物和添加更强大的武器给英雄。新战斧现在出现在攻击选择列表中。它滚动6*骰子(150)来造成伤害!

代码语言:javascript
复制
hero.addAttackMethod("battleaxe", 6, 150, 
                     "You swing the enchanted Mighty BattleAxe, crashing it into the _monster_!");
alert("You gain the Mighty BattleAxe!");

alert("A massive intimidating dragon lord appears!! It does not look happy.");

// now see how easy to have the hero fight a new monster
var masterDragon = new monster("Dragon Lord", 3000);
do {
    attack(hero, masterDragon);
} while (masterDragon.HP > 0);

有更好的方法可以使用。您可以将攻击方法保留在“闭包”中,然后怪物可以检查所使用的攻击类型。我没干。

你可以让怪物反击,让PC也有防御反应!

这只是你如何安排事情的开始.

票数 0
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/163266

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档