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:
- 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.
- resolve_attack figures out what happens when one person tries to hit another.
- hold_funeral remembers the departed.
- 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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.