1
-
1
+ // Hero classes
2
2
const heroClasses = {
3
3
warrior : { charClass : "Warrior" , life : 30 , damage : 4 } ,
4
4
rogue : { charClass : "Rogue" , life : 25 , damage : 3 } ,
5
5
sorcerer : { charClass : "Sorcerer" , life : 20 , damage : 5 }
6
6
} ;
7
7
8
- // Monsters classes and randomizer
8
+ // Monsters classes
9
9
const monsterClasses = {
10
10
zombie : { charClass : "Zombie" , life : 8 , damage : 4 } ,
11
11
skeleton : { charClass : "Skeleton" , life : 10 , damage : 6 } ,
12
12
holem : { charClass : "Holem" , life : 15 , damage : 6 }
13
13
} ;
14
14
15
- // Hero and Monsters constructors
15
+ // Char
16
+ const Char = function ( ) { } ;
17
+
18
+ Char . prototype . getName = function ( ) {
19
+ return this . heroName || `I am ${ this . charClass } I don\`t have name` ;
20
+ } ;
21
+ Char . prototype . getCharClass = function ( ) { return this . charClass ; } ;
22
+
23
+ Char . prototype . attack = function ( enemy ) {
24
+ let attacker , attacked ;
25
+ if ( this instanceof Hero ) {
26
+ if ( enemy instanceof Hero ) return "I will attack only monsters" ;
27
+ attacker = 'Hero' ;
28
+ attacked = enemy . charClass ;
29
+ }
30
+ if ( this instanceof Monster ) {
31
+ if ( enemy instanceof Monster ) return "I will attack only Hero" ;
32
+ attacker = this . charClass ;
33
+ attacked = 'Hero' ;
34
+ }
35
+ enemy . life = Math . max ( enemy . life - this . damage , 0 ) ;
36
+ if ( ! enemy . life ) return ( `${ attacker } attacked, ${ attacked } killed` ) ;
37
+ return ( `${ attacker } attacked, done ${ this . damage } damage to ${ attacked } ` ) ;
38
+ } ;
39
+
40
+
41
+ // Hero and Monster constructors
16
42
const Hero = function ( name , charClass ) {
17
43
if ( heroClasses . hasOwnProperty ( charClass ) ) {
18
44
this . heroName = name ;
19
45
Object . assign ( this , heroClasses [ charClass ] ) ;
20
- this . getName = function ( ) { return this . heroName ; } ;
21
- this . getCharClass = function ( ) { return this . charClass ; } ;
22
46
}
23
47
else throw new Error ( "Incorrect character class provided" ) ;
24
48
} ;
25
49
26
50
const Monster = function ( charClass ) {
27
51
if ( monsterClasses . hasOwnProperty ( charClass ) ) {
28
52
Object . assign ( this , monsterClasses [ charClass ] ) ;
29
- this . getName = function ( ) { return ( `I am ${ this . charClass } I don\`t have name` ) ; } ;
30
- this . getCharClass = function ( ) { return this . charClass ; } ;
31
53
}
32
54
else throw new Error ( "Incorrect character class provided" ) ;
33
55
} ;
34
56
35
-
36
- Hero . prototype . attack = function ( enemy ) {
37
- if ( enemy instanceof Hero ) return "I will attack only monsters" ;
38
- else {
39
- enemy . life = Math . max ( enemy . life - this . damage , 0 ) ;
40
- if ( ! enemy . life ) return ( `Hero attacked, ${ enemy . charClass } killed` ) ;
41
- return ( `Hero attacked, done ${ this . damage } damage to ${ enemy . charClass } ` ) ;
42
- }
43
- } ;
44
-
45
- Monster . prototype . attack = function ( enemy ) {
46
- if ( enemy instanceof Monster ) return "I will attack only Hero" ;
47
- else {
48
- enemy . life = Math . max ( enemy . life - this . damage , 0 ) ;
49
- if ( ! enemy . life ) return ( `${ this . charClass } attacked, Hero killed` ) ;
50
- return ( `${ this . charClass } attacked, done ${ this . damage } damage to ${ enemy . charClass } ` ) ;
51
- }
52
- } ;
53
-
54
- // GENERAL_ATTACK_MESSAGE
55
- // "CHARACTER_CLASS killed" - this action will kill target
56
- // "done AMOUNT_OF_DAMAGE damage to CHARACTER_CLASS";
57
+ Hero . prototype = new Char ( ) ;
58
+ Monster . prototype = new Char ( ) ;
59
+ //--------------------------------
57
60
58
61
const statuses = {
59
62
idle : "Idle" ,
@@ -66,9 +69,9 @@ const Game = function() {
66
69
this . status = statuses . idle ;
67
70
} ;
68
71
69
- Object . defineProperty ( Game . prototype , 'lives ' , {
72
+ Object . defineProperty ( Game . prototype , 'monstersAlive ' , {
70
73
get : function ( ) {
71
- return this . monsters [ 0 ] . life + this . monsters [ 1 ] . life ;
74
+ return this . monsters . some ( monster => monster . life > 0 ) ;
72
75
}
73
76
} ) ;
74
77
@@ -78,7 +81,7 @@ Game.prototype.addHero = function(hero) {
78
81
else if ( this . hero ) throw new Error ( "Only one hero can exist" ) ;
79
82
else {
80
83
this . hero = hero ;
81
- return ( " Hero created, welcome %s" , this . hero . heroName ) ;
84
+ return ( ` Hero created, welcome ${ this . hero . heroName } ` ) ;
82
85
}
83
86
} ;
84
87
@@ -110,7 +113,7 @@ Game.prototype.finishJourney = function() {
110
113
this . status = statuses . finished ;
111
114
return "The Game is finished. Hero is dead :(" ;
112
115
}
113
- else if ( this . monsters [ 0 ] . life + this . monsters [ 1 ] . life === 0 ) {
116
+ else if ( ! this . monstersAlive ) {
114
117
return "The Game is finished. Monsters are dead. Congratulations" ;
115
118
}
116
119
else return "Don`t stop. Some monsters are still alive. Kill`em all" ;
@@ -121,32 +124,45 @@ Game.prototype.fight = function() {
121
124
throw new Error ( "Begin your journey to start fighting monsters" ) ;
122
125
}
123
126
127
+ if ( ! this . hero . life || ! this . monstersAlive ) return this . finishJourney ( ) ;
128
+
124
129
let currentMonster ;
125
- if ( this . monsters [ 0 ] . life ) currentMonster = this . monsters [ 0 ] ;
126
- else if ( this . monsters [ 1 ] . life ) currentMonster = this . monsters [ 1 ] ;
130
+ for ( let m of this . monsters ) {
131
+ if ( m . life ) {
132
+ currentMonster = m ;
133
+ break ;
134
+ }
135
+ }
127
136
128
137
while ( this . hero . life && currentMonster . life ) {
129
138
this . hero . attack ( currentMonster ) ;
130
139
if ( currentMonster . life ) currentMonster . attack ( this . hero ) ;
131
140
}
132
- if ( ! this . hero . life ) return "Monster win" ;
133
- else if ( ! currentMonster . life ) return "Hero win" ;
141
+ if ( ! this . hero . life ) {
142
+ this . status = statuses . finished ;
143
+ return "Monster win" ;
144
+ }
145
+ else if ( ! this . monstersAlive ) {
146
+ this . status = statuses . finished ;
147
+ return "Hero win" ;
148
+ }
149
+ return "Hero win" ;
134
150
} ;
135
151
136
152
137
153
/* Game Population mechanism should go below */
138
154
139
155
Game . prototype . populate = function ( ) {
140
156
const heroName = prompt ( 'Name your Hero:' , 'Chuck Norris' ) ;
141
- const heroClass = prompt ( 'What is your Hero?' , 'warrior | rogue | sorcerer | random' ) . toLowerCase ( ) . trim ( ) ;
157
+ const heroClass = prompt ( 'What is your Hero?' , 'warrior | rogue | sorcerer | random' ) . trim ( ) ;
142
158
if ( heroClass === 'random' ) this . addRandomHero ( heroName ) ;
143
159
else {
144
160
const theHero = new Hero ( heroName , heroClass ) ;
145
161
this . addHero ( theHero ) ;
146
162
}
147
163
148
- while ( this . monsters . length < 2 ) {
149
- const monsterClass = prompt ( `What is your Monster ${ this . monsters . length + 1 } ?` , 'zombie | skeleton | holem | random' ) . toLowerCase ( ) . trim ( ) ;
164
+ while ( this . monsters . length < Game . maxMonsters ) {
165
+ const monsterClass = prompt ( `What is your Monster ${ this . monsters . length + 1 } ?` , 'zombie | skeleton | holem | random' ) . trim ( ) ;
150
166
if ( monsterClass === 'random' ) this . addRandomMonster ( ) ;
151
167
else {
152
168
const theMonster = new Monster ( monsterClass ) ;
0 commit comments