0

I'm attempting to create a Choose Your Own Adventure type of game, and I'm currently trying to write a 'battle' script. What I've got so far is:

var name = "Anon";
var health = 100;
var youAttack = [name + " hits the " + opp + " with his sword", name + " uses magic!", name + " is too scared to fight!"];
var youBattle = function() {
    var youBattle = youAttack[Math.floor(Math.random() * 3)];
    return youBattle;
};



var opp = "Orc";
var oppHealth = 100;
var oppAttack = ["The " + opp + " hits you with his hammer!", "The " + opp + " does nothing!", "The " + opp + " back hands you!"];
var oppBattle = function() {
    var oppBattle = oppAttack[Math.floor(Math.random() * 3)];
    return oppBattle;
};

oppBattle();

youBattle();

I've done it like this so the opponent and player names can easily be changed.

What I'm struggling to figure out is how I can add / remove health from both the opponent and the player depending what attack is used. Obviously no health would be removed if the opp / player does nothing.

Is there a way I can do this without a bunch of messy if / else statements?

I was hoping for something easy like name + " hits the " + opp + " with his sword" + health = health - 10; but obviously that didn't work.

Thanks in advance!

5
  • So you want to add or remove that value in certain condition? Commented Oct 2, 2014 at 23:18
  • Yes. Say the Orc hits you with a hammer, you lose 10 health. But if he back hands you, you only lose 5. How would I add that to my array, or with minimal coding? Commented Oct 2, 2014 at 23:20
  • The youAttack variable is not evaluated at run-time, it gets initialized with the constants as soon as you declare it. Build the string in runtime, or (dirty) out quotes all around it and use eval(). Commented Oct 2, 2014 at 23:21
  • Sorry @Jongware, I'm still pretty new to JavaScript and I can't say I really understand what you think I should do. Isn't eval() used for maths equations? I'm not sure how to incorporate that. Commented Oct 2, 2014 at 23:31
  • Basically, you have something like a = 5; b = a + 1; a = 10; and you expect b to change to 11. JavaScript does not work that way. eval is not limited to maths, it can "evaluate" (i.e. "run") any Javascript. Look it up somewhere in the documentation. Commented Oct 2, 2014 at 23:34

2 Answers 2

2

http://jsbin.com/qerud/3/edit

Hope this isn't too much code:

var Attack = function(hero,opp,damageReceived,damageGiven,message){ 

        this.message = message;
        this.damageGiven = damageGiven;
        this.damageReceived = damageReceived;
        this.opp = opp;
        this.hero = hero;
        this.attack = function(opp){
            this.hero.health -= damageReceived;
            this.opp.health -= damageGiven;
            return this.message;
        };

};

var Character = function(name,health){
    this.name = name;
    this.health = health;
};

hero = new Character('Anon',100);
orc = new Character('Orc',150);

attack1 = new Attack(hero,orc,5,0,"The " + orc.name + " back hands you!");
attack2 = new Attack(hero,orc,0,0,hero.name + " is too scared to fight!");
attack3 = new Attack(hero,orc,15,0,"The " + orc.name + " hits you with his hammer!");
attack4 = new Attack(hero,orc,0,25,hero.name + " uses magic!");

attacks = [attack1,attack2,attack3,attack4];

while(hero.health > 0 && orc.health > 0){
    console.log(attacks[Math.floor(Math.random() * 4)].attack());
    console.log('Hero Health: '+ hero.health);
    console.log('Orc Health: '+ orc.health);
}

if(hero.health  > 0 ){
    console.log(hero.name + ' won');
} else {
    console.log('The ' + orc.name + ' won');

}
Sign up to request clarification or add additional context in comments.

1 Comment

I can consolidate this even more by not throwing in the hero / enemy to the Constructor but for the concept it is better to have it in there.
1

I can tell you first hand that trying to write this type of code uses a lot of if/else and more statements, regardless of what language you're using. You can use an array to hold the values of your attack patterns:

var attackName = ["Punch", "Sword", "Magic"]
var attackDamage = [3, 5, 4]

function youAttack(ATK, PHit) {

    if(playerHit) {
        playerDamage = ATK + PHit;
        oppHealth = oppHealth - playerDamage;
        return oppHeath;
    } else {
        alert("You missed!");
    }
}

But, without seeing exactly what you're doing I cannot say how you should do your attacks and damages. I can only assume. You will need a system of evaluating attacks, misses, etc. that does use IF/ELSE Statements at least somewhere.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.