Created
October 5, 2018 17:45
-
-
Save AGoblinKing/0c29633cf6f9625d734e0d8169d2cead to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MAX_HP = 20; | |
const State = { | |
last: { | |
HP: MAX_HP, | |
action: "none" | |
} | |
}; | |
const Utility = { | |
walkForward: { | |
scorer: (warrior) => warrior.feel().isEmpty() ? 0.5 : 0, | |
do: (warrior) => warrior.walk() | |
}, | |
walkBackward: { | |
scorer: (warrior) => { | |
const missingHP = (1 - warrior.health() / MAX_HP) | |
return warrior.feel().isUnit() && missingHP >= 0.5 ? 1 : 0; | |
}, | |
do: (warrior) => warrior.walk("backward") | |
}, | |
attack : { | |
scorer: (warrior) => warrior.feel().getUnit().isEnemy() ? 0.5 : 0, | |
do : (warrior) => warrior.attack() | |
}, | |
heal : { | |
scorer: (warrior) => { | |
const health = warrior.health(); | |
const facing = warrior.feel(); | |
// fak, healing isn't working | |
if(State.last.action === "heal" && State.last.HP < health) { | |
return 0; | |
} | |
if(health < MAX_HP && facing.isEmpty()) { | |
return 0.6; | |
} | |
return 0; | |
}, | |
do: (warrior) => warrior.rest() | |
} | |
}; | |
const utilitySorter = (a, b) => a.score > b.score ? -1 : 1; | |
class Player { | |
playTurn(warrior) { | |
this.consider(warrior); | |
} | |
consider(warrior) { | |
const feel = warrior.feel(); | |
const scores = Object.keys(Utility).map((key) => Object.assign( | |
{ key, score: Utility[key].scorer(warrior) }, | |
Utility[key], | |
)); | |
scores.sort(utilitySorter); | |
scores[0].do(warrior); | |
State.last.hp = warrior.health(); | |
State.last.action = scores[0].key; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment