If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Need help simplifying code.

can someone please help me simplify the following code and email me at bigbrother328@cox.net, I want it so that all the monsters and moves are defined before hand and then doing something like YourMonster = FireMonster (that would be if they picked fire type), same with moves, for example something like (YourMonster.move1 = Punch)

Sounds like you're too lazy to go through the code, make changes and simplify the source yourself. What makes you think anyone here would want to do it for free? If you would like to pay somebody you should go to a freelancing web-site or contact somebody directly.

Sounds like you're too lazy to go through the code, make changes and simplify the source yourself. What makes you think anyone here would want to do it for free? If you would like to pay somebody you should go to a freelancing web-site or contact somebody directly.

I posted on these forums for help, I did not say I would not do it myself (and i didn't ask you to do it for me), I am new to this language and still learning, I don't know how to simplify it, i just need someone to show me what i need to do in order to simplify the code, any help would be great, please don't criticize my thread.

from anywhere, you can then do handy things like "var moveTypes=Object.keys(cmLUT)" to grab an array of all move types, meaning you won't have to cut and paste squat to expand.

a reference is always going to run faster than a calculation, and each if and case is a calculation.
i handle unknown values using the Look-Up-Table object ( .Default) and default operators ( || ), which only execute if needed instead of each time the function executes.

the function now has no ifs or forks, so it's much faster to execute, and has been reduced to one line.

can you see how that's more efficient and flexible ?

EDIT:
as a bonus, you'll get to brag that you use OOP instead of procedural design in your game.

Last edited by rnd me; 03-02-2013 at 07:08 PM.

Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

by the way is there any way i could maybe create a function beforehand and make each monster a member of the function, if so can you please show me what to do, because if i can do this it will save me the trouble of coding the same monsters and moves, over and over again for each player, and drastically cut down code

for clarification on me coding the same thing over and over again and how doing this could save a ton of code

So basically, you place all of the properties shared across all monsters inside of the _default object. If you want to create a monster with different attributes, you can set them when invoking the constructor with 'new', passing the custom attributes to the 'custom' argument.

Dracula is a particularly scary monster, so we bumped the default scariness from 3, up to 5. Casper, on the other hand, is a very average monster. Casper's scariness in this example remains the default- which is 3. Both monsters retain a 'life' value of 10- because of the default.

This isn't very interesting yet. Monsters can take action. For sake of simplicity, I'll make an action that affects two monsters. I want every monster to have the ability to 'scare' other monsters.

So basically, what we would do is change the prototype of Monster- and it will affect all monsters.

Code:

Monster.prototype.scare = function (m) {
m.life -= this.scariness;
};

We could technically define this after we declare the monsters using 'new' and it will still affect them. But it's much more clear to put this directly underneith the Monster constructor- because it extends Monster's prototype (for clearity).

We take an arg 'm' which we assume to be a Monster instance. If we say:

Code:

dracula.scare(casper)

.. then we want casper's (m becomes casper) life to be negatively influenced by dracula's scare. 'this' belongs to the monster who decided to scare- that monster is clearly dracula because we said 'dracula.scare'. In my opinion, this is very clear and easy-to-read code. It's also got a little elegance, but not as much as you might find in JavaScript if you decided to go the functional route. Since you like C++, though, I think you'll like this pattern better than the functional alternatives.

Hopefully that helps you with classical OOP in JavaScript. Notice private variables are only possible with closure- that is, using 'var' to define variables in a function, so that the 'var' is only accessible in that function and child functions/methods.