can somebody help me simplify this code please?

can somebody help me simplify this code please, so that i only have to declare each monster and move once, i know its in Javascript, but i think it can pretty easily read

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/************ 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;
	}
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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;
};

closed account (D4S8vCM9)
Just an idea which came in my mind spontanously, use: the prototype pattern http://en.wikipedia.org/wiki/Prototype_pattern
I'm not that familiar with this language, my friend was helping me, and i cant explain it to him, but i know it can be simplified further, based on what i did with my c++ code
I can't help but another place to try and get some good help would be this forum. http://forum.codecall.net/
Topic archived. No new replies allowed.