Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rescue('backward') is undefined #262

Closed
lightbluepoppy opened this issue Oct 7, 2022 · 3 comments
Closed

rescue('backward') is undefined #262

lightbluepoppy opened this issue Oct 7, 2022 · 3 comments

Comments

@lightbluepoppy
Copy link

lightbluepoppy commented Oct 7, 2022

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
Copy link

facing same issue, some solution?

@HaydenHart
Copy link

same here

@olistic
Copy link
Owner

olistic commented Apr 4, 2023

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.

@olistic olistic closed this as completed Apr 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants