Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: (OT) Brawling Javascript

by tilly (Archbishop)
on Oct 11, 2004 at 23:31 UTC ( #398332=note: print w/replies, xml ) Need Help??

in reply to (OT) Brawling Javascript

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.

Replies are listed 'Best First'.
Re^2: (OT) Brawling Javascript
by mariol on Oct 12, 2004 at 00:14 UTC
    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.
        Thank you tilly, I will try what you have suggested and i will not be posting anymore in here since they are not pearl questions but I thank you for your help and I will try to use what you have given me to learn more on it.
      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.
        oh ok thank you.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://398332]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2018-06-18 12:15 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (109 votes). Check out past polls.