Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

(OT) Brawling Javascript

by mariol
on Oct 11, 2004 at 22:12 UTC ( #398314=offtopicroot: print w/replies, xml ) Need Help??

This posting has been designated as off-topic for the PerlMonks web site. Please do not respond to it.

Distinguished monks, I am trying to write down a program in javascript to make a brawling script for this: Max Number of Attacks: XP Level / 3 (Round Up) Hit Roll: Dexterity * 20 + Random (-100 to 100) Difficulty Factor: Targets Dodge * 20 Success: IF Hit Roll >= Difficulty Critical Hit (Damage * 2): IF Random = 100 Damage (Min Damage = 1): (Strength - 2) + Random (0 to 5) Would you please help, whenever i try I get stock.

Janitored by Arunbear - retitled from 'Brawling Javascript'

Replies are listed 'Best First'.
Re: (OT) Brawling Javascript
by tilly (Archbishop) on Oct 11, 2004 at 23:31 UTC
    What is this doing on a Perl site?

    This site is dedicated to learning, not getting other people to do your work. We'll help you learn, we won't generally write your own script for you.

    I'll make the rest of my reply based on the assumption that you want to learn.

    First of all the question is unclear. What exactly should a brawling script do? Go through a single hit? Or have 2 opponents duel to the death? (Possibly with random comments from, "XXX mercilessly attacks thin air." to "XXX delivers a crushing blow to the chest, doing 34 HP damage. A bloody smear marks YYY's passing.")

    Either way your problem is probably that you're trying to bite off too much at once. There are two common strategies to use in this case - top down or bottom up.

    For bottom up, you'd write a function that does something simple and necessary, then test it. For instance figures out what the results of a single blow are. Try running that function and see if it works. And another to update a character's stats after the blow. Try having one monster hit another one and see whether the other monster accumulates damage. Then continue to layer one task on top of another until you've got the whole thing. For instance next might be a function to track who strikes whom in what order. And so on.

    For top down you'd look at the overall problem and try to break it into pieces. For instance you might say that a brawl breaks into attacks. So first you need to write a control loop that figures out who attacks whom in what order. (Be sure that this loop can tell if someone is dead!) And then try to write code for how to handle a single attack.

    Both approaches work (and pros work them together). Either approach gives you a way to avoid getting stuck.

    Remember. Work in small simple pieces. Stop often to see that you're still on a track to succeed.

      Thank you, you are right I did want to learn i want to create it to loop but when i do it it gets stock. this is what i have writen so far. <script> var player_one; var player_two; var start; var dexterity=0; var level=2; var n_attacks; var hit_roll; var random; var difficulty; var dodge=2; var critical; var damage; var strength=2; var random_two; var random_one; var success; function focusout() { if (!start) { n_attacks=Math.round(level/3); random=Math.random()*100; document.write(Math.round(random_one=random)); hit_roll=Math.round(dexterity*20+random_one); difficulty=Math.round(dodge*20); r=Math.random()*5; random_two=Math.round(r); document.write("
      "); } } </script>
        I have successfully helped people learn to get through exactly the barrier that you have. Perhaps there is a better way, but the most successful approach that I found involved letting that person be stuck for a couple of weeks to make them learn for themselves that they cannot hold a complex problem in their head and tackle it all at once.

        Do you know how to create an object in JavaScript? You do it something like this. This will let you pass people around to functions without a big maze of variables to declare.

        var player_one = { dexterity: 0, strength: 1, level: 2, hp: 25 };
        Do you know how to write a function? I'm rusty on my JavaScript and didn't test, so there may be obvious errors, but here's one you might write from the top-down approach:
        function do_battle (player1, player2) { var attack_order = get_attack_order(player1, player2); var turn = 0; while ((player1.hp > 0) && (player2.hp > 0)) { turn++; document.write("turn " + turn + " begins!<br>"); for (attacker in attack_order) { if (1 == attacker) { resolve_attack(player1, player2); if (player2.hp < 1) break; } else { resolve_attack(player2, player1); if (player1.hp < 1) break; } } } hold_funeral(player1.hp < 1 ? player1 : player2); celebrate_victory(player1.hp < 1 ? player2 : player1); }
        This requires you to write the following additional attacksfunctions:
        1. get_attack_order decides what sequence of attacks happen in a turn. It gets 2 people and returns an array of 1's and 2's.
        2. resolve_attack figures out what happens when one person tries to hit another.
        3. hold_funeral remembers the departed.
        4. celebrate_victory for the winner.
        I'll let you write those functions yourself. If you get stuck, you're taking on too much at once. Break the problem up.
        You appear to be horribly confused. Do you see the name of this website? Perlmonks. Perl Monks. This means we do Perl here. Not javascript. Perhaps you can find some other website that does javascript.

Log In?

What's my password?
Create A New User
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2018-05-23 19:39 GMT
Find Nodes?
    Voting Booth?