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. |