olistic / warriorjs

🏰 An exciting game of programming and Artificial Intelligence
https://warriorjs.com
MIT License
9.41k stars 488 forks source link

rescue('backward') is undefined #262

Closed lightbluepoppy closed 1 year ago

lightbluepoppy commented 1 year ago

Environment

Steps to reproduce

On website Baby Steps - Level 6

class Player {
  /**
   * Plays a warrior turn.
   *
   * @param {Warrior} warrior The warrior.
   */
  constructor() {
    this.direction = 'backward'
    this.health = 20
  }
  playTurn(warrior) {
    // Cool code goes here
    warrior.think(`"My HP is ${warrior.health()}."`)
    this.action(warrior)
    this.health = warrior.health()
  }

  action(warrior) {
    const isEnemyLongRange = this.isEnemyLongRange(warrior)
    const isEnemyInFront = this.isEnemyInFront(warrior)
    const isCaptivated = this.isCaptivated(warrior)
    const isWallInFront = this.isWallInFront(warrior)
    const isInjured = this.isInjured(warrior)
    const isLowHp = this.isLowHp(warrior)

    // const hitWall = () => {
    // }
    if (isWallInFront) {
      warrior.think(`"There\'s a wall. I need to turn around."`)
      this.switchDirection()
      warrior.walk(this.direction)
      return
    }

    // const captiveIsPresent = () => {
    // }
    if (isCaptivated && !isInjured) {
      warrior.think(`"There\'s a captive! I need to rescue first."`)
      warrior.rescue('backward')
      return
    }

    // const archerIsPresent = () => {
    // }
    if (!isEnemyInFront && isEnemyLongRange) {
      warrior.think(`"There\'s an archer! I need to defeat it first."`)
      warrior.walk(this.direction)
      return
    }

    // const noEnemyAround = () => {
    // }
    if (!isEnemyInFront && isInjured && !isEnemyLongRange) {
      warrior.think(`"There\'s no enemy around. I gotta rest."`)
      warrior.rest()
      return
    }

    // const emergency = () => {
    // }
    if (isLowHp && isEnemyInFront && !isEnemyLongRange) {
      warrior.think(`"There\'s no Archer, I should retreat now."`)
      this.switchDirection()
      warrior.walk(this.direction)
      this.switchDirection()
      return
    }

    // const enemyIsPresent = () => {
    // }
    if (isEnemyInFront) {
      warrior.think(`"There\'s an enemy. Slaughter it!!!!!"`)
      warrior.attack(this.direction)
      return
    }

    // const peaceful = () => {
    // }
    warrior.think(`"Adventure time!"`)
    warrior.walk(this.direction)
    return
  }

  switchDirection() {
    this.direction === 'backward'
      ? (this.direction = 'forward')
      : (this.direction = 'backward')
  }
  isSomethingInFront(warrior) {
    return !warrior.feel(this.direction).isEmpty()
  }
  isEnemyLongRange(warrior) {
    return this.health > warrior.health()
  }
  isEnemyInFront(warrior) {
    return this.isSomethingInFront(warrior)
      ? warrior.feel(this.direction).getUnit().isEnemy()
      : false
  }
  isCaptivated(warrior) {
    return this.isSomethingInFront(warrior) && !this.isEnemyInFront(warrior)
      ? warrior.feel(this.direction).getUnit().isBound()
      : false
  }
  isWallInFront(warrior) {
    return warrior.feel(this.direction).isWall()
  }
  isInjured(warrior) {
    return warrior.health() < warrior.maxHealth()
  }
  isLowHp(warrior) {
    return warrior.health() <= 10
  }
}

Expected Behavior

Actual Behavior

Error: Cannot read property 'isEnemy' of undefined
joseasanchezzz91 commented 1 year ago

facing same issue, some solution?

HaydenHart commented 1 year ago

same here

olistic commented 1 year ago

The issue is that isSomethingInFront() can return true when that "something" is a wall, and not a unit. In that case, getUnit() will return undefined, and anything you call on it will return an error (e.g. Cannot read property 'isEnemy' of undefined).

You can update the definition of isEnemyInFront to check isSomethingInFront && !isWallInFront.