Skip to content

Commit 0717714

Browse files
committed
intermediate level 3
1 parent 9240b20 commit 0717714

File tree

2 files changed

+71
-23
lines changed

2 files changed

+71
-23
lines changed

xavdid-intermediate/Player.js

+54-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// const maxHealth = 20
2-
const directions = ['forward', 'backward', 'left', 'right']
2+
const directions = ['forward', 'right', 'backward', 'left']
33
// const util = require('util')
44

55
class Player { // eslint-disable-line no-unused-vars
@@ -8,22 +8,21 @@ class Player { // eslint-disable-line no-unused-vars
88
this._tooLow = 12
99
this._healTo = 17
1010
this._turn = 1
11-
this._captives = 0
11+
this._captives = 1
1212
this._healing = false
1313
}
1414

1515
playTurn (warrior) {
1616
// 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)) {
1819
// do nothing, acted already
19-
} else if (this.didHeal(warrior)) {
20+
} else if (this.didAttack(warrior)) {
2021
// do nothing, acted already
21-
} else if (this._captives > 0 && this.didHelpCaptive(warrior)) {
22+
} else if (this.didHeal(warrior)) {
2223
// do nothing, acted already
23-
// && this._captives > 0
24-
// this._captives -= 1
2524
// } else if (warrior.feel().isWall()) {
26-
// warrior.pivot()
25+
// warrior.pivot()
2726
} else {
2827
// basic action
2928
warrior.walk(warrior.directionOfStairs())
@@ -43,10 +42,27 @@ class Player { // eslint-disable-line no-unused-vars
4342
didAttack (warrior) {
4443
// look for adjacent enemies to fight, then shoot at the farthest ones
4544
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)) {
4849
return true
4950
} 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+
}
5066
// can't shoot yet
5167
// enemyDir = this.furthestEnemyDirection(warrior)
5268
// if (enemyDir) {
@@ -58,10 +74,17 @@ class Player { // eslint-disable-line no-unused-vars
5874
}
5975
}
6076

77+
// handles retreating and heading
6178
didHeal (warrior) {
6279
if (warrior.health() < this._tooLow || this._healing) {
6380
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)
6588
return true
6689
} else {
6790
warrior.rest()
@@ -82,6 +105,7 @@ class Player { // eslint-disable-line no-unused-vars
82105
directions.forEach(d => {
83106
if (warrior.feel(d).isCaptive()) {
84107
warrior.rescue(d)
108+
this._captives -= 1
85109
acted = true
86110
}
87111
})
@@ -130,6 +154,21 @@ class Player { // eslint-disable-line no-unused-vars
130154
return dir
131155
}
132156

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+
133172
distanceToEnemy (warrior, look) {
134173
return look.findIndex(i => i.isEnemy())
135174
}
@@ -148,4 +187,8 @@ class Player { // eslint-disable-line no-unused-vars
148187

149188
return furthestEnemyDirection
150189
}
190+
191+
opposite (dir) {
192+
return directions[(directions.indexOf(dir) + 2) % 4]
193+
}
151194
}

xavdid-intermediate/README

+17-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
Level 2
1+
Level 3
22

3-
Another large room, but with several enemies blocking your way to the stairs.
3+
You feel slime on all sides, you're surrounded!
44

5-
Tip: Just like walking, you can attack and feel in multiple directions ('forward', 'left', 'right', 'backward').
5+
Tip: Call `warrior.bind(direction)` to bind an enemy to keep him from attacking. Bound enemies look like captives.
66

77

8-
╔════╗
9-
║@s ║
10-
║ sS>║
11-
╚════╝
8+
╔═══╗
9+
║>s ║
10+
║s@s║
11+
║ C ║
12+
╚═══╝
1213

1314
> = stairs
1415
@ = xavdid
15-
S = thickSludge
16+
C = captive
1617
s = sludge
1718

1819

@@ -22,13 +23,17 @@ Warrior abilities:
2223

2324
warrior.walk()
2425

25-
warrior.feel() (NEW!)
26+
warrior.feel()
2627

27-
warrior.attack() (NEW!)
28+
warrior.attack()
2829

29-
warrior.health() (NEW!)
30+
warrior.health()
3031

31-
warrior.rest() (NEW!)
32+
warrior.rest()
33+
34+
warrior.bind() (NEW!)
35+
36+
warrior.rescue() (NEW!)
3237

3338

3439
When you're done editing Player.js, run the warriorjs command again.

0 commit comments

Comments
 (0)