1
1
// const maxHealth = 20
2
- const directions = [ 'forward' , 'backward ' , 'left ' , 'right ' ]
2
+ const directions = [ 'forward' , 'right ' , 'backward ' , 'left ' ]
3
3
// const util = require('util')
4
4
5
5
class Player { // eslint-disable-line no-unused-vars
@@ -8,22 +8,21 @@ class Player { // eslint-disable-line no-unused-vars
8
8
this . _tooLow = 12
9
9
this . _healTo = 17
10
10
this . _turn = 1
11
- this . _captives = 0
11
+ this . _captives = 1
12
12
this . _healing = false
13
13
}
14
14
15
15
playTurn ( warrior ) {
16
16
// console.log(`** Begin turn ${this._turn}`)
17
- if ( this . didAttack ( warrior ) ) {
17
+ // clear captives first, so anything left must be an enemy
18
+ if ( this . _captives > 0 && this . didHelpCaptive ( warrior ) ) {
18
19
// do nothing, acted already
19
- } else if ( this . didHeal ( warrior ) ) {
20
+ } else if ( this . didAttack ( warrior ) ) {
20
21
// do nothing, acted already
21
- } else if ( this . _captives > 0 && this . didHelpCaptive ( warrior ) ) {
22
+ } else if ( this . didHeal ( warrior ) ) {
22
23
// do nothing, acted already
23
- // && this._captives > 0
24
- // this._captives -= 1
25
24
// } else if (warrior.feel().isWall()) {
26
- // warrior.pivot()
25
+ // warrior.pivot()
27
26
} else {
28
27
// basic action
29
28
warrior . walk ( warrior . directionOfStairs ( ) )
@@ -43,10 +42,27 @@ class Player { // eslint-disable-line no-unused-vars
43
42
didAttack ( warrior ) {
44
43
// look for adjacent enemies to fight, then shoot at the farthest ones
45
44
let enemyDir = this . adjacentEnemyDirection ( warrior )
46
- if ( enemyDir ) {
47
- warrior . attack ( enemyDir )
45
+ if ( enemyDir && this . numAdjacentEnemies ( warrior ) > 1 ) {
46
+ warrior . bind ( enemyDir )
47
+ return true
48
+ } else if ( warrior . health ( ) < 5 && this . didHeal ( warrior ) ) {
48
49
return true
49
50
} else {
51
+ if ( enemyDir ) {
52
+ warrior . attack ( enemyDir )
53
+ return true
54
+ } else {
55
+ let captiveDir = this . adjacentCaptiveDirection ( warrior )
56
+ if ( captiveDir && this . _captives === 0 ) {
57
+ // don't get too low- if everyone is bound, take time to heal
58
+ if ( this . didHeal ( warrior ) ) {
59
+ // acted, do nothing
60
+ } else {
61
+ warrior . attack ( captiveDir )
62
+ }
63
+ return true
64
+ }
65
+ }
50
66
// can't shoot yet
51
67
// enemyDir = this.furthestEnemyDirection(warrior)
52
68
// if (enemyDir) {
@@ -58,10 +74,17 @@ class Player { // eslint-disable-line no-unused-vars
58
74
}
59
75
}
60
76
77
+ // handles retreating and heading
61
78
didHeal ( warrior ) {
62
79
if ( warrior . health ( ) < this . _tooLow || this . _healing ) {
63
80
if ( this . underAttack ( warrior ) ) {
64
- warrior . walk ( 'backward' )
81
+ let retreatDir
82
+ if ( this . opposite ( this . adjacentEnemyDirection ( warrior ) ) . isEmpty ( ) ) {
83
+ retreatDir = this . opposite ( this . adjacentEnemyDirection ( warrior ) )
84
+ } else {
85
+ retreatDir = this . opposite ( warrior . directionOfStairs ( ) )
86
+ }
87
+ warrior . walk ( retreatDir )
65
88
return true
66
89
} else {
67
90
warrior . rest ( )
@@ -82,6 +105,7 @@ class Player { // eslint-disable-line no-unused-vars
82
105
directions . forEach ( d => {
83
106
if ( warrior . feel ( d ) . isCaptive ( ) ) {
84
107
warrior . rescue ( d )
108
+ this . _captives -= 1
85
109
acted = true
86
110
}
87
111
} )
@@ -130,6 +154,21 @@ class Player { // eslint-disable-line no-unused-vars
130
154
return dir
131
155
}
132
156
157
+ // number of adjacent enemies
158
+ numAdjacentEnemies ( warrior ) {
159
+ return directions . map ( d => warrior . feel ( d ) . isEnemy ( ) ) . filter ( d => d ) . length
160
+ }
161
+
162
+ adjacentCaptiveDirection ( warrior ) {
163
+ let dir = false
164
+ directions . forEach ( d => {
165
+ if ( warrior . feel ( d ) . isCaptive ( ) && ! dir ) {
166
+ dir = d
167
+ }
168
+ } )
169
+ return dir
170
+ }
171
+
133
172
distanceToEnemy ( warrior , look ) {
134
173
return look . findIndex ( i => i . isEnemy ( ) )
135
174
}
@@ -148,4 +187,8 @@ class Player { // eslint-disable-line no-unused-vars
148
187
149
188
return furthestEnemyDirection
150
189
}
190
+
191
+ opposite ( dir ) {
192
+ return directions [ ( directions . indexOf ( dir ) + 2 ) % 4 ]
193
+ }
151
194
}
0 commit comments