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

Scala sample bot #1

Open
pathikrit opened this issue Nov 10, 2017 · 0 comments
Open

Scala sample bot #1

pathikrit opened this issue Nov 10, 2017 · 0 comments

Comments

@pathikrit
Copy link

Here is the literal translation of the Java client into Scala:

import battlehack17._

object RobotPlayer {

  def main(args: Array[String]): Unit = {
    BHLogger.setVerbosity(BHLogger.VERBOSITY_CHATTYDEV)
    val game = new Game("rick-bot")
    game.waitForStart()
    while (!game.over) {
      game.waitTillNextTurn()

      /* YOUR CODE HERE */
      val mybots = game.gameInfo.local.getMyEntities
      val robots = mybots.filter(_.isRobot)
      robots.forEach(handleRobot(_, game))

      /* DON'T PUT CODE AFTER HERE */
      if (game.gameInfo.myTurn) game.sendTurn()
    }
    BHLogger.log("Done!", BHLogger.VERBOSITY_OUTPUT)
  }

  /**
    * This code will handle the actions of a single robot.
    *
    * @param robot The robot to handle
    * @param game  The game (contains info on map and entities)
    */
  def handleRobot(robot: EntityData, game: Game): Unit = {
    if (!robot.canAct) return // This robot can't act! We don't want to waste time on it.

    for (eachDirection <- Direction.cardinalDirections) { // We access our local copy of the game info to determine if we control the
      // sectors around this robot.
      // The local copy might desync from the server (in theory, it won't)
      // But, it means that all our robots will have updated information on what
      // we THINK will happen.

      game.gameInfo.local.controlArea(robot.location.add(eachDirection)) match {
        case AreaControlType.ALLY_CONTROLS_SECTOR =>
        case AreaControlType.ENEMY_CONTROLS_SECTOR =>
        case AreaControlType.NUETRAL_SECTOR =>
          if (robot.canBuild(eachDirection)) {
            val buildWorked = robot.build(eachDirection)
            // We built something!
            if (buildWorked) return
            //If this didn't work, something must have gone wrong...
          }
        case AreaControlType.NO_SECTOR =>
      }
    }
    // Okay, we didn't build any statues. Let's move towards the nearest enemy statue.
    // (Almost) All of Battlehacks iterables are provided to you via streams.
    // Streams are a feature of Java 1.8 that make manipulation of the iterable
    // really easy (compared to an array or list)
    // We'll start off by getting a stream of all the entities around this bot within throwing range
    // NOTE: One great thing about streams is that you can apply several functions
    val nearby = robot.nearbyEntities(Constants.THROW_TILE_DISTANCE)
    robot.build(Direction.NORTH, true)
  }
}
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

1 participant