阅读 651

Warriorjs 完全通关指南

1.简介

项目导航

GitHub
https://github.com/olistic/warriorjs
复制代码
官网
https://warrior.js.org/en/
复制代码

2.快速开始

Install WarriorJS with npm:

npm install --global @warriorjs/cli
复制代码

Launch the game:

warriorjs
复制代码

Create your warrior.

You'll be pointed to a README file with instructions for the first level.

3.卡源码

源码未review,加之个人技术有限,勿嫌弃~
复制代码

Level 1

class Player {
  playTurn(warrior) {
    // Cool code goes here.
    warrior.walk()
  }
}
复制代码

Level 2

class Player {
  playTurn(warrior) {
    // Cool code goes here.
    if(warrior.feel().isEnemy()){
      warrior.attack()
    }else{
      warrior.walk()
    }
  }
}
复制代码

Level 3

const hp = 20

class Player {
  playTurn(warrior) {
    // Cool code goes here.
    if(warrior.feel().isEnemy()){
      warrior.attack()
    }else{
      if(warrior.health() < hp){
        warrior.rest()
      }else{
        warrior.walk()
      }
    }
  }
}
复制代码

Level 4

class Player {

  constructor() {
    this.hp_max = 20
    this.hp_stash = this.hp_max
  }

  playTurn(warrior) {
    // Cool code goes here.
    if (this.shouleAttack(warrior)) {
      warrior.attack()
    } else {
      if (this.shouldHeal(warrior)) {
        warrior.rest()
      } else {
        warrior.walk()
      }
    }

    this.hp_stash = warrior.health()
  }

  shouleAttack(warrior){
    return warrior.feel().getUnit() && warrior.feel().getUnit().isEnemy()
  }

  shouldHeal(warrior) {
    if (warrior.health() < this.hp_max && !this.isDamage(warrior)) {
      return true
    }
    return false
  }

  isDamage(warrior) {
    return this.hp_stash > warrior.health()
  }
}
复制代码

Level 5

class Player {

  constructor() {
    this.hp_max = 20
    this.hp_stash = this.hp_max
  }

  playTurn(warrior) {
    // Cool code goes here.
    if (!this.dealSpace(warrior)) {
      if (this.shouldHeal(warrior)) {
        warrior.rest()
      } else {
        warrior.walk()
      }
    }

    this.hp_stash = warrior.health()
  }

  dealSpace(warrior) {
    let unit = warrior.feel().getUnit()
    if (unit) {
      if (this.shouldAttack(unit)) {
        warrior.attack()
      } else if (this.shouldRescue(unit)) {
        warrior.rescue()
      }
      return true
    } else {
      return false
    }
  }

  shouldRescue(unit) {
    return unit.isBound()
  }

  shouldAttack(unit) {
    return unit.isEnemy()
  }

  shouldHeal(warrior) {
    if (warrior.health() < this.hp_max && !this.isDamage(warrior)) {
      return true
    }
    return false
  }

  isDamage(warrior) {
    return this.hp_stash > warrior.health()
  }
}
复制代码

Level 6 (90,375)

class Player {
  constructor() {
    this.actions = {
      walk: 0,
      attack: 1,
      rescue: 2,
      rest: 3
    }

    this.hp_max = 20
    this.hp_now = this.hp_max
    this.hp_stash = this.hp_max
    this.hp_rest = this.hp_max * 0.1

    this.direction = 'backward'
    this.isEscape = false
  }

  playTurn(warrior) {
    // Cool code goes here.
    this.hp_now = warrior.health()

    let space = warrior.feel(this.direction)
    switch (this.dealSpace(space)) {
      case 1: {
        // Unit
        let unit = space.getUnit()
        switch (this.dealUnit(unit)) {
          case 0:
            // Empty

            break;
          case 1:
            // Enemy
            this.active(warrior, 'attack')

            return
          case 2:
            // Bound
            this.active(warrior, 'rescue')

            return

          default:
            break;
        }
      }
        break;
      case 2: {
        // Wall
        this.direction = 'forward'
      }
        break;
      default:
        break;
    }

    if(!this.isEscape && this.shouldEscape(false)){
      this.isEscape = true
      this.direction = 'backward'
    }

    if (this.isEscape) {
      if (this.hp_now < this.hp_max) {
        if (this.isDamage()) {
          this.active(warrior, 'walk')
        } else {
          this.active(warrior, 'rest')
        }
        return
      }else{
        this.direction = 'forward'
        this.isEscape = false
      }
    }

    if (this.shouldHeal()) {
      this.active(warrior, 'rest')
    } else {
      this.active(warrior, 'walk')
    }
  }

  active(warrior, action) {
    switch (this.actions[action]) {
      case 0:
        warrior.walk(this.direction)
        break;
      case 1:
        warrior.attack(this.direction)
        break;
      case 2:
        warrior.rescue(this.direction)
        break;
      case 3:
        warrior.rest(this.direction)
        break;
    }
    this.hp_stash = warrior.health()
  }

  dealSpace(space) {
    /* 
      0: Empty
      1: Unit
      2:Wall
    */
    if (space.isWall()) {
      return 2
    }

    if (space.isUnit()) {
      return 1
    }

    return 0
  }

  dealUnit(unit) {
    /*
      0: Empty
      1: Enemy
      2: Bound
    */
    if (unit.isEnemy()) {
      return 1
    }

    if (unit.isBound()) {
      return 2
    }

    return 0
  }

  // 当前是否应该治疗
  shouldHeal() {
    if (this.hp_now < this.hp_max && !this.isDamage()) {
      return true
    }
    return false
  }

  // 是否正在受到伤害
  isDamage() {
    return (this.damage() > 0)
  }

  // 伤害量
  damage() {
    return this.hp_stash - this.hp_now
  }

  // 濒死
  dying() {
    return this.damage() >= this.hp_now
  }

  // 是否后撤
  shouldEscape(touch) {
    // 近战,伤害大于恢复且濒死
    if(touch && this.damage() >= this.hp_rest && this.dying()){
      return true
    }

    // 远战,小于半血
    if(this.damage() && this.hp_now < this.hp_max / 2){
      return true
    }

    return false
  }
}
复制代码

Level 7 (37,412)

class Player {
  constructor() {
    this.actions = {
      walk: 0,
      attack: 1,
      rescue: 2,
      rest: 3
    }

    this.hp_max = 20
    this.hp_now = this.hp_max
    this.hp_stash = this.hp_max
    this.hp_rest = this.hp_max * 0.1

    this.direction = 'forward'
    this.isEscape = false
  }

  playTurn(warrior) {
    // Cool code goes here.
    this.hp_now = warrior.health()


    if(!this.isEscape && this.shouldEscape(this.isTouch(warrior))){
      this.isEscape = true
      this.reverse()
    }

    if (this.isEscape) {
      if (this.hp_now < this.hp_max) {
        warrior.think(this.damage())
        if (this.damage()) {
          this.active(warrior, 'walk')
        } else {
          this.active(warrior, 'rest')
        }
        return
      }else{
        this.isEscape = false
        this.reverse()
      }
    }


    let space = warrior.feel(this.direction)
    switch (this.dealSpace(space)) {
      case 1: {
        // Unit
        let unit = space.getUnit()
        switch (this.dealUnit(unit)) {
          case 0:
            // Empty
            break;
          case 1:
            // Enemy
            this.active(warrior, 'attack')
            return
          case 2:
            // Bound
            this.active(warrior, 'rescue')
            return
          default:
            break;
        }
      }
        break;
      case 2: {
        // Wall
        this.reverse()
      }
        break;
      default:
        break;
    }

    if (this.shouldHeal()) {
      this.active(warrior, 'rest')
    } else {
      this.active(warrior, 'walk')
    }
  }

  active(warrior, action) {
    switch (this.actions[action]) {
      case 0:
        warrior.walk(this.direction)
        break;
      case 1:
        warrior.attack(this.direction)
        break;
      case 2:
        warrior.rescue(this.direction)
        break;
      case 3:
        warrior.rest(this.direction)
        break;
    }
    this.hp_stash = warrior.health()
  }

  dealSpace(space) {
    /* 
      0: Empty
      1: Unit
      2:Wall
    */
    if (space.isWall()) {
      return 2
    }

    if (space.isUnit()) {
      return 1
    }

    return 0
  }

  dealUnit(unit) {
    /*
      0: Empty
      1: Enemy
      2: Bound
    */
    if (unit.isEnemy()) {
      return 1
    }

    if (unit.isBound()) {
      return 2
    }

    return 0
  }

  // 调转方向
  reverse(){
    if(this.direction === 'backward'){
      this.direction = 'forward'
    }else{
      this.direction = 'backward'
    }
  }

  // 当前是否应该治疗
  shouldHeal() {
    if (this.hp_now < this.hp_max && !this.damage()) {
      return true
    }
    return false
  }

  // 受到伤害量
  damage() {
    let sum = parseInt(this.hp_stash) - parseInt(this.hp_now)
    if(sum > 0){
      return sum 
    }
    return 0
  }

  // 濒死
  dying() {
    return (this.damage() >= this.hp_now)
  }

  // 是否和已解除enemy
  isTouch(warrior){
    if(warrior.feel(this.direction).isUnit() && warrior.feel(this.direction).getUnit().isEnemy()){
      return true
    }

    return false
  }

  // 是否后撤
  shouldEscape(touch) {
    // 近战,伤害大于恢复且濒死
    if(touch && this.damage() >= this.hp_rest && this.dying()){
      return true
    }

    // 远战,小于半血
    if(!touch && this.damage() && this.hp_now < this.hp_max / 2){
      return true
    }

    return false
  }
}
复制代码

Level 8 (46,458)

class Player {
  constructor() {
    this.actions = {
      walk: 0,
      attack: 1,
      rescue: 2,
      rest: 3,
      shoot: 4
    }

    this.hp_max = 20
    this.hp_now = this.hp_max
    this.hp_stash = this.hp_max
    this.hp_rest = this.hp_max * 0.1

    this.direction = 'forward'
    this.isEscape = false
  }

  playTurn(warrior) {
    // Cool code goes here.
    this.hp_now = warrior.health()

    if (!this.isEscape && this.shouldEscape(this.isTouch(warrior))) {
      this.isEscape = true
      this.reverse()
    }

    if (this.isEscape) {
      if (this.hp_now < this.hp_max) {
        warrior.think(this.damage())
        if (this.damage()) {
          this.active(warrior, 'walk')
        } else {
          this.active(warrior, 'rest')
        }
        return
      } else {
        this.isEscape = false
        this.reverse()
      }
    }


    // 判断是否进行远程打击
    if(this.shoot(warrior)){
      return
    }


    let space = warrior.feel(this.direction)
    switch (this.dealSpace(space)) {
      case 1: {
        // Unit
        let unit = space.getUnit()
        switch (this.dealUnit(unit)) {
          case 0:
            // Empty
            break;
          case 1:
            // Enemy
            this.active(warrior, 'attack')
            return
          case 2:
            // Bound
            this.active(warrior, 'rescue')
            return
          default:
            break;
        }
      }
        break;
      case 2: {
        // Wall
        this.reverse()
      }
        break;
      default:
        break;
    }

    if (this.shouldHeal()) {
      this.active(warrior, 'rest')
    } else {
      this.active(warrior, 'walk')
    }
  }

  active(warrior, action) {
    switch (this.actions[action]) {
      case 0:
        warrior.walk(this.direction)
        break;
      case 1:
        warrior.attack(this.direction)
        break;
      case 2:
        warrior.rescue(this.direction)
        break;
      case 3:
        warrior.rest(this.direction)
        break;
      case 4:
        warrior.shoot(this.direction)
        break;
    }
    this.hp_stash = warrior.health()
  }

  dealSpace(space) {
    /* 
      0: Empty
      1: Unit
      2:Wall
    */
    if (space.isWall()) {
      return 2
    }

    if (space.isUnit()) {
      return 1
    }

    return 0
  }

  dealUnit(unit) {
    /*
      0: Empty
      1: Enemy
      2: Bound
    */
    if (unit.isEnemy()) {
      return 1
    }

    if (unit.isBound()) {
      return 2
    }

    return 0
  }

  // 调转方向
  reverse() {
    if (this.direction === 'backward') {
      this.direction = 'forward'
    } else {
      this.direction = 'backward'
    }
  }

  // 当前是否应该治疗
  shouldHeal() {
    if (this.hp_now < this.hp_max && !this.damage()) {
      return true
    }
    return false
  }

  // 受到伤害量
  damage() {
    let sum = parseInt(this.hp_stash) - parseInt(this.hp_now)
    if (sum > 0) {
      return sum
    }
    return 0
  }

  // 濒死
  dying() {
    return (this.damage() >= this.hp_now)
  }

  // 是否和已解除enemy
  isTouch(warrior) {
    if (warrior.feel(this.direction).isUnit() && warrior.feel(this.direction).getUnit().isEnemy()) {
      return true
    }

    return false
  }

  // 是否后撤
  shouldEscape(touch) {
    // 近战,伤害大于恢复且濒死
    if (touch && this.damage() >= this.hp_rest && this.dying()) {
      return true
    }

    // 远战,小于半血
    if (!touch && this.damage() && this.hp_now < this.hp_max / 2) {
      return true
    }

    return false
  }

  shoot(warrior) {
    let spaces = warrior.look(this.direction)

    for(let i in spaces){
      let space = spaces[i]
      
      if(this.dealSpace(space) === 1){
        let unit = space.getUnit()
        switch (this.dealUnit(unit)) {
          case 0:
            // Empty
            break;
          case 1:
            // Enemy
            if(i > 0){
              // 远程,选择射击,若为0,则按照原有逻辑近战
              this.active(warrior, 'shoot')
            }
            return true
          case 2:
            // Bound
            return false
          default:
            break;
        }
      }
    }
    return false
  }
}
复制代码

Level 9 (98,556)

class Player {
  constructor() {
    this.actions = {
      walk: 0,
      attack: 1,
      rescue: 2,
      rest: 3,
      shoot: 4
    }

    this.hp_max = 20
    this.hp_now = this.hp_max
    this.hp_stash = this.hp_max
    this.hp_rest = this.hp_max * 0.1

    this.direction = 'forward'
    this.isEscape = false

    this.units = {
      forward:[],
      backward:[]
    }
  }

  playTurn(warrior) {
    // Cool code goes here.
    this.hp_now = warrior.health()

    if (!this.isEscape && this.shouldEscape(this.isTouch(warrior))) {
      this.isEscape = true
      this.reverse()
    }

    if (this.isEscape) {
      if (this.hp_now < this.hp_max) {
        warrior.think(this.damage())
        if (this.damage()) {
          this.active(warrior, 'walk')
        } else {
          this.active(warrior, 'rest')
        }
        return
      } else {
        this.isEscape = false
        this.reverse()
      }
    }

    // 判断是否进行远程打击
    this.lookAround(warrior)

    if(this.shoot(this.units.backward,'backward')){
      this.active(warrior, 'shoot','backward')
      return
    }else if(this.shoot(this.units.forward,'forward')){
      this.active(warrior, 'shoot','forward')
      return
    }


    let space = warrior.feel(this.direction)
    switch (this.dealSpace(space)) {
      case 1: {
        // Unit
        let unit = space.getUnit()
        switch (this.dealUnit(unit)) {
          case 0:
            // Empty
            break;
          case 1:
            // Enemy
            this.active(warrior, 'attack')
            return
          case 2:
            // Bound
            this.active(warrior, 'rescue')
            return
          default:
            break;
        }
      }
        break;
      case 2: {
        // Wall
        this.reverse()
      }
        break;
      default:
        break;
    }

    if (this.shouldHeal()) {
      this.active(warrior, 'rest')
    } else {
      this.active(warrior, 'walk')
    }
  }

  active(warrior, action,direction) {

    direction = direction ? direction : this.direction

    switch (this.actions[action]) {
      case 0:
        warrior.walk(direction)
        break;
      case 1:
        warrior.attack(direction)
        break;
      case 2:
        warrior.rescue(direction)
        break;
      case 3:
        warrior.rest(direction)
        break;
      case 4:
        warrior.shoot(direction)
        break;
    }
    this.hp_stash = warrior.health()
  }

  dealSpace(space) {
    /* 
      0: Empty
      1: Unit
      2:Wall
    */
    if (space.isWall()) {
      return 2
    }

    if (space.isUnit()) {
      return 1
    }

    return 0
  }

  dealUnit(unit) {
    /*
      0: Empty
      1: Enemy
      2: Bound
    */
    if (unit.isEnemy()) {
      return 1
    }

    if (unit.isBound()) {
      return 2
    }

    return 0
  }

  // 调转方向
  reverse() {
    if (this.direction === 'backward') {
      this.direction = 'forward'
    } else {
      this.direction = 'backward'
    }
  }

  // 当前是否应该治疗
  shouldHeal() {
    if (this.hp_now < this.hp_max && !this.damage()) {
      return true
    }
    return false
  }

  // 受到伤害量
  damage() {
    let sum = parseInt(this.hp_stash) - parseInt(this.hp_now)
    if (sum > 0) {
      return sum
    }
    return 0
  }

  // 濒死
  dying() {
    return (this.damage() >= this.hp_now)
  }

  // 是否已接触enemy
  isTouch(warrior) {
    if (warrior.feel(this.direction).isUnit() && warrior.feel(this.direction).getUnit().isEnemy()) {
      return true
    }

    return false
  }

  // 是否后撤
  shouldEscape(touch) {
    // 近战,伤害大于恢复且濒死
    if (touch && this.damage() >= this.hp_rest && this.dying()) {
      return true
    }

    // 远战,小于半血
    if (!touch && this.damage() && this.hp_now < this.hp_max / 2) {
      return true
    }

    return false
  }

  lookAround(warrior){
    this.units.forward = this.getUnits(warrior.look('forward')) 
    this.units.backward = this.getUnits(warrior.look('backward')) 
  }


  getUnits(spaces){
    let units = []
    for(let i in spaces){
      let space = spaces[i]
      if(this.dealSpace(space) === 1){
        units.push(space.getUnit())
      }
    }
    return units
  }

  shoot(units,direction) {
    for(let i in units){
        let unit = units[i]
        switch (this.dealUnit(unit)) {
          case 0:
            // Empty
            break;
          case 1:
            // Enemy
              // 远程,选择射击,若为0,则按照原有逻辑近战
              if(i <= 0){
                return true
              }else{
                return false
              }
          case 2:
            // Bound
            this.direction = direction

            return false
          default:
            break;
        }
    }
    return false
  }
}
复制代码

epic Mode

first score 556 - epic score 598 (A)
CONGRATULATIONS! You have climbed to the top of the tower.
Warrior Score: 74
Time Bonus: 8
Clear Bonus: 16
Level Grade: A
Total Score: 500 + 98 = 598
Your average grade for this tower is: A

  Level 1: S
  Level 2: A
  Level 3: S
  Level 4: A
  Level 5: A
  Level 6: A
  Level 7: S
  Level 8: S
  Level 9: A
  
复制代码