www.webdeveloper.com
Results 1 to 10 of 10

Thread: Need help simplifying code.

  1. #1
    Join Date
    Mar 2013
    Posts
    8

    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)

    Code:
    /************ MONSTER CLASS ************/
    var Monster = new Object();
    Monster.MonsterName="Demo";
    Monster.Type="";
    Monster.Advantage="";
    Monster.Disadvantage="";
    Monster.Atk=0;
    Monster.Def=0;
    Monster.HP=0;
    Monster.SpAtk=0;
    Monster.SpDef=0;
    Monster.Speed=0;
    Monster.Level=0;
    Monster.Experience=0;
    Monster.Move=[{
                "MoveName":"",
                 "MoveType":"",
                 "MoveCategory":"",
                 "MovePower":0,
                 "MoveAccuracy":0,
                 "MovePP":0
                }];
    
    
    /**************** TO CONVERT JSON OBJECT TO STRING ***********/
    Monster.toJSON = function(key)
     {
        var replacement = new Object();
        for (var val in this)
        {
          replacement[val] = this[val]
            /*if (typeof (this[val]) === 'string')
                replacement[val] = this[val].toUpperCase();
            else
                replacement[val] = this[val]*/
        }
        return replacement;
    };
    
    /************* FOR COPYING OBJECTS *****************/
    function shallowCopy(obj) {
        var result = {};
        for (var i in obj) {
            result[i] = obj[i];
        }
        return result;
    }
    
    /************* FOR MONSTERS INTIALIZATION *****************/
    var IntializeMonsters = function(YourMonsterType,YourMonster,OppMonster){   
       IntializeYourMonster(YourMonsterType,YourMonster);
       IntializeOppMonster(OppMonster);
    }
    var   IntializeYourMonster = function(YourMonsterType,YourMonster){
       switch(YourMonsterType)
       {
          case 1:  //FIRE
          {
             YourMonster['Type'] = "Fire";
             YourMonster['Advantage'] = "Nature";
             YourMonster['Disadvantage'] = "Water";
             YourMonster['MonsterName'] = "FireType";
             YourMonster['Atk'] = 63;
             YourMonster['Def'] = 45;
             YourMonster['SpAtk'] = 45;
             YourMonster['SpDef'] = 45;
             YourMonster['HP'] = 65;
             YourMonster['Speed'] = 45;
             YourMonster['Level'] = 5;
             YourMonster['Experience'] = 1000;      
          }
          break;
          case 2: //WATER
          {
             YourMonster['Type'] = "Water";
             YourMonster['Advantage'] = "Fire";
             YourMonster['Disadvantage'] = "Nature";
             YourMonster['MonsterName'] = "WaterType";
             YourMonster['Atk'] = 55;
             YourMonster['Def'] = 45;
             YourMonster['SpAtk'] = 63;
             YourMonster['SpDef'] = 45;
             YourMonster['HP'] = 55;
             YourMonster['Speed'] = 45;
             YourMonster['Level'] = 5;
             YourMonster['Experience'] = 1000;      
          }      
          break;
          case 3: //NATURE
          {
             YourMonster['Type'] = "Nature";
             YourMonster['Advantage'] = "Water";
             YourMonster['Disadvantage'] = "Fire";
             YourMonster['MonsterName'] = "NatureType";
             YourMonster['Atk'] = 45;
             YourMonster['Def'] = 55;
             YourMonster['SpAtk'] = 45;
             YourMonster['SpDef'] = 55;
             YourMonster['HP'] = 45;
             YourMonster['Speed'] = 63;
             YourMonster['Level'] = 5;
             YourMonster['Experience'] = 1000;         
          }
          break;      
       }
    };
       
    var   IntializeOppMonster = function(OppMonster){   
       var urMonsterOptn =   Math.floor((Math.random()*3)+1);
       switch(urMonsterOptn)
       {
          case 1:  //FIRE
          {
             OppMonster['Type'] = "Fire";
             OppMonster['Advantage'] = "Nature";
             OppMonster['Disadvantage'] = "Water";
             OppMonster['MonsterName'] = "FireType";
             OppMonster['Atk'] = 63;
             OppMonster['Def'] = 45;
             OppMonster['SpAtk'] = 45;
             OppMonster['SpDef'] = 45;
             OppMonster['HP'] = 65;
             OppMonster['Speed'] = 45;
             OppMonster['Level'] = 5;
             OppMonster['Experience'] = 1000;         
          }   
          break;
          case 2: //WATER
                {
             OppMonster['Type'] = "Water";
             OppMonster['Advantage'] = "Fire";
             OppMonster['Disadvantage'] = "Nature";
             OppMonster['MonsterName'] = "WaterType";
             OppMonster['Atk'] = 55;
             OppMonster['Def'] = 45;
             OppMonster['SpAtk'] = 63;
             OppMonster['SpDef'] = 45;
             OppMonster['HP'] = 55;
             OppMonster['Speed'] = 45;
             OppMonster['Level'] = 5;
             OppMonster['Experience'] = 1000;         
          }
          break;
          case 3: //NATURE
                {
             OppMonster['Type'] = "Nature";
             OppMonster['Advantage'] = "Water";
             OppMonster['Disadvantage'] = "Fire";
             OppMonster['MonsterName'] = "NatureType";
             OppMonster['Atk'] = 45;
             OppMonster['Def'] = 55;
             OppMonster['SpAtk'] = 45;
             OppMonster['SpDef'] = 55;
             OppMonster['HP'] = 45;
             OppMonster['Speed'] = 63;
             OppMonster['Level'] = 5;
             OppMonster['Experience'] = 1000;   
          }
          break;      
       }
    };
    
    /********************* FOR UPDATING MOVE **************************/
    var UpdateMove = function(YourMoveType,YourMonster,OppMonster){   
       UpdateYourMove(YourMoveType,YourMonster);
       UpdateOppMove(OppMonster);   
       var debug;
       debug=0;
    };
    
    var UpdateYourMove=function(move,YourMonster)
    {   
       if(move == "Punch")
       {      
          YourMonster['Move'][0]['MoveName']="Punch";
          YourMonster['Move'][0]['MoveType']="Normal";
          YourMonster['Move'][0]['MoveCategory']="Physical";
          YourMonster['Move'][0]['MovePower']=20;
          YourMonster['Move'][0]['MoveAccuracy']=100;
          YourMonster['Move'][0]['MovePP']=10;
       }
       else if(move == "Kick")
       {
          YourMonster['Move'][0]['MoveName']="Kick";
          YourMonster['Move'][0]['MoveType']="Normal";
          YourMonster['Move'][0]['MoveCategory']="Physical";
          YourMonster['Move'][0]['MovePower']=25;
          YourMonster['Move'][0]['MoveAccuracy']=100;
          YourMonster['Move'][0]['MovePP']=10;
       }
       else if(move == "Tackle")
       {   
          YourMonster['Move'][0]['MoveName']="Tackle";
          YourMonster['Move'][0]['MoveType']="Normal";
          YourMonster['Move'][0]['MoveCategory']="Physical";
          YourMonster['Move'][0]['MovePower']=30;
          YourMonster['Move'][0]['MoveAccuracy']=100;
          YourMonster['Move'][0]['MovePP']=10;
       }
       else if(move == "FirePunch")
       {
          YourMonster['Move'][0]['MoveName']="FirePunch";
          YourMonster['Move'][0]['MoveType']="Fire";
          YourMonster['Move'][0]['MoveCategory']="Physical";
          YourMonster['Move'][0]['MovePower']=20;
          YourMonster['Move'][0]['MoveAccuracy']=100;
          YourMonster['Move'][0]['MovePP']=10;
       }
       else if(move == "WaterPunch")
       {
          YourMonster['Move'][0]['MoveName']="WaterPunch";
          YourMonster['Move'][0]['MoveType']="Water";
          YourMonster['Move'][0]['MoveCategory']="Physical";
          YourMonster['Move'][0]['MovePower']=20;
          YourMonster['Move'][0]['MoveAccuracy']=100;
          YourMonster['Move'][0]['MovePP']=10;
       }
       else if(move == "NaturePunch")
       {
          YourMonster['Move'][0]['MoveName']="NaturePunch";
          YourMonster['Move'][0]['MoveType']="Nature";
          YourMonster['Move'][0]['MoveCategory']="Physical";
          YourMonster['Move'][0]['MovePower']=20;
          YourMonster['Move'][0]['MoveAccuracy']=100;
          YourMonster['Move'][0]['MovePP']=10;
       }
    };
    
    var UpdateOppMove=function(OppMonster)
    {
       var oppMovieChoice =   Math.floor((Math.random()*6)+1);   
       switch(oppMovieChoice)
       {
       case 1:
          {
             OppMonster['Move'][0]['MoveName']="Punch";
             OppMonster['Move'][0]['MoveType']="Normal";
             OppMonster['Move'][0]['MoveCategory']="Physical";
             OppMonster['Move'][0]['MovePower']=20;
             OppMonster['Move'][0]['MoveAccuracy']=100;
             OppMonster['Move'][0]['MovePP']=10;         
          } break;
       case 2:
          {
             OppMonster['Move'][0]['MoveName']="Kick";
             OppMonster['Move'][0]['MoveType']="Normal";
             OppMonster['Move'][0]['MoveCategory']="Physical";
             OppMonster['Move'][0]['MovePower']=25;
             OppMonster['Move'][0]['MoveAccuracy']=100;
             OppMonster['Move'][0]['MovePP']=10;         
          }break;
       case 3:
          {   
             OppMonster['Move'][0]['MoveName']="Tackle";
             OppMonster['Move'][0]['MoveType']="Normal";
             OppMonster['Move'][0]['MoveCategory']="Physical";
             OppMonster['Move'][0]['MovePower']=30;
             OppMonster['Move'][0]['MoveAccuracy']=100;
             OppMonster['Move'][0]['MovePP']=10;         
          }break;
       case 4:
          {
             OppMonster['Move'][0]['MoveName']="FirePunch";
             OppMonster['Move'][0]['MoveType']="Fire";
             OppMonster['Move'][0]['MoveCategory']="Physical";
             OppMonster['Move'][0]['MovePower']=20;
             OppMonster['Move'][0]['MoveAccuracy']=100;
             OppMonster['Move'][0]['MovePP']=10;         
          }break;
       case 5:
          {
             OppMonster['Move'][0]['MoveName']="WaterPunch";
             OppMonster['Move'][0]['MoveType']="Water";
             OppMonster['Move'][0]['MoveCategory']="Physical";
             OppMonster['Move'][0]['MovePower']=20;
             OppMonster['Move'][0]['MoveAccuracy']=100;
             OppMonster['Move'][0]['MovePP']=10;
             
          }break;
       case 6:
          {         
             OppMonster['Move'][0]['MoveName']="NaturePunch";
             OppMonster['Move'][0]['MoveType']="Nature";
             OppMonster['Move'][0]['MoveCategory']="Physical";
             OppMonster['Move'][0]['MovePower']=20;
             OppMonster['Move'][0]['MoveAccuracy']=100;
             OppMonster['Move'][0]['MovePP']=10;         
          }break;
       }   
    };

  2. #2
    Join Date
    Mar 2013
    Posts
    8
    Part 2 as i could not fit
    Code:
    /************* FOR GETTING MONSTER RELATED INFORMATION *************/
    var GetAttack = function(monster)
    {
       return monster['Atk'];   
    };
    var GetDefense = function(monster)
    {
       return monster['Def'];   
    };
    var GetType = function(monster)
    {
       return monster['Type'];   
    };
    var GetSpAttack = function(monster)
    {
       return monster['SpAtk'];   
    };
    var GetSpDefense = function(monster)
    {
       return monster['SpDef'];   
    };
    var GetLevel = function(monster){
       return monster['Level'];   
    };
    var GetHealth = function(monster){
       return monster['HP'];
    };
    var SetHealth = function(monster,hp){
       monster['HP'] = hp;
    };
    
    
    /************* FOR GETTING MOVE RELATED INFORMATION *************/
    var GetMoveName = function(monster){
       return monster['Move'][0]['MoveName'];      
    };
    var GetMoveCategory = function(monster){
       return monster['Move'][0]['MoveCategory'];
    };
    var GetMoveType = function(monster){
       return monster['Move'][0]['MoveType'];   
    };
    var GetMovePower = function(monster){
       return monster['Move'][0]['MovePower'];      
    };
    
    
    /************* FOR COMAPERING MONSTERS *************/
    var CompareMonster = function(Attacker,Defender){   
       if (GetMoveType(Attacker) === "Fire" && GetType(Defender) === "Water")
          return 0.5;
       if (GetMoveType(Attacker) === "Fire" && GetType(Defender) === "Nature")
          return 2.0;
       if (GetMoveType(Attacker) === "Water" && GetType(Defender) === "Fire")
          return 2.0;
       if (GetMoveType(Attacker) === "Water" && GetType(Defender) === "Nature")
          return 0.5;
       if (GetMoveType(Attacker) === "Nature" && GetType(Defender) === "Fire")
          return 0.5;
       if (GetMoveType(Attacker) === "Nature" && GetType(Defender) === "Water")
          return 2.0;
       return 1;   //   same types
    
    };
    
    var SameTypeAttackBonus = function(monster)
    {   
       if (GetMoveType(monster) == GetType(monster))
           return 1.5;
        else
          return 1;
    };

  3. #3
    Join Date
    Feb 2013
    Posts
    46
    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.

  4. #4
    Join Date
    Mar 2013
    Posts
    8
    Quote Originally Posted by s-p-n View Post
    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.

  5. #5
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    i would use object literals and a merge function to drastically cut down on the # of chars used...
    or at least use an object to get all of the dots except one as a single word:
    Code:
    var him=OppMonster['Move'][0];
           him['MoveName']="Punch";
           him['MoveType']="Normal";
           him['MoveCategory']="Physical";
           him['MovePower']=20;
           him['MoveAccuracy']=100;
           him['MovePP']=10;
    and "him['MoveType']" is wordier than "him.MoveType" - if no spaces in key, use dots...

    you can also nest some of thos repetititve IFs using ternary:

    Code:
    var mt=GetMoveType(Attacker), dt= GetType(Defender) ;
    
    //then for each move type, you can do in one short line:
       if ( mt === "Fire"){   return dt=== "Water" ? 0.5 : 2.0;}
    instead of

    Code:
       if (GetMoveType(Attacker) === "Fire" && GetType(Defender) === "Water")
          return 0.5;
       if (GetMoveType(Attacker) === "Fire" && GetType(Defender) === "Nature")
          return 2.0;
    long-term, moving those values into objects instead of fork code is going to make it a lot easier to modify and expand the game later.

    the pattern there would be something LIKE this:
    Code:
    var cmLUT={
      Fire: { Water: 0.5, Nature: 2 },
      Water: {Fire: 2, Nature: 0.5,
     Nature: {Fire: 0.5, Water: 2},
     Default: {Fire: 1, Water: 1, Nature: 1} 
    }
    
    var CompareMonster = function(Attacker,Defender){   
      return cmLUT[GetMoveType(Attacker) || "Default" ][GetType(Defender)] || 1 ;
    };
    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 08:08 PM.

  6. #6
    Join Date
    Mar 2013
    Posts
    8
    thanks for the advice, i will start working on that right away

  7. #7
    Join Date
    Mar 2013
    Posts
    8
    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

    Code:
          case 1:  //FIRE
          {
             YourMonster['Type'] = "Fire";
             YourMonster['Advantage'] = "Nature";
             YourMonster['Disadvantage'] = "Water";
             YourMonster['MonsterName'] = "FireType";
             YourMonster['Atk'] = 63;
             YourMonster['Def'] = 45;
             YourMonster['SpAtk'] = 45;
             YourMonster['SpDef'] = 45;
             YourMonster['HP'] = 65;
             YourMonster['Speed'] = 45;
             YourMonster['Level'] = 5;
             YourMonster['Experience'] = 1000;      
          }
          break;
    Code:
    case 1:  //FIRE
          {
             OppMonster['Type'] = "Fire";
             OppMonster['Advantage'] = "Nature";
             OppMonster['Disadvantage'] = "Water";
             OppMonster['MonsterName'] = "FireType";
             OppMonster['Atk'] = 63;
             OppMonster['Def'] = 45;
             OppMonster['SpAtk'] = 45;
             OppMonster['SpDef'] = 45;
             OppMonster['HP'] = 65;
             OppMonster['Speed'] = 45;
             OppMonster['Level'] = 5;
             OppMonster['Experience'] = 1000;
          }
          break;
    AND
    Code:
    if(move == "Punch")
       {      
          YourMonster['Move'][0]['MoveName']="Punch";
          YourMonster['Move'][0]['MoveType']="Normal";
          YourMonster['Move'][0]['MoveCategory']="Physical";
          YourMonster['Move'][0]['MovePower']=20;
          YourMonster['Move'][0]['MoveAccuracy']=100;
          YourMonster['Move'][0]['MovePP']=10;
       }
    Code:
    case 1:
          {
             OppMonster['Move'][0]['MoveName']="Punch";
             OppMonster['Move'][0]['MoveType']="Normal";
             OppMonster['Move'][0]['MoveCategory']="Physical";
             OppMonster['Move'][0]['MovePower']=20;
             OppMonster['Move'][0]['MoveAccuracy']=100;
             OppMonster['Move'][0]['MovePP']=10;         
          } break;

  8. #8
    Join Date
    Mar 2013
    Location
    Brisneyland, Australia
    Posts
    5
    You mean something like this?:

    Code:
    var monsterStuff = {
      var1: 0,
      var2: 0,
      varMove: function(someone,vars) {
      //code
      },
      varPunch: function(someone,someoneelse,vars) {
      //code
      }
    }
    function move() {
      //some code
      mosnterStuff.varMove(someMontser,somewhere);
    
    }

  9. #9
    Join Date
    Mar 2013
    Posts
    8
    i was thinking something like this, although this is c++ code and i don't know how to do it in javascript

    Code:
    void Monster::FireType () //The Fire Type
    {	Type = "Fire";
    	Advantage = "Nature";
    	Disadvantage = "Water";
    	MonsterName = "FireType";
    	Atk = 63;
    	Def = 45;
    	SpAtk = 45;
    	SpDef = 45;
    	HP = 65;
    	Speed = 45;
    	Level = 5;
    	Experience = 1000;
    	move1 = Punch;
    	move2 = Kick;
    	move3 = Tackle;
    	move4 = FirePunch;
    }
    Code:
    void Monster::YourType ()	//chooses your monster
    {	string CharacterDetails;
    	cin >> CharacterDetails;   
            if(CharacterDetails == "fire")
            {FireType ();}
            
    }
    Code:
    void Monster::OpponentType ()//chooses opponents monster
    {int TypeOfCharacter;
      srand(time(0));
      TypeOfCharacter = rand() % 3 + 1;
      switch (TypeOfCharacter)
    	{case 1: FireType ();}
            break;
    }

  10. #10
    Join Date
    Feb 2013
    Posts
    46
    Here's a pattern you may be interested in:
    Code:
    /**
     * Monster (constructor)
     * use: 
     *    new Monster('nameStr', {custom: data});
     * args:
     *    [required] name (string)
     *    [optional] custom (object)
     * description:
     *     Given at least a name, Monster will create a new monster. Providing custom data will override default data. Custom data that is not found in _default will be ignored.
     **/
    function Monster (name, custom) {
        var _default = {
            life: 10,
            scariness: 3
        };
        var custom = custom || {};
        var i;
        if (name === void 0) { // void 0 is undefined..
            throw "Monsters require a name.";
        }
        this.name = name;
        for (i in _default) {
            this[i] = custom[i] || _default[i];
        }
    };
    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.

    Here's an example of using 'new' to create a vampire and a ghost:
    Code:
    var dracula = new Monster('vampire', {scariness: 5});
    var casper = new Monster('ghost');
    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.

    Here's a simple example usage on jsfiddle:
    http://jsfiddle.net/6CVaV/

    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.
    Last edited by s-p-n; 03-03-2013 at 12:45 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles