SitePoint Sponsor

User Tag List

Results 1 to 1 of 1

Threaded View

  1. #1
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help simplifying code.

    I'm new to JavaScript and, I need help simplifying my JavaScript code, I think i'm coding the monsters and moves a lot more than i have to if you can help, i was thinking something like YourMonster = FireMonster (that would be if they picked fire type), same with moves, for example something like (Monster.Move1 = Punch) but i don't know how.

    Code JavaScript:
    /************ 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; 
       }    
    }; 
     
    /************* 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; 
    };
    Last edited by cpradio; Mar 2, 2013 at 07:11.


Bookmarks

Posting Permissions

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